001: // StrutsTestCase - a JUnit extension for testing Struts actions
002: // within the context of the ActionServlet.
003: // Copyright (C) 2002 Deryl Seale
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the Apache Software License as
007: // published by the Apache Software Foundation; either version 1.1
008: // of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: // Apache Software Foundation Licens for more details.
014: //
015: // You may view the full text here: http://www.apache.org/LICENSE.txt
016:
017: package servletunit.tests;
018:
019: import junit.framework.TestCase;
020: import servletunit.HttpServletRequestSimulator;
021: import servletunit.ServletContextSimulator;
022:
023: import javax.servlet.RequestDispatcher;
024: import java.io.File;
025: import java.text.ParseException;
026: import java.text.SimpleDateFormat;
027: import java.util.Enumeration;
028: import java.util.Locale;
029: import java.util.Date;
030:
031: /**
032: * A Junit based test of the HttpServletResponseSimulator class
033: * @author Sean Pritchard
034: * @version 1.0
035: */
036:
037: public class TestHttpServletRequestSimulator extends TestCase {
038:
039: HttpServletRequestSimulator request;
040: ServletContextSimulator context;
041:
042: public TestHttpServletRequestSimulator(String testCase) {
043: super (testCase);
044: }
045:
046: public void setUp() {
047: context = new ServletContextSimulator();
048: request = new HttpServletRequestSimulator(context);
049: }
050:
051: public void testAddParameterArray() {
052: String[] values = { "value1", "value2" };
053: request.addParameter("name1", values);
054: String[] result = request.getParameterValues("name1");
055: if (result.length != 2)
056: fail();
057: if (!((result[0].equals("value1")) && (result[1]
058: .equals("value2"))))
059: fail();
060: }
061:
062: public void testGetParameterValuesSingle() {
063: request.addParameter("name1", "value1");
064: String[] result = request.getParameterValues("name1");
065: if (result.length != 1)
066: fail();
067: if (!(result[0].equals("value1")))
068: fail();
069: }
070:
071: public void testGetParameterWithNullKey() {
072: String result = request.getParameter(null);
073: assertNull(result);
074:
075: }
076:
077: public void testGetParameterValuesWithNullKey() {
078: String[] result = request.getParameterValues(null);
079: assertNull(result);
080:
081: }
082:
083: public void testGetParameterWithArray() {
084: String[] values = { "value1", "value2" };
085: request.addParameter("name1", values);
086: String result = request.getParameter("name1");
087: if (!(result.equals("value1")))
088: fail();
089: }
090:
091: public void testSetAttributeNullValue() {
092: request.setAttribute("test1", "value1");
093: assertEquals(request.getAttribute("test1"), "value1");
094: request.setAttribute("test1", null);
095: assertNull(request.getAttribute("test1"));
096: }
097:
098: public void testSetAttribute() {
099: request.setAttribute("test1", "value1");
100: assertEquals(request.getAttribute("test1"), "value1");
101: }
102:
103: public void testIsUserInRole() {
104: request.setUserRole("role1");
105: assertTrue(request.isUserInRole("role1"));
106: }
107:
108: public void testIsUserInRoleFalse() {
109: request.setUserRole("role2");
110: assertTrue(!request.isUserInRole("role1"));
111: }
112:
113: public void testGetServerPort() {
114: request.setServerPort(8080);
115: assertEquals(request.getServerPort(), 8080);
116: }
117:
118: public void testGetRequestURLWithQueryString() {
119: request
120: .setRequestURL("https://server:8080/my/request/url?param=1");
121: assertEquals("https://server:8080/my/request/url", request
122: .getRequestURL().toString());
123: assertEquals("param=1", request.getQueryString());
124: assertEquals("https", request.getScheme());
125: assertTrue(request.isSecure());
126: assertEquals("/my/request/url", request.getRequestURI());
127: assertEquals("server", request.getServerName());
128: assertEquals(8080, request.getServerPort());
129: }
130:
131: public void testGetRequestURLWithoutQueryStringOrPort() {
132: request.setRequestURL("http://server/my/request/url");
133: assertEquals("http://server/my/request/url", request
134: .getRequestURL().toString());
135: assertNull(request.getQueryString());
136: assertEquals("http", request.getScheme());
137: assertTrue(!request.isSecure());
138: assertEquals("/my/request/url", request.getRequestURI());
139: assertEquals("server", request.getServerName());
140: assertEquals(80, request.getServerPort());
141: }
142:
143: public void testGetSecurePort() {
144: request.setRequestURL("https://server/my/request/url?param=1");
145: assertTrue(request.isSecure());
146: assertEquals(443, request.getServerPort());
147: }
148:
149: public void testGetLocales() {
150: Enumeration enumeration = request.getLocales();
151: for (Object enumObject = enumeration.nextElement(); enumeration
152: .hasMoreElements(); enumObject = enumeration
153: .nextElement()) {
154: Locale locale = (Locale) enumObject;
155: if (!locale.equals(Locale.US))
156: fail();
157: }
158: }
159:
160: public void testGetDateHeaderNoHeader() {
161: assertEquals(-1, request.getDateHeader("foo"));
162: }
163:
164: public void testGetDateHeaderBadHeader() {
165: request.setHeader("DATE_HEADER", "foofoofoo");
166: try {
167: request.getDateHeader("DATE_HEADER");
168: } catch (IllegalArgumentException iae) {
169: return;
170: }
171: fail();
172: }
173:
174: public void testGetDateHeader() throws ParseException {
175: String date = "05/23/73 8:05 PM, PST";
176: long time = new SimpleDateFormat().parse(date).getTime();
177: request.setDateHeader("DATE_HEADER", time);
178: assertEquals(time, request.getDateHeader("DATE_HEADER"));
179: }
180:
181: public void testGetRealPath() {
182: File file = new File(System.getProperty("basedir"));
183: context.setContextDirectory(file);
184: assertEquals(new File(file, "test.html").getAbsolutePath(),
185: request.getRealPath("/test.html"));
186: }
187:
188: public void testGetRealPathNotSet() {
189: assertNull(request.getRealPath("/test.html"));
190: }
191:
192: public void testReturnsRequestDispatcher() {
193: RequestDispatcher dispatcher = request
194: .getRequestDispatcher("/test/login.jsp");
195: assertNotNull(dispatcher);
196: }
197:
198: }
|