Author: ivaynberg
Date: Mon Mar  1 16:03:22 2010
New Revision: 917565

URL: http://svn.apache.org/viewvc?rev=917565&view=rev
Log:
a messy rework to reintroduce mockhttpservletrequest back into tester to make 
multipart testing possible again

Modified:
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java
    
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java?rev=917565&r1=917564&r2=917565&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/mock/MockHttpServletRequest.java
 Mon Mar  1 16:03:22 2010
@@ -19,9 +19,9 @@
 import java.io.BufferedReader;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.CharArrayReader;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.security.Principal;
@@ -46,6 +46,8 @@
 
 import org.apache.wicket.Application;
 import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.ng.mock.MockRequestParameters;
+import org.apache.wicket.ng.request.Url;
 import org.apache.wicket.protocol.http.RequestUtils;
 import org.apache.wicket.protocol.http.WicketURLDecoder;
 import org.apache.wicket.protocol.http.WicketURLEncoder;
@@ -379,7 +381,8 @@
         */
        public String getContextPath()
        {
-               return "/" + application.getName();
+               // return "/" + application.getName();
+               return "/context";
        }
 
        /**
@@ -730,7 +733,7 @@
         */
        public BufferedReader getReader() throws IOException
        {
-               return new BufferedReader(new CharArrayReader(new char[0]));
+               return new BufferedReader(new 
InputStreamReader(getInputStream()));
        }
 
        /**
@@ -890,7 +893,7 @@
         */
        public String getServletPath()
        {
-               return getContextPath();
+               return "/servlet";
        }
 
        /**
@@ -1411,13 +1414,30 @@
         */
        private byte[] buildRequest()
        {
+               if (uploadedFiles == null)
+               {
+                       if (post.getParameterNames().size() == 0)
+                       {
+                               return "".getBytes();
+                       }
+                       Url url = new Url();
+                       for (Iterator<String> iterator = 
post.getParameterNames().iterator(); iterator.hasNext();)
+                       {
+                               final String name = iterator.next();
+                               url.setQueryParameter(name, 
post.getParameterValue(name.toString()));
+                       }
+                       String body = url.toString().substring(1);
+                       return body.getBytes();
+               }
+
+
                try
                {
                        // Build up the input stream based on the files and 
parameters
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
                        // Add parameters
-                       for (Iterator<String> iterator = 
parameters.keySet().iterator(); iterator.hasNext();)
+                       for (Iterator<String> iterator = 
post.getParameterNames().iterator(); iterator.hasNext();)
                        {
                                final String name = iterator.next();
                                newAttachment(out);
@@ -1426,7 +1446,7 @@
                                out.write("\"".getBytes());
                                out.write(crlf.getBytes());
                                out.write(crlf.getBytes());
-                               
out.write(parameters.get(name).toString().getBytes());
+                               
out.write(post.getParameterValue(name).toString().getBytes());
                                out.write(crlf.getBytes());
                        }
 
@@ -1502,4 +1522,29 @@
        {
                return 80;
        }
+
+       public void setUrl(Url url)
+       {
+               setURL(url.toString());
+       }
+
+       public Url getUrl()
+       {
+               String url = getRequestURI();
+               url += "?" + getQueryString();
+               return Url.parse(url);
+       }
+
+       public MockRequestParameters getPostParameters()
+       {
+               return post;
+       }
+
+
+       public String getFilterPrefix()
+       {
+               return getServletPath().substring(1);
+       }
+
+       private final MockRequestParameters post = new MockRequestParameters();
 }

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java?rev=917565&r1=917564&r2=917565&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebRequest.java
 Mon Mar  1 16:03:22 2010
@@ -105,8 +105,9 @@
                }
                StringBuilder url = new StringBuilder();
                String uri = request.getRequestURI();
-               
url.append(Strings.stripJSessionId(uri.substring(request.getContextPath().length()
 +
-                       filterPrefix.length() + 1)));
+               uri = Strings.stripJSessionId(uri);
+               final int start = request.getContextPath().length() + 
filterPrefix.length() + 1;
+               url.append(uri.substring(start));
 
                String query = request.getQueryString();
                if (!Strings.isEmpty(query))

