001: /*
002: * Copyright 2007 Gerd Ziegler (www.gerdziegler.de)
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: * http://www.apache.org/licenses/LICENSE-2.0
007: * Unless required by applicable law or agreed to in writing,
008: * software distributed under the License is distributed on an
009: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
010: * either express or implied. See the License for the specific
011: * language governing permissions and limitations under the License.
012: * 22.09.2007
013: * @author www.gerdziegler.de
014: */
015: package org.ztemplates.test.actions.urlhandler.tree.parameters;
016:
017: import java.util.HashMap;
018: import java.util.Map;
019:
020: import junit.framework.TestCase;
021:
022: import org.apache.log4j.Logger;
023: import org.ztemplates.actions.urlhandler.ZUrl;
024: import org.ztemplates.actions.urlhandler.tree.ZTreeUrlHandler;
025: import org.ztemplates.actions.urlhandler.tree.ZUrlHandlerFactory;
026:
027: public class ParameterTest extends TestCase {
028: static Logger log = Logger.getLogger(ParameterTest.class);
029:
030: ZTreeUrlHandler up;
031:
032: protected void setUp() throws Exception {
033: super .setUp();
034: up = (ZTreeUrlHandler) ZUrlHandlerFactory.create(
035: ParameterTest.class.getPackage().getName(),
036: ZUrlHandlerFactory.defaultSecurityService);
037: }
038:
039: protected void tearDown() throws Exception {
040: up = null;
041: super .tearDown();
042: }
043:
044: public void testParametersInRootClass() throws Exception {
045: Map<String, String[]> param = new HashMap<String, String[]>();
046: param.put("param2", new String[] { "value2" });
047:
048: Handler obj = (Handler) up.process("/mytext/nested/katzeklo",
049: param);
050: assertNotNull(obj);
051: assertEquals(obj.getParam2(), "value2", obj.getParam2());
052: }
053:
054: public void testParametersInRootClassNull() throws Exception {
055: Map<String, String[]> param = new HashMap<String, String[]>();
056: param.put("param2", new String[] { "" });
057:
058: Handler obj = (Handler) up.process("/mytext/nested/katzeklo",
059: param);
060: assertNotNull(obj);
061: assertNull("'" + obj.getParam2() + "'", obj.getParam2());
062: }
063:
064: public void testParametersInNestedClass() throws Exception {
065: Map<String, String[]> param = new HashMap<String, String[]>();
066: param.put("param3", new String[] { "value3" });
067:
068: Handler obj = (Handler) up.process("/mytext/nested/katzeklo",
069: param);
070: assertNotNull(obj);
071: assertEquals(obj.getNested().getParam3(), "value3", obj
072: .getNested().getParam3());
073: }
074:
075: public void testParametersAreAssignedDepthFirst() throws Exception {
076: Map<String, String[]> param = new HashMap<String, String[]>();
077: param.put("param1", new String[] { "value1" });
078:
079: Handler obj = (Handler) up.process("/mytext/nested/katzeklo",
080: param);
081: assertNotNull(obj);
082: assertNull(obj.getParam1(), obj.getParam1());
083: assertEquals(obj.getNested().getParam1(), "value1", obj
084: .getNested().getParam1());
085: }
086:
087: public void testUrlParamInRoot() throws Exception {
088: Handler obj1 = new Handler();
089: obj1.setParam1("value1");
090: String surl = up.createUrl(obj1);
091: ZUrl parsedUrl = up.parse(surl);
092:
093: assertEquals("value1", parsedUrl.getParameterMap()
094: .get("param1")[0]);
095: }
096:
097: public void testUrlParametersInNested() throws Exception {
098: Handler obj1 = new Handler();
099:
100: NestedHandler nested = new NestedHandler();
101: obj1.setNested(nested);
102: nested.setParam1("value1n");
103: String surl = up.createUrl(obj1);
104: ZUrl parsedUrl = up.parse(surl);
105:
106: assertEquals("value1n", parsedUrl.getParameterMap().get(
107: "param1")[0]);
108: }
109:
110: public void testUrlParametersInBoth() throws Exception {
111: Handler obj1 = new Handler();
112: obj1.setParam1("value1");
113:
114: NestedHandler nested = new NestedHandler();
115: nested.setParam1("value1n");
116:
117: obj1.setNested(nested);
118: String surl = up.createUrl(obj1);
119: ZUrl parsedUrl = up.parse(surl);
120:
121: assertEquals("value1n", parsedUrl.getParameterMap().get(
122: "param1")[0]);
123: }
124:
125: public void testUrlParametersInBothAll() throws Exception {
126: Handler obj1 = new Handler();
127: obj1.setParam1("value1");
128: obj1.setParam2("value2");
129:
130: NestedHandler nested = new NestedHandler();
131: nested.setParam1("value1n");
132: nested.setParam3("value3n");
133:
134: obj1.setNested(nested);
135: String surl = up.createUrl(obj1);
136: ZUrl parsedUrl = up.parse(surl);
137:
138: assertEquals("value1n", parsedUrl.getParameterMap().get(
139: "param1")[0]);
140: assertEquals("value2", parsedUrl.getParameterMap()
141: .get("param2")[0]);
142: assertEquals("value3n", parsedUrl.getParameterMap().get(
143: "param3")[0]);
144: }
145:
146: public void testParametersArray() throws Exception {
147: Map<String, String[]> param = new HashMap<String, String[]>();
148: param.put("paramArr4",
149: new String[] { "valueArr1", "valueArr2", });
150:
151: Handler obj = (Handler) up.process("/mytext/nested/katzeklo",
152: param);
153: assertEquals("valueArr1", obj.getParamArr4()[0]);
154: assertEquals("valueArr2", obj.getParamArr4()[1]);
155: }
156:
157: public void testParametersEncoded() throws Exception {
158: Map<String, String[]> param = new HashMap<String, String[]>();
159: param.put("paramArr4", new String[] { "valueArr1 val2",
160: "valueArr2 val2", });
161:
162: Handler obj = (Handler) up.process("/mytext/nested/katzeklo",
163: param);
164: assertEquals("valueArr1 val2", obj.getParamArr4()[0]);
165: assertEquals("valueArr2 val2", obj.getParamArr4()[1]);
166: }
167:
168: public void testParamProp() throws Exception {
169: Map<String, String[]> param = new HashMap<String, String[]>();
170: param.put("paramProp", new String[] { "valParamProp", });
171:
172: Handler obj = (Handler) up.process("/mytext/nested/katzeklo",
173: param);
174: assertEquals("valParamProp", obj.getParamProp()
175: .getStringValue());
176: }
177: }
|