001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.tests;
016:
017: import java.util.HashMap;
018: import java.util.Map;
019: import junit.framework.TestCase;
020: import org.araneaframework.core.StandardPath;
021: import org.araneaframework.http.core.StandardServletInputData;
022: import org.araneaframework.http.util.ServletUtil;
023: import org.springframework.mock.web.MockHttpServletRequest;
024:
025: /**
026: * @author "Toomas Römer" <toomas@webmedia.ee>
027: *
028: */
029: public class StandardServletInputDataTests extends TestCase {
030: private MockHttpServletRequest request;
031: private StandardServletInputData input;
032:
033: public void setUp() {
034: request = new MockHttpServletRequest();
035: request.addParameter("foo", "bar");
036: request.addParameter("a.foo", "a bar");
037: request.addParameter("a.extra.foo", "a extra bar");
038: request.addParameter("a.extra.foo2", "a extra bar2");
039: input = new StandardServletInputData(request);
040: }
041:
042: public void testNormalGetScopedData() {
043: assertEquals("a extra bar", input.getScopedData(
044: new StandardPath("a.extra")).get("foo"));
045: }
046:
047: public void testNormalGetScopedDataMultiple() {
048: request = new MockHttpServletRequest();
049: request.addParameter("foo", "bar");
050: request.addParameter("a.foo", "a bar");
051: request.addParameter("a.extra.foo", "a extra bar");
052: request.addParameter("a.extra.foo2", "a extra bar2");
053:
054: assertEquals("a extra bar2", input.getScopedData(
055: new StandardPath("a.extra")).get("foo2"));
056: }
057:
058: //empty path has to return empty scoped data
059: public void testNoScopeGetScopedData() {
060: assertEquals(0, input.getScopedData(new StandardPath(""))
061: .size());
062: }
063:
064: public void testWrongPathGetScopedData() {
065: request = new MockHttpServletRequest();
066: request.setAttribute("a", "b");
067: request.setAttribute("a.b", "b");
068: request.setAttribute("a.b.c", "b");
069:
070: input = new StandardServletInputData(request);
071: assertEquals(null, input.getScopedData(
072: new StandardPath("a.b.c.d")).get("c"));
073: }
074:
075: public void testNonValidPath() {
076: request = new MockHttpServletRequest();
077: request.addParameter("a...foo", "b");
078: request.addParameter(".", "c");
079: request.addParameter(".", "c");
080: input = new StandardServletInputData(request);
081: assertEquals(null, input.getScopedData(new StandardPath(""))
082: .get("."));
083: }
084:
085: public void testChangeGlobalData() {
086: try {
087: input.getGlobalData().put("a", "b");
088: fail("Was able to modify a unmodifiable map");
089: } catch (UnsupportedOperationException e) {
090: //success
091: }
092: }
093:
094: public void testChangeScopedData() {
095: try {
096: //valid scope
097: input.getScopedData(new StandardPath("a.extra")).put("a",
098: "b");
099: fail("Was able to modify a unmodifiable map");
100: } catch (UnsupportedOperationException e) {
101: //success
102: }
103: }
104:
105: public void testChangeEmptyScopedData() {
106: try {
107: //invalid scope
108: input.getScopedData(
109: new StandardPath("nonexistent.scope.iam")).put("a",
110: "b");
111: fail("Was able to modify a unmodifiable map");
112: } catch (UnsupportedOperationException e) {
113: //success
114: }
115: }
116:
117: public void testGetRequest() {
118: assertEquals(ServletUtil.getRequest(input), request);
119: }
120:
121: public void testExtendNarrow() {
122: Map map = new HashMap();
123: input.extend(Map.class, map);
124: assertEquals(map, input.narrow(Map.class));
125: }
126: }
|