Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestUri01.java
Go to the documentation of this file.
1package jau.test.net;
2
3import java.io.File;
4import java.io.IOException;
5import java.net.URI;
6import java.net.URISyntaxException;
7
8import org.jau.io.IOUtil;
9import org.jau.net.Uri;
10import org.junit.Assert;
11import org.junit.FixMethodOrder;
12import org.junit.Test;
13import org.junit.runners.MethodSorters;
14
15import jau.test.junit.util.JunitTracer;
16
17@FixMethodOrder(MethodSorters.NAME_ASCENDING)
18public class TestUri01 extends JunitTracer {
19
20 @Test
21 public void test00BasicCoding() throws IOException, URISyntaxException {
22 final String string = "Hallo Welt öä";
23 System.err.println("sp1 "+string);
24 final File file = new File(string);
25 System.err.println("file "+file);
26 System.err.println("file.path.dec "+file.getPath());
27 System.err.println("file.path.abs "+file.getAbsolutePath());
28 System.err.println("file.path.can "+file.getCanonicalPath());
29 final Uri uri0 = Uri.valueOf(file);
30 URIDumpUtil.showUri(uri0);
32
33 boolean ok = true;
34 {
35 final String s2 = IOUtil.slashify(file.getAbsolutePath(), true /* startWithSlash */, file.isDirectory() /* endWithSlash */);
36 System.err.println("uri2.slashify: "+s2);
37 final Uri uri1 = Uri.create(Uri.FILE_SCHEME, null, s2, null);
38 final boolean equalEncoded= uri0.getEncoded().equals(uri1.getEncoded());
39 final boolean equalPath = uri0.path.decode().equals(uri1.path.decode());
40 final boolean equalASCII= uri0.toASCIIString().equals(uri1.toASCIIString());
41 System.err.println("uri2.enc : "+uri1.getEncoded()+" - "+(equalEncoded?"OK":"ERROR"));
42 System.err.println("uri2.pathD : "+uri1.path.decode()+" - "+(equalPath?"OK":"ERROR"));
43 System.err.println("uri2.asciiE: "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR"));
44 ok = equalEncoded && equalPath && equalASCII && ok;
45 }
46 {
47 final String s2 = "/"+string;
48 System.err.println("uri3.orig: "+s2);
49 final Uri uri1 = Uri.create(Uri.FILE_SCHEME, s2, null);
50 final String rString = "file:/Hallo%20Welt%20öä";
51 final String rPath = s2;
52 final String rASCII = "file:/Hallo%20Welt%20%C3%B6%C3%A4";
53 final boolean equalEncoded = rString.equals(uri1.toString());
54 final boolean equalPath = rPath.equals(uri1.path.decode());
55 final boolean equalASCII= rASCII.equals(uri1.toASCIIString().get());
56 System.err.println("uri3.enc : "+uri1.toString()+" - "+(equalEncoded?"OK":"ERROR"));
57 System.err.println("uri3.pathD : "+uri1.path.decode()+" - "+(equalPath?"OK":"ERROR"));
58 System.err.println("uri3.asciiE: "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR"));
59 ok = equalEncoded && equalPath && equalASCII && ok;
60 }
61 {
62 final String s2 = "//lala.org/"+string;
63 System.err.println("uri4.orig: "+s2);
64 final Uri uri1 = Uri.create(Uri.HTTP_SCHEME, s2, null);
65 final String rString = "http://lala.org/Hallo%20Welt%20öä";
66 final String rPath = "/"+string;
67 final String rASCII = "http://lala.org/Hallo%20Welt%20%C3%B6%C3%A4";
68 final boolean equalString= rString.equals(uri1.toString());
69 final boolean equalPath = rPath.equals(uri1.path.decode());
70 final boolean equalASCII= rASCII.equals(uri1.toASCIIString().get());
71 System.err.println("uri4.enc : "+uri1.toString()+" - "+(equalString?"OK":"ERROR"));
72 System.err.println("uri4.pathD : "+uri1.path.decode()+" - "+(equalPath?"OK":"ERROR"));
73 System.err.println("uri4.asciiE: "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR"));
74 ok = equalString && equalPath && equalASCII && ok;
75 }
76 Assert.assertTrue("One or more errors occured see stderr above", ok);
77 }
78
79 @Test
80 public void test02URIEscapeSpecialChars() throws IOException, URISyntaxException {
81 {
82 final String vanilla = "XXX ! # $ & ' ( ) * + , / : ; = ? @ [ ]";
83 final Uri.Encoded escaped = Uri.Encoded.cast("XXX%20!%20%23%20%24%20%26%20%27%20%28%20%29%20%2A%20%2B%20%2C%20/%20%3A%20%3B%20%3D%20%3F%20%40%20%5B%20%5D");
84 System.err.println("vanilla "+vanilla);
85 final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL);
86 System.err.println("esc1 "+esc1);
87 Assert.assertEquals(escaped, esc1);
88
89 final String invEsc1 = esc1.decode();
90 System.err.println("inv(esc1) "+invEsc1);
91 Assert.assertEquals(vanilla, invEsc1);
92 }
93 {
94 final String vanilla = "/XXX R!# R$&'()*+,/:;=?z@y[x]";
95 final Uri.Encoded escaped = Uri.Encoded.cast("/XXX%20R!%23%20R%24%26%27%28%29%2A%2B%2C/%3A%3B%3D%3Fz%40y%5Bx%5D");
96 System.err.println("vanilla "+vanilla);
97 final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL);
98 System.err.println("esc1 "+esc1);
99 Assert.assertEquals(escaped, esc1);
100
101 final String invEsc1 = esc1.decode();
102 System.err.println("inv(esc1) "+invEsc1);
103 Assert.assertEquals(vanilla, invEsc1);
104 }
105 {
106 // Bug 908: $ ^ ~ # [ ]
107 final String vanilla = "/XXX $ ^ ~ # [ ]";
108 showDump0x(vanilla);
109 }
110 {
111 // Windows invalid File characters: * ? " < > |
112 final String vanilla = "/XXX ! & ' ( ) + , / ; = @ [ ]";
113 showDump0x(vanilla);
114 }
115 }
116 @Test
117 public void test03URIEscapeCommonChars() throws IOException, URISyntaxException {
118 {
119 final String vanilla = "/XXX \"%-.<>\\^_`{|}~";
120 final Uri.Encoded escaped = Uri.Encoded.cast("/XXX%20%22%25-.%3C%3E%5C%5E_%60%7B%7C%7D~");
121 System.err.println("vanilla "+vanilla);
122 final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL);
123 System.err.println("esc1 "+esc1);
124 Assert.assertEquals(escaped, esc1);
125
126 final String invEsc1 = esc1.decode();
127 System.err.println("inv(esc1) "+invEsc1);
128 Assert.assertEquals(vanilla, invEsc1);
129 showDump0x(vanilla);
130 }
131 }
132 private static void showDump0x(final String string) throws IOException, URISyntaxException {
133 final File file = new File(string);
134 System.err.println("file "+file);
135 System.err.println("file.path.dec "+file.getPath());
136 System.err.println("file.path.abs "+file.getAbsolutePath());
137 System.err.println("file.path.can "+file.getCanonicalPath());
138
139 System.err.println("File-path -> Uri:");
140 final Uri uri0 = Uri.valueOfFilepath(string);
141 URIDumpUtil.showUri(uri0);
142
143 System.err.println("Uri -> File:");
144 final Uri uri2 = Uri.valueOf(file);
145 URIDumpUtil.showUri(uri2);
146
147 System.err.println("Uri -> URI:");
148 final URI uri3 = uri2.toURI();
149 URIDumpUtil.showURI(uri3);
150
151 System.err.println("URI -> Uri (keep encoding):");
152 final Uri uri4 = Uri.valueOf(uri3);
153 URIDumpUtil.showUri(uri4);
154
155 System.err.println("URI -> Uri (re-encode):");
156 final Uri uri5 = Uri.valueOf(uri3);
157 URIDumpUtil.showUri(uri5);
158 }
159
160 @Test
161 public void test04EqualsAndHashCode() throws IOException, URISyntaxException {
162 {
163 final Uri uri0 = Uri.cast("http://localhost/test01.html#tag01");
164 final Uri uri1 = Uri.create("http", null, "localhost", -1, "/test01.html", null, "tag01");
165 final Uri uri2 = Uri.create("http", "localhost", "/test01.html", "tag01");
166
167 Assert.assertEquals(uri0, uri1);
168 Assert.assertEquals(uri0.hashCode(), uri1.hashCode());
169
170 Assert.assertEquals(uri0, uri2);
171 Assert.assertEquals(uri0.hashCode(), uri2.hashCode());
172
173 Assert.assertEquals(uri1, uri2);
174 Assert.assertEquals(uri1.hashCode(), uri2.hashCode());
175
176 final Uri uriA = Uri.create("http", null, "localhost", -1, "/test02.html", null, "tag01");
177 final Uri uriB = Uri.create("http", null, "localhost", -1, "/test01.html", null, "tag02");
178 final Uri uriC = Uri.create("http", null, "lalalhost", -1, "/test01.html", null, "tag01");
179 final Uri uriD = Uri.create("sftp", null, "localhost", -1, "/test01.html", null, "tag01");
180
181 Assert.assertNotEquals(uri1, uriA);
182 Assert.assertNotEquals(uri1, uriB);
183 Assert.assertNotEquals(uri1, uriC);
184 Assert.assertNotEquals(uri1, uriD);
185 }
186 { // 3 [scheme:][//[user-info@]host[:port]]path[?query][#fragment]
187 final Uri uri0 = Uri.cast("http://user@localhost:80/test01.html?test=01&test=02#tag01");
188 final Uri uri1 = Uri.create("http", "user", "localhost", 80, "/test01.html", "test=01&test=02", "tag01");
189
190 Assert.assertEquals(uri0, uri1);
191 Assert.assertEquals(uri0.hashCode(), uri1.hashCode());
192
193 final Uri uriA = Uri.cast("http://user@localhost:80/test01.html?test=01&test=02#tag02");
194 final Uri uriB = Uri.cast("http://user@localhost:80/test01.html?test=01&test=03#tag01");
195 final Uri uriC = Uri.cast("http://user@localhost:80/test04.html?test=01&test=02#tag01");
196 final Uri uriD = Uri.cast("http://user@localhost:88/test01.html?test=01&test=02#tag01");
197 final Uri uriE = Uri.cast("http://user@lalalhost:80/test01.html?test=01&test=02#tag01");
198 final Uri uriF = Uri.cast("http://test@localhost:80/test01.html?test=01&test=02#tag01");
199 final Uri uriG = Uri.cast("sftp://user@localhost:80/test01.html?test=01&test=02#tag01");
200
201 Assert.assertNotEquals(uri1, uriA);
202 Assert.assertNotEquals(uri1, uriB);
203 Assert.assertNotEquals(uri1, uriC);
204 Assert.assertNotEquals(uri1, uriD);
205 Assert.assertNotEquals(uri1, uriE);
206 Assert.assertNotEquals(uri1, uriF);
207 Assert.assertNotEquals(uri1, uriG);
208 }
209 }
210
211 @Test
212 public void test05Contained() throws IOException, URISyntaxException {
213 {
214 final Uri input = Uri.cast("http://localhost/test01.html#tag01");
215 final Uri contained = input.getContainedUri();
216 Assert.assertNull(contained);
217 }
218 {
219 final Uri input = Uri.cast("jar:http://localhost/test01.jar!/com/jogamp/Lala.class#tag01");
220 final Uri expected = Uri.cast("http://localhost/test01.jar#tag01");
221 final Uri contained = input.getContainedUri();
222 URIDumpUtil.showUri(input);
223 URIDumpUtil.showUri(contained);
224 Assert.assertEquals(expected, contained);
225 Assert.assertEquals(expected.hashCode(), contained.hashCode());
226 }
227 {
228 final Uri input = Uri.cast("jar:file://localhost/test01.jar!/");
229 final Uri expected = Uri.cast("file://localhost/test01.jar");
230 final Uri contained = input.getContainedUri();
231 URIDumpUtil.showUri(input);
232 URIDumpUtil.showUri(contained);
233 Assert.assertEquals(expected, contained);
234 Assert.assertEquals(expected.hashCode(), contained.hashCode());
235 }
236 {
237 final Uri input = Uri.cast("sftp:http://localhost/test01.jar?lala=01#tag01");
238 final Uri expected = Uri.cast("http://localhost/test01.jar?lala=01#tag01");
239 final Uri contained = input.getContainedUri();
240 URIDumpUtil.showUri(input);
241 URIDumpUtil.showUri(contained);
242 Assert.assertEquals(expected, contained);
243 Assert.assertEquals(expected.hashCode(), contained.hashCode());
244 }
245 }
246
247 @Test
248 public void test08NormalizedHierarchy() throws IOException, URISyntaxException {
249 {
250 final Uri input = Uri.cast("./dummy/nop/../a.txt");
251 final Uri expected = Uri.cast("dummy/a.txt");
252 URIDumpUtil.showUri(input);
253 final Uri normal = input.getNormalized();
254 Assert.assertEquals(expected, normal);
255 }
256 {
257 final Uri input = Uri.cast("../dummy/nop/../a.txt");
258 final Uri expected = Uri.cast("../dummy/a.txt");
259 URIDumpUtil.showUri(input);
260 final Uri normal = input.getNormalized();
261 Assert.assertEquals(expected, normal);
262 }
263 {
264 final Uri input = Uri.cast("http://localhost/dummy/../");
265 final Uri expected = Uri.cast("http://localhost/");
266 URIDumpUtil.showUri(input);
267 final Uri normal = input.getNormalized();
268 Assert.assertEquals(expected, normal);
269 }
270 {
271 final Uri input = Uri.cast("http://localhost/dummy/./../");
272 final Uri expected = Uri.cast("http://localhost/");
273 URIDumpUtil.showUri(input);
274 final Uri normal = input.getNormalized();
275 Assert.assertEquals(expected, normal);
276 }
277 {
278 final Uri input = Uri.cast("http://localhost/dummy/../aa/././../");
279 final Uri expected = Uri.cast("http://localhost/");
280 URIDumpUtil.showUri(input);
281 final Uri normal = input.getNormalized();
282 Assert.assertEquals(expected, normal);
283 }
284 {
285 final Uri input = Uri.cast("http://localhost/test/dummy/./../text.txt");
286 final Uri expected = Uri.cast("http://localhost/test/text.txt");
287 URIDumpUtil.showUri(input);
288 final Uri normal = input.getNormalized();
289 Assert.assertEquals(expected, normal);
290 }
291 {
292 final Uri input = Uri.cast("http://localhost/test/dummy/../text.txt?lala=01&lili=02#frag01");
293 final Uri expected = Uri.cast("http://localhost/test/text.txt?lala=01&lili=02#frag01");
294 URIDumpUtil.showUri(input);
295 final Uri normal = input.getNormalized();
296 Assert.assertEquals(expected, normal);
297 }
298 }
299
300 @Test
301 public void test09NormalizedOpaque() throws IOException, URISyntaxException {
302 {
303 final Uri input = Uri.cast("jar:http://localhost/dummy/../abc.jar!/");
304 final Uri expected = Uri.cast("jar:http://localhost/abc.jar!/");
305 URIDumpUtil.showUri(input);
306 final Uri normal = input.getNormalized();
307 Assert.assertEquals(expected, normal);
308 }
309 {
310 final Uri input = Uri.cast("jar:http://localhost/test/./dummy/../abc.jar!/");
311 final Uri expected = Uri.cast("jar:http://localhost/test/abc.jar!/");
312 URIDumpUtil.showUri(input);
313 final Uri normal = input.getNormalized();
314 Assert.assertEquals(expected, normal);
315 }
316 {
317 final Uri input = Uri.cast("jar:http://localhost/test/dummy/../abc.jar!/a/b/C.class");
318 final Uri expected = Uri.cast("jar:http://localhost/test/abc.jar!/a/b/C.class");
319 URIDumpUtil.showUri(input);
320 final Uri normal = input.getNormalized();
321 Assert.assertEquals(expected, normal);
322 }
323 {
324 final Uri input = Uri.cast("jar:http://localhost/test/dummy/../abc.jar!/a/b/C.class?lala=01&lili=02#frag01");
325 final Uri expected = Uri.cast("jar:http://localhost/test/abc.jar!/a/b/C.class?lala=01&lili=02#frag01");
326 URIDumpUtil.showUri(input);
327 final Uri normal = input.getNormalized();
328 Assert.assertEquals(expected, normal);
329 }
330 }
331
332 @Test
333 public void test10ParentAndDirHierarchy() throws IOException, URISyntaxException {
334 {
335 final Uri input = Uri.cast("http://localhost/");
336 URIDumpUtil.showUri(input);
337 final Uri directory = input.getDirectory();
338 Assert.assertEquals(input, directory);
339 final Uri parent = input.getParent();
340 Assert.assertNull(parent);
341 }
342 {
343 final Uri input = Uri.cast("http://localhost/dummy/../test/");
344 final Uri expectedD = Uri.cast("http://localhost/test/");
345 final Uri expectedP = Uri.cast("http://localhost/");
346 URIDumpUtil.showUri(input);
347 final Uri directory = input.getDirectory();
348 Assert.assertEquals(expectedD, directory);
349 final Uri parent = input.getParent();
350 Assert.assertEquals(expectedP, parent);
351 }
352 {
353 final Uri input = Uri.cast("http://localhost/dummy/../test/dummy/../");
354 final Uri expectedD = Uri.cast("http://localhost/test/");
355 final Uri expectedP = Uri.cast("http://localhost/");
356 URIDumpUtil.showUri(input);
357 final Uri directory = input.getDirectory();
358 Assert.assertEquals(expectedD, directory);
359 final Uri parent = input.getParent();
360 Assert.assertEquals(expectedP, parent);
361 }
362 {
363 final Uri input = Uri.cast("http://localhost/dir/test01.jar?lala=01#frag01");
364 final Uri expParen1 = Uri.cast("http://localhost/dir/?lala=01#frag01");
365 final Uri expFolde1 = expParen1;
366 final Uri expParen2 = Uri.cast("http://localhost/?lala=01#frag01");
367 final Uri expFolde2 = expParen1; // is folder already
368 final Uri expParen3 = null;
369 final Uri expFolde3 = expParen2;
370 Assert.assertNotEquals(input, expParen1);
371 Assert.assertNotEquals(expParen1, expParen2);
372 Assert.assertNotEquals(expParen1, expParen3);
373 URIDumpUtil.showUri(input);
374
375 final Uri parent1 = input.getParent();
376 Assert.assertEquals(expParen1, parent1);
377 Assert.assertEquals(expParen1.hashCode(), parent1.hashCode());
378 final Uri folder1 = input.getDirectory();
379 Assert.assertEquals(expFolde1, folder1);
380
381 final Uri parent2 = parent1.getParent();
382 Assert.assertEquals(expParen2, parent2);
383 Assert.assertEquals(expParen2.hashCode(), parent2.hashCode());
384 final Uri folder2 = parent1.getDirectory();
385 Assert.assertEquals(expFolde2, folder2);
386
387 final Uri parent3 = parent2.getParent();
388 Assert.assertEquals(expParen3, parent3); // NULL!
389 final Uri folder3 = parent2.getDirectory();
390 Assert.assertEquals(expFolde3, folder3); // NULL!
391 }
392 }
393
394 @Test
395 public void test11ParentAndDirOpaque() throws IOException, URISyntaxException {
396 {
397 final Uri input = Uri.cast("jar:http://localhost/test.jar!/");
398 URIDumpUtil.showUri(input);
399 final Uri directory = input.getDirectory();
400 Assert.assertEquals(input, directory);
401 final Uri parent = input.getParent();
402 Assert.assertNull(parent);
403 }
404 {
405 final Uri input = Uri.cast("jar:http://localhost/dummy/../test/test.jar!/");
406 final Uri expectedD = Uri.cast("jar:http://localhost/test/test.jar!/");
407 final Uri expectedP = null;
408 URIDumpUtil.showUri(input);
409 final Uri directory = input.getDirectory();
410 Assert.assertEquals(expectedD, directory);
411 final Uri parent = input.getParent();
412 Assert.assertEquals(expectedP, parent);
413 }
414 {
415 final Uri input = Uri.cast("jar:http://localhost/dummy/../test/dummy/../test.jar!/a/b/C.class");
416 final Uri expectedD = Uri.cast("jar:http://localhost/test/test.jar!/a/b/");
417 final Uri expectedP = Uri.cast("jar:http://localhost/test/test.jar!/a/b/");
418 URIDumpUtil.showUri(input);
419 final Uri directory = input.getDirectory();
420 Assert.assertEquals(expectedD, directory);
421 final Uri parent = input.getParent();
422 Assert.assertEquals(expectedP, parent);
423 }
424 {
425 final Uri input = Uri.cast("jar:http://localhost/test01.jar!/com/Lala.class?lala=01#frag01");
426 final Uri expParen1 = Uri.cast("jar:http://localhost/test01.jar!/com/?lala=01#frag01");
427 final Uri expFolde1 = expParen1;
428 final Uri expParen2 = Uri.cast("jar:http://localhost/test01.jar!/?lala=01#frag01");
429 final Uri expFolde2 = expParen1; // is folder already
430 final Uri expParen3 = null;
431 final Uri expFolde3 = expParen2; // is folder already
432 Assert.assertNotEquals(input, expParen1);
433 Assert.assertNotEquals(expParen1, expParen2);
434 Assert.assertNotEquals(expParen1, expParen3);
435 URIDumpUtil.showUri(input);
436
437 final Uri parent1 = input.getParent();
438 Assert.assertEquals(expParen1, parent1);
439 Assert.assertEquals(expParen1.hashCode(), parent1.hashCode());
440 final Uri folder1 = input.getDirectory();
441 Assert.assertEquals(expFolde1, folder1);
442
443 final Uri parent2 = parent1.getParent();
444 Assert.assertEquals(expParen2, parent2);
445 Assert.assertEquals(expParen2.hashCode(), parent2.hashCode());
446 final Uri folder2 = parent1.getDirectory();
447 Assert.assertEquals(expFolde2, folder2);
448
449 final Uri parent3 = parent2.getParent();
450 Assert.assertEquals(expParen3, parent3); // NULL
451 final Uri folder3 = parent2.getDirectory();
452 Assert.assertEquals(expFolde3, folder3);
453 }
454 }
455
456 public static void main(final String args[]) throws IOException {
457 final String tstname = TestUri01.class.getName();
458 org.junit.runner.JUnitCore.main(tstname);
459 }
460}
void test08NormalizedHierarchy()
Definition: TestUri01.java:248
void test02URIEscapeSpecialChars()
Definition: TestUri01.java:80
void test03URIEscapeCommonChars()
Definition: TestUri01.java:117
static void main(final String args[])
Definition: TestUri01.java:456
void test10ParentAndDirHierarchy()
Definition: TestUri01.java:333
static void showUri(final Uri uri)
static void showURI(final URI uri)
static void showReencodedURIOfUri(final Uri uri)
Just showing different encoding of Uri -> URI.