Repository navigation
WW-5827 check the preparer type before instantiating it and do not cache failed preparer lookups #2084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arunmanni-ai
wants to merge
5
commits into
apache:main
Choose a base branch
from
arunmanni-ai:WW-5827-preparer-type-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WW-5827 check the preparer type before instantiating it and do not cache failed preparer lookups #2084
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
17289d2
WW-5827 check the preparer type before instantiating it and do not ca…
arunmanni-ai d93bd11
WW-5827 drop the inline comments and the untyped javadoc notes
arunmanni-ai ead24f8
WW-5827 drop the inline comments and the untyped javadoc notes
arunmanni-ai 75c21cb
WW-5827 drop the trailing whitespace left by the previous push
arunmanni-ai 39d93e9
WW-5827 check the preparer type in StrutsPreparerFactory before build…
arunmanni-ai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
plugins/tiles/src/test/java/org/apache/struts2/tiles/StrutsPreparerFactoryTypeCheckTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.struts2.tiles; | ||
|
|
||
| import org.apache.struts2.ActionContext; | ||
| import org.apache.struts2.ObjectFactory; | ||
| import org.apache.struts2.inject.Container; | ||
| import org.apache.tiles.api.AttributeContext; | ||
| import org.apache.tiles.api.preparer.ViewPreparer; | ||
| import org.apache.tiles.request.Request; | ||
| import org.apache.tiles.request.reflect.CannotInstantiateObjectException; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertSame; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.anyMap; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * The factory applies the ViewPreparer type before the bean is built, so a configured name that does | ||
| * not denote a preparer is rejected without anything being constructed, and a name that cannot be | ||
| * built is not constructed a second time through the superclass. | ||
| */ | ||
| public class StrutsPreparerFactoryTypeCheckTest { | ||
|
|
||
| /** | ||
| * The evidence lives in its own class so that reading it does not initialise {@link NotAPreparer}. | ||
| */ | ||
| public static final class Evidence { | ||
| public static volatile boolean constructed; | ||
| public static volatile int preparerConstructions; | ||
| } | ||
|
|
||
| public static class NotAPreparer { | ||
| public NotAPreparer() { | ||
| Evidence.constructed = true; | ||
| } | ||
| } | ||
|
|
||
| public static class LegitimatePreparer implements ViewPreparer { | ||
| public LegitimatePreparer() { | ||
| Evidence.preparerConstructions++; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(Request tilesContext, AttributeContext attributeContext) { | ||
| } | ||
| } | ||
|
|
||
| private ObjectFactory objectFactory; | ||
| private StrutsPreparerFactory factory; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| Evidence.constructed = false; | ||
| Evidence.preparerConstructions = 0; | ||
|
|
||
| objectFactory = mock(ObjectFactory.class); | ||
| Container container = mock(Container.class); | ||
| when(container.getInstance(ObjectFactory.class)).thenReturn(objectFactory); | ||
|
|
||
| ActionContext.of().withContainer(container).bind(); | ||
|
|
||
| factory = new StrutsPreparerFactory(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| ActionContext.clear(); | ||
| Evidence.constructed = false; | ||
| } | ||
|
|
||
| @Test | ||
| public void nonPreparerIsRejectedBeforeTheBeanIsBuilt() throws Exception { | ||
| // A class literal does not initialise the class it denotes. | ||
| String name = NotAPreparer.class.getName(); | ||
|
|
||
| CannotInstantiateObjectException e = assertThrows(CannotInstantiateObjectException.class, | ||
| () -> factory.getPreparer(name, null)); | ||
| assertNotNull(e.getMessage()); | ||
|
|
||
| verify(objectFactory, never()).buildBean(anyString(), anyMap()); | ||
| assertFalse("a class that is not a ViewPreparer must not be constructed", Evidence.constructed); | ||
| } | ||
|
|
||
| @Test | ||
| public void beanThatIsNotAPreparerIsReportedAndNotBuiltTwice() throws Exception { | ||
| String name = "someContainerManagedName"; | ||
| when(objectFactory.buildBean(anyString(), anyMap())).thenReturn("not a preparer"); | ||
|
|
||
| assertThrows(CannotInstantiateObjectException.class, () -> factory.getPreparer(name, null)); | ||
|
|
||
| verify(objectFactory).buildBean(anyString(), anyMap()); | ||
| assertFalse(Evidence.constructed); | ||
| } | ||
|
|
||
| @Test | ||
| public void buildFailureStillFallsBackToTheSuperclassAndConstructsOnce() throws Exception { | ||
| String name = LegitimatePreparer.class.getName(); | ||
| when(objectFactory.buildBean(anyString(), anyMap())).thenThrow(new IllegalStateException("boom")); | ||
|
|
||
| assertNotNull("a build failure must still fall back to the superclass", factory.getPreparer(name, null)); | ||
| assertEquals("the preparer must be constructed exactly once", 1, Evidence.preparerConstructions); | ||
| } | ||
|
|
||
| @Test | ||
| public void genuinePreparerIsBuiltThroughTheObjectFactory() throws Exception { | ||
| String name = LegitimatePreparer.class.getName(); | ||
| LegitimatePreparer expected = new LegitimatePreparer(); | ||
| when(objectFactory.buildBean(anyString(), anyMap())).thenReturn(expected); | ||
|
|
||
| ViewPreparer preparer = factory.getPreparer(name, null); | ||
|
|
||
| assertSame(expected, preparer); | ||
| assertSame("a resolved preparer must still be cached and reused", preparer, factory.getPreparer(name, null)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.