4import java.io.IOException;
6import java.net.URISyntaxException;
8import org.jau.io.IOUtil;
10import org.junit.Assert;
11import org.junit.FixMethodOrder;
13import org.junit.runners.MethodSorters;
15import jau.test.junit.util.JunitTracer;
17@FixMethodOrder(MethodSorters.NAME_ASCENDING)
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);
35 final String s2 = IOUtil.slashify(file.getAbsolutePath(),
true , file.isDirectory() );
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;
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;
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;
76 Assert.assertTrue(
"One or more errors occured see stderr above", ok);
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);
89 final String invEsc1 = esc1.decode();
90 System.err.println(
"inv(esc1) "+invEsc1);
91 Assert.assertEquals(vanilla, invEsc1);
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);
101 final String invEsc1 = esc1.decode();
102 System.err.println(
"inv(esc1) "+invEsc1);
103 Assert.assertEquals(vanilla, invEsc1);
107 final String vanilla =
"/XXX $ ^ ~ # [ ]";
112 final String vanilla =
"/XXX ! & ' ( ) + , / ; = @ [ ]";
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);
126 final String invEsc1 = esc1.decode();
127 System.err.println(
"inv(esc1) "+invEsc1);
128 Assert.assertEquals(vanilla, invEsc1);
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());
139 System.err.println(
"File-path -> Uri:");
140 final Uri uri0 = Uri.valueOfFilepath(
string);
143 System.err.println(
"Uri -> File:");
144 final Uri uri2 = Uri.valueOf(file);
147 System.err.println(
"Uri -> URI:");
148 final URI uri3 = uri2.toURI();
151 System.err.println(
"URI -> Uri (keep encoding):");
152 final Uri uri4 = Uri.valueOf(uri3);
155 System.err.println(
"URI -> Uri (re-encode):");
156 final Uri uri5 = Uri.valueOf(uri3);
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");
167 Assert.assertEquals(uri0, uri1);
168 Assert.assertEquals(uri0.hashCode(), uri1.hashCode());
170 Assert.assertEquals(uri0, uri2);
171 Assert.assertEquals(uri0.hashCode(), uri2.hashCode());
173 Assert.assertEquals(uri1, uri2);
174 Assert.assertEquals(uri1.hashCode(), uri2.hashCode());
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");
181 Assert.assertNotEquals(uri1, uriA);
182 Assert.assertNotEquals(uri1, uriB);
183 Assert.assertNotEquals(uri1, uriC);
184 Assert.assertNotEquals(uri1, uriD);
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");
190 Assert.assertEquals(uri0, uri1);
191 Assert.assertEquals(uri0.hashCode(), uri1.hashCode());
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");
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);
214 final Uri input = Uri.cast(
"http://localhost/test01.html#tag01");
215 final Uri contained = input.getContainedUri();
216 Assert.assertNull(contained);
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();
224 Assert.assertEquals(expected, contained);
225 Assert.assertEquals(expected.hashCode(), contained.hashCode());
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();
233 Assert.assertEquals(expected, contained);
234 Assert.assertEquals(expected.hashCode(), contained.hashCode());
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();
242 Assert.assertEquals(expected, contained);
243 Assert.assertEquals(expected.hashCode(), contained.hashCode());
250 final Uri input = Uri.cast(
"./dummy/nop/../a.txt");
251 final Uri expected = Uri.cast(
"dummy/a.txt");
253 final Uri normal = input.getNormalized();
254 Assert.assertEquals(expected, normal);
257 final Uri input = Uri.cast(
"../dummy/nop/../a.txt");
258 final Uri expected = Uri.cast(
"../dummy/a.txt");
260 final Uri normal = input.getNormalized();
261 Assert.assertEquals(expected, normal);
264 final Uri input = Uri.cast(
"http://localhost/dummy/../");
265 final Uri expected = Uri.cast(
"http://localhost/");
267 final Uri normal = input.getNormalized();
268 Assert.assertEquals(expected, normal);
271 final Uri input = Uri.cast(
"http://localhost/dummy/./../");
272 final Uri expected = Uri.cast(
"http://localhost/");
274 final Uri normal = input.getNormalized();
275 Assert.assertEquals(expected, normal);
278 final Uri input = Uri.cast(
"http://localhost/dummy/../aa/././../");
279 final Uri expected = Uri.cast(
"http://localhost/");
281 final Uri normal = input.getNormalized();
282 Assert.assertEquals(expected, normal);
285 final Uri input = Uri.cast(
"http://localhost/test/dummy/./../text.txt");
286 final Uri expected = Uri.cast(
"http://localhost/test/text.txt");
288 final Uri normal = input.getNormalized();
289 Assert.assertEquals(expected, normal);
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");
295 final Uri normal = input.getNormalized();
296 Assert.assertEquals(expected, normal);
303 final Uri input = Uri.cast(
"jar:http://localhost/dummy/../abc.jar!/");
304 final Uri expected = Uri.cast(
"jar:http://localhost/abc.jar!/");
306 final Uri normal = input.getNormalized();
307 Assert.assertEquals(expected, normal);
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!/");
313 final Uri normal = input.getNormalized();
314 Assert.assertEquals(expected, normal);
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");
320 final Uri normal = input.getNormalized();
321 Assert.assertEquals(expected, normal);
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");
327 final Uri normal = input.getNormalized();
328 Assert.assertEquals(expected, normal);
335 final Uri input = Uri.cast(
"http://localhost/");
337 final Uri directory = input.getDirectory();
338 Assert.assertEquals(input, directory);
339 final Uri parent = input.getParent();
340 Assert.assertNull(parent);
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/");
347 final Uri directory = input.getDirectory();
348 Assert.assertEquals(expectedD, directory);
349 final Uri parent = input.getParent();
350 Assert.assertEquals(expectedP, parent);
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/");
357 final Uri directory = input.getDirectory();
358 Assert.assertEquals(expectedD, directory);
359 final Uri parent = input.getParent();
360 Assert.assertEquals(expectedP, parent);
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;
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);
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);
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);
387 final Uri parent3 = parent2.getParent();
388 Assert.assertEquals(expParen3, parent3);
389 final Uri folder3 = parent2.getDirectory();
390 Assert.assertEquals(expFolde3, folder3);
397 final Uri input = Uri.cast(
"jar:http://localhost/test.jar!/");
399 final Uri directory = input.getDirectory();
400 Assert.assertEquals(input, directory);
401 final Uri parent = input.getParent();
402 Assert.assertNull(parent);
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;
409 final Uri directory = input.getDirectory();
410 Assert.assertEquals(expectedD, directory);
411 final Uri parent = input.getParent();
412 Assert.assertEquals(expectedP, parent);
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/");
419 final Uri directory = input.getDirectory();
420 Assert.assertEquals(expectedD, directory);
421 final Uri parent = input.getParent();
422 Assert.assertEquals(expectedP, parent);
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;
430 final Uri expParen3 =
null;
431 final Uri expFolde3 = expParen2;
432 Assert.assertNotEquals(input, expParen1);
433 Assert.assertNotEquals(expParen1, expParen2);
434 Assert.assertNotEquals(expParen1, expParen3);
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);
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);
449 final Uri parent3 = parent2.getParent();
450 Assert.assertEquals(expParen3, parent3);
451 final Uri folder3 = parent2.getDirectory();
452 Assert.assertEquals(expFolde3, folder3);
456 public static void main(
final String args[])
throws IOException {
457 final String tstname =
TestUri01.class.getName();
458 org.junit.runner.JUnitCore.
main(tstname);
void test04EqualsAndHashCode()
void test08NormalizedHierarchy()
void test02URIEscapeSpecialChars()
void test11ParentAndDirOpaque()
void test03URIEscapeCommonChars()
static void main(final String args[])
void test10ParentAndDirHierarchy()
void test09NormalizedOpaque()
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.