This is an automated email from the ASF dual-hosted git repository. pedro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/wicket.git
commit 61f4695ae61bb3e5866d4a39771f36492d280d8c Author: Pedro Santos <[email protected]> AuthorDate: Mon Nov 4 15:54:07 2024 -0300 WICKET-7125 setBeanManager method test --- pom.xml | 4 +- wicket-cdi/pom.xml | 17 ++---- .../apache/wicket/cdi/CdiConfigurationTest.java | 51 +++++++++++++++--- .../org/apache/wicket/cdi/CdiWicketTester.java | 12 +++-- .../java/org/apache/wicket/cdi/ContextManager.java | 13 ++--- .../wicket/cdi/ConversationPropagatorTest.java | 8 +-- .../org/apache/wicket/cdi/WicketCdiTestCase.java | 63 +++++++++++----------- .../cdi/testapp/AlternativeTestAppScope.java | 34 ++++++++++++ .../cdi/testapp/ModelWithInjectedDependency.java | 40 ++++++++++++++ 9 files changed, 170 insertions(+), 72 deletions(-) diff --git a/pom.xml b/pom.xml index 556953b51e..26ad083928 100644 --- a/pom.xml +++ b/pom.xml @@ -145,7 +145,7 @@ <assertj-core.version>3.26.3</assertj-core.version> <bouncycastle.version>1.79</bouncycastle.version> <byte-buddy.version>1.15.10</byte-buddy.version> - <cdi-unit.version>4.2.0</cdi-unit.version> + <cdi-unit.version>5.0.0-EA7</cdi-unit.version> <commons-collections4.version>4.4</commons-collections4.version> <commons-fileupload.version>2.0.0-M2</commons-fileupload.version> <commons-io.version>2.17.0</commons-io.version> @@ -638,7 +638,7 @@ </exclusions> </dependency> <dependency> - <groupId>org.jglue.cdi-unit</groupId> + <groupId>io.github.cdi-unit</groupId> <artifactId>cdi-unit</artifactId> <version>${cdi-unit.version}</version> <scope>test</scope> diff --git a/wicket-cdi/pom.xml b/wicket-cdi/pom.xml index e34624a5e6..ae40a917cf 100644 --- a/wicket-cdi/pom.xml +++ b/wicket-cdi/pom.xml @@ -68,20 +68,9 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.junit.vintage</groupId> - <artifactId>junit-vintage-engine</artifactId> - <version>${junit.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.jglue.cdi-unit</groupId> - <artifactId>cdi-unit</artifactId> - <exclusions> - <exclusion> - <artifactId>org.jboss.spec.javax.annotation</artifactId> - <groupId>jboss-annotations-api_1.2_spec</groupId> - </exclusion> - </exclusions> + <groupId>io.github.cdi-unit</groupId> + <artifactId>cdi-unit</artifactId> + <scope>test</scope> </dependency> </dependencies> <build> diff --git a/wicket-cdi/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java b/wicket-cdi/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java index 0ee921bade..c7cb4218e6 100644 --- a/wicket-cdi/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java +++ b/wicket-cdi/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java @@ -16,22 +16,30 @@ */ package org.apache.wicket.cdi; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - +import io.github.cdiunit.ActivatedAlternatives; +import jakarta.enterprise.inject.spi.BeanManager; +import jakarta.inject.Inject; +import org.apache.wicket.Application; +import org.apache.wicket.cdi.testapp.AlternativeTestAppScope; +import org.apache.wicket.cdi.testapp.ModelWithInjectedDependency; import org.apache.wicket.cdi.testapp.TestConversationPage; import org.apache.wicket.cdi.testapp.TestPage; -import org.apache.wicket.util.tester.WicketTester; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + /** * @author jsarman */ -// FIXME Wicket 10. Re-enable once cdi-unit is adapted to jakarta.** +// FIXME Wicket 11. Re-enable once the tests are moved to its own module @Disabled +@ActivatedAlternatives(AlternativeTestAppScope.class) class CdiConfigurationTest extends WicketCdiTestCase { + @Inject + BeanManager beanManager; + @Test void testApplicationScope() { @@ -40,6 +48,14 @@ class CdiConfigurationTest extends WicketCdiTestCase tester.assertLabel("appscope", "Test ok"); } + @Test + void testUsesCdiJUnitConfiguration() + { + configure(new CdiConfiguration().setBeanManager(beanManager)); + tester.startPage(TestPage.class); + tester.assertLabel("appscope", "Alternative ok"); + } + @Test void testConversationScope() { @@ -52,6 +68,26 @@ class CdiConfigurationTest extends WicketCdiTestCase } } + @Test + void testNotConfigured() + { + assertThrows(IllegalStateException.class, () -> { + new ModelWithInjectedDependency(); + }); + + } + + @Test + void testAlreadyConfigured() + { + configure(new CdiConfiguration()); + + assertThrows(IllegalStateException.class, () -> { + CdiConfiguration.get(Application.get()).setBeanManager(beanManager); + }); + + } + @Test void testConfigureTwice() { @@ -66,13 +102,12 @@ class CdiConfigurationTest extends WicketCdiTestCase @Test void testApplicationLevelConfiguration() { - WicketTester tester = new WicketTester(); CdiConfiguration config = new CdiConfiguration(); for (ConversationPropagation cp : ConversationPropagation.values()) { config.setPropagation(cp); assertEquals(cp, config.getPropagation()); } - config.configure(tester.getApplication()); + configure(config); } } diff --git a/wicket-cdi/src/test/java/org/apache/wicket/cdi/CdiWicketTester.java b/wicket-cdi/src/test/java/org/apache/wicket/cdi/CdiWicketTester.java index e4a4bc4d55..1547b00bc2 100644 --- a/wicket-cdi/src/test/java/org/apache/wicket/cdi/CdiWicketTester.java +++ b/wicket-cdi/src/test/java/org/apache/wicket/cdi/CdiWicketTester.java @@ -22,9 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.regex.Matcher; import java.util.regex.Pattern; -import jakarta.annotation.PreDestroy; -import jakarta.inject.Inject; - import org.apache.wicket.Page; import org.apache.wicket.core.request.handler.IPageRequestHandler; import org.apache.wicket.protocol.http.WebApplication; @@ -35,6 +32,8 @@ import org.apache.wicket.util.tester.WicketTester; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import jakarta.annotation.PreDestroy; + /** * @author jsarman */ @@ -43,16 +42,19 @@ public class CdiWicketTester extends WicketTester private static final Pattern COUNT_PATTERN = Pattern.compile("COUNT=x([0-9]+)x"); private static final Logger logger = LoggerFactory.getLogger(CdiWicketTester.class); - @Inject ContextManager contextManager; public CdiWicketTester(WebApplication app) { super(app); - NonContextual.of(CdiWicketTester.class).inject(this); getHttpSession().setTemporary(false); } + public void setContextManager(ContextManager contextManager) + { + this.contextManager = contextManager; + } + /** * Process the request by first activating the contexts on initial call. This call is called * recursively in the super class so keep track of the topmost call and only activate and diff --git a/wicket-cdi/src/test/java/org/apache/wicket/cdi/ContextManager.java b/wicket-cdi/src/test/java/org/apache/wicket/cdi/ContextManager.java index 7017c963eb..1a440498c2 100644 --- a/wicket-cdi/src/test/java/org/apache/wicket/cdi/ContextManager.java +++ b/wicket-cdi/src/test/java/org/apache/wicket/cdi/ContextManager.java @@ -16,6 +16,12 @@ */ package org.apache.wicket.cdi; +import org.jboss.weld.bean.builtin.BeanManagerProxy; +import org.jboss.weld.module.web.servlet.HttpContextLifecycle; +import org.jboss.weld.servlet.spi.helpers.AcceptingHttpContextActivationFilter; + +import io.github.cdiunit.internal.servlet.CdiUnitInitialListenerImpl; +import io.github.cdiunit.internal.servlet.LifecycleAwareRequest; import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.inject.spi.BeanManager; @@ -23,10 +29,6 @@ import jakarta.inject.Inject; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; -import org.jboss.weld.bean.builtin.BeanManagerProxy; -import org.jboss.weld.module.web.servlet.HttpContextLifecycle; -import org.jboss.weld.servlet.spi.helpers.AcceptingHttpContextActivationFilter; - /** * @author jsarman */ @@ -72,8 +74,7 @@ public class ContextManager if (currentRequest != null) return; - // FIXME Wicket 10 - currentRequest = null;// new LifecycleAwareRequest(new CdiUnitInitialListenerImpl(), new javax.servlet.http.HttpServletRequest(request)); + currentRequest = new LifecycleAwareRequest(new CdiUnitInitialListenerImpl(), request); lifecycle.requestInitialized(currentRequest, null); } diff --git a/wicket-cdi/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java b/wicket-cdi/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java index e18971cfd4..d61cf2d577 100644 --- a/wicket-cdi/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java +++ b/wicket-cdi/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java @@ -18,9 +18,6 @@ package org.apache.wicket.cdi; import static org.junit.jupiter.api.Assertions.assertEquals; -import jakarta.enterprise.context.Conversation; -import jakarta.inject.Inject; - import org.apache.wicket.cdi.testapp.TestConversationPage; import org.apache.wicket.cdi.testapp.TestConversationalPage; import org.apache.wicket.core.request.mapper.MountedMapper; @@ -28,10 +25,13 @@ import org.apache.wicket.request.mapper.parameter.PageParameters; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import jakarta.enterprise.context.Conversation; +import jakarta.inject.Inject; + /** * @author jsarman */ -// FIXME Wicket 10. Re-enable once cdi-unit is adapted to jakarta.** +//FIXME Wicket 11. Re-enable once the tests are moved to its own module @Disabled class ConversationPropagatorTest extends WicketCdiTestCase { diff --git a/wicket-cdi/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java b/wicket-cdi/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java index b63b00c58b..a2d9c8df37 100644 --- a/wicket-cdi/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java +++ b/wicket-cdi/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java @@ -16,8 +16,6 @@ */ package org.apache.wicket.cdi; -import jakarta.inject.Inject; - import org.apache.wicket.Component; import org.apache.wicket.Page; import org.apache.wicket.ThreadContext; @@ -29,16 +27,18 @@ import org.apache.wicket.mock.MockApplication; import org.apache.wicket.protocol.http.WebApplication; import org.apache.wicket.request.mapper.parameter.PageParameters; import org.apache.wicket.util.tester.WicketTester; -import org.jglue.cdiunit.AdditionalClasses; -import org.jglue.cdiunit.CdiRunner; -import org.junit.After; -import org.junit.Before; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; + +import io.github.cdiunit.AdditionalClasses; +import io.github.cdiunit.junit5.CdiJUnit5Extension; +import jakarta.inject.Inject; /** * @author jsarman */ -@RunWith(CdiRunner.class) // TODO upgrade to junit 5 ExtendWith when Cdi project has Junit 5 support +@ExtendWith(CdiJUnit5Extension.class) @AdditionalClasses({ CdiWicketTester.class, BehaviorInjector.class, CdiConfiguration.class, CdiShutdownCleaner.class, ComponentInjector.class, ConversationExpiryChecker.class, ConversationPropagator.class, DetachEventEmitter.class, SessionInjector.class, @@ -53,7 +53,9 @@ public abstract class WicketCdiTestCase protected CdiWicketTester newWicketTester(WebApplication app) { - return new CdiWicketTester(app); + CdiWicketTester cdiWicketTester = new CdiWicketTester(app); + cdiWicketTester.setContextManager(contextManager); + return cdiWicketTester; } public void configure(CdiConfiguration configuration) @@ -61,7 +63,7 @@ public abstract class WicketCdiTestCase configuration.configure(tester.getApplication()); } - @After + @AfterEach public void end() { if (contextManager.isRequestActive()) @@ -69,9 +71,13 @@ public abstract class WicketCdiTestCase contextManager.deactivateContexts(); contextManager.destroy(); } + tester.destroy(); + + // make sure no leaked BeanManager are present + BeanManagerLookup.detach(); } - @Before + @BeforeEach public void commonBefore() { // make sure no leaked threadlocals are present @@ -90,17 +96,8 @@ public abstract class WicketCdiTestCase } /** - * - */ - @After - public void commonAfter() - { - tester.destroy(); - } - - /** - * Use <code>-Dwicket.replace.expected.results=true</code> to automatically - * replace the expected output file. + * Use <code>-Dwicket.replace.expected.results=true</code> to automatically replace the expected + * output file. * * @param <T> * @@ -109,14 +106,14 @@ public abstract class WicketCdiTestCase * @throws Exception */ protected <T extends Page> void executeTest(final Class<T> pageClass, final String filename) - throws Exception + throws Exception { tester.executeTest(getClass(), pageClass, filename); } /** - * Use <code>-Dwicket.replace.expected.results=true</code> to automatically - * replace the expected output file. + * Use <code>-Dwicket.replace.expected.results=true</code> to automatically replace the expected + * output file. * * @param page * @param filename @@ -128,8 +125,8 @@ public abstract class WicketCdiTestCase } /** - * Use <code>-Dwicket.replace.expected.results=true</code> to automatically - * replace the expected output file. + * Use <code>-Dwicket.replace.expected.results=true</code> to automatically replace the expected + * output file. * * @param <T> * @@ -138,8 +135,8 @@ public abstract class WicketCdiTestCase * @param filename * @throws Exception */ - protected <T extends Page> void executeTest(final Class<T> pageClass, - PageParameters parameters, final String filename) throws Exception + protected <T extends Page> void executeTest(final Class<T> pageClass, PageParameters parameters, + final String filename) throws Exception { tester.executeTest(getClass(), pageClass, parameters, filename); } @@ -151,7 +148,7 @@ public abstract class WicketCdiTestCase * @throws Exception */ protected void executeListener(final Component component, final String filename) - throws Exception + throws Exception { tester.executeListener(getClass(), component, filename); } @@ -163,14 +160,14 @@ public abstract class WicketCdiTestCase * @throws Exception */ protected void executeBehavior(final AbstractAjaxBehavior behavior, final String filename) - throws Exception + throws Exception { tester.executeBehavior(getClass(), behavior, filename); } /** - * Returns the current Maven build directory taken from the <tt>basedir</tt> - * system property, or null if not set + * Returns the current Maven build directory taken from the <tt>basedir</tt> system property, or + * null if not set * * @return path with a trailing slash */ diff --git a/wicket-cdi/src/test/java/org/apache/wicket/cdi/testapp/AlternativeTestAppScope.java b/wicket-cdi/src/test/java/org/apache/wicket/cdi/testapp/AlternativeTestAppScope.java new file mode 100644 index 0000000000..045b762cd0 --- /dev/null +++ b/wicket-cdi/src/test/java/org/apache/wicket/cdi/testapp/AlternativeTestAppScope.java @@ -0,0 +1,34 @@ +/* + * 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.wicket.cdi.testapp; + +import jakarta.enterprise.inject.Alternative; + +/** + * @author jsarman + */ +@Alternative +public class AlternativeTestAppScope extends TestAppScope +{ + + @Override + public String test() + { + return "Alternative ok"; + } + +} diff --git a/wicket-cdi/src/test/java/org/apache/wicket/cdi/testapp/ModelWithInjectedDependency.java b/wicket-cdi/src/test/java/org/apache/wicket/cdi/testapp/ModelWithInjectedDependency.java new file mode 100644 index 0000000000..fb443880bc --- /dev/null +++ b/wicket-cdi/src/test/java/org/apache/wicket/cdi/testapp/ModelWithInjectedDependency.java @@ -0,0 +1,40 @@ +/* + * 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.wicket.cdi.testapp; + +import org.apache.wicket.cdi.NonContextual; +import org.apache.wicket.model.IModel; + +import jakarta.inject.Inject; + +public class ModelWithInjectedDependency implements IModel<String> +{ + @Inject + TestAppScope appScope; + + public ModelWithInjectedDependency() + { + NonContextual.of(ModelWithInjectedDependency.class).inject(this); + } + + @Override + public String getObject() + { + return "not used"; + } + +}
