1 package org.apache.turbine.util.uri;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.assertFalse;
25
26 import org.apache.fulcrum.parser.DefaultParameterParser;
27 import org.apache.fulcrum.parser.ParameterParser;
28 import org.apache.fulcrum.parser.ParserService;
29 import org.apache.turbine.services.TurbineServices;
30 import org.apache.turbine.test.BaseTestCase;
31 import org.apache.turbine.util.ServerData;
32 import org.apache.turbine.util.TurbineConfig;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37
38
39
40
41
42
43
44
45 public class TurbineURITest extends BaseTestCase
46 {
47 private TurbineURI turi;
48
49 private ParserService parserService;
50
51 private static TurbineConfig tc = null;
52
53
54 @BeforeClass
55 public static void init() {
56 tc = new TurbineConfig(
57 ".",
58 "/conf/test/CompleteTurbineResources.properties");
59 tc.initialize();
60 }
61
62
63
64
65 @Before
66 public void setup()
67 {
68
69
70 ServerData sd = new ServerData("www.testserver.com",
71 URIConstants.HTTP_PORT, URIConstants.HTTP, "/servlet/turbine",
72 "/context");
73 turi = new TurbineURI(sd);
74
75 parserService = (ParserService)TurbineServices.getInstance().getService(ParserService.ROLE);
76 }
77
78
79
80
81 @AfterClass
82 public static void tearDown()
83 {
84 if (tc != null)
85 {
86 tc.dispose();
87 }
88
89 }
90
91 @Test public void testAddRemove()
92 {
93
94 assertFalse("TurbineURI should not have a pathInfo", turi.hasPathInfo());
95 assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
96 turi.addPathInfo("test", "x");
97 assertTrue("TurbineURI must have a pathInfo", turi.hasPathInfo());
98 assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
99 turi.removePathInfo("test");
100 assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
101 assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
102
103 assertFalse("TurbineURI should not have a queryData", turi.hasQueryData());
104 assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
105 turi.addQueryData("test", "x");
106 assertTrue("TurbineURI must have a queryData", turi.hasQueryData());
107 assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
108 turi.removeQueryData("test");
109 assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
110 assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
111 }
112
113 @Test public void testEmptyAndNullQueryData()
114 {
115
116 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
117 turi.addQueryData("test", "");
118 assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
119 turi.removeQueryData("test");
120
121
122 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
123 turi.addQueryData("test", null);
124 assertEquals("/context/servlet/turbine?test=null", turi
125 .getRelativeLink());
126 turi.removeQueryData("test");
127 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
128 }
129
130 @Test public void testEmptyAndNullPathInfo()
131 {
132
133 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
134 turi.addPathInfo("test", "");
135
136 assertEquals("/context/servlet/turbine/test/", turi.getRelativeLink());
137 turi.removePathInfo("test");
138
139
140 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
141 turi.addPathInfo("test", null);
142 assertEquals("/context/servlet/turbine/test/null", turi
143 .getRelativeLink());
144 turi.removePathInfo("test");
145 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
146 }
147
148 @Test public void testAddEmptyParameterParser()
149 {
150 ParameterParser pp = new DefaultParameterParser();
151 turi.add(1, pp);
152 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
153 }
154
155 @Test public void testAddParameterParser() throws InstantiationException
156 {
157 ParameterParser pp = parserService.getParser(DefaultParameterParser.class);
158 pp.add("test", "");
159 turi.add(1, pp);
160 assertEquals("/context/servlet/turbine?test=", turi.getRelativeLink());
161 turi.removeQueryData("test");
162 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
163
164 parserService.putParser(pp);
165 pp = parserService.getParser(DefaultParameterParser.class);
166 pp.add("test", (String) null);
167 turi.add(1, pp);
168
169
170
171
172 turi.removeQueryData("test");
173 assertEquals("/context/servlet/turbine", turi.getRelativeLink());
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 }
194
195 }