From f4f4901ca034b487bac00527c2626863729740d7 Mon Sep 17 00:00:00 2001 From: Michael Mosmann Date: Wed, 17 Apr 2013 22:50:16 +0200 Subject: [PATCH 1/2] bugfix --- .../wicket/util/tester/BaseWicketTester.java | 69 ++++++++++--------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java index af80fd03a87..7ed729b8ddf 100644 --- a/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java +++ b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java @@ -42,6 +42,7 @@ import javax.servlet.http.HttpSession; import junit.framework.AssertionFailedError; + import org.apache.wicket.Application; import org.apache.wicket.Component; import org.apache.wicket.IPageManagerProvider; @@ -376,7 +377,8 @@ private void setupNextRequestCycle() // Preserve response cookies in redirects // XXX: is this really needed ? Browsers wont do that, but some // Wicket tests assert that a cookie is in the response, - // even after redirects (see org.apache.wicket.util.cookies.SetCookieAndRedirectTest.statefulPage()) + // even after redirects (see +// org.apache.wicket.util.cookies.SetCookieAndRedirectTest.statefulPage()) // They should assert that the cookie is in the next *request* if (lastResponse != null && lastResponse.isRedirect()) { @@ -435,10 +437,9 @@ private void cleanupFeedbackMessages(IFeedbackMessageFilter filter) } /** - * Simulates browser behavior by preserving all non-removed cookies from - * the previous request. - * A cookie is removed if the response contains a cookie with the same - * name, path and domain and max-age=0 + * Simulates browser behavior by preserving all non-removed cookies from the previous request. A + * cookie is removed if the response contains a cookie with the same name, path and domain and + * max-age=0 */ private void transferRequestCookies() { @@ -686,7 +687,7 @@ protected boolean processRequest(final MockHttpServletRequest forcedRequest, try { - transferRequestCookies(); + // transferRequestCookies(); applyRequest(); requestCycle.scheduleRequestHandlerAfterCurrent(null); @@ -1265,10 +1266,10 @@ public FormTester newFormTester(final String path, final boolean fillBlankString * Process a component. A web page will be automatically created with the markup created in * {@link #createPageMarkup(String)}. *

- * Note: the instantiated component will have an auto-generated id. To - * reach any of its children use their relative path to the component itself. For example - * if the started component has a child a Link component with id "link" then after starting - * the component you can click it with: tester.clickLink("link") + * Note: the instantiated component will have an auto-generated id. To reach + * any of its children use their relative path to the component itself. For example if the + * started component has a child a Link component with id "link" then after starting the + * component you can click it with: tester.clickLink("link") *

