You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by cr...@apache.org on 2007/03/06 22:17:53 UTC

svn commit: r515308 - in /beehive/trunk/netui: src/scoping/org/apache/beehive/netui/pageflow/scoping/internal/ScopedResponseImpl.java test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedResponseTest.java

Author: crogers
Date: Tue Mar  6 13:17:52 2007
New Revision: 515308

URL: http://svn.apache.org/viewvc?view=rev&rev=515308
Log:
Fixed NPE in ScopedResponseImpl.getFirstHeader() (BEEHIVE-1182). Also added a new JUnit test for the ScopedResponse.

Tests: NetUI BVT (WinXP passed)


Added:
    beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedResponseTest.java   (with props)
Modified:
    beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/internal/ScopedResponseImpl.java

Modified: beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/internal/ScopedResponseImpl.java
URL: http://svn.apache.org/viewvc/beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/internal/ScopedResponseImpl.java?view=diff&rev=515308&r1=515307&r2=515308
==============================================================================
--- beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/internal/ScopedResponseImpl.java (original)
+++ beehive/trunk/netui/src/scoping/org/apache/beehive/netui/pageflow/scoping/internal/ScopedResponseImpl.java Tue Mar  6 13:17:52 2007
@@ -33,9 +33,9 @@
 
 
 /**
- * A wrapper around HttpServletResponse, associated with a given scope-key.  Delegates to the wrapped
- * response object for some functionality, but prevents output or error codes or forwards from actually
- * happening.
+ * A wrapper around HttpServletResponse, associated with a given scope-key.
+ * Delegates to the wrapped response object for some functionality, but
+ * prevents output or error codes or forwards from actually happening.
  */
 public class ScopedResponseImpl
         extends HttpServletResponseWrapper
@@ -48,7 +48,8 @@
     private String _redirectURI = null;
     private String _statusMessage = null;
 
-    /** Map of name (String) -> headers (List).  There can be more than one for each name. **/
+    // Map of name (String) -> headers (List).
+    // There can be more than one for each name.
     private HashMap _headers = new HashMap();
 
     private static final String SET_COOKIE = "Set-Cookie";
