Author: marrink Date: Mon Mar 24 08:04:57 2008 New Revision: 640436 URL: http://svn.apache.org/viewvc?rev=640436&view=rev Log: RESOLVED - issue WICKET-1410: WicketTester and temporary sessions https://issues.apache.org/jira/browse/WICKET-1410
Added: wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/stateless/TemporarySessionTest.java (with props) Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java?rev=640436&r1=640435&r2=640436&view=diff ============================================================================== --- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java (original) +++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java Mon Mar 24 08:04:57 2008 @@ -183,7 +183,8 @@ private boolean useMultiPartContentType; /** - * Create the request using the supplied session object. + * Create the request using the supplied session object. Note that in order for temporary + * sessions to work, the supplied session must be an instance of [EMAIL PROTECTED] MockHttpSession} * * @param application * The application that this request is for @@ -193,7 +194,7 @@ * The current servlet context */ public MockHttpServletRequest(final Application application, final HttpSession session, - final ServletContext context) + final ServletContext context) { this.application = application; this.session = session; @@ -233,14 +234,14 @@ if (file.exists() == false) { throw new IllegalArgumentException( - "File does not exists. You must provide an existing file: " + - file.getAbsolutePath()); + "File does not exists. You must provide an existing file: " + + file.getAbsolutePath()); } if (file.isFile() == false) { throw new IllegalArgumentException( - "You can only add a File, which is not a directory. Only files can be uploaded."); + "You can only add a File, which is not a directory. Only files can be uploaded."); } if (uploadedFiles == null) @@ -410,7 +411,7 @@ catch (ParseException e) { throw new IllegalArgumentException("Can't convert header to date " + name + ": " + - value); + value); } } @@ -779,6 +780,8 @@ */ public String getRequestedSessionId() { + if (session instanceof MockHttpSession && ((MockHttpSession)session).isTemporary()) + return null; return session.getId(); } @@ -870,6 +873,8 @@ */ public HttpSession getSession() { + if (session instanceof MockHttpSession && ((MockHttpSession)session).isTemporary()) + return null; return session; } @@ -882,7 +887,9 @@ */ public HttpSession getSession(boolean b) { - return session; + if (b && session instanceof MockHttpSession) + ((MockHttpSession)session).setTemporary(false); + return getSession(); } /** @@ -1153,7 +1160,7 @@ { parameters.putAll(params); parameters.put(WebRequestCodingStrategy.BOOKMARKABLE_PAGE_PARAMETER_NAME, page.getClass() - .getName()); + .getName()); } /** @@ -1170,7 +1177,7 @@ { final Class clazz = ((BookmarkablePageLink)component).getPageClass(); parameters.put(WebRequestCodingStrategy.BOOKMARKABLE_PAGE_PARAMETER_NAME, pageMapName + - ':' + clazz.getName()); + ':' + clazz.getName()); } else { @@ -1199,18 +1206,18 @@ else { throw new IllegalArgumentException( - "The component class doesn't seem to implement any of the known *Listener interfaces: " + - component.getClass()); + "The component class doesn't seem to implement any of the known *Listener interfaces: " + + component.getClass()); } parameters.put(WebRequestCodingStrategy.INTERFACE_PARAMETER_NAME, pageMapName + ':' + - component.getPath() + ':' + (version == 0 ? "" : "" + version) + ':' + - Classes.simpleName(clazz) + "::"); + component.getPath() + ':' + (version == 0 ? "" : "" + version) + ':' + + Classes.simpleName(clazz) + "::"); if (component.isStateless() && component.getPage().isBookmarkable()) { parameters.put(WebRequestCodingStrategy.BOOKMARKABLE_PAGE_PARAMETER_NAME, - pageMapName + ':' + component.getPage().getClass().getName()); + pageMapName + ':' + component.getPage().getClass().getName()); } } } @@ -1239,8 +1246,8 @@ String value = (String)values.get(component); if (value != null) { - parameters.put(((FormComponent)component).getInputName(), values - .get(component)); + parameters.put(((FormComponent)component).getInputName(), + values.get(component)); valuesApplied.put(component.getId(), component); } } @@ -1259,9 +1266,8 @@ diff.remove(iter.next()); } - log - .error("Parameter mismatch: didn't find all components referenced in parameter 'values': " + - diff.keySet()); + log.error("Parameter mismatch: didn't find all components referenced in parameter 'values': " + + diff.keySet()); } } @@ -1301,13 +1307,13 @@ { headers.clear(); addHeader("Accept", "text/xml,application/xml,application/xhtml+xml," - + "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); + + "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); addHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); Locale l = Locale.getDefault(); addHeader("Accept-Language", l.getLanguage().toLowerCase() + "-" + - l.getCountry().toLowerCase() + "," + l.getLanguage().toLowerCase() + ";q=0.5"); + l.getCountry().toLowerCase() + "," + l.getLanguage().toLowerCase() + ";q=0.5"); addHeader("User-Agent", - "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7) Gecko/20040707 Firefox/0.9.2"); + "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7) Gecko/20040707 Firefox/0.9.2"); } private static final String crlf = "\r\n"; Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java?rev=640436&r1=640435&r2=640436&view=diff ============================================================================== --- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java (original) +++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java Mon Mar 24 08:04:57 2008 @@ -45,6 +45,8 @@ private final String id = (new UID()).toString().replace(':', '_').replace('-', '_'); private long lastAccessedTime = 0; + + private boolean temporary = true; /** * Create the session. @@ -249,5 +251,25 @@ public void timestamp() { lastAccessedTime = System.currentTimeMillis(); + } + + /** + * Indicates the state of the session. Temporary or persisted. + * + * @return true if this is a temporary session, false otherwise + */ + public final boolean isTemporary() + { + return temporary; + } + + /** + * Changes the state of this session. Temporary or persisted. + * Uppon creation all sessions are temporary. + * @param temporary trur, for a temporary session, false for a persisted session + */ + public final void setTemporary(boolean temporary) + { + this.temporary = temporary; } } Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java?rev=640436&r1=640435&r2=640436&view=diff ============================================================================== --- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java (original) +++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java Mon Mar 24 08:04:57 2008 @@ -187,6 +187,7 @@ // Construct mock session, request and response servletSession = new MockHttpSession(context); + servletSession.setTemporary(initializeHttpSessionAsTemporary()); servletRequest = new MockHttpServletRequest(this.application, servletSession, context); servletResponse = new MockHttpServletResponse(servletRequest); @@ -217,6 +218,18 @@ } /** + * Callback to signal the application to create temporary sessions instead of normal sessions. + * This should only be used if you want to test stuff like [EMAIL PROTECTED] Session#bind()}. Default + * returns false. + * + * @return true if sessions should be temporary by default, otherwise false + */ + public boolean initializeHttpSessionAsTemporary() + { + return false; + } + + /** * Used to create a new mock servlet context. * * @param path @@ -405,7 +418,7 @@ * * @param cycle */ - private void postProcessRequestCycle(WebRequestCycle cycle) + public final void postProcessRequestCycle(WebRequestCycle cycle) { previousRenderedPage = lastRenderedPage; @@ -532,7 +545,8 @@ wicketRequest = application.newWebRequest(servletRequest); wicketResponse = application.newWebResponse(servletResponse); WebRequestCycle requestCycle = createRequestCycle(); - application.getSessionStore().bind(wicketRequest, wicketSession); + if (!initializeHttpSessionAsTemporary()) + application.getSessionStore().bind(wicketRequest, wicketSession); wicketResponse.setAjax(wicketRequest.isAjax()); return requestCycle; } Added: wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/stateless/TemporarySessionTest.java URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/stateless/TemporarySessionTest.java?rev=640436&view=auto ============================================================================== --- wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/stateless/TemporarySessionTest.java (added) +++ wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/stateless/TemporarySessionTest.java Mon Mar 24 08:04:57 2008 @@ -0,0 +1,92 @@ +/* + * 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.stateless; + +import org.apache.wicket.Session; +import org.apache.wicket.WicketTestCase; +import org.apache.wicket.protocol.http.WebApplication; +import org.apache.wicket.stateless.pages.HomePage; +import org.apache.wicket.stateless.pages.LoginPage; +import org.apache.wicket.util.tester.FormTester; +import org.apache.wicket.util.tester.WicketTester; + +/** + * A test to detect temporary sessions. However since the http stack has been mocked for this test, + * it is more a test to see if our mocks can handle temporary sessions then it is to see if wicket + * supports temporary sessions. + * + * @author marrink + */ +public class TemporarySessionTest extends WicketTestCase +{ +// private WicketTester tester; + + /** + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception + { + tester = new WicketTester(new WebApplication() + { + + public Class getHomePage() + { + return HomePage.class; + } + + protected void outputDevelopmentModeWarning() + { + // Do nothing. + } + }, "src/test/java/" + getClass().getPackage().getName().replace('.', '/')) + { + public boolean initializeHttpSessionAsTemporary() + { + return true; + } + }; + } + + + /** + * @see junit.framework.TestCase#tearDown() + */ +// protected void tearDown() throws Exception +// { +// tester.destroy(); +// } + /** + * Test if we can keep a session temporary. + */ + public void testSessionIsTemporary() + { + tester.startPage(LoginPage.class); + tester.setupRequestAndResponse(); + assertTrue(tester.getWicketSession().isTemporary()); + tester.processRequestCycle(LoginPage.class); + FormTester form = tester.newFormTester("signInPanel:signInForm"); + form.setValue("username", "test"); + form.setValue("password", "test"); + tester.getWicketSession().bind(); + form.submit(); + tester.assertRenderedPage(HomePage.class); + tester.setupRequestAndResponse(); + assertFalse(Session.get().isTemporary()); + tester.processRequestCycle(LoginPage.class); + } + +} Propchange: wicket/trunk/jdk-1.4/wicket/src/test/java/org/apache/wicket/stateless/TemporarySessionTest.java ------------------------------------------------------------------------------ svn:mime-type = text/plain