A couple of weeks ago i bugged Johan one night about WicketTester not
being able to handle temporary wicket sessions, i probably did not
explain myself very well at that time because he did not seem to get
my point.
It was not that big a deal then and i decided to take a crack at it
some time later.
Sometime later happened to be when most of the devs where in Thailand,
but i got it to work and i thought i share my findings here. Maybe
WicketTester can be patched or maybe someone else can benefit from
this at a later time.

Lets see if i can explain myself better this time :)
Because MockWebApplication automatically does
application.getSessionStore().bind(wicketRequest, wicketSession); in
setupRequestAndResponse the session is automatically bound and it is
not possible to test for Session.isTemporary() and thus truly testing
your statelesspages.
I know you have a StatelessComponentTest but it does not test if a
session is bound, which is what i wanted to test in order to test my
bind() moment.
As it turned out all i had to do was make a few small changes to make
it work, see patch below.
Theoretically all the test should still run fine after my
modifications. Unfortunately they did not and i had to build in a
boolean to revert to the old behavior. I have not looked extensively
why some of the tests failed withe the temporary session, maybe one of
the devs can take a look.
In the patch you will also notice i changed
MockWebApplication#postProcessRequestCycle from private to public this
is because processRequestCycle throws away the requestcycle and i
needed a way to process the request without that side effect. To
enable temporary sessions overwrite
MockWebApplication#initializeHttpSessionAsTemporary to return true.
Feel free to use / modify the code in the patch as i have made a copy
in my tests for the required classes. The changes are small and should
be self explanatory.

Maurice

Index: src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java
===================================================================
--- src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java   
(revision
618675)
+++ src/main/java/org/apache/wicket/protocol/http/MockHttpServletRequest.java   
(working
copy)
@@ -174,7 +174,7 @@

        private String path;

-       private final HttpSession session;
+       private final MockHttpSession session;

        private String url;

@@ -192,7 +192,7 @@
         * @param context
         *            The current servlet context
         */
-       public MockHttpServletRequest(final Application application, final
HttpSession session,
+       public MockHttpServletRequest(final Application application, final
MockHttpSession session,
                        final ServletContext context)
        {
                this.application = application;
@@ -779,6 +779,8 @@
         */
        public String getRequestedSessionId()
        {
+               if (session.isTemporary())
+                       return null;
                return session.getId();
        }

@@ -870,6 +872,8 @@
         */
        public HttpSession getSession()
        {
+               if (session.isTemporary())
+                       return null;
                return session;
        }

@@ -882,7 +886,9 @@
         */
        public HttpSession getSession(boolean b)
        {
-               return session;
+               if (b)
+                       session.setTemporary(false);
+               return getSession();
        }

        /**
Index: src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java
===================================================================
--- src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java  
(revision
618675)
+++ src/main/java/org/apache/wicket/protocol/http/MockHttpSession.java  (working
copy)
@@ -45,6 +45,8 @@
        private final String id = (new UID()).toString().replace(':',
'_').replace('-', '_');

        private long lastAccessedTime = 0;
+       
+       private boolean temporary = true;

        /**
         * Create the session.
@@ -250,4 +252,24 @@
        {
                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;
+       }
 }
\ No newline at end of file
Index: src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java
===================================================================
--- src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java       
(revision
630015)
+++ src/main/java/org/apache/wicket/protocol/http/MockWebApplication.java       
(working
copy)
@@ -17,7 +17,6 @@
 package org.apache.wicket.protocol.http;

 import java.io.File;
-import java.io.IOException;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
@@ -187,6 +186,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);

@@ -216,6 +216,11 @@
                
this.application.getResourceSettings().setResourcePollFrequency(null);
        }

+       public boolean initializeHttpSessionAsTemporary()
+       {
+               return false;
+       }
+
        /**
         * Used to create a new mock servlet context.
         *
@@ -385,9 +390,10 @@
                try
                {
                        cycle.request();
-                       if (cycle.wasHandled() == false)
+                       if (cycle.wasHandled() == false)
                        {
-                               cycle.setRequestTarget(new
WebErrorCodeResponseTarget(HttpServletResponse.SC_NOT_FOUND));
+                               cycle.setRequestTarget(new 
WebErrorCodeResponseTarget(
+                                       HttpServletResponse.SC_NOT_FOUND));
                        }
                        createRequestCycle();
                }
@@ -402,7 +408,7 @@
         *
         * @param cycle
         */
-       private void postProcessRequestCycle(WebRequestCycle cycle)
+       public final void postProcessRequestCycle(WebRequestCycle cycle)
        {
                previousRenderedPage = lastRenderedPage;

@@ -529,7 +535,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;
        }

Reply via email to