View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.utils;
18  
19  import static org.apache.juneau.common.utils.Utils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.junit.jupiter.params.*;
24  import org.junit.jupiter.params.provider.*;
25  
26  /**
27   * Verifies that the getUri() methods in UriContext work correctly.
28   */
29  class UriContextUriCombo_Test extends TestBase {
30  
31  	private static final Data[] DATA = {
32  
33  		// Happy cases - All URL parts known.
34  		data(
35  			"Happy-1",
36  			input(
37  				"http://foo.com:123","/context","/resource","/path"
38  			),
39  			results(
40  				"http://foo.com:123",
41  				"http://foo.com:123/context",
42  				"http://foo.com:123/context/resource",
43  				"http://foo.com:123/context/resource/path",
44  				"/context",
45  				"/context/resource",
46  				"/context/resource/path"
47  			)
48  		),
49  		data(
50  			"Happy-2",
51  			input(
52  				"http://foo.com:123","/c1/c2","/r1/r2","/p1/p2"
53  			),
54  			results(
55  				"http://foo.com:123",
56  				"http://foo.com:123/c1/c2",
57  				"http://foo.com:123/c1/c2/r1/r2",
58  				"http://foo.com:123/c1/c2/r1/r2/p1/p2",
59  				"/c1/c2",
60  				"/c1/c2/r1/r2",
61  				"/c1/c2/r1/r2/p1/p2"
62  			)
63  		),
64  		data(
65  			"NoAuthority-1",
66  			input(
67  				"","/context","/resource","/path"
68  			),
69  			results(
70  				"/",
71  				"/context",
72  				"/context/resource",
73  				"/context/resource/path",
74  				"/context",
75  				"/context/resource",
76  				"/context/resource/path"
77  			)
78  		),
79  		data(
80  			"NoContext-1",
81  			input(
82  				"http://foo.com:123","","/resource","/path"
83  			),
84  			results(
85  				"http://foo.com:123",
86  				"http://foo.com:123",
87  				"http://foo.com:123/resource",
88  				"http://foo.com:123/resource/path",
89  				"/",
90  				"/resource",
91  				"/resource/path"
92  			)
93  		),
94  		data(
95  			"NoResource-1",
96  			input(
97  				"http://foo.com:123","/context","","/path"
98  			),
99  			results(
100 				"http://foo.com:123",
101 				"http://foo.com:123/context",
102 				"http://foo.com:123/context",
103 				"http://foo.com:123/context/path",
104 				"/context",
105 				"/context",
106 				"/context/path"
107 			)
108 		),
109 		data(
110 			"NoPath-1",
111 			input(
112 				"http://foo.com:123","/context","/resource",""
113 			),
114 			results(
115 				"http://foo.com:123",
116 				"http://foo.com:123/context",
117 				"http://foo.com:123/context/resource",
118 				"http://foo.com:123/context/resource",
119 				"/context",
120 				"/context/resource",
121 				"/context/resource"
122 			)
123 		),
124 		data(
125 			"NoAuthorityNoContext-1",
126 			input(
127 				"","","/resource","/path"
128 			),
129 			results(
130 				"/",
131 				"/",
132 				"/resource",
133 				"/resource/path",
134 				"/",
135 				"/resource",
136 				"/resource/path"
137 			)
138 		),
139 		data(
140 			"NoContextNoResource-1",
141 			input(
142 				"http://foo.com:123","","","/path"
143 			),
144 			results(
145 				"http://foo.com:123",
146 				"http://foo.com:123",
147 				"http://foo.com:123",
148 				"http://foo.com:123/path",
149 				"/",
150 				"/",
151 				"/path"
152 			)
153 		),
154 		data(
155 			"NoAuthorityNoContextNoResource-1",
156 			input(
157 				"","","","/path"
158 			),
159 			results(
160 				"/",
161 				"/",
162 				"/",
163 				"/path",
164 				"/",
165 				"/",
166 				"/path"
167 			)
168 		),
169 		data(
170 			"Nothing-1",
171 			input(
172 				"","","",""
173 			),
174 			results(
175 				"/",
176 				"/",
177 				"/",
178 				"/",
179 				"/",
180 				"/",
181 				"/"
182 			)
183 		)
184 	};
185 
186 	private static class Data {
187 		final String label;
188 		final Input in;
189 		final Results r;
190 
191 		public Data(String label, Input in, Results r) {
192 			this.label = label;
193 			this.in = in;
194 			this.r = r;
195 		}
196 	}
197 
198 	private static class Input {
199 		final UriContext uriContext;
200 
201 		public Input(String authority, String context, String resource, String path) {
202 			this.uriContext = UriContext.of(authority, context, resource, path);
203 		}
204 	}
205 
206 	private static class Results {
207 		final String eAbsoluteAuthority, eAbsoluteContext, eAbsoluteResource, eAbsolutePath, eRootRelativeContext, eRootRelativeResource, eRootRelativePath;
208 
209 		public Results(String eAbsoluteAuthority, String eAbsoluteContext, String eAbsoluteResource, String eAbsolutePath,
210 					String eRootRelativeContext, String eRootRelativeResource, String eRootRelativePath) {
211 			this.eAbsoluteAuthority = eAbsoluteAuthority;
212 			this.eAbsoluteContext = eAbsoluteContext;
213 			this.eAbsoluteResource = eAbsoluteResource;
214 			this.eAbsolutePath = eAbsolutePath;
215 			this.eRootRelativeContext = eRootRelativeContext;
216 			this.eRootRelativeResource = eRootRelativeResource;
217 			this.eRootRelativePath = eRootRelativePath;
218 		}
219 	}
220 
221 	public static Data data(String label, Input input, Results results) {
222 		return new Data(label, input, results);
223 	}
224 
225 	public static Input input(String authority, String context, String resource, String path) {
226 		return new Input(authority, context, resource, path);
227 	}
228 
229 	public static Results results(String eAbsoluteAuthority, String eAbsoluteContext, String eAbsoluteResource, String eAbsolutePath,
230 			String eRootRelativeContext, String eRootRelativeResource, String eRootRelativePath) {
231 		return new Results(eAbsoluteAuthority, eAbsoluteContext, eAbsoluteResource, eAbsolutePath, eRootRelativeContext, eRootRelativeResource, eRootRelativePath);
232 	}
233 
234 	static Data[] data() {
235 		return DATA;
236 	}
237 
238 	@ParameterizedTest
239 	@MethodSource("data")
240 	void a01_testAbsoluteAuthority(Data d) {
241 		assertEquals(d.r.eAbsoluteAuthority, d.in.uriContext.getAbsoluteAuthority(), fms("{0}: testAbsoluteAuthority() failed", d.label));
242 	}
243 
244 	@ParameterizedTest
245 	@MethodSource("data")
246 	void a02_testAbsoluteContext(Data d) {
247 		assertEquals(d.r.eAbsoluteContext, d.in.uriContext.getAbsoluteContextRoot(), fms("{0}: testAbsoluteContext() failed", d.label));
248 	}
249 
250 	@ParameterizedTest
251 	@MethodSource("data")
252 	void a03_testAbsoluteResource(Data d) {
253 		assertEquals(d.r.eAbsoluteResource, d.in.uriContext.getAbsoluteServletPath(), fms("{0}: testAbsoluteResource() failed", d.label));
254 	}
255 
256 	@ParameterizedTest
257 	@MethodSource("data")
258 	void a04_testAbsolutePath(Data d) {
259 		assertEquals(d.r.eAbsolutePath, d.in.uriContext.getAbsolutePathInfo(), fms("{0}: testAbsolutePath() failed", d.label));
260 	}
261 
262 	@ParameterizedTest
263 	@MethodSource("data")
264 	void a05_testRootRelativeContext(Data d) {
265 		assertEquals(d.r.eRootRelativeContext, d.in.uriContext.getRootRelativeContextRoot(), fms("{0}: testRootRelativeContext() failed", d.label));
266 	}
267 
268 	@ParameterizedTest
269 	@MethodSource("data")
270 	void a06_testRootRelativeResource(Data d) {
271 		assertEquals(d.r.eRootRelativeResource, d.in.uriContext.getRootRelativeServletPath(), fms("{0}: testRootRelativeResource() failed", d.label));
272 	}
273 
274 	@ParameterizedTest
275 	@MethodSource("data")
276 	void a07_testRootRelativePath(Data d) {
277 		assertEquals(d.r.eRootRelativePath, d.in.uriContext.getRootRelativePathInfo(), fms("{0}: testRootRelativePath() failed", d.label));
278 	}
279 }