Modified: 
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java?rev=917565&r1=917564&r2=917565&view=diff
==============================================================================
--- 
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
 (original)
+++ 
wicket/trunk/wicket/src/main/java/org/apache/wicket/util/tester/BaseWicketTester.java
 Mon Mar  1 16:03:22 2010
@@ -67,7 +67,6 @@
 import org.apache.wicket.ng.mock.MockApplication;
 import org.apache.wicket.ng.mock.MockPageManager;
 import org.apache.wicket.ng.mock.MockSessionStore;
-import org.apache.wicket.ng.mock.MockWebRequest;
 import org.apache.wicket.ng.mock.MockWebResponse;
 import org.apache.wicket.ng.request.IRequestMapper;
 import org.apache.wicket.ng.request.Url;
@@ -86,6 +85,9 @@
 import org.apache.wicket.pageStore.IPageManagerContext;
 import org.apache.wicket.protocol.http.MockServletContext;
 import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
+import org.apache.wicket.protocol.http.mock.MockHttpSession;
+import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
 import org.apache.wicket.session.ISessionStore;
 import org.apache.wicket.settings.IRequestCycleSettings.RenderStrategy;
 import org.apache.wicket.util.IProvider;
@@ -140,22 +142,24 @@
                }
        }
 
+       private org.apache.wicket.protocol.http.mock.MockServletContext 
servletContext;
+       private MockHttpSession hsession;
 
        private final WebApplication application;
 
        private boolean followRedirects = true;
        private int redirectCount;
 
-       private MockWebRequest lastRequest;
+       private MockHttpServletRequest lastRequest;
        private MockWebResponse lastResponse;
 
-       private final List<MockWebRequest> previousRequests = new 
ArrayList<MockWebRequest>();
+       private final List<MockHttpServletRequest> previousRequests = new 
ArrayList<MockHttpServletRequest>();
        private final List<MockWebResponse> previousResponses = new 
ArrayList<MockWebResponse>();
 
        private final ThreadContext oldThreadContext;
 
        /** current request */
-       private MockWebRequest request;
+       private MockHttpServletRequest request;
        /** current response */
        private MockWebResponse response;
 
@@ -227,6 +231,10 @@
         */
        public BaseWicketTester(final WebApplication application)
        {
+               servletContext = new 
org.apache.wicket.protocol.http.mock.MockServletContext(application,
+                       "");
+               hsession = new MockHttpSession(servletContext);
+
                oldThreadContext = ThreadContext.detach();
 
                this.application = application;
@@ -255,11 +263,13 @@
 
        private void setupNextRequestCycle()
        {
-               request = new MockWebRequest(Url.parse("/"));
+               request = new MockHttpServletRequest(application, hsession, 
servletContext);
+               request.setURL(request.getContextPath() + 
request.getServletPath() + "/");
                response = new MockWebResponse();
 
 
-               requestCycle = application.createRequestCycle(request, 
response);
+               requestCycle = application.createRequestCycle(new 
ServletWebRequest(request,
+                       request.getFilterPrefix()), response);
                requestCycle.setCleanupFeedbackMessagesOnDetach(false);
                ThreadContext.setRequestCycle(requestCycle);
 
@@ -281,15 +291,15 @@
                ThreadContext.setSession(session);
        }
 
-       public MockWebRequest getRequest()
+       public MockHttpServletRequest getRequest()
        {
                return request;
        }
 
-       public void setRequest(MockWebRequest request)
+       public void setRequest(MockHttpServletRequest request)
        {
                this.request = request;
-               requestCycle.setRequest(request);
+               requestCycle.setRequest(new ServletWebRequest(request, 
request.getServletPath()));
        }
 
 
@@ -348,7 +358,7 @@
         *            request to process
         * 
         */
-       public void processRequest(MockWebRequest request)
+       public void processRequest(MockHttpServletRequest request)
        {
                processRequest(request, null);
        }
@@ -363,7 +373,8 @@
         *            optional parameter to override parsing the request URL 
and force
         *            {...@link IRequestHandler}
         */
-       public boolean processRequest(MockWebRequest request, IRequestHandler 
forcedRequestHandler)
+       public boolean processRequest(MockHttpServletRequest request,
+               IRequestHandler forcedRequestHandler)
        {
                return processRequest(request, forcedRequestHandler, false);
        }
@@ -374,7 +385,7 @@
        }
 
 
