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());
35 final String s2 =
IOUtil.
slashify(file.getAbsolutePath(),
true , file.isDirectory() );
36 System.err.println(
"uri2.slashify: "+s2);
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);
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";
54 final boolean equalPath = rPath.equals(uri1.
path.
decode());
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);
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";
69 final boolean equalPath = rPath.equals(uri1.
path.
decode());
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);
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);
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);
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:");
143 System.err.println(
"Uri -> File:");
147 System.err.println(
"Uri -> URI:");
148 final URI uri3 = uri2.
toURI();
151 System.err.println(
"URI -> Uri (keep encoding):");
155 System.err.println(
"URI -> Uri (re-encode):");
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);
170 Assert.assertEquals(uri0, uri2);
173 Assert.assertEquals(uri1, uri2);
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);
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");
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");
224 Assert.assertEquals(expected, contained);
228 final Uri input =
Uri.
cast(
"jar:file://localhost/test01.jar!/");
229 final Uri expected =
Uri.
cast(
"file://localhost/test01.jar");
233 Assert.assertEquals(expected, contained);
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");
242 Assert.assertEquals(expected, contained);
250 final Uri input =
Uri.
cast(
"./dummy/nop/../a.txt");
254 Assert.assertEquals(expected, normal);
257 final Uri input =
Uri.
cast(
"../dummy/nop/../a.txt");
258 final Uri expected =
Uri.
cast(
"../dummy/a.txt");
261 Assert.assertEquals(expected, normal);
264 final Uri input =
Uri.
cast(
"http://localhost/dummy/../");
265 final Uri expected =
Uri.
cast(
"http://localhost/");
268 Assert.assertEquals(expected, normal);
271 final Uri input =
Uri.
cast(
"http://localhost/dummy/./../");
272 final Uri expected =
Uri.
cast(
"http://localhost/");
275 Assert.assertEquals(expected, normal);
278 final Uri input =
Uri.
cast(
"http://localhost/dummy/../aa/././../");
279 final Uri expected =
Uri.
cast(
"http://localhost/");
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");
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");
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!/");
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!/");
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");
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");
328 Assert.assertEquals(expected, normal);
335 final Uri input =
Uri.
cast(
"http://localhost/");
338 Assert.assertEquals(input, directory);
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/");
348 Assert.assertEquals(expectedD, directory);
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/");
358 Assert.assertEquals(expectedD, directory);
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);
376 Assert.assertEquals(expParen1, parent1);
379 Assert.assertEquals(expFolde1, folder1);
382 Assert.assertEquals(expParen2, parent2);
385 Assert.assertEquals(expFolde2, folder2);
388 Assert.assertEquals(expParen3, parent3);
390 Assert.assertEquals(expFolde3, folder3);
397 final Uri input =
Uri.
cast(
"jar:http://localhost/test.jar!/");
400 Assert.assertEquals(input, directory);
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;
410 Assert.assertEquals(expectedD, directory);
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/");
420 Assert.assertEquals(expectedD, directory);
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);
438 Assert.assertEquals(expParen1, parent1);
441 Assert.assertEquals(expFolde1, folder1);
444 Assert.assertEquals(expParen2, parent2);
447 Assert.assertEquals(expFolde2, folder2);
450 Assert.assertEquals(expParen3, parent3);
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.
static String slashify(final String path, final boolean startWithSlash, final boolean endWithSlash)
Immutable RFC3986 encoded string.
static Encoded cast(final String encoded)
Casts the given encoded String by creating a new Encoded instance.
final String get()
Returns the encoded String.
final boolean equals(final Object o)
final String decode()
Decodes the string argument which is assumed to be encoded in the x-www-form-urlencoded MIME conten...
This class implements an immutable Uri as defined by RFC 2396.
Uri getDirectory()
Returns this Uri's directory Uri.
static final String PATH_LEGAL
Valid charset for RFC 2396 path, additional to legal alphanum characters.
final java.net.URI toURI()
Returns a new URI instance using the encoded input string, new URI(uri.input), i.e.
final String toString()
Returns the encoded input as String, never null, same as getEncoded().
final Encoded getEncoded()
Returns the encoded input, never null.
final Uri getParent()
Returns this Uri's parent directory Uri.
ASCIIEncoded toASCIIString()
Returns the encoded input encoded in US-ASCII.
static final String HTTP_SCHEME
{@value}
static Uri cast(final String encodedUri)
Casts the given encoded String to a new Encoded instance used to create the resulting Uri instance vi...
final boolean equals(final Object o)
final Uri getContainedUri()
If this instance's schemeSpecificPart contains a Uri itself, a sub-Uri, return schemeSpecificPart + #...
static Uri valueOfFilepath(final String path)
Creates a new Uri instance using the given file-path argument.
static Uri valueOf(final File file)
Creates a new Uri instance using the given File instance.
static final String FILE_SCHEME
{@value}
final Encoded path
Encoded path part of scheme-specific-part, never null.
static Uri create(final String scheme, final String ssp, final String fragment)
Creates a new Uri instance using the given unencoded arguments.
final Uri getNormalized()
Normalizes this Uri's path and return the normalized form if it differs, otherwise this instance.