Author: apetrelli
Date: Fri Apr 30 19:30:25 2010
New Revision: 939789
URL: http://svn.apache.org/viewvc?rev=939789&view=rev
Log:
TILESSB-31
Completed tests for tiles-request-servlet.
Added:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/NotAServletEnvironmentExceptionTest.java
(with props)
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletApplicationContextTest.java
(with props)
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletUtilTest.java
(with props)
Modified:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/NotAServletEnvironmentException.java
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/ServletRequest.java
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletRequestTest.java
Modified:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/NotAServletEnvironmentException.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/NotAServletEnvironmentException.java?rev=939789&r1=939788&r2=939789&view=diff
==============================================================================
---
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/NotAServletEnvironmentException.java
(original)
+++
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/NotAServletEnvironmentException.java
Fri Apr 30 19:30:25 2010
@@ -55,7 +55,7 @@ public class NotAServletEnvironmentExcep
* @param e The exception to be wrapped.
* @since 2.2.0
*/
- public NotAServletEnvironmentException(Exception e) {
+ public NotAServletEnvironmentException(Throwable e) {
super(e);
}
@@ -66,7 +66,7 @@ public class NotAServletEnvironmentExcep
* @param e The exception to be wrapped.
* @since 2.2.0
*/
- public NotAServletEnvironmentException(String message, Exception e) {
+ public NotAServletEnvironmentException(String message, Throwable e) {
super(message, e);
}
Modified:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/ServletRequest.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/ServletRequest.java?rev=939789&r1=939788&r2=939789&view=diff
==============================================================================
---
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/ServletRequest.java
(original)
+++
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/main/java/org/apache/tiles/request/servlet/ServletRequest.java
Fri Apr 30 19:30:25 2010
@@ -226,7 +226,7 @@ public class ServletRequest extends Abst
* @param path The path to forward to.
* @throws IOException If something goes wrong during the operation.
*/
- protected void forward(String path) throws IOException {
+ private void forward(String path) throws IOException {
RequestDispatcher rd = request.getRequestDispatcher(path);
if (rd == null) {
Added:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/NotAServletEnvironmentExceptionTest.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/NotAServletEnvironmentExceptionTest.java?rev=939789&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/NotAServletEnvironmentExceptionTest.java
(added)
+++
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/NotAServletEnvironmentExceptionTest.java
Fri Apr 30 19:30:25 2010
@@ -0,0 +1,77 @@
+/*
+ * $Id$
+ *
+ * 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.tiles.request.servlet;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Tests {...@link NotAServletEnvironmentException}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class NotAServletEnvironmentExceptionTest {
+
+ /**
+ * Test method for {...@link
NotAServletEnvironmentException#NotAServletEnvironmentException()}.
+ */
+ @Test
+ public void testNotAServletEnvironmentException() {
+ NotAServletEnvironmentException exception = new
NotAServletEnvironmentException();
+ assertNull(exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
+ /**
+ * Test method for {...@link
NotAServletEnvironmentException#NotAServletEnvironmentException(java.lang.String)}.
+ */
+ @Test
+ public void testNotAServletEnvironmentExceptionString() {
+ NotAServletEnvironmentException exception = new
NotAServletEnvironmentException("my message");
+ assertEquals("my message", exception.getMessage());
+ assertNull(exception.getCause());
+ }
+
+ /**
+ * Test method for {...@link
NotAServletEnvironmentException#NotAServletEnvironmentException(java.lang.Throwable)}.
+ */
+ @Test
+ public void testNotAServletEnvironmentExceptionThrowable() {
+ Throwable cause = new Throwable();
+ NotAServletEnvironmentException exception = new
NotAServletEnvironmentException(cause);
+ assertEquals(cause.toString(), exception.getMessage());
+ assertEquals(cause, exception.getCause());
+ }
+
+ /**
+ * Test method for {...@link
NotAServletEnvironmentException#NotAServletEnvironmentException(java.lang.String,
java.lang.Throwable)}.
+ */
+ @Test
+ public void testNotAServletEnvironmentExceptionStringThrowable() {
+ Throwable cause = new Throwable();
+ NotAServletEnvironmentException exception = new
NotAServletEnvironmentException("my message", cause);
+ assertEquals("my message", exception.getMessage());
+ assertEquals(cause, exception.getCause());
+ }
+
+}
Propchange:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/NotAServletEnvironmentExceptionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/NotAServletEnvironmentExceptionTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletApplicationContextTest.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletApplicationContextTest.java?rev=939789&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletApplicationContextTest.java
(added)
+++
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletApplicationContextTest.java
Fri Apr 30 19:30:25 2010
@@ -0,0 +1,98 @@
+package org.apache.tiles.request.servlet;
+
+import static org.easymock.EasyMock.*;
+import static org.easymock.classextension.EasyMock.*;
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Set;
+
+import javax.servlet.ServletContext;
+
+import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
+import org.apache.tiles.request.collection.ScopeMap;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests {...@link ServletApplicationContext}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ServletApplicationContextTest {
+
+ private ServletContext servletContext;
+
+ private ServletApplicationContext context;
+
+ /**
+ * Sets up the test.
+ */
+ @Before
+ public void setUp() {
+ servletContext = createMock(ServletContext.class);
+ context = new ServletApplicationContext(servletContext);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletApplicationContext#getContext()}.
+ */
+ @Test
+ public void testGetContext() {
+ replay(servletContext);
+ assertEquals(servletContext, context.getContext());
+ verify(servletContext);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletApplicationContext#getApplicationScope()}.
+ */
+ @Test
+ public void testGetApplicationScope() {
+ replay(servletContext);
+ assertTrue(context.getApplicationScope() instanceof ScopeMap);
+ verify(servletContext);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletApplicationContext#getInitParams()}.
+ */
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testGetInitParams() {
+ replay(servletContext);
+ assertTrue(context.getInitParams() instanceof ReadOnlyEnumerationMap);
+ verify(servletContext);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletApplicationContext#getResource(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ */
+ @Test
+ public void testGetResource() throws IOException {
+ URL url = new URL("http://tiles.apache.org/");
+ expect(servletContext.getResource("/my/path")).andReturn(url);
+
+ replay(servletContext);
+ assertEquals(url, context.getResource("/my/path"));
+ verify(servletContext);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletApplicationContext#getResources(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ */
+ @Test
+ public void testGetResources() throws IOException {
+ URL url = new URL("http://tiles.apache.org/");
+ expect(servletContext.getResource("/my/path")).andReturn(url);
+
+ replay(servletContext);
+ Set<URL> urls = context.getResources("/my/path");
+ assertEquals(1, urls.size());
+ assertTrue(urls.contains(url));
+ verify(servletContext);
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletApplicationContextTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletApplicationContextTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletRequestTest.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletRequestTest.java?rev=939789&r1=939788&r2=939789&view=diff
==============================================================================
---
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletRequestTest.java
(original)
+++
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletRequestTest.java
Fri Apr 30 19:30:25 2010
@@ -1,290 +1,374 @@
-/*
- * $Id$
- *
- * 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.tiles.request.servlet;
+import static org.easymock.EasyMock.*;
+import static org.easymock.classextension.EasyMock.*;
+import static org.junit.Assert.*;
+
import java.io.IOException;
import java.io.PrintWriter;
-import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import junit.framework.TestCase;
-
-import org.apache.shale.test.mock.MockHttpServletRequest;
-import org.apache.shale.test.mock.MockHttpServletResponse;
-import org.apache.shale.test.mock.MockHttpSession;
-import org.apache.shale.test.mock.MockServletContext;
import org.apache.tiles.request.ApplicationContext;
-import org.easymock.classextension.EasyMock;
+import org.apache.tiles.request.collection.AddableParameterMap;
+import org.apache.tiles.request.collection.HeaderValuesMap;
+import org.apache.tiles.request.collection.ReadOnlyEnumerationMap;
+import org.apache.tiles.request.collection.ScopeMap;
+import org.junit.Before;
+import org.junit.Test;
/**
+ * Tests {...@link ServletRequest}.
+ *
* @version $Rev$ $Date$
*/
-public class ServletRequestTest extends TestCase {
+public class ServletRequestTest {
+
+ private ApplicationContext applicationContext;
+
+ private HttpServletRequest request;
+
+ private HttpServletResponse response;
+
+ private ServletRequest req;
/**
- * The request context.
+ * Sets up the test.
*/
- private ServletRequest context;
+ @Before
+ public void setUp() {
+ applicationContext = createMock(ApplicationContext.class);
+ request = createMock(HttpServletRequest.class);
+ response = createMock(HttpServletResponse.class);
+ req = new ServletRequest(applicationContext, request, response);
+ }
/**
- * The servlet context.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#doForward(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ * @throws ServletException If something goes wrong.
*/
- private MockServletContext servletContext;
+ @Test
+ public void testDoForward() throws ServletException, IOException {
+ RequestDispatcher rd = createMock(RequestDispatcher.class);
+
+ expect(response.isCommitted()).andReturn(false);
+ expect(request.getRequestDispatcher("/my/path")).andReturn(rd);
+ rd.forward(request, response);
+
+ replay(applicationContext, request, response, rd);
+ req.doForward("/my/path");
+ verify(applicationContext, request, response, rd);
+ }
/**
- * The Tiles application context.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#doForward(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ * @throws ServletException If something goes wrong.
*/
- private ApplicationContext applicationContext;
+ @Test(expected=IOException.class)
+ public void testDoForwardNoDispatcher() throws IOException {
+ expect(response.isCommitted()).andReturn(false);
+ expect(request.getRequestDispatcher("/my/path")).andReturn(null);
+
+ replay(applicationContext, request, response);
+ try {
+ req.doForward("/my/path");
+ } finally {
+ verify(applicationContext, request, response);
+ }
+ }
- /** {...@inheritdoc} */
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- servletContext = new MockServletContext();
- applicationContext = EasyMock.createMock(ApplicationContext.class);
- Map<String, Object> applicationScope = new HashMap<String, Object>();
- applicationScope.put("applicationAttribute1", "applicationValue1");
- applicationScope.put("applicationAttribute2", "applicationValue2");
- EasyMock.expect(applicationContext.getApplicationScope()).andReturn(
- applicationScope);
- Map<String, String> initParams = new HashMap<String, String>();
- initParams.put("initParameter1", "initParameterValue1");
- EasyMock.expect(applicationContext.getInitParams()).andReturn(
- initParams);
- MockHttpSession session = new MockHttpSession(servletContext);
- MockHttpServletRequest request = new MockHttpServletRequest(session);
- MockHttpServletResponse response = new MockHttpServletResponse();
- request.addHeader("Content-Type", "text/html");
- request.addParameter("myParam", "value1");
- request.addParameter("myParam", "value2");
-
- context = new ServletRequest(applicationContext, request,
- response);
-
- Map<String, Object> requestScope = context.getRequestScope();
- requestScope.put("attribute1", "value1");
- requestScope.put("attribute2", "value2");
-
- Map<String, Object> sessionScope = context.getSessionScope();
- sessionScope.put("sessionAttribute1", "sessionValue1");
- sessionScope.put("sessionAttribute2", "sessionValue2");
- EasyMock.replay(applicationContext);
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#doForward(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ * @throws ServletException If something goes wrong.
+ */
+ @Test(expected=IOException.class)
+ public void testDoForwardServletException() throws ServletException,
IOException {
+ RequestDispatcher rd = createMock(RequestDispatcher.class);
+
+ expect(response.isCommitted()).andReturn(false);
+ expect(request.getRequestDispatcher("/my/path")).andReturn(rd);
+ rd.forward(request, response);
+ expectLastCall().andThrow(new ServletException());
+
+ replay(applicationContext, request, response, rd);
+ try {
+ req.doForward("/my/path");
+ } finally {
+ verify(applicationContext, request, response, rd);
+ }
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#doForward(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ * @throws ServletException If something goes wrong.
+ */
+ @Test
+ public void testDoForwardInclude() throws ServletException, IOException {
+ RequestDispatcher rd = createMock(RequestDispatcher.class);
+
+ expect(response.isCommitted()).andReturn(true);
+ expect(request.getRequestDispatcher("/my/path")).andReturn(rd);
+ rd.include(request, response);
+
+ replay(applicationContext, request, response, rd);
+ req.doForward("/my/path");
+ verify(applicationContext, request, response, rd);
}
/**
- * Tests getting the header.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#doInclude(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ * @throws ServletException If something goes wrong.
*/
+ @Test
+ public void testDoInclude() throws IOException, ServletException {
+ RequestDispatcher rd = createMock(RequestDispatcher.class);
+
+ expect(request.getRequestDispatcher("/my/path")).andReturn(rd);
+ rd.include(request, response);
+
+ replay(applicationContext, request, response, rd);
+ req.doInclude("/my/path");
+ verify(applicationContext, request, response, rd);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#doInclude(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ */
+ @Test(expected=IOException.class)
+ public void testDoIncludeNoDispatcher() throws IOException {
+ expect(request.getRequestDispatcher("/my/path")).andReturn(null);
+
+ replay(applicationContext, request, response);
+ try {
+ req.doInclude("/my/path");
+ } finally {
+ verify(applicationContext, request, response);
+ }
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#doInclude(java.lang.String)}.
+ * @throws IOException If something goes wrong.
+ * @throws ServletException If something goes wrong.
+ */
+ @Test(expected=IOException.class)
+ public void testDoIncludeServletException() throws IOException,
ServletException {
+ RequestDispatcher rd = createMock(RequestDispatcher.class);
+
+ expect(request.getRequestDispatcher("/my/path")).andReturn(rd);
+ rd.include(request, response);
+ expectLastCall().andThrow(new ServletException());
+
+ replay(applicationContext, request, response, rd);
+ try {
+ req.doInclude("/my/path");
+ } finally {
+ verify(applicationContext, request, response, rd);
+ }
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getHeader()}.
+ */
+ @Test
public void testGetHeader() {
- Map<String, String> map = context.getHeader();
- assertTrue("The header does not contain a set value", "text/html"
- .equals(map.get("Content-Type")));
- doTestReadMap(map, String.class, String.class, "header map");
+ assertTrue(req.getHeader() instanceof AddableParameterMap);
}
/**
- * Tests getting the header value.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getHeaderValues()}.
*/
+ @Test
public void testGetHeaderValues() {
- Map<String, String[]> map = context.getHeaderValues();
- String[] array = map.get("Content-Type");
- assertTrue("The header does not contain a set value", array.length == 1
- && "text/html".equals(array[0]));
- doTestReadMap(map, String.class, String[].class, "header values map");
+ assertTrue(req.getHeaderValues() instanceof HeaderValuesMap);
}
/**
- * Tests getting the parameters.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getParam()}.
*/
+ @SuppressWarnings("unchecked")
+ @Test
public void testGetParam() {
- Map<String, String> map = context.getParam();
- assertTrue("The parameters do not contain a set value", "value1"
- .equals(map.get("myParam"))
- || "value2".equals(map.get("myParam")));
- doTestReadMap(map, String.class, String.class, "parameter map");
+ assertTrue(req.getParam() instanceof ReadOnlyEnumerationMap);
}
/**
- * Tests getting the parameter values.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getParamValues()}.
*/
+ @SuppressWarnings("unchecked")
+ @Test
public void testGetParamValues() {
- Map<String, String[]> map = context.getParamValues();
- String[] array = map.get("myParam");
- assertTrue(
- "The parameters not contain a set value",
- array.length == 2
- && (("value1".equals(array[0]) && "value2"
- .equals(array[1])) || ("value1"
- .equals(array[1]) &&
"value2".equals(array[0]))));
- doTestReadMap(map, String.class, String[].class, "parameter values
map");
+ Map<String, String[]> paramMap = createMock(Map.class);
+
+ expect(request.getParameterMap()).andReturn(paramMap);
+
+ replay(applicationContext, request, response, paramMap);
+ assertEquals(paramMap, req.getParamValues());
+ verify(applicationContext, request, response, paramMap);
}
/**
- * Tests getting request scope attributes.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getRequestScope()}.
*/
+ @Test
public void testGetRequestScope() {
- Map<String, Object> map = context.getRequestScope();
- assertTrue("The request scope does not contain a set value", "value1"
- .equals(map.get("attribute1")));
- assertTrue("The request scope does not contain a set value", "value2"
- .equals(map.get("attribute2")));
- doTestReadMap(map, String.class, Object.class, "request scope map");
+ assertTrue(req.getRequestScope() instanceof ScopeMap);
}
/**
- * Tests getting session scope attributes.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getSessionScope()}.
*/
+ @Test
public void testGetSessionScope() {
- Map<String, Object> map = context.getSessionScope();
- assertTrue("The session scope does not contain a set value",
- "sessionValue1".equals(map.get("sessionAttribute1")));
- assertTrue("The session scope does not contain a set value",
- "sessionValue2".equals(map.get("sessionAttribute2")));
- doTestReadMap(map, String.class, Object.class, "session scope map");
+ assertTrue(req.getSessionScope() instanceof ScopeMap);
}
/**
- * Tests {...@link ServletRequest#getApplicationContext()}.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getNativeScopes()}.
*/
- public void testGetApplicationContext() {
- assertTrue("The objects are not the same",
- applicationContext == context.getApplicationContext());
+ @Test
+ public void testGetNativeScopes() {
+ assertArrayEquals(new String[] {"request", "session", "application"},
req.getNativeScopes());
}
/**
- * Tests getting application scope attributes.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getOutputStream()}.
+ * @throws IOException If something goes wrong.
*/
- public void testGetApplicationScope() {
- Map<String, Object> map = context.getApplicationScope();
- assertTrue("The application scope does not contain a set value",
- "applicationValue1".equals(map.get("applicationAttribute1")));
- assertTrue("The application scope does not contain a set value",
- "applicationValue2".equals(map.get("applicationAttribute2")));
- doTestReadMap(map, String.class, Object.class, "application scope
map");
+ @Test
+ public void testGetOutputStream() throws IOException {
+ ServletOutputStream os = createMock(ServletOutputStream.class);
+
+ expect(response.getOutputStream()).andReturn(os);
+
+ replay(applicationContext, request, response, os);
+ assertEquals(req.getOutputStream(), os);
+ verify(applicationContext, request, response, os);
}
/**
- * Tests getting init parameters.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getWriter()}.
+ * @throws IOException If something goes wrong.
*/
- public void testGetInitParams() {
- Map<String, String> map =
context.getApplicationContext().getInitParams();
- assertTrue("The init parameters do not contain a set value",
- "initParameterValue1".equals(map.get("initParameter1")));
- doTestReadMap(map, String.class, String.class,
- "init parameters scope map");
+ @Test
+ public void testGetWriter() throws IOException {
+ PrintWriter os = createMock(PrintWriter.class);
+
+ expect(response.getWriter()).andReturn(os);
+
+ replay(applicationContext, request, response, os);
+ assertEquals(req.getWriter(), os);
+ verify(applicationContext, request, response, os);
}
/**
- * Tests {...@link ServletRequest#getOutputStream()}.
- *
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getPrintWriter()}.
* @throws IOException If something goes wrong.
*/
- public void testGetOutputStream() throws IOException {
- HttpServletRequest request = EasyMock
- .createMock(HttpServletRequest.class);
- HttpServletResponse response = EasyMock
- .createMock(HttpServletResponse.class);
- ApplicationContext applicationContext = EasyMock
- .createMock(ApplicationContext.class);
- ServletOutputStream os =
EasyMock.createMock(ServletOutputStream.class);
- EasyMock.expect(response.getOutputStream()).andReturn(os);
- EasyMock.replay(request, response, applicationContext, os);
- ServletRequest requestContext = new ServletRequest(
- applicationContext, request, response);
- assertEquals(os, requestContext.getOutputStream());
- EasyMock.verify(request, response, applicationContext, os);
+ @Test
+ public void testGetPrintWriter() throws IOException {
+ PrintWriter os = createMock(PrintWriter.class);
+
+ expect(response.getWriter()).andReturn(os);
+
+ replay(applicationContext, request, response, os);
+ assertEquals(req.getPrintWriter(), os);
+ verify(applicationContext, request, response, os);
}
/**
- * Tests {...@link ServletRequest#getWriter()}.
- *
- * @throws IOException If something goes wrong.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#isResponseCommitted()}.
*/
- public void testGetWriter() throws IOException {
- HttpServletRequest request = EasyMock
- .createMock(HttpServletRequest.class);
- HttpServletResponse response = EasyMock
- .createMock(HttpServletResponse.class);
- ApplicationContext applicationContext = EasyMock
- .createMock(ApplicationContext.class);
- PrintWriter writer = EasyMock.createMock(PrintWriter.class);
- EasyMock.expect(response.getWriter()).andReturn(writer);
- EasyMock.replay(request, response, applicationContext, writer);
- ServletRequest requestContext = new ServletRequest(
- applicationContext, request, response);
- assertEquals(writer, requestContext.getWriter());
- EasyMock.verify(request, response, applicationContext, writer);
+ @Test
+ public void testIsResponseCommitted() {
+ expect(response.isCommitted()).andReturn(true);
+
+ replay(applicationContext, request, response);
+ assertTrue(req.isResponseCommitted());
+ verify(applicationContext, request, response);
}
/**
- * Tests {...@link ServletRequest#getPrintWriter()}.
- *
- * @throws IOException If something goes wrong.
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#setContentType(java.lang.String)}.
*/
- public void testGetPrintWriter() throws IOException {
- HttpServletRequest request = EasyMock
- .createMock(HttpServletRequest.class);
- HttpServletResponse response = EasyMock
- .createMock(HttpServletResponse.class);
- ApplicationContext applicationContext = EasyMock
- .createMock(ApplicationContext.class);
- PrintWriter writer = EasyMock.createMock(PrintWriter.class);
- EasyMock.expect(response.getWriter()).andReturn(writer);
- EasyMock.replay(request, response, applicationContext, writer);
- ServletRequest requestContext = new ServletRequest(
- applicationContext, request, response);
- assertEquals(writer, requestContext.getPrintWriter());
- EasyMock.verify(request, response, applicationContext, writer);
+ @Test
+ public void testSetContentType() {
+ response.setContentType("text/html");
+
+ replay(applicationContext, request, response);
+ req.setContentType("text/html");
+ verify(applicationContext, request, response);
}
/**
- * Tests a generic map.
- *
- * @param <K> The key type.
- * @param <V> The value type.
- * @param currentMap The map to check.
- * @param keyClass The key class.
- * @param valueClass The value class.
- * @param mapName The name of the map to test (for messages).
- */
- private <K, V> void doTestReadMap(Map<K, V> currentMap, Class<K> keyClass,
- Class<V> valueClass, String mapName) {
- int size1, size2;
- size1 = currentMap.keySet().size();
- size2 = currentMap.entrySet().size();
- assertEquals("The map" + mapName
- + " has keySet and entrySet of different size", size1, size2);
- for (K key : currentMap.keySet()) {
- assertTrue("The key is not of class" + keyClass.getName(), keyClass
- .isInstance(key));
- V value = currentMap.get(key);
- assertTrue("The value is not of class" + valueClass.getName(),
- valueClass.isInstance(value));
- assertTrue("The map " + mapName
- + " does not return the correct value for 'containsValue'",
- currentMap.containsValue(value));
- }
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getRequestLocale()}.
+ */
+ @Test
+ public void testGetRequestLocale() {
+ Locale locale = Locale.ITALY;
+
+ expect(request.getLocale()).andReturn(locale);
+
+ replay(applicationContext, request, response);
+ assertEquals(locale, req.getRequestLocale());
+ verify(applicationContext, request, response);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getRequestObjects()}.
+ */
+ @Test
+ public void testGetRequestObjects() {
+ replay(applicationContext, request, response);
+ assertArrayEquals(new Object[] {request, response},
req.getRequestObjects());
+ verify(applicationContext, request, response);
}
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getRequest()}.
+ */
+ @Test
+ public void testGetRequest() {
+ replay(applicationContext, request, response);
+ assertEquals(request, req.getRequest());
+ verify(applicationContext, request, response);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#getResponse()}.
+ */
+ @Test
+ public void testGetResponse() {
+ replay(applicationContext, request, response);
+ assertEquals(response, req.getResponse());
+ verify(applicationContext, request, response);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletRequest#isUserInRole(java.lang.String)}.
+ */
+ @Test
+ public void testIsUserInRole() {
+ expect(request.isUserInRole("myrole")).andReturn(true);
+
+ replay(applicationContext, request, response);
+ assertTrue(req.isUserInRole("myrole"));
+ verify(applicationContext, request, response);
+ }
+
}
Added:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletUtilTest.java
URL:
http://svn.apache.org/viewvc/tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletUtilTest.java?rev=939789&view=auto
==============================================================================
---
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletUtilTest.java
(added)
+++
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletUtilTest.java
Fri Apr 30 19:30:25 2010
@@ -0,0 +1,66 @@
+/**
+ *
+ */
+package org.apache.tiles.request.servlet;
+
+import static org.easymock.classextension.EasyMock.*;
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.apache.tiles.request.ApplicationContext;
+import org.apache.tiles.request.util.ApplicationAccess;
+import org.junit.Test;
+
+/**
+ * Tests {...@link ServletUtil}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ServletUtilTest {
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletUtil#wrapServletException(javax.servlet.ServletException,
java.lang.String)}.
+ */
+ @Test
+ public void testWrapServletException() {
+ ServletException servletException = new ServletException();
+ IOException exception =
ServletUtil.wrapServletException(servletException, "my message");
+ assertEquals(servletException, exception.getCause());
+ assertEquals("my message", exception.getMessage());
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletUtil#wrapServletException(javax.servlet.ServletException,
java.lang.String)}.
+ */
+ @Test
+ public void testWrapServletExceptionWithCause() {
+ Throwable cause = createMock(Throwable.class);
+
+ replay(cause);
+ ServletException servletException = new ServletException(cause);
+ IOException exception =
ServletUtil.wrapServletException(servletException, "my message");
+ assertEquals(cause, exception.getCause());
+ assertEquals("my message", exception.getMessage());
+ verify(cause);
+ }
+
+ /**
+ * Test method for {...@link
org.apache.tiles.request.servlet.ServletUtil#getApplicationContext(javax.servlet.ServletContext)}.
+ */
+ @Test
+ public void testGetApplicationContext() {
+ ServletContext servletContext = createMock(ServletContext.class);
+ ApplicationContext applicationContext =
createMock(ApplicationContext.class);
+
+ expect(servletContext.getAttribute(ApplicationAccess
+ .APPLICATION_CONTEXT_ATTRIBUTE)).andReturn(applicationContext);
+
+ replay(servletContext, applicationContext);
+ assertEquals(applicationContext,
ServletUtil.getApplicationContext(servletContext));
+ verify(servletContext, applicationContext);
+ }
+}
Propchange:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletUtilTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/sandbox/trunk/tiles-request/tiles-request-servlet/src/test/java/org/apache/tiles/request/servlet/ServletUtilTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL