001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.upload.services;
016:
017: import static org.easymock.EasyMock.*;
018: import static org.testng.Assert.*;
019:
020: import org.apache.tapestry.upload.services.ParametersServletRequestWrapper;
021: import org.testng.annotations.Test;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import java.util.Enumeration;
025: import java.util.HashSet;
026: import java.util.Map;
027: import java.util.Set;
028:
029: public class ParametersServletRequestWrapperTest {
030: @Test
031: public void getParameterNamesIsNotDelegated() throws Exception {
032: HttpServletRequest request = createMock(HttpServletRequest.class);
033:
034: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
035: request);
036:
037: replay(request);
038:
039: Enumeration names = wrapper.getParameterNames();
040:
041: verify(request);
042:
043: assertNotNull(names);
044: assertFalse(names.hasMoreElements());
045: }
046:
047: @Test
048: public void getParameterMapIsNotDelegated() throws Exception {
049: HttpServletRequest request = createMock(HttpServletRequest.class);
050:
051: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
052: request);
053:
054: replay(request);
055:
056: Map parameters = wrapper.getParameterMap();
057:
058: verify(request);
059: assertNotNull(parameters);
060: assertTrue(parameters.isEmpty());
061: }
062:
063: @Test
064: public void getParameterIsNotDelegated() throws Exception {
065: HttpServletRequest request = createMock(HttpServletRequest.class);
066:
067: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
068: request);
069:
070: replay(request);
071:
072: String value = wrapper.getParameter("foo");
073:
074: verify(request);
075: assertNull(value);
076: }
077:
078: @Test
079: public void getParameterValuesIsNotDelegated() throws Exception {
080: HttpServletRequest request = createMock(HttpServletRequest.class);
081:
082: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
083: request);
084:
085: replay(request);
086:
087: String[] values = wrapper.getParameterValues("foo");
088:
089: verify(request);
090: assertNull(values);
091: }
092:
093: @Test
094: public void getParameterForSingleValue() throws Exception {
095: HttpServletRequest request = createMock(HttpServletRequest.class);
096: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
097: request);
098: replay(request);
099:
100: wrapper.addParameter("foo", "blah");
101:
102: assertEquals(wrapper.getParameter("foo"), "blah");
103: verify(request);
104: }
105:
106: @Test
107: public void getParameterForMultiValueGivesFirstValue()
108: throws Exception {
109: HttpServletRequest request = createMock(HttpServletRequest.class);
110: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
111: request);
112: replay(request);
113:
114: wrapper.addParameter("foo", "blah");
115: wrapper.addParameter("foo", "another");
116:
117: assertEquals(wrapper.getParameter("foo"), "blah");
118: verify(request);
119: }
120:
121: @Test
122: public void getParameterValuesForMultiValueGivesAll()
123: throws Exception {
124: HttpServletRequest request = createMock(HttpServletRequest.class);
125: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
126: request);
127: replay(request);
128:
129: wrapper.addParameter("foo", "blah");
130: wrapper.addParameter("foo", "another");
131:
132: assertEquals(wrapper.getParameterValues("foo"), new String[] {
133: "blah", "another" });
134: verify(request);
135: }
136:
137: @Test
138: public void getParameterNamesHasAllNames() throws Exception {
139: HttpServletRequest request = createMock(HttpServletRequest.class);
140: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
141: request);
142: replay(request);
143:
144: wrapper.addParameter("one", "blah");
145: wrapper.addParameter("two", "another");
146:
147: Enumeration nameEnumerator = wrapper.getParameterNames();
148: Set<String> names = new HashSet<String>();
149: assertTrue(nameEnumerator.hasMoreElements());
150: names.add((String) nameEnumerator.nextElement());
151: assertTrue(nameEnumerator.hasMoreElements());
152: names.add((String) nameEnumerator.nextElement());
153:
154: assertTrue(names.contains("one"));
155: assertTrue(names.contains("two"));
156: verify(request);
157: }
158:
159: @Test
160: public void getParameterMapHasAllValues() throws Exception {
161: HttpServletRequest request = createMock(HttpServletRequest.class);
162: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
163: request);
164: replay(request);
165:
166: wrapper.addParameter("single", "blah");
167: wrapper.addParameter("multi", "one");
168: wrapper.addParameter("multi", "two");
169:
170: Map parameters = wrapper.getParameterMap();
171: assertEquals(parameters.size(), 2);
172: assertEquals(parameters.get("single"), "blah");
173: assertEquals((String[]) parameters.get("multi"), new String[] {
174: "one", "two" });
175:
176: verify(request);
177: }
178: }
|