package com.jogamp.common.net; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import jogamp.common.os.PlatformPropsImpl; import org.junit.Assert; import org.junit.Test; import com.jogamp.common.net.URIDumpUtil; import com.jogamp.common.os.Platform; import com.jogamp.junit.util.JunitTracer; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class TestUri01 extends JunitTracer { @Test public void test00URIEscapeSpecialChars() throws IOException, URISyntaxException { { final String vanilla = "XXX ! # $ & ' ( ) * + , / : ; = ? @ [ ]"; 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"); System.err.println("vanilla "+vanilla); final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL); System.err.println("esc1 "+esc1); Assert.assertEquals(escaped, esc1); final String invEsc1 = esc1.decode(); System.err.println("inv(esc1) "+invEsc1); Assert.assertEquals(vanilla, invEsc1); } { final String vanilla = "/XXX R!# R$&'()*+,/:;=?z@y[x]"; 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"); System.err.println("vanilla "+vanilla); final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL); System.err.println("esc1 "+esc1); Assert.assertEquals(escaped, esc1); final String invEsc1 = esc1.decode(); System.err.println("inv(esc1) "+invEsc1); Assert.assertEquals(vanilla, invEsc1); } { // Bug 908: $ ^ ~ # [ ] final String vanilla = "/XXX $ ^ ~ # [ ]"; showDump0x(vanilla); } { // Windows invalid File characters: * ? " < > | final String vanilla = "/XXX ! & ' ( ) + , / ; = @ [ ]"; showDump0x(vanilla); } } @Test public void test01URIEscapeCommonChars() throws IOException, URISyntaxException { { final String vanilla = "/XXX \"%-.<>\\^_`{|}~"; final Uri.Encoded escaped = Uri.Encoded.cast("/XXX%20%22%25-.%3C%3E%5C%5E_%60%7B%7C%7D~"); System.err.println("vanilla "+vanilla); final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL); System.err.println("esc1 "+esc1); Assert.assertEquals(escaped, esc1); final String invEsc1 = esc1.decode(); System.err.println("inv(esc1) "+invEsc1); Assert.assertEquals(vanilla, invEsc1); showDump0x(vanilla); } } private static void showDump0x(final String string) throws IOException, URISyntaxException { final File file = new File(string); System.err.println("file "+file); System.err.println("file.path.dec "+file.getPath()); System.err.println("file.path.abs "+file.getAbsolutePath()); System.err.println("file.path.can "+file.getCanonicalPath()); System.err.println("File-path -> Uri:"); final Uri uri0 = Uri.valueOfFilepath(string); URIDumpUtil.showUri(uri0); System.err.println("Uri -> File:"); final Uri uri2 = Uri.valueOf(file); URIDumpUtil.showUri(uri2); System.err.println("Uri -> URI:"); final URI uri3 = uri2.toURI(); URIDumpUtil.showURI(uri3); System.err.println("URI -> Uri (keep encoding):"); final Uri uri4 = Uri.valueOf(uri3, false); URIDumpUtil.showUri(uri4); System.err.println("URI -> Uri (re-encode):"); final Uri uri5 = Uri.valueOf(uri3, true); URIDumpUtil.showUri(uri5); } @Test public void test03EqualsAndHashCode() throws IOException, URISyntaxException { { final Uri uri0 = Uri.cast("http://localhost/test01.html#tag01"); final Uri uri1 = Uri.create("http", null, "localhost", -1, "/test01.html", null, "tag01"); final Uri uri2 = Uri.create("http", "localhost", "/test01.html", "tag01"); Assert.assertEquals(uri0, uri1); Assert.assertEquals(uri0.hashCode(), uri1.hashCode()); Assert.assertEquals(uri0, uri2); Assert.assertEquals(uri0.hashCode(), uri2.hashCode()); Assert.assertEquals(uri1, uri2); Assert.assertEquals(uri1.hashCode(), uri2.hashCode()); final Uri uriA = Uri.create("http", null, "localhost", -1, "/test02.html", null, "tag01"); final Uri uriB = Uri.create("http", null, "localhost", -1, "/test01.html", null, "tag02"); final Uri uriC = Uri.create("http", null, "lalalhost", -1, "/test01.html", null, "tag01"); final Uri uriD = Uri.create("sftp", null, "localhost", -1, "/test01.html", null, "tag01"); Assert.assertNotEquals(uri1, uriA); Assert.assertNotEquals(uri1, uriB); Assert.assertNotEquals(uri1, uriC); Assert.assertNotEquals(uri1, uriD); } { // 3 [scheme:][//[user-info@]host[:port]]path[?query][#fragment] final Uri uri0 = Uri.cast("http://user@localhost:80/test01.html?test=01&test=02#tag01"); final Uri uri1 = Uri.create("http", "user", "localhost", 80, "/test01.html", "test=01&test=02", "tag01"); Assert.assertEquals(uri0, uri1); Assert.assertEquals(uri0.hashCode(), uri1.hashCode()); final Uri uriA = Uri.cast("http://user@localhost:80/test01.html?test=01&test=02#tag02"); final Uri uriB = Uri.cast("http://user@localhost:80/test01.html?test=01&test=03#tag01"); final Uri uriC = Uri.cast("http://user@localhost:80/test04.html?test=01&test=02#tag01"); final Uri uriD = Uri.cast("http://user@localhost:88/test01.html?test=01&test=02#tag01"); final Uri uriE = Uri.cast("http://user@lalalhost:80/test01.html?test=01&test=02#tag01"); final Uri uriF = Uri.cast("http://test@localhost:80/test01.html?test=01&test=02#tag01"); final Uri uriG = Uri.cast("sftp://user@localhost:80/test01.html?test=01&test=02#tag01"); Assert.assertNotEquals(uri1, uriA); Assert.assertNotEquals(uri1, uriB); Assert.assertNotEquals(uri1, uriC); Assert.assertNotEquals(uri1, uriD); Assert.assertNotEquals(uri1, uriE); Assert.assertNotEquals(uri1, uriF); Assert.assertNotEquals(uri1, uriG); } } @Test public void test04ContainedUri() throws IOException, URISyntaxException { { final Uri input = Uri.cast("http://localhost/test01.html#tag01"); final Uri contained = input.getContainedUri(); Assert.assertNull(contained); } { final Uri input = Uri.cast("jar:http://localhost/test01.jar!/com/jogamp/Lala.class#tag01"); final Uri expected = Uri.cast("http://localhost/test01.jar#tag01"); final Uri contained = input.getContainedUri(); Assert.assertEquals(expected, contained); Assert.assertEquals(expected.hashCode(), contained.hashCode()); } { final Uri input = Uri.cast("jar:file://localhost/test01.jar!/"); final Uri expected = Uri.cast("file://localhost/test01.jar"); final Uri contained = input.getContainedUri(); Assert.assertEquals(expected, contained); Assert.assertEquals(expected.hashCode(), contained.hashCode()); } { final Uri input = Uri.cast("sftp:http://localhost/test01.jar?lala=01#tag01"); final Uri expected = Uri.cast("http://localhost/test01.jar?lala=01#tag01"); final Uri contained = input.getContainedUri(); Assert.assertEquals(expected, contained); Assert.assertEquals(expected.hashCode(), contained.hashCode()); } } @Test public void test05ParentUri() throws IOException, URISyntaxException { { final Uri input = Uri.cast("http://localhost/"); final Uri parent = input.getParent(); Assert.assertNull(parent); } { final Uri input = Uri.cast("jar:http://localhost/test01.jar!/com/Lala.class"); final Uri expected1 = Uri.cast("jar:http://localhost/test01.jar!/com/"); final Uri expected2 = Uri.cast("jar:http://localhost/test01.jar!/"); final Uri expected3 = Uri.cast("jar:http://localhost/"); final Uri parent1 = input.getParent(); final Uri parent2 = parent1.getParent(); final Uri parent3 = parent2.getParent(); Assert.assertEquals(expected1, parent1); Assert.assertEquals(expected1.hashCode(), parent1.hashCode()); Assert.assertEquals(expected2, parent2); Assert.assertEquals(expected2.hashCode(), parent2.hashCode()); Assert.assertEquals(expected3, parent3); Assert.assertEquals(expected3.hashCode(), parent3.hashCode()); } { final Uri input = Uri.cast("http://localhost/dir/test01.jar?lala=01#frag01"); final Uri expected1 = Uri.cast("http://localhost/dir/"); final Uri expected2 = Uri.cast("http://localhost/"); final Uri parent1 = input.getParent(); final Uri parent2 = parent1.getParent(); Assert.assertEquals(expected1, parent1); Assert.assertEquals(expected1.hashCode(), parent1.hashCode()); Assert.assertEquals(expected2, parent2); Assert.assertEquals(expected2.hashCode(), parent2.hashCode()); } } @Test public void test10HttpUri2URL() throws IOException, URISyntaxException { testUri2URL(getSimpleTestName("."), TestUri03Resolving.uriHttpSArray); } @Test public void test20FileUnixUri2URL() throws IOException, URISyntaxException { testUri2URL(getSimpleTestName("."), TestUri03Resolving.uriFileSArrayUnix); } @Test public void test21FileWindowsUri2URL() throws IOException, URISyntaxException { testUri2URL(getSimpleTestName("."), TestUri03Resolving.uriFileSArrayWindows); } @Test public void test30FileUnixUri2URL() throws IOException, URISyntaxException { if( Platform.OSType.WINDOWS != PlatformPropsImpl.OS_TYPE ) { testFile2Uri(getSimpleTestName("."), TestUri03Resolving.fileSArrayUnix); } } @Test public void test31FileWindowsUri2URL() throws IOException, URISyntaxException { if( Platform.OSType.WINDOWS == PlatformPropsImpl.OS_TYPE ) { testFile2Uri(getSimpleTestName("."), TestUri03Resolving.fileSArrayWindows); } } static void testUri2URL(final String testname, final String[][] uriSArray) throws IOException, URISyntaxException { boolean ok = true; for(int i=0; i