* * @param @@ -1288,10 +1289,10 @@ public final C startComponentInPage(final Class compone * provided. In case pageMarkup is null, the markup will be automatically created with * {@link #createPageMarkup(String)}. *

- * Note: the instantiated component will have an auto-generated id. To - * reach any of its children use their relative path to the component itself. For example - * if the started component has a child a Link component with id "link" then after starting - * the component you can click it with: tester.clickLink("link") + * Note: the instantiated component will have an auto-generated id. To reach + * any of its children use their relative path to the component itself. For example if the + * started component has a child a Link component with id "link" then after starting the + * component you can click it with: tester.clickLink("link") *

* * @param @@ -1333,10 +1334,10 @@ public final C startComponentInPage(final Class compone * Process a component. A web page will be automatically created with markup created by the * {@link #createPageMarkup(String)}. *

- * Note: the component id is set by the user. To - * reach any of its children use this id + their relative path to the component itself. For example - * if the started component has id compId and a Link child component component with id "link" - * then after starting the component you can click it with: tester.clickLink("compId:link") + * Note: the component id is set by the user. To reach any of its children use + * this id + their relative path to the component itself. For example if the started component + * has id compId and a Link child component component with id "link" then after + * starting the component you can click it with: tester.clickLink("compId:link") *

* * @param @@ -1356,10 +1357,10 @@ public final C startComponentInPage(final C component) * provided. In case {@code pageMarkup} is null, the markup will be automatically created with * {@link #createPageMarkup(String)}. *

- * Note: the component id is set by the user. To - * reach any of its children use this id + their relative path to the component itself. For example - * if the started component has id compId and a Link child component component with id "link" - * then after starting the component you can click it with: tester.clickLink("compId:link") + * Note: the component id is set by the user. To reach any of its children use + * this id + their relative path to the component itself. For example if the started component + * has id compId and a Link child component component with id "link" then after + * starting the component you can click it with: tester.clickLink("compId:link") *

* * @param @@ -1557,7 +1558,7 @@ public Component getComponentFromLastRenderedPage(String path, String componentIdPageId = componentInPage.component.getId() + ':'; if (path.startsWith(componentIdPageId) == false) { - path = componentIdPageId + path; + path = componentIdPageId + path; } } @@ -1647,7 +1648,7 @@ public Result isVisible(final String path) if (component == null) { result = Result.fail("path: '" + path + "' does no exist for page: " + - Classes.simpleName(getLastRenderedPage().getClass())); + Classes.simpleName(getLastRenderedPage().getClass())); } else { @@ -1854,7 +1855,8 @@ public void clickLink(String path, boolean isAjax) "not be invoked when AJAX (javascript) is disabled."); } - List behaviors = WicketTesterHelper.findAjaxEventBehaviors(linkComponent, "onclick"); + List behaviors = WicketTesterHelper.findAjaxEventBehaviors( + linkComponent, "onclick"); for (AjaxEventBehavior behavior : behaviors) { executeBehavior(behavior); @@ -1865,7 +1867,8 @@ public void clickLink(String path, boolean isAjax) // Link. else if (linkComponent instanceof AjaxFallbackLink && isAjax) { - List behaviors = WicketTesterHelper.findAjaxEventBehaviors(linkComponent, "onclick"); + List behaviors = WicketTesterHelper.findAjaxEventBehaviors( + linkComponent, "onclick"); for (AjaxEventBehavior behavior : behaviors) { executeBehavior(behavior); @@ -1938,12 +1941,13 @@ else if (link instanceof ResourceLink) { Method getURL = ResourceLink.class.getDeclaredMethod("getURL", new Class[0]); getURL.setAccessible(true); - CharSequence url = (CharSequence) getURL.invoke(link); + CharSequence url = (CharSequence)getURL.invoke(link); executeUrl(url.toString()); } catch (Exception x) { - throw new RuntimeException("An error occurred while clicking on a ResourceLink", x); + throw new RuntimeException( + "An error occurred while clicking on a ResourceLink", x); } } else @@ -2250,7 +2254,7 @@ public void executeAjaxEvent(final String componentPath, final String event) * Simulates the firing of all ajax timer behaviors on the page * * @param page - * the page which timers will be executed + * the page which timers will be executed */ public void executeAllTimerBehaviors(final MarkupContainer page) { @@ -2279,7 +2283,8 @@ private void internalExecuteAllTimerBehaviors(final Component component) { if (log.isDebugEnabled()) { - log.debug("Triggering AjaxSelfUpdatingTimerBehavior: {}", component.getClassRelativePath()); + log.debug("Triggering AjaxSelfUpdatingTimerBehavior: {}", + component.getClassRelativePath()); } executeBehavior(timer); @@ -2329,8 +2334,8 @@ public void executeAjaxEvent(final Component component, final String event) checkUsability(component, true); - List ajaxEventBehaviors = WicketTesterHelper.findAjaxEventBehaviors(component, - event); + List ajaxEventBehaviors = WicketTesterHelper.findAjaxEventBehaviors( + component, event); for (AjaxEventBehavior ajaxEventBehavior : ajaxEventBehaviors) { executeBehavior(ajaxEventBehavior); From f30b7b3f06d28c84e41c1b0a5233b6f2decf5c44 Mon Sep 17 00:00:00 2001 From: Michael Mosmann Date: Wed, 17 Apr 2013 23:27:19 +0200 Subject: [PATCH 2/2] WICKET-5147 test written before fix and fixed it --- .../wicket/util/tester/BaseWicketTester.java | 2 - .../wicket/util/tester/WicketTesterTest.java | 62 ++++++++++++------- .../tester/apps_1/AbstractSetCookiePage.html | 5 ++ .../tester/apps_1/AbstractSetCookiePage.java | 28 +++++++++ .../SetCookieOnStartAndRedirectPage.java | 35 +++++++++++ .../tester/apps_1/SetCookieSecondPage.java | 22 +++++++ 6 files changed, 131 insertions(+), 23 deletions(-) create mode 100644 wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/AbstractSetCookiePage.html create mode 100644 wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/AbstractSetCookiePage.java create mode 100644 wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/SetCookieOnStartAndRedirectPage.java create mode 100644 wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/SetCookieSecondPage.java diff --git a/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java index 7ed729b8ddf..339974c557f 100644 --- a/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java +++ b/wicket-core/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java @@ -687,8 +687,6 @@ protected boolean processRequest(final MockHttpServletRequest forcedRequest, try { - // transferRequestCookies(); - applyRequest(); requestCycle.scheduleRequestHandlerAfterCurrent(null); diff --git a/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java b/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java index cedd26d1a5a..0442ad8aba7 100644 --- a/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java @@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletResponse; import junit.framework.AssertionFailedError; + import org.apache.wicket.Component; import org.apache.wicket.MockPageParametersAware; import org.apache.wicket.MockPageWithLink; @@ -69,6 +70,7 @@ import org.apache.wicket.util.tester.apps_1.Book; import org.apache.wicket.util.tester.apps_1.CreateBook; import org.apache.wicket.util.tester.apps_1.MyMockApplication; +import org.apache.wicket.util.tester.apps_1.SetCookieOnStartAndRedirectPage; import org.apache.wicket.util.tester.apps_1.SuccessPage; import org.apache.wicket.util.tester.apps_1.ViewBook; import org.apache.wicket.util.tester.apps_6.LinkPage; @@ -286,7 +288,7 @@ public void assertEnabled() throws Exception tester.assertRenderedPage(LinkPage.class); tester.getComponentFromLastRenderedPage("ajaxLinkWithSetResponsePageClass").setEnabled( - false); + false); try { tester.assertEnabled("ajaxLinkWithSetResponsePageClass"); @@ -699,7 +701,7 @@ public void submittingFormWithAjaxEventSubmitsFormValues() FormTester form = tester.newFormTester("form"); form.setValue("name", "New name"); tester.executeAjaxEvent(MockPageWithFormAndAjaxFormSubmitBehavior.EVENT_COMPONENT, - "onclick"); + "onclick"); MockPageWithFormAndAjaxFormSubmitBehavior page = (MockPageWithFormAndAjaxFormSubmitBehavior)tester.getLastRenderedPage(); Pojo pojo = page.getPojo(); @@ -865,7 +867,7 @@ public void clickResourceLinkWithSomeCommaAppendedUrl() BlockedResourceLinkPage.class.getSimpleName() + ".html,xml"; tester.executeUrl(url); assertNull("Comma separated extensions are not supported and wont find any resource", - tester.getLastResponse()); + tester.getLastResponse()); } /** @@ -925,6 +927,25 @@ public void cookieIsFoundWhenAddedToResponse() assertEquals(cookies.iterator().next().getValue(), "value"); } + /** + * Test for WICKET-5147 + */ + @Test + public void onlyOneCookieIsFoundAfterMultipleResponses() + { + for (int i = 0; i < 3; i++) + { + tester.startPage(SetCookieOnStartAndRedirectPage.class); + Collection responseCookies = tester.getLastResponse().getCookies(); + assertEquals(1, responseCookies.size()); + + Cookie responseCookie = responseCookies.iterator().next(); + assertEquals(responseCookie.getName(), "Key"); + + assertEquals(1, tester.getRequest().getCookies().length); + } + } + /** * Test for WICKET-3123 */ @@ -1162,7 +1183,7 @@ public void clickResourceLinkWithResource() /** * https://issues.apache.org/jira/browse/WICKET-4810 - * + * * Clicking on ResourceLink should deliver the resource reference's content */ @Test @@ -1171,8 +1192,9 @@ public void clickResourceLinkWithResourceReference() MockPageWithLink page = new MockPageWithLink(); String content = "content"; final ByteArrayResource resource = new ByteArrayResource("text/plain", content.getBytes(), - "fileName.txt"); - ResourceReference reference = new ResourceReference(WicketTesterTest.class, "resourceLinkWithResourceReferenceTest") + "fileName.txt"); + ResourceReference reference = new ResourceReference(WicketTesterTest.class, + "resourceLinkWithResourceReferenceTest") { @Override public IResource getResource() @@ -1319,48 +1341,46 @@ public void formSubmitSendsFormInputInRequest() } /** - * A cookie set in the request headers should not be - * expected in the response headers unless the page - * sets it explicitly. - * + * A cookie set in the request headers should not be expected in the response headers unless the + * page sets it explicitly. + * * https://issues.apache.org/jira/browse/WICKET-4989 */ @Test public void cookieSetInRequestShouldNotBeInResponse() { - //start and render the test page + // start and render the test page tester.getRequest().addCookie(new Cookie("dummy", "sample")); tester.startPage(tester.getApplication().getHomePage()); - //assert rendered page class + // assert rendered page class tester.assertRenderedPage(tester.getApplication().getHomePage()); - Assert.assertEquals("The cookie should not be in the response unless explicitly set", - 0, tester.getLastResponse().getCookies().size()); + Assert.assertEquals("The cookie should not be in the response unless explicitly set", 0, + tester.getLastResponse().getCookies().size()); // The cookie should be in each following request unless the server code // schedules it for removal it with cookie.setMaxAge(0) - Assert.assertEquals("The cookie should be in each following request", - 1, tester.getRequest().getCookies().length); + Assert.assertEquals("The cookie should be in each following request", 1, + tester.getRequest().getCookies().length); } /** - * The response cookie should not be the same instance as the request - * cookie. - * + * The response cookie should not be the same instance as the request cookie. + * * https://issues.apache.org/jira/browse/WICKET-4989 */ @Test public void doNotReuseTheSameInstanceOfTheCookieForRequestAndResponse() { - //start and render the test page + // start and render the test page String cookieName = "cookieName"; String cookieValue = "cookieValue"; Cookie requestCookie = new Cookie(cookieName, cookieValue); tester.getRequest().addCookie(requestCookie); tester.startPage(new CookiePage(cookieName, cookieValue)); - //assert rendered page class + // assert rendered page class tester.assertRenderedPage(CookiePage.class); Cookie responseCookie = tester.getLastResponse().getCookies().get(0); diff --git a/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/AbstractSetCookiePage.html b/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/AbstractSetCookiePage.html new file mode 100644 index 00000000000..8a6a4c06da4 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/AbstractSetCookiePage.html @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/AbstractSetCookiePage.java b/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/AbstractSetCookiePage.java new file mode 100644 index 00000000000..ff7f7345bb3 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/AbstractSetCookiePage.java @@ -0,0 +1,28 @@ +/* + * 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.util.tester.apps_1; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.util.cookies.CookieUtils; + +public class AbstractSetCookiePage extends WebPage +{ + protected AbstractSetCookiePage() + { + new CookieUtils().save("Key", "" + System.currentTimeMillis()); + } +} diff --git a/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/SetCookieOnStartAndRedirectPage.java b/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/SetCookieOnStartAndRedirectPage.java new file mode 100644 index 00000000000..78a44575863 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/SetCookieOnStartAndRedirectPage.java @@ -0,0 +1,35 @@ +/* + * 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.util.tester.apps_1; + +import org.apache.wicket.RestartResponseException; + + +public class SetCookieOnStartAndRedirectPage extends AbstractSetCookiePage +{ + public SetCookieOnStartAndRedirectPage() + { + + } + + @Override + protected void onBeforeRender() + { + super.onBeforeRender(); + throw new RestartResponseException(SetCookieSecondPage.class); + } +} diff --git a/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/SetCookieSecondPage.java b/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/SetCookieSecondPage.java new file mode 100644 index 00000000000..69215190426 --- /dev/null +++ b/wicket-core/src/test/java/org/apache/wicket/util/tester/apps_1/SetCookieSecondPage.java @@ -0,0 +1,22 @@ +/* + * 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.util.tester.apps_1; + +public class SetCookieSecondPage extends AbstractSetCookiePage +{ + +}