Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestUri03Resolving.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;
7import java.net.URL;
8import java.net.URLConnection;
9
10import org.jau.net.Uri;
11import org.jau.sys.PlatformProps;
12import org.jau.sys.PlatformTypes;
13import org.junit.Assert;
14import org.junit.FixMethodOrder;
15import org.junit.Test;
16import org.junit.runners.MethodSorters;
17
18import jau.test.junit.util.JunitTracer;
19
20@FixMethodOrder(MethodSorters.NAME_ASCENDING)
21public class TestUri03Resolving extends JunitTracer {
22
23 // Bug 908, issues w/ windows file path char: $ ^ ~ # [ ]
24
25 private static final String[][] uriHttpSArray = new String[][] {
26 new String[] {"http://localhost/gluegen/build-x86_64/gluegen-rt.jar"},
27
28 new String[] {"http://localhost/gluegen/"+'\u0394'+"/gluegen-rt.jar"},
29
30 new String[] {"http://localhost/gluegen/build-x86_64%20lala/gluegen-rt.jar"},
31
32 new String[] {"http://localhost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar"},
33
34 new String[] {"jar:http://localhost/gluegen/build-x86_64/gluegen-rt.jar!/"},
35
36 new String[] {"jar:http://localhost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/"},
37
38 new String[] {"jar:http://localhost/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
39
40 new String[] {"jar:http://localhost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
41
42 new String[] {"jar:http://localhost/gluegen/R%23/gluegen-rt.jar!/"},
43
44 new String[] {"jar:http://localhost/gluegen/A%24/B%5E/C~/D%23/E%5B/F%5D/gluegen-rt.jar!/"},
45
46 new String[] {"jar:http://localhost/gluegen/%24/%5E/~/%23/%5B/%5D/gluegen-rt.jar!/"},
47
48 new String[] {"jar:http://localhost/gluegen/"+'\u0394'+"/gluegen-rt.jar!/"},
49 };
50
51 private static final String[][] uriFileSArrayUnix = new String[][] {
52 new String[] {"file:/gluegen/build-x86_64/gluegen-rt.jar"},
53
54 new String[] {"file:/gluegen/"+'\u0394'+"/gluegen-rt.jar"},
55
56 new String[] {"file:/gluegen/build-x86_64%20lala/gluegen-rt.jar"},
57
58 new String[] {"file:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar"},
59
60 new String[] {"jar:file:/gluegen/build-x86_64/gluegen-rt.jar!/"},
61
62 new String[] {"jar:file:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/"},
63
64 new String[] {"jar:file:/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
65
66 new String[] {"jar:file:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
67
68 new String[] {"jar:file://filehost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
69
70 new String[] {"jar:file:/gluegen/R%23/gluegen-rt.jar!/"},
71
72 new String[] {"jar:file:/gluegen/A%24/B%5E/C~/D%23/E%5B/F%5D/gluegen-rt.jar!/"},
73
74 new String[] {"jar:file:/gluegen/%24/%5E/~/%23/%5B/%5D/gluegen-rt.jar!/"},
75
76 new String[] {"jar:file:/gluegen/"+'\u0394'+"/gluegen-rt.jar!/"},
77 };
78
79 private static final String[][] uriFileSArrayWindows = new String[][] {
80 new String[] {"file:/C%3A/gluegen/build-x86_64/gluegen-rt.jar"},
81
82 new String[] {"file:/C%3A/gluegen/"+'\u0394'+"/gluegen-rt.jar"},
83
84 new String[] {"file:/C%3A/gluegen/build-x86_64%20lala/gluegen-rt.jar"},
85
86 new String[] {"file:/C%3A/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar"},
87
88 new String[] {"jar:file:/C%3A/gluegen/build-x86_64/gluegen-rt.jar!/"},
89
90 new String[] {"jar:file:/C%3A/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/"},
91
92 new String[] {"jar:file:/C%3A/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
93
94 new String[] {"jar:file:/C%3A/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
95
96 new String[] {"jar:file:///C%3A/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
97
98 new String[] {"jar:file://filehost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
99
100 new String[] {"jar:file:/C%3A/gluegen/R%23/gluegen-rt.jar!/"},
101
102 new String[] {"jar:file:/C%3A/gluegen/A%24/B%5E/C~/D%23/E%5B/F%5D/gluegen-rt.jar!/"},
103
104 new String[] {"jar:file:/C%3A/gluegen/%24/%5E/~/%23/%5B/%5D/gluegen-rt.jar!/"},
105
106 new String[] {"jar:file:/C%3A/gluegen/"+'\u0394'+"/gluegen-rt.jar!/"},
107 };
108
109 private static final String[][] urlFileSArrayWindows = new String[][] {
110 new String[] {"file:/C:/gluegen/build-x86_64/gluegen-rt.jar"},
111
112 new String[] {"file:/C:/gluegen/"+'\u0394'+"/gluegen-rt.jar"},
113
114 new String[] {"file:/C:/gluegen/build-x86_64%20lala/gluegen-rt.jar"},
115
116 new String[] {"file:/C:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar"},
117
118 new String[] {"jar:file:/C:/gluegen/build-x86_64/gluegen-rt.jar!/"},
119
120 new String[] {"jar:file:/C:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/"},
121
122 new String[] {"jar:file:/C:/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
123
124 new String[] {"jar:file:/C:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
125
126 new String[] {"jar:file:///C:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
127
128 new String[] {"jar:file://filehost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class"},
129
130 new String[] {"jar:file:/C:/gluegen/R%23/gluegen-rt.jar!/"},
131
132 new String[] {"jar:file:/C:/gluegen/A%24/B%5E/C~/D%23/E%5B/F%5D/gluegen-rt.jar!/"},
133
134 new String[] {"jar:file:/C:/gluegen/%24/%5E/~/%23/%5B/%5D/gluegen-rt.jar!/"},
135
136 new String[] {"jar:file:/C:/gluegen/"+'\u0394'+"/gluegen-rt.jar!/"},
137 };
138
139 public static final String[][] fileSArrayUnix = new String[][] {
140 new String[] {"/gluegen/build-x86_64/gluegen-rt.jar",
141 "file:/gluegen/build-x86_64/gluegen-rt.jar",
142 "/gluegen/build-x86_64/gluegen-rt.jar"},
143
144 new String[] {"/gluegen/"+'\u0394'+"/gluegen-rt.jar",
145 "file:/gluegen/"+'\u0394'+"/gluegen-rt.jar",
146 "/gluegen/"+'\u0394'+"/gluegen-rt.jar"},
147
148 new String[] {"/gluegen/build-x86_64 lala/gluegen-rt.jar",
149 "file:/gluegen/build-x86_64%20lala/gluegen-rt.jar",
150 "/gluegen/build-x86_64 lala/gluegen-rt.jar"},
151
152 new String[] {"/gluegen/build-x86_64 öä lala/gluegen-rt.jar",
153 "file:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar",
154 "/gluegen/build-x86_64 öä lala/gluegen-rt.jar"},
155
156 new String[] {"/gluegen/A$/B^/C~/D#/E[/F]/gluegen-rt.jar",
157 "file:/gluegen/A%24/B%5E/C~/D%23/E%5B/F%5D/gluegen-rt.jar",
158 "/gluegen/A$/B^/C~/D#/E[/F]/gluegen-rt.jar" },
159
160 new String[] {"/gluegen/$/^/~/#/[/]/gluegen-rt.jar",
161 "file:/gluegen/%24/%5E/~/%23/%5B/%5D/gluegen-rt.jar",
162 "/gluegen/$/^/~/#/[/]/gluegen-rt.jar" },
163 };
164
165 public static final String[][] fileSArrayWindows = new String[][] {
166 new String[] {"C:/gluegen/build-x86_64/gluegen-rt.jar",
167 "file:/C%3A/gluegen/build-x86_64/gluegen-rt.jar",
168 "C:\\gluegen\\build-x86_64\\gluegen-rt.jar"},
169
170 new String[] {"C:/gluegen/"+'\u0394'+"/gluegen-rt.jar",
171 "file:/C%3A/gluegen/"+'\u0394'+"/gluegen-rt.jar",
172 "C:\\gluegen\\"+'\u0394'+"\\gluegen-rt.jar"},
173
174 new String[] {"C:/gluegen/build-x86_64 lala/gluegen-rt.jar",
175 "file:/C%3A/gluegen/build-x86_64%20lala/gluegen-rt.jar",
176 "C:\\gluegen\\build-x86_64 lala\\gluegen-rt.jar"},
177
178 new String[] {"C:/gluegen/build-x86_64 öä lala/gluegen-rt.jar",
179 "file:/C%3A/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar",
180 "C:\\gluegen\\build-x86_64 öä lala\\gluegen-rt.jar"},
181
182 new String[] {"C:\\gluegen\\build-x86_64 öä lala\\gluegen-rt.jar",
183 "file:/C%3A/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar",
184 "C:\\gluegen\\build-x86_64 öä lala\\gluegen-rt.jar"},
185
186 new String[] {"\\\\filehost\\gluegen\\build-x86_64 öä lala\\gluegen-rt.jar",
187 "file://filehost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar",
188 "\\\\filehost\\gluegen\\build-x86_64 öä lala\\gluegen-rt.jar"},
189
190 new String[] {"C:/gluegen/A$/B^/C~/D#/E[/F]/gluegen-rt.jar",
191 "file:/C%3A/gluegen/A%24/B%5E/C~/D%23/E%5B/F%5D/gluegen-rt.jar",
192 "C:\\gluegen\\A$\\B^\\C~\\D#\\E[\\F]\\gluegen-rt.jar" },
193
194 new String[] {"C:/gluegen/$/^/~/#/[/]/gluegen-rt.jar",
195 "file:/C%3A/gluegen/%24/%5E/~/%23/%5B/%5D/gluegen-rt.jar",
196 "C:\\gluegen\\$\\^\\~\\#\\[\\]\\gluegen-rt.jar" },
197 };
198
199 @Test
200 public void test01HttpUri2URL() throws IOException, URISyntaxException {
201 testUri2URL(getSimpleTestName("."), uriHttpSArray);
202 }
203
204 @Test
205 public void test02FileUnixUri2URL() throws IOException, URISyntaxException {
206 testUri2URL(getSimpleTestName("."), uriFileSArrayUnix);
207 }
208
209 @Test
210 public void test03FileWindowsUri2URL() throws IOException, URISyntaxException {
211 testUri2URL(getSimpleTestName("."), uriFileSArrayWindows);
212 }
213
214 @Test
215 public void test11HttpURL2Uri() throws IOException, URISyntaxException {
216 testURL2Uri(getSimpleTestName("."), uriHttpSArray);
217 }
218
219 @Test
220 public void test12FileUnixURL2Uri() throws IOException, URISyntaxException {
221 testURL2Uri(getSimpleTestName("."), uriFileSArrayUnix);
222 }
223
224 @Test
225 public void test13FileWindowsURL2Uri() throws IOException, URISyntaxException {
226 testURL2Uri(getSimpleTestName("."), urlFileSArrayWindows);
227 }
228
229 @Test
230 public void test24FileUnixURI2URL() throws IOException, URISyntaxException {
231 if( PlatformTypes.OSType.WINDOWS != PlatformProps.OS ) {
232 testFile2Uri(getSimpleTestName("."), fileSArrayUnix);
233 }
234 }
235
236 @Test
237 public void test25FileWindowsURI2URL() throws IOException, URISyntaxException {
238 if( PlatformTypes.OSType.WINDOWS == PlatformProps.OS ) {
239 testFile2Uri(getSimpleTestName("."), fileSArrayWindows);
240 }
241 }
242
243 static void testUri2URL(final String testname, final String[][] uriSArray) throws IOException, URISyntaxException {
244 boolean ok = true;
245 for(int i=0; i<uriSArray.length; i++) {
246 final String[] uriSPair = uriSArray[i];
247 final String uriSource = uriSPair[0];
248 System.err.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS "+testname+": "+(i+1)+"/"+uriSArray.length);
249 ok = testUri2URL(Uri.Encoded.cast(uriSource)) && ok;
250 System.err.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE "+testname+": "+(i+1)+"/"+uriSArray.length);
251 }
252 Assert.assertTrue("One or more errors occured see stderr above", ok);
253 }
254
255 static boolean testUri2URL(final Uri.Encoded uriSource) throws IOException, URISyntaxException {
256 System.err.println("uriSource : "+uriSource);
257 final Uri uri0 = new Uri(uriSource);
258 URIDumpUtil.showUri(uri0);
259
260 final URI actualURI = uri0.toURI();
261 URIDumpUtil.showURI(actualURI);
262 final Uri.Encoded actualURIStr = Uri.Encoded.cast(actualURI.toString());
263
264 final URL actualURL = uri0.toURL();
265 URIDumpUtil.showURL(actualURL);
266 final Uri.Encoded actualURLStr = Uri.Encoded.cast(actualURL.toExternalForm());
267
268 System.err.println("expected_URX: "+uriSource);
269
270 final boolean equalsURI = uriSource.equals(actualURIStr);
271 System.err.println("actual URI: "+actualURIStr+" - "+(equalsURI?"OK":"ERROR"));
272 final boolean equalsURL = uriSource.equals(actualURLStr);
273 System.err.println("actual URL: "+actualURLStr+" - "+(equalsURL?"OK":"ERROR"));
274 URIDumpUtil.showReencodedURIOfUri(uri0);
275 URIDumpUtil.showReencodedUriOfURI(actualURI);
276
277 boolean ok = equalsURL && equalsURI;
278
279 // now test open ..
280 Throwable t = null;
281 URLConnection con = null;
282 try {
283 con = actualURL.openConnection();
284 } catch (final Throwable _t) {
285 t = _t;
286 }
287 if( null != t ) {
288 System.err.println("XXX: "+t.getClass().getName()+": "+t.getMessage());
289 t.printStackTrace();
290 } else {
291 System.err.println("XXX: No openConnection() failure");
292 System.err.println("XXX: "+con);
293 }
294
295 if( uri0.scheme.toString().equals(Uri.JAR_SCHEME) ) {
296 // Extended tests on JAR Uri
297 final Uri uriSub0 = uri0.getContainedUri();
298 Assert.assertNotNull(uriSub0);
299 System.err.println("EXT JAR contained:");
300 URIDumpUtil.showUri(uriSub0);
301 final Uri uriSubDir0 = uriSub0.getDirectory();
302 final Uri uriSubParent0 = uriSub0.getParent();
303 System.err.println("EXT JAR contained Dir:");
304 URIDumpUtil.showUri(uriSubDir0);
305 System.err.println("EXT JAR contained Parent:");
306 URIDumpUtil.showUri(uriSubParent0);
307 ok = uriSubDir0.equals(uriSubParent0) && ok;
308 }
309 return ok;
310 }
311
312 static void testURL2Uri(final String testname, final String[][] urlSArray) throws IOException, URISyntaxException {
313 boolean ok = true;
314 for(int i=0; i<urlSArray.length; i++) {
315 final String[] uriSPair = urlSArray[i];
316 final String uriSource = uriSPair[0];
317 System.err.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS "+testname+": "+(i+1)+"/"+urlSArray.length);
318 ok = testURL2Uri(new URL(uriSource)) && ok;
319 System.err.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE "+testname+": "+(i+1)+"/"+urlSArray.length);
320 }
321 Assert.assertTrue("One or more errors occured see stderr above", ok);
322 }
323
324 static boolean testURL2Uri(final URL urlSource) throws IOException, URISyntaxException {
325 System.err.println("URL Source : "+urlSource);
326 URIDumpUtil.showURL(urlSource);
327
328 final URI uriSource = urlSource.toURI();
329 URIDumpUtil.showURI(uriSource);
330
331 final Uri uri0 = Uri.valueOf(urlSource);
332 URIDumpUtil.showUri(uri0);
333
334 final URL uriToURL = uri0.toURL();
335 URIDumpUtil.showURL(uriToURL);
336
337 // now test open ..
338 Throwable t = null;
339 URLConnection con = null;
340 try {
341 con = uriToURL.openConnection();
342 } catch (final Throwable _t) {
343 t = _t;
344 }
345 if( null != t ) {
346 System.err.println("XXX: "+t.getClass().getName()+": "+t.getMessage());
347 t.printStackTrace();
348 } else {
349 System.err.println("XXX: No openConnection() failure");
350 System.err.println("XXX: "+con);
351 }
352
353 boolean ok = true;
354
355 if( uri0.scheme.toString().equals(Uri.JAR_SCHEME) ) {
356 // Extended tests on JAR Uri
357 final Uri uriSub0 = uri0.getContainedUri();
358 Assert.assertNotNull(uriSub0);
359 System.err.println("EXT JAR contained:");
360 URIDumpUtil.showUri(uriSub0);
361 final Uri uriSubDir0 = uriSub0.getDirectory();
362 final Uri uriSubParent0 = uriSub0.getParent();
363 System.err.println("EXT JAR contained Dir:");
364 URIDumpUtil.showUri(uriSubDir0);
365 System.err.println("EXT JAR contained Parent:");
366 URIDumpUtil.showUri(uriSubParent0);
367 ok = uriSubDir0.equals(uriSubParent0) && ok;
368 }
369 return ok;
370 }
371
372 static void testFile2Uri(final String testname, final String[][] uriSArray) throws IOException, URISyntaxException {
373 boolean ok = true;
374 for(int i=0; i<uriSArray.length; i++) {
375 final String[] uriSPair = uriSArray[i];
376 final String uriSource = uriSPair[0];
377 final String uriEncExpected= uriSPair[1];
378 final String fileExpected= uriSPair[2];
379 System.err.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS "+testname+": "+(i+1)+"/"+uriSArray.length);
380 ok = testFile2Uri(uriSource, Uri.Encoded.cast(uriEncExpected), fileExpected) && ok;
381 System.err.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE "+testname+": "+(i+1)+"/"+uriSArray.length);
382 }
383 Assert.assertTrue("One or more errors occured see stderr above", ok);
384 }
385
386 static boolean testFile2Uri(final String fileSource, final Uri.Encoded uriEncExpected, final String fileExpected) throws IOException, URISyntaxException {
387 System.err.println("fileSource: "+fileSource);
388 final File file = new File(fileSource);
389 System.err.println("file: "+file.getAbsolutePath());
390
391 final Uri uri0 = Uri.valueOf(file);
392 URIDumpUtil.showReencodedURIOfUri(uri0);
393
394 final URL actualUrl = uri0.toURL();
395 final File actualFile = uri0.toFile();
396 final boolean equalsFilePath = fileExpected.equals(actualFile.getPath());
397 System.err.println("expected_path: "+fileExpected);
398 System.err.println("actual___file-path: "+actualFile+" - "+(equalsFilePath?"OK":"ERROR"));
399 final boolean equalsEncUri = uriEncExpected.equals(uri0.getEncoded());
400 System.err.println("expected__encUri: "+uriEncExpected);
401 System.err.println("actual_______Uri: "+uri0.getEncoded()+" - "+(equalsEncUri?"OK":"ERROR"));
402 final boolean ok = equalsEncUri && equalsFilePath;
403
404 System.err.println("actual_______URL: "+actualUrl.toExternalForm());
405
406 // now test open ..
407 Throwable t = null;
408 URLConnection con = null;
409 try {
410 con = actualUrl.openConnection();
411 } catch (final Throwable _t) {
412 t = _t;
413 }
414 if( null != t ) {
415 System.err.println("XXX: "+t.getClass().getName()+": "+t.getMessage());
416 t.printStackTrace();
417 } else {
418 System.err.println("XXX: No openConnection() failure");
419 System.err.println("XXX: "+con);
420 }
421 return ok;
422 }
423
424 public static void main(final String args[]) throws IOException {
425 final String tstname = TestUri03Resolving.class.getName();
426 org.junit.runner.JUnitCore.main(tstname);
427 }
428}
static void main(final String args[])