@@ -226,7 +227,8 @@
     /**
      * Gets all headers with the given name.
      *
-     * @return a List of headers (String, Integer, Date, Cookie), or <code>null</code> if none are found.
+     * @return a List of headers (String, Integer, Date, Cookie), or
+     *         <code>null</code> if none are found.
      */
     public List getHeaders( String name )
     {
@@ -235,13 +237,13 @@
 
     /**
      * Gets the first header with the given name.
-     * @return an Object (String, Integer, Date, Cookie) that is the first header with the given name,
-     *         or <code>null</code> if none is found.
+     * @return an Object (String, Integer, Date, Cookie) that is the first
+     *         header with the given name, or <code>null</code> if none is found.
      */
     public Object getFirstHeader( String name )
     {
         List foundHeaders = ( List ) _headers.get( name );
-        return ! foundHeaders.isEmpty() ? foundHeaders.get( 0 ) : null;
+        return foundHeaders != null && !foundHeaders.isEmpty() ? foundHeaders.get( 0 ) : null;
     }
 
     protected void addObjectHeader( String name, Object val )

Added: beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedResponseTest.java
URL: http://svn.apache.org/viewvc/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedResponseTest.java?view=auto&rev=515308
==============================================================================
--- beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedResponseTest.java (added)
+++ beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedResponseTest.java Tue Mar  6 13:17:52 2007
@@ -0,0 +1,179 @@
+/*
+ * 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.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.test.pageflow.scoping;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.beehive.netui.test.servlet.ServletFactory;
+import org.apache.beehive.netui.pageflow.scoping.ScopedRequest;
+import org.apache.beehive.netui.pageflow.scoping.ScopedResponse;
+import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils;
+
+public class ScopedResponseTest extends TestCase {
+
+    private HttpServletRequest _fauxRequest = null;
+    private HttpServletResponse _fauxResponse = null;
+    private ScopedRequest _fauxScopedRequest = null;
+    private ScopedResponse _fauxScopedResponse = null;
+    protected String _servletPath = "/somePageFlow/begin.do";
+    protected String _scopeId = "_scopeTest";
+
+    public ScopedResponseTest(String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        return new TestSuite(ScopedResponseTest.class);
+    }
+
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(suite());
+    }
+
+    public HttpServletRequest getRequest() {
+        return _fauxRequest;
+    }
+
+    public HttpServletResponse getResponse() {
+        return _fauxResponse;
+    }
+
+    public ScopedRequest getScopedRequest() {
+        return _fauxScopedRequest;
+    }
+
+    public ScopedResponse getScopedResponse() {
+        return _fauxScopedResponse;
+    }
+
+    protected void setUp() {
+        _fauxRequest = ServletFactory.getServletRequest();
+        _fauxResponse = ServletFactory.getServletResponse();
+        String requestUri = _fauxRequest.getContextPath() + _servletPath;
+        _fauxScopedRequest = ScopedServletUtils.getScopedRequest(_fauxRequest, requestUri, null, _scopeId, false);
+        _fauxScopedResponse = ScopedServletUtils.getScopedResponse(_fauxResponse, _fauxScopedRequest);
+    }
+
+    protected void tearDown() {
+    }
+
+    public void testScopedResponse() {
+        ScopedResponse scopedResponse = getScopedResponse();
+        assertTrue(getResponse() == scopedResponse.getOuterResponse());
+
+        // test that we get the same cached scoped response
+        ScopedResponse cachedScopedResponse =
+                ScopedServletUtils.getScopedResponse(getResponse(), getScopedRequest());
+        assertEquals(scopedResponse, cachedScopedResponse);
+    }
+
+    public void testScopedResponseCookies() {
+        ScopedResponse scopedResponse = getScopedResponse();
+
+        String cookieName = "cookieName";
+        String cookieValue = "cookieValue";
+        javax.servlet.http.Cookie cookie =
+                new javax.servlet.http.Cookie(cookieName, cookieValue);
+        assertNull(scopedResponse.getCookie(cookieName));
+
+        scopedResponse.addCookie(cookie);
+        assertEquals(scopedResponse.getCookie(cookieName), cookie);
+
+        javax.servlet.http.Cookie[] cookies = scopedResponse.getCookies();
+        cookies = scopedResponse.getCookies();
+        assertNotNull("The scoped response cookies was returned as null", cookies);
+        assertEquals(cookies[0], cookie);
+    }
+
+    public void testScopedResponseHeaders() {
+        ScopedResponse scopedResponse = getScopedResponse();
+
+        String headerName = "headerName";
+        String headerValue = "headerValue";
+        assertNull(scopedResponse.getFirstHeader(headerName));
+        scopedResponse.addHeader(headerName, headerValue);
+        assertEquals((String) scopedResponse.getFirstHeader(headerName),
+                     headerValue);
+
+        String anotherValue = "anotherValue";
+        scopedResponse.addHeader(headerName, anotherValue);
+        java.util.List headerList = scopedResponse.getHeaders(headerName);
+        assertEquals(headerList.size(), 2);
+
+        String intHeaderName = "intHeaderName";
+        int intHeaderValue = 411;
+        scopedResponse.addIntHeader(intHeaderName, intHeaderValue);
+        assertEquals(((Integer) scopedResponse.getFirstHeader(intHeaderName)).intValue(),
+                     intHeaderValue);
+
+        String dateHeaderName = "dateHeaderName";
+        long dateHeaderValue = 967615200000L;
+        scopedResponse.addDateHeader(dateHeaderName, dateHeaderValue);
+        java.util.Date date = (java.util.Date) scopedResponse.getFirstHeader(dateHeaderName);
+        assertEquals(date.getTime(), dateHeaderValue);
+
+        java.util.Map headers = scopedResponse.getHeaders();
+        assertEquals(headers.size(), 3);
+    }
+
+    public void testScopedResponseStatus() {
+        ScopedResponse scopedResponse = getScopedResponse();
+        scopedResponse.setStatus(HttpServletResponse.SC_NOT_FOUND);
+        assertEquals(scopedResponse.getStatusCode(), HttpServletResponse.SC_NOT_FOUND);
+        assertEquals(scopedResponse.getStatusMessage(), "");
+
+        String message = "Timed Out";
+        scopedResponse.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT, message);
+        assertEquals(scopedResponse.getStatusCode(), HttpServletResponse.SC_REQUEST_TIMEOUT);
+        assertEquals(scopedResponse.getStatusMessage(), message);
+        assertFalse(scopedResponse.isError());
+
+        try {
+            message = "Need to be authorized";
+            scopedResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, message);
+            assertTrue(scopedResponse.isError());
+            assertEquals(scopedResponse.getStatusCode(), HttpServletResponse.SC_UNAUTHORIZED);
+            assertEquals(scopedResponse.getStatusMessage(), message);
+        }
+        catch (java.io.IOException ioe) {
+            fail("Test failed on sendError(sc, message) with an IOException: "
+                 + ioe.getMessage());
+        }
+    }
+
+    public void testScopedResponseRedirects() {
+        ScopedResponse scopedResponse = getScopedResponse();
+        assertFalse(scopedResponse.didRedirect());
+        String location = "myPage.jsp";
+        try {
+            scopedResponse.sendRedirect(location);
+            assertTrue(scopedResponse.didRedirect());
+            assertEquals(scopedResponse.getRedirectURI(), location);
+        }
+        catch (java.io.IOException ioe) {
+            fail("Test failed on sendError(sc, message) with an IOException: "
+                 + ioe.getMessage());
+        }
+    }
+}

Propchange: beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/pageflow/scoping/ScopedResponseTest.java
------------------------------------------------------------------------------
    svn:eol-style = native