Updated Branches: refs/heads/wicket-6.x be7783803 -> c3a35e74e
Massive improvements in testcases voor wicket-cdi Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/c3a35e74 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/c3a35e74 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/c3a35e74 Branch: refs/heads/wicket-6.x Commit: c3a35e74ed34cf4149f6519253b924f0c459e09c Parents: be77838 Author: Emond Papegaaij <[email protected]> Authored: Wed Nov 27 21:59:51 2013 +0100 Committer: Emond Papegaaij <[email protected]> Committed: Wed Nov 27 21:59:51 2013 +0100 ---------------------------------------------------------------------- .../java/org/apache/wicket/WicketTestCase.java | 161 ------------------- .../apache/wicket/cdi/CdiConfigurationTest.java | 2 +- .../org/apache/wicket/cdi/CdiWicketTester.java | 131 +++++++++++++++ .../org/apache/wicket/cdi/ContextManager.java | 105 ++++++++++++ .../wicket/cdi/ConversationPropagatorTest.java | 85 +++------- .../apache/wicket/cdi/WicketCdiTestCase.java | 129 ++++++++++++++- .../cdi/testapp/TestConversationBean.java | 5 + .../cdi/testapp/TestConversationPage.java | 13 +- .../cdi/testapp/TestConversationalPage.java | 6 +- .../testapp/TestNonAutoConversationalPage.html | 12 -- .../testapp/TestNonAutoConversationalPage.java | 88 ---------- .../cdi/testapp/TestNonConversationalPage.java | 6 +- .../wicket/cdi/util/tester/CdiWicketTester.java | 122 -------------- .../wicket/cdi/util/tester/ContextManager.java | 105 ------------ 14 files changed, 389 insertions(+), 581 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/WicketTestCase.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/WicketTestCase.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/WicketTestCase.java deleted file mode 100644 index eaed456..0000000 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/WicketTestCase.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * 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; - -import org.apache.wicket.behavior.AbstractAjaxBehavior; -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.junit.After; -import org.junit.Assert; -import org.junit.Before; - -/** - * Base class for tests which require comparing wicket response with a file. - * <p> - * To create/replace the expected result file with the new content, define the system property like - * -Dwicket.replace.expected.results=true - */ -public abstract class WicketTestCase extends Assert -{ - /** */ - protected WicketTester tester; - - /** - * @see junit.framework.TestCase#setUp() - */ - @Before - public void commonBefore() - { - // make sure no leaked threadlocals are present - ThreadContext.detach(); - - WebApplication application = newApplication(); - tester = newWicketTester(application); - } - - /** - * @return the application that should be used for the test - */ - protected WebApplication newApplication() - { - return new MockApplication(); - } - - /** - * In case you need to subclass WicketTester and want to be independent on possible changes in - * setUp(). - * - * @param app - * @return WIcketTester - */ - protected WicketTester newWicketTester(final WebApplication app) - { - return new WicketTester(app); - } - - /** - * - */ - @After - public void commonAfter() - { - tester.destroy(); - } - - /** - * Use <code>-Dwicket.replace.expected.results=true</code> to automatically replace the expected - * output file. - * - * @param <T> - * - * @param pageClass - * @param filename - * @throws Exception - */ - protected <T extends Page> void executeTest(final Class<T> pageClass, final String filename) - throws Exception - { - tester.executeTest(getClass(), pageClass, filename); - } - - /** - * Use <code>-Dwicket.replace.expected.results=true</code> to automatically replace the expected - * output file. - * - * @param page - * @param filename - * @throws Exception - */ - protected void executeTest(final Page page, final String filename) throws Exception - { - tester.executeTest(getClass(), page, filename); - } - - /** - * Use <code>-Dwicket.replace.expected.results=true</code> to automatically replace the expected - * output file. - * - * @param <T> - * - * @param pageClass - * @param parameters - * @param 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); - } - - /** - * - * @param component - * @param filename - * @throws Exception - */ - protected void executeListener(final Component component, final String filename) - throws Exception - { - tester.executeListener(getClass(), component, filename); - } - - /** - * - * @param behavior - * @param filename - * @throws Exception - */ - protected void executeBehavior(final AbstractAjaxBehavior behavior, final String filename) - 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 - * - * @return path with a trailing slash - */ - public String getBasedir() - { - return WicketTester.getBasedir(); - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java index 7e89c10..723db16 100644 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiConfigurationTest.java @@ -41,7 +41,7 @@ public class CdiConfigurationTest extends WicketCdiTestCase tester.startPage(TestConversationPage.class); for (int i = 0; i < 20; i++) { - tester.assertLabel("count", i + ""); + tester.assertCount(i); tester.clickLink("increment"); } } http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiWicketTester.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiWicketTester.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiWicketTester.java new file mode 100644 index 0000000..3a1870e --- /dev/null +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/CdiWicketTester.java @@ -0,0 +1,131 @@ +/* + * 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; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.annotation.PreDestroy; +import javax.inject.Inject; + +import org.apache.wicket.Page; +import org.apache.wicket.cdi.CdiConfiguration; +import org.apache.wicket.cdi.ConversationPropagator; +import org.apache.wicket.cdi.NonContextual; +import org.apache.wicket.protocol.http.WebApplication; +import org.apache.wicket.protocol.http.mock.MockHttpServletRequest; +import org.apache.wicket.request.IRequestHandler; +import org.apache.wicket.request.Url; +import org.apache.wicket.util.tester.WicketTester; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author jsarman + */ +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); + } + + /** + * 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 deactivate the contexts during that + * time. + * + * @param forcedRequest + * @param forcedRequestHandler + * @param redirect + * @return + */ + @Override + protected boolean processRequest(final MockHttpServletRequest forcedRequest, + final IRequestHandler forcedRequestHandler, final boolean redirect) + { + if (getLastRequest() != null) + { + contextManager.deactivateContexts(); + } + contextManager.activateContexts(forcedRequest == null ? getRequest() : forcedRequest); + return super.processRequest(forcedRequest, forcedRequestHandler, redirect); + } + + @Override + public Url urlFor(IRequestHandler handler) + { + Url ret = super.urlFor(handler); + final CdiConfiguration configuration = CdiConfiguration.get(getApplication()); + Page page = ConversationPropagator.getPage(handler); + if (configuration.getPropagation().propagatesVia(handler, page)) + { + if (page != null) + { + String cid = ConversationPropagator.getConversationIdFromPage(page); + ret.addQueryParameter(ConversationPropagator.CID, cid); + } + } + return ret; + } + + @PreDestroy + public void finish() + { + try + { + logger.debug("Destroying Cdi Wicket Tester"); + if (getLastRequest() != null) + { + contextManager.deactivateContexts(); + } + contextManager.destroy(); + destroy(); + } + catch (Throwable t) + { + } + } + + /** + * Asserts that the respons contains the right count. This can only be done + * by parsing the markup because models only contain valid values during a + * request, not after. + * + * @param count + * TODO + */ + public void assertCount(int count) + { + assertTrue("Response does not contain a count", + getLastResponseAsString().contains("COUNT=x")); + Matcher matcher = COUNT_PATTERN.matcher(getLastResponseAsString()); + assertTrue(matcher.find()); + assertEquals(Integer.toString(count), matcher.group(1)); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ContextManager.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ContextManager.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ContextManager.java new file mode 100644 index 0000000..3e22aef --- /dev/null +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ContextManager.java @@ -0,0 +1,105 @@ +/* + * 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; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.spi.BeanManager; +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +import org.jboss.weld.bean.builtin.BeanManagerProxy; +import org.jboss.weld.servlet.HttpContextLifecycle; +import org.jboss.weld.servlet.spi.helpers.AcceptingHttpContextActivationFilter; +import org.jglue.cdiunit.internal.LifecycleAwareRequest; + +/** + * @author jsarman + */ +@ApplicationScoped +public class ContextManager +{ + private HttpServletRequest currentRequest; + + @Inject + private BeanManager beanManager; + + private HttpContextLifecycle lifecycle; + + private HttpSession currentSession; + + @PostConstruct + public void setup() + { + try + { + lifecycle = new HttpContextLifecycle(BeanManagerProxy.unwrap(beanManager), + AcceptingHttpContextActivationFilter.INSTANCE, true, true); + } + catch (NoSuchMethodError e) + { + try + { + lifecycle = HttpContextLifecycle.class.getConstructor(BeanManager.class, + AcceptingHttpContextActivationFilter.class).newInstance( + BeanManagerProxy.unwrap(beanManager), + AcceptingHttpContextActivationFilter.INSTANCE); + } + catch (Exception e1) + { + throw new RuntimeException(e1); + } + } + lifecycle.setConversationActivationEnabled(true); + } + + public void activateContexts(HttpServletRequest request) + { + if (currentRequest != null) + return; + + currentRequest = new LifecycleAwareRequest(lifecycle, request, currentSession); + lifecycle.requestInitialized(currentRequest, null); + } + + public void deactivateContexts() + { + lifecycle.requestDestroyed(currentRequest); + currentSession = currentRequest.getSession(false); + currentRequest = null; + } + + public void destroy() + { + if (currentRequest != null) + { + currentSession = currentRequest.getSession(false); + } + + if (currentSession != null) + { + lifecycle.sessionDestroyed(currentSession); + currentSession = null; + } + } + + public boolean isRequestActive() + { + return currentRequest != null; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java index d827b11..eefebde 100644 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/ConversationPropagatorTest.java @@ -19,7 +19,6 @@ package org.apache.wicket.cdi; import org.apache.wicket.cdi.testapp.TestConversationPage; import org.apache.wicket.cdi.testapp.TestConversationalPage; import org.apache.wicket.request.mapper.parameter.PageParameters; -import org.junit.Ignore; import org.junit.Test; /** @@ -28,7 +27,6 @@ import org.junit.Test; public class ConversationPropagatorTest extends WicketCdiTestCase { @Test - @Ignore("Testcase and auto conversations do not match") public void testAutoConversationNonBookmarkable() { configure(new CdiConfiguration()); @@ -37,34 +35,18 @@ public class ConversationPropagatorTest extends WicketCdiTestCase int i; for (i = 0; i < 3; i++) { - tester.assertLabel("count", i + ""); + tester.assertCount(i); tester.clickLink("increment"); } tester.clickLink("next"); - // at this point counter = 3, auto conversation is still enabled and - // remains enabled, because TestConversationalPage is part of this - // request for (; i < 6; i++) { - // first iteration: i == 3, counter == 3, conversation active - // second iteration: i == 4, counter == 4, conversation transient - // third iteration: i == 5, counter == 1: FAIL - tester.assertLabel("count", i + ""); - - // first iteration: conversation is still long-running, counter is - // incremented, after which auto conversation is disabled and the - // conversation ended - - // second iteration: transient conversation, counter starts at 1, - // conversation remains transient tester.clickLink("increment"); - + tester.assertCount(1); } - } @Test - @Ignore("Testcase and auto conversations do not match") public void testAutoConversationBookmarkable() { configure(new CdiConfiguration()); @@ -75,18 +57,14 @@ public class ConversationPropagatorTest extends WicketCdiTestCase int i; for (i = 0; i < 3; i++) { - tester.assertLabel("count", i + ""); + tester.assertCount(i); tester.clickLink("increment"); } tester.clickLink("next"); - // The conversation should auto end and not create another one - // so the next page just keeps getting 1 because the conversationscoped - // bean - // doesnt persist across requests. for (i = 0; i < 3; i++) { tester.clickLink("increment"); - tester.assertLabel("count", 1 + ""); + tester.assertCount(1); } } @@ -99,16 +77,15 @@ public class ConversationPropagatorTest extends WicketCdiTestCase int i; for (i = 0; i < 3; i++) { - tester.assertLabel("count", i + ""); + tester.assertCount(i); tester.clickLink("increment"); } tester.clickLink("next"); for (; i < 6; i++) { - tester.assertLabel("count", i + ""); + tester.assertCount(i); tester.clickLink("increment"); } - } @Test @@ -121,79 +98,55 @@ public class ConversationPropagatorTest extends WicketCdiTestCase int i; for (i = 0; i < 3; i++) { - tester.assertLabel("count", i + ""); - tester.clickLink("increment"); - } - tester.clickLink("next"); - for (; i < 6; i++) - { - tester.assertLabel("count", i + ""); - tester.clickLink("increment"); - } - - } - - @Test - public void testPropagationNone() - { - configure(new CdiConfiguration().setPropagation(ConversationPropagation.NONE)); - - tester.startPage(TestConversationPage.class); - int i; - for (i = 0; i < 3; i++) - { + tester.assertCount(i); tester.clickLink("increment"); - tester.assertLabel("count", "1"); } tester.clickLink("next"); for (; i < 6; i++) { + tester.assertCount(i); tester.clickLink("increment"); - tester.assertLabel("count", "1"); } - } @Test - @Ignore("Testcase and auto conversations do not match") - public void testGlobalAutoSettingNonBookmarkable() + public void testPropagationNonBookmarkable() { configure(new CdiConfiguration()); - tester.startPage(TestConversationPage.class, new PageParameters().add("auto", true)); + tester.startPage(TestConversationPage.class, + new PageParameters().add("pageType", "bookmarkable")); int i; for (i = 0; i < 3; i++) { - tester.assertLabel("count", i + ""); + tester.assertCount(i); tester.clickLink("increment"); } tester.clickLink("next"); for (; i < 6; i++) { - tester.assertLabel("count", i + ""); tester.clickLink("increment"); + tester.assertCount(1); } } @Test - @Ignore("Testcase and auto conversations do not match") - public void testGlobalAutoSettingBookmarkable() + public void testPropagationNone() { - configure(new CdiConfiguration()); + configure(new CdiConfiguration().setPropagation(ConversationPropagation.NONE)); - tester.startPage(TestConversationPage.class, - new PageParameters().add("auto", true).add("pageType", "bookmarkable")); + tester.startPage(TestConversationPage.class); int i; for (i = 0; i < 3; i++) { - tester.assertLabel("count", i + ""); tester.clickLink("increment"); + tester.assertCount(1); } tester.clickLink("next"); - for (i = 0; i < 3; i++) + for (; i < 6; i++) { - tester.assertLabel("count", i + ""); tester.clickLink("increment"); + tester.assertCount(1); } } } http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java index 496d092..3f82075 100644 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/WicketCdiTestCase.java @@ -18,17 +18,22 @@ package org.apache.wicket.cdi; import javax.inject.Inject; -import org.apache.wicket.WicketTestCase; +import org.apache.wicket.Component; +import org.apache.wicket.Page; +import org.apache.wicket.ThreadContext; +import org.apache.wicket.behavior.AbstractAjaxBehavior; import org.apache.wicket.cdi.testapp.TestAppScope; import org.apache.wicket.cdi.testapp.TestCdiApplication; import org.apache.wicket.cdi.testapp.TestConversationBean; -import org.apache.wicket.cdi.util.tester.CdiWicketTester; -import org.apache.wicket.cdi.util.tester.ContextManager; +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.Assert; +import org.junit.Before; import org.junit.runner.RunWith; /** @@ -40,13 +45,14 @@ import org.junit.runner.RunWith; ConversationPropagator.class, DetachEventEmitter.class, SessionInjector.class, TestAppScope.class, TestConversationBean.class, TestCdiApplication.class, AutoConversation.class }) -public abstract class WicketCdiTestCase extends WicketTestCase +public abstract class WicketCdiTestCase extends Assert { @Inject private ContextManager contextManager; + /** */ + protected CdiWicketTester tester; - @Override - protected WicketTester newWicketTester(WebApplication app) + protected CdiWicketTester newWicketTester(WebApplication app) { return new CdiWicketTester(app); } @@ -65,4 +71,115 @@ public abstract class WicketCdiTestCase extends WicketTestCase contextManager.destroy(); } } + + /** + * @see junit.framework.TestCase#setUp() + */ + @Before + public void commonBefore() + { + // make sure no leaked threadlocals are present + ThreadContext.detach(); + + WebApplication application = newApplication(); + tester = newWicketTester(application); + } + + /** + * @return the application that should be used for the test + */ + protected WebApplication newApplication() + { + return new MockApplication(); + } + + /** + * + */ + @After + public void commonAfter() + { + tester.destroy(); + } + + /** + * Use <code>-Dwicket.replace.expected.results=true</code> to automatically + * replace the expected output file. + * + * @param <T> + * + * @param pageClass + * @param filename + * @throws Exception + */ + protected <T extends Page> void executeTest(final Class<T> pageClass, final String filename) + throws Exception + { + tester.executeTest(getClass(), pageClass, filename); + } + + /** + * Use <code>-Dwicket.replace.expected.results=true</code> to automatically + * replace the expected output file. + * + * @param page + * @param filename + * @throws Exception + */ + protected void executeTest(final Page page, final String filename) throws Exception + { + tester.executeTest(getClass(), page, filename); + } + + /** + * Use <code>-Dwicket.replace.expected.results=true</code> to automatically + * replace the expected output file. + * + * @param <T> + * + * @param pageClass + * @param parameters + * @param 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); + } + + /** + * + * @param component + * @param filename + * @throws Exception + */ + protected void executeListener(final Component component, final String filename) + throws Exception + { + tester.executeListener(getClass(), component, filename); + } + + /** + * + * @param behavior + * @param filename + * @throws Exception + */ + protected void executeBehavior(final AbstractAjaxBehavior behavior, final String filename) + 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 + * + * @return path with a trailing slash + */ + public String getBasedir() + { + return WicketTester.getBasedir(); + } } http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationBean.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationBean.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationBean.java index ef61441..140ecdb 100644 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationBean.java +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationBean.java @@ -42,6 +42,11 @@ public class TestConversationBean implements Serializable return counter.get(); } + public String getCountStr() + { + return "COUNT=x" + getCount() + "x"; + } + public void increment() { http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationPage.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationPage.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationPage.java index cb0138d..d421e39 100644 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationPage.java +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationPage.java @@ -16,8 +16,6 @@ */ package org.apache.wicket.cdi.testapp; -import java.util.Random; - import javax.enterprise.context.Conversation; import javax.inject.Inject; @@ -41,8 +39,6 @@ public class TestConversationPage extends WebPage @Inject TestConversationBean counter; - Random random = new Random(); - public TestConversationPage() { this(new PageParameters()); @@ -52,13 +48,10 @@ public class TestConversationPage extends WebPage { super(parameters); - if (!parameters.get("auto").toBoolean()) - { - conversation.begin(); + conversation.begin(); + System.out.println("Opened Conversion with id = " + conversation.getId()); - System.out.println("Opened Conversion with id = " + conversation.getId()); - } - add(new Label("count", new PropertyModel<Integer>(this, "counter.count"))); + add(new Label("count", new PropertyModel<String>(this, "counter.countStr"))); add(new Link<Void>("increment") { http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationalPage.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationalPage.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationalPage.java index a106936..5e41fd5 100644 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationalPage.java +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestConversationalPage.java @@ -16,8 +16,6 @@ */ package org.apache.wicket.cdi.testapp; -import java.util.Random; - import javax.inject.Inject; import org.apache.wicket.cdi.ConversationalComponent; @@ -40,8 +38,6 @@ public class TestConversationalPage extends WebPage implements ConversationalCom @Inject TestConversationBean counter; - Random random = new Random(); - public TestConversationalPage() { this(new PageParameters()); @@ -52,7 +48,7 @@ public class TestConversationalPage extends WebPage implements ConversationalCom { logger.debug("Starting TestConversationalPage"); - add(new Label("count", new PropertyModel<Integer>(this, "counter.count"))); + add(new Label("count", new PropertyModel<String>(this, "counter.countStr"))); add(new Link<Void>("increment") { http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonAutoConversationalPage.html ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonAutoConversationalPage.html b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonAutoConversationalPage.html deleted file mode 100644 index b3550f1..0000000 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonAutoConversationalPage.html +++ /dev/null @@ -1,12 +0,0 @@ -<!DOCTYPE html> -<html xmlns:wicket> -<head> - <title></title> - <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> -</head> -<body> -<span wicket:id="count">100</span> -<a wicket:id="increment">increment</a> -<a wicket:id="next">next</a> -</body> -</html> http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonAutoConversationalPage.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonAutoConversationalPage.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonAutoConversationalPage.java deleted file mode 100644 index 2a78914..0000000 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonAutoConversationalPage.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 java.util.Random; - -import javax.enterprise.context.Conversation; -import javax.inject.Inject; - -import org.apache.wicket.markup.html.WebPage; -import org.apache.wicket.markup.html.basic.Label; -import org.apache.wicket.markup.html.link.Link; -import org.apache.wicket.model.PropertyModel; -import org.apache.wicket.request.mapper.parameter.PageParameters; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @author jsarman - */ -public class TestNonAutoConversationalPage extends WebPage -{ - private static final long serialVersionUID = 1L; - private static final Logger logger = LoggerFactory.getLogger(TestConversationPage.class); - @Inject - TestConversationBean counter; - - @Inject - Conversation conversation; - - Random random = new Random(); - - public TestNonAutoConversationalPage() - { - this(new PageParameters()); - } - - - public TestNonAutoConversationalPage(final PageParameters pp) - { - if (pp.get("startConveration").toBoolean(true)) - { - conversation.begin(); - } - logger.debug("Starting TestConversationalPage"); - - add(new Label("count", new PropertyModel<Integer>(this, "counter.count"))); - - add(new Link<Void>("increment") - { - private static final long serialVersionUID = 1L; - - @Override - public void onClick() - { - counter.increment(); - } - }); - add(new Link<Void>("next") - { - private static final long serialVersionUID = 1L; - - @Override - public void onClick() - { - String pageType = pp.get("pageType").toString("nonbookmarkable"); - if ("bookmarkable".equals(pageType.toLowerCase())) - setResponsePage(TestNonConversationalPage.class); - else - setResponsePage(new TestNonConversationalPage()); - } - }); - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonConversationalPage.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonConversationalPage.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonConversationalPage.java index 8af7bac..7728e81 100644 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonConversationalPage.java +++ b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/testapp/TestNonConversationalPage.java @@ -16,8 +16,6 @@ */ package org.apache.wicket.cdi.testapp; -import java.util.Random; - import javax.inject.Inject; import org.apache.wicket.markup.html.WebPage; @@ -38,12 +36,10 @@ public class TestNonConversationalPage extends WebPage @Inject TestConversationBean counter; - Random random = new Random(); - public TestNonConversationalPage() { logger.debug("Starting TestConversationalPage"); - add(new Label("count", new PropertyModel<Integer>(this, "counter.count"))); + add(new Label("count", new PropertyModel<String>(this, "counter.countStr"))); add(new Link<Void>("increment") { http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/util/tester/CdiWicketTester.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/util/tester/CdiWicketTester.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/util/tester/CdiWicketTester.java deleted file mode 100644 index b5d4eb7..0000000 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/util/tester/CdiWicketTester.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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.util.tester; - -import java.util.concurrent.atomic.AtomicInteger; - -import javax.annotation.PreDestroy; -import javax.inject.Inject; - -import org.apache.wicket.Page; -import org.apache.wicket.cdi.CdiConfiguration; -import org.apache.wicket.cdi.ConversationPropagator; -import org.apache.wicket.cdi.NonContextual; -import org.apache.wicket.protocol.http.WebApplication; -import org.apache.wicket.protocol.http.mock.MockHttpServletRequest; -import org.apache.wicket.request.IRequestHandler; -import org.apache.wicket.request.Url; -import org.apache.wicket.util.tester.WicketTester; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @author jsarman - */ -public class CdiWicketTester extends WicketTester -{ - private static final Logger logger = LoggerFactory.getLogger(CdiWicketTester.class); - - @Inject - ContextManager contextManager; - - private AtomicInteger count = new AtomicInteger(); - - public CdiWicketTester(WebApplication app) - { - super(app); - NonContextual.of(CdiWicketTester.class).inject(this); - } - - /** - * 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 deactivate the contexts during that - * time. - * - * @param forcedRequest - * @param forcedRequestHandler - * @param redirect - * @return - */ - @Override - protected boolean processRequest(final MockHttpServletRequest forcedRequest, - final IRequestHandler forcedRequestHandler, final boolean redirect) - { - if (count.getAndIncrement() == 0) - { - - if (getLastRequest() != null) - { - contextManager.deactivateContexts(); - } - contextManager.activateContexts(forcedRequest == null ? getRequest() : forcedRequest); - } - try - { - return super.processRequest(forcedRequest, forcedRequestHandler, redirect); - } - finally - { - count.decrementAndGet(); - } - } - - @Override - public Url urlFor(IRequestHandler handler) - { - Url ret = super.urlFor(handler); - final CdiConfiguration configuration = CdiConfiguration.get(getApplication()); - Page page = ConversationPropagator.getPage(handler); - if (configuration.getPropagation().propagatesVia(handler, page)) - { - if (page != null) - { - String cid = ConversationPropagator.getConversationIdFromPage(page); - ret.addQueryParameter(ConversationPropagator.CID, cid); - } - } - return ret; - } - - @PreDestroy - public void finish() - { - try - { - logger.debug("Destroying Cdi Wicket Tester"); - if (getLastRequest() != null) - { - contextManager.deactivateContexts(); - } - contextManager.destroy(); - destroy(); - } - catch (Throwable t) - { - } - } -} http://git-wip-us.apache.org/repos/asf/wicket/blob/c3a35e74/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/util/tester/ContextManager.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/util/tester/ContextManager.java b/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/util/tester/ContextManager.java deleted file mode 100644 index fb9eeb1..0000000 --- a/wicket-experimental/wicket-cdi-1.1/src/test/java/org/apache/wicket/cdi/util/tester/ContextManager.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.util.tester; - -import javax.annotation.PostConstruct; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.spi.BeanManager; -import javax.inject.Inject; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; - -import org.jboss.weld.bean.builtin.BeanManagerProxy; -import org.jboss.weld.servlet.HttpContextLifecycle; -import org.jboss.weld.servlet.spi.helpers.AcceptingHttpContextActivationFilter; -import org.jglue.cdiunit.internal.LifecycleAwareRequest; - -/** - * @author jsarman - */ -@ApplicationScoped -public class ContextManager -{ - private HttpServletRequest currentRequest; - - @Inject - private BeanManager beanManager; - - private HttpContextLifecycle lifecycle; - - private HttpSession currentSession; - - @PostConstruct - public void setup() - { - try - { - lifecycle = new HttpContextLifecycle(BeanManagerProxy.unwrap(beanManager), - AcceptingHttpContextActivationFilter.INSTANCE, true, true); - } - catch (NoSuchMethodError e) - { - try - { - lifecycle = HttpContextLifecycle.class.getConstructor(BeanManager.class, - AcceptingHttpContextActivationFilter.class).newInstance( - BeanManagerProxy.unwrap(beanManager), - AcceptingHttpContextActivationFilter.INSTANCE); - } - catch (Exception e1) - { - throw new RuntimeException(e1); - } - } - lifecycle.setConversationActivationEnabled(true); - } - - public void activateContexts(HttpServletRequest request) - { - if (currentRequest != null) - return; - - currentRequest = new LifecycleAwareRequest(lifecycle, request, currentSession); - lifecycle.requestInitialized(currentRequest, null); - } - - public void deactivateContexts() - { - lifecycle.requestDestroyed(currentRequest); - currentSession = currentRequest.getSession(false); - currentRequest = null; - } - - public void destroy() - { - if (currentRequest != null) - { - currentSession = currentRequest.getSession(false); - } - - if (currentSession != null) - { - lifecycle.sessionDestroyed(currentSession); - currentSession = null; - } - } - - public boolean isRequestActive() - { - return currentRequest != null; - } -}