-       private boolean processRequest(MockWebRequest forcedRequest,
+       private boolean processRequest(MockHttpServletRequest forcedRequest,
                IRequestHandler forcedRequestHandler, boolean redirect)
        {
 
@@ -413,7 +424,7 @@
                                }
                        }
 
-                       requestCycle.setRequest(request);
+                       requestCycle.setRequest(new ServletWebRequest(request, 
request.getFilterPrefix()));
 
                        if (!requestCycle.processRequestAndDetach())
                        {
@@ -468,9 +479,12 @@
 
                // transfer cookies from previous request to previous response, 
quirky but how old stuff
                // worked...
-               for (Cookie cookie : lastRequest.getCookies())
+               if (lastRequest.getCookies() != null)
                {
-                       lastResponse.addCookie(cookie);
+                       for (Cookie cookie : lastRequest.getCookies())
+                       {
+                               lastResponse.addCookie(cookie);
+                       }
                }
 
        }
@@ -488,7 +502,8 @@
         */
        public Page startPage(IPageProvider pageProvider)
        {
-               request = new MockWebRequest(new Url());
+               request = new MockHttpServletRequest(application, hsession, 
servletContext);
+               request.setURL(request.getContextPath() + 
request.getServletPath() + "/");
                IRequestHandler handler = new 
RenderPageRequestHandler(pageProvider);
                processRequest(request, handler);
                return getLastRenderedPage();
@@ -522,7 +537,7 @@
        /**
         * @return list of prior requests
         */
-       public List<MockWebRequest> getPreviousRequests()
+       public List<MockHttpServletRequest> getPreviousRequests()
        {
                return Collections.unmodifiableList(previousRequests);
        }
@@ -617,8 +632,10 @@
                        component.getPage(), component), listener);
 
                Url url = urlFor(handler);
-
-               processRequest(new MockWebRequest(url), null);
+               MockHttpServletRequest request = new 
MockHttpServletRequest(application, hsession,
+                       servletContext);
+               request.setUrl(url);
+               processRequest(request, null);
        }
 
 
@@ -651,8 +668,8 @@
                Url url = Url.parse(behavior.getCallbackUrl().toString());
                transform(url);
                request.setUrl(url);
-               request.setHeader("Wicket-Ajax-BaseURL", url.toString());
-               request.setHeader("Wicket-Ajax", "Wicket-Ajax");
+               request.addHeader("Wicket-Ajax-BaseURL", url.toString());
+               request.addHeader("Wicket-Ajax", "Wicket-Ajax");
                processRequest();
        }
 
@@ -668,8 +685,8 @@
        {
                transform(url);
                request.setUrl(url);
-               request.setHeader("Wicket-Ajax-BaseURL", url.toString());
-               request.setHeader("Wicket-Ajax", "Wicket-Ajax");
+               request.addHeader("Wicket-Ajax-BaseURL", url.toString());
+               request.addHeader("Wicket-Ajax", "Wicket-Ajax");
                processRequest();
        }
 
@@ -1548,9 +1565,10 @@
 
                Url url = Url.parse(behavior.getCallbackUrl().toString());
                transform(url);
-               request.setHeader("Wicket-Ajax-BaseURL", url.toString());
-               request.setHeader("Wicket-Ajax", "Wicket-Ajax");
-               processRequest(request.requestWithUrl(url), null);
+               request.addHeader("Wicket-Ajax-BaseURL", url.toString());
+               request.addHeader("Wicket-Ajax", "Wicket-Ajax");
+               request.setUrl(url);
+               processRequest(request, null);
        }
 
        /**
@@ -1658,7 +1676,7 @@
                return response;
        }
 
-       public MockWebRequest getLastRequest()
+       public MockHttpServletRequest getLastRequest()
        {
                return lastRequest;
        }


Reply via email to