diff options
-rwxr-xr-x | make/scripts/java-win64.bat | 5 | ||||
-rwxr-xr-x | make/scripts/runtest-x64.bat | 8 | ||||
-rwxr-xr-x | make/scripts/runtest.sh | 9 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/IOUtil.java | 130 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/JarUtil.java | 5 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/net/TestURIQueryProps.java | 50 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/net/TestUrisWithAssetHandler.java | 50 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/net/URIDumpUtil.java | 49 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/net/URLCompositionTest.java | 241 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestIOUtilURICompose.java | 92 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/TestIOUtilURIHandling.java | 150 |
11 files changed, 523 insertions, 266 deletions
diff --git a/make/scripts/java-win64.bat b/make/scripts/java-win64.bat index a5b673f..c8e6be8 100755 --- a/make/scripts/java-win64.bat +++ b/make/scripts/java-win64.bat @@ -13,5 +13,8 @@ set CP_ALL=.;%BLD_DIR%\gluegen-rt.jar;%BLD_DIR%\test\build\gluegen-test.jar;lib\ echo CP_ALL %CP_ALL%
-%J2RE_HOME%\bin\java -classpath %CP_ALL% "-Djava.library.path=%LIB_DIR%" "-Dsun.java2d.noddraw=true" "-Dsun.awt.noerasebackground=true" %1 %2 %3 %4 %5 %6 %7 %8 %9 > java-win64.log 2>&1
+set D_ARGS="-Djogamp.debug.IOUtil"
+REM set D_ARGS="-Djogamp.debug=all"
+
+%J2RE_HOME%\bin\java -classpath %CP_ALL% %D_ARGS% "-Djava.library.path=%LIB_DIR%" "-Dsun.java2d.noddraw=true" "-Dsun.awt.noerasebackground=true" %1 %2 %3 %4 %5 %6 %7 %8 %9 > java-win64.log 2>&1
diff --git a/make/scripts/runtest-x64.bat b/make/scripts/runtest-x64.bat index 2e7f1d0..91cba6a 100755 --- a/make/scripts/runtest-x64.bat +++ b/make/scripts/runtest-x64.bat @@ -1,5 +1,11 @@ REM scripts\java-win64.bat com.jogamp.common.GlueGenVersion REM scripts\java-win64.bat com.jogamp.common.util.TestVersionInfo REM scripts\java-win64.bat com.jogamp.gluegen.test.junit.generation.Test1p1JavaEmitter -scripts\java-win64.bat com.jogamp.common.os.TestElfReader01 +REM scripts\java-win64.bat com.jogamp.common.os.TestElfReader01 + +REM scripts\java-win64.bat com.jogamp.common.util.TestIOUtil01 2>&1 | tee -a $LOG +REM scripts\java-win64.bat com.jogamp.common.util.TestIOUtilURICompose 2>&1 | tee -a $LOG +scripts\java-win64.bat com.jogamp.common.util.TestIOUtilURIHandling 2>&1 | tee -a $LOG +REM scripts\java-win64.bat com.jogamp.common.net.TestUrisWithAssetHandler 2>&1 | tee -a $LOG +REM scripts\java-win64.bat com.jogamp.common.net.TestURIQueryProps 2>&1 | tee -a $LOG diff --git a/make/scripts/runtest.sh b/make/scripts/runtest.sh index 2681b84..028eae2 100755 --- a/make/scripts/runtest.sh +++ b/make/scripts/runtest.sh @@ -52,7 +52,7 @@ rm -f $LOG #D_ARGS="-Djogamp.debug.Lock" #D_ARGS="-Djogamp.debug.Lock -Djogamp.debug.Lock.TraceLock" #D_ARGS="-Djogamp.debug.Lock.TraceLock" -#D_ARGS="-Djogamp.debug.IOUtil" +D_ARGS="-Djogamp.debug.IOUtil" #D_ARGS="-Djogamp.debug=all" function onetest() { @@ -75,7 +75,7 @@ function onetest() { echo } # -onetest com.jogamp.common.GlueGenVersion 2>&1 | tee -a $LOG +#onetest com.jogamp.common.GlueGenVersion 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestSystemPropsAndEnvs 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestVersionInfo 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestVersionNumber 2>&1 | tee -a $LOG @@ -94,14 +94,17 @@ onetest com.jogamp.common.GlueGenVersion 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestPlatform01 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestRunnableTask01 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestIOUtil01 2>&1 | tee -a $LOG +#onetest com.jogamp.common.util.TestIOUtilURICompose 2>&1 | tee -a $LOG +onetest com.jogamp.common.util.TestIOUtilURIHandling 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestTempJarCache 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestJarUtil 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestValueConversion 2>&1 | tee -a $LOG #onetest com.jogamp.common.util.TestSyncRingBuffer01 $* #onetest com.jogamp.common.util.TestLFRingBuffer01 $* +#onetest com.jogamp.common.net.TestUrisWithAssetHandler 2>&1 | tee -a $LOG +#onetest com.jogamp.common.net.TestURIQueryProps 2>&1 | tee -a $LOG #onetest com.jogamp.common.net.AssetURLConnectionUnregisteredTest 2>&1 | tee -a $LOG #onetest com.jogamp.common.net.AssetURLConnectionRegisteredTest 2>&1 | tee -a $LOG -#onetest com.jogamp.common.net.URLCompositionTest 2>&1 | tee -a $LOG #onetest com.jogamp.junit.sec.TestSecIOUtil01 2>&1 | tee -a $LOG #onetest com.jogamp.common.nio.TestBuffersFloatDoubleConversion 2>&1 | tee -a $LOG #onetest com.jogamp.common.nio.TestPointerBufferEndian 2>&1 | tee -a $LOG diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index 16f191b..ef6a633 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -497,57 +497,119 @@ public class IOUtil { * for <i>file scheme</i>, i.e. <code>file:/</code>. * Otherwise the default {@link URL} translation {@link URI#toURL()} is being used. * <p> - * The folloing cases are considered: + * The following cases are considered: * <ul> * <li><i>file schema</i> is converted via <code>new File(uri).getPath()</code>.</li> * <li><i>jar scheme</i> * <ul> - * <li>subprotocol is being converted as above, if <i>file scheme</i>.</li> + * <li>sub-protocol is being converted as above, if <i>file scheme</i>.</li> * <li>JAR entry is not converted but preserved.</li> * </ul></li> * </ul> * </p> + * <p> + * Tested w/ unit test <code>com.jogamp.common.util.TestIOUtilURIHandling</code> + * </p> * @param uri * @return * @throws IOException * @throws IllegalArgumentException * @throws URISyntaxException */ - public static URL toURL(URI uri) throws IOException, IllegalArgumentException, URISyntaxException { + public static URL toURL(final URI uri) throws IOException, IllegalArgumentException, URISyntaxException { URL url = null; final String uriSchema = uri.getScheme(); - final boolean isJAR = IOUtil.JAR_SCHEME.equals(uriSchema); - final URI specificURI = isJAR ? JarUtil.getJarSubURI(uri) : uri; + final boolean isJar = IOUtil.JAR_SCHEME.equals(uriSchema); + final URI specificURI = isJar ? JarUtil.getJarSubURI(uri) : uri; + final boolean hasJarSubURI = specificURI != uri; + if( DEBUG ) { + System.out.println("IOUtil.toURL.0: isJAR "+isJar+", hasSubURI "+hasJarSubURI+Platform.getNewline()+ + "\t, uri "+uri+Platform.getNewline()+ + "\t -> "+specificURI.toString()); + } int mode = 0; if( IOUtil.FILE_SCHEME.equals( specificURI.getScheme() ) ) { File f; try { f = new File(specificURI); - } catch( IllegalArgumentException iae) { + } catch(Exception iae) { if( DEBUG ) { - System.out.println("toURL: "+uri+" -> File("+specificURI+") failed: "+iae.getMessage()); + System.out.println("Catched "+iae.getClass().getSimpleName()+": new File("+specificURI+") failed: "+iae.getMessage()); + iae.printStackTrace(); } f = null; } if( null != f ) { - if( specificURI == uri ) { - url = new URL(IOUtil.FILE_SCHEME+IOUtil.SCHEME_SEPARATOR+f.getPath()); - // url = f.toURI().toURL(); // Doesn't work, since it uses encoded path! - mode = 1; - } else { - final String post = isJAR ? IOUtil.JAR_SCHEME_SEPARATOR + JarUtil.getJarEntry(uri) : ""; - final String urlS = uriSchema+IOUtil.SCHEME_SEPARATOR+IOUtil.FILE_SCHEME+IOUtil.SCHEME_SEPARATOR+f.getPath()+post; - url = new URL(urlS); - mode = 2; + String urlS = null; + try { + final String fPath = f.getPath(); + final String fPathUriS = encodeFilePathToURI(fPath); + /** + * Below 'url = f.toURI().toURL()' Doesn't work, since it uses encoded path, + * but we need the decoded path due to subsequent file access. + * URI: jar:file:/C:/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/ + * File: file:/C:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar + * URI: fUri file:/C:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar + * URL: fUrl file:/C:/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar + * + * Goal: file:/C:/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/ + */ + if(DEBUG) { + try { + final URI fUri = f.toURI(); + final URL fUrl = fUri.toURL(); + System.out.println("IOUtil.toURL.1b: fUri "+fUri+Platform.getNewline()+ + "\t, fUrl "+fUrl); + } catch (Exception ee) { + System.out.println("Catched "+ee.getClass().getSimpleName()+": f.toURI().toURL() failed: "+ee.getMessage()); + ee.printStackTrace(); + } + } + if( !hasJarSubURI ) { + urlS = IOUtil.FILE_SCHEME+IOUtil.SCHEME_SEPARATOR+fPathUriS; + if( DEBUG ) { + System.out.println("IOUtil.toURL.1: fPath "+fPath+Platform.getNewline()+ + "\t -> "+fPathUriS+Platform.getNewline()+ + "\t -> "+urlS); + } + url = new URL(urlS); + mode = 1; + } else { + final String jarEntry = JarUtil.getJarEntry(uri); + final String post = isJar ? IOUtil.JAR_SCHEME_SEPARATOR + jarEntry : ""; + urlS = uriSchema+IOUtil.SCHEME_SEPARATOR+IOUtil.FILE_SCHEME+IOUtil.SCHEME_SEPARATOR+fPathUriS+post; + if( DEBUG ) { + System.out.println("IOUtil.toURL.2: fPath "+fPath+Platform.getNewline()+ + "\t -> "+fPathUriS+Platform.getNewline()+ + "\t, jarEntry "+jarEntry+Platform.getNewline()+ + "\t, post "+post+Platform.getNewline()+ + "\t -> "+urlS); + } + url = new URL(urlS); + mode = 2; + } + } catch (Exception mue) { + if( DEBUG ) { + System.out.println("Catched "+mue.getClass().getSimpleName()+": new URL("+urlS+") failed: "+mue.getMessage()); + mue.printStackTrace(); + } } } } if( null == url ) { - url = uri.toURL(); - mode = 3; + try { + url = uri.toURL(); + mode = 3; + } catch (Exception e) { + if( DEBUG ) { + System.out.println("Catched "+e.getClass().getSimpleName()+": "+uri+".toURL() failed: "+e.getMessage()); + e.printStackTrace(); + } + } } if( DEBUG ) { - System.err.println("IOUtil.toURL: "+uri+", isJar "+isJAR+": "+specificURI+" -> mode "+mode+", "+url); + System.err.println("IOUtil.toURL.X: mode "+mode+", "+uri+Platform.getNewline()+ + "\t -> "+url); } return url; } @@ -800,6 +862,36 @@ public class IOUtil { return patternSpaceEnc.matcher(s).replaceAll(" "); } + private static final Pattern patternSingleBS = Pattern.compile("\\\\{1,}"); + + /** + * Escapes file path characters not complying w/ RFC 2396 and the {@link URI#URI(String)} ctor. + * <p> + * Processes input filePath if {@link File#separatorChar} <code> != '/'</code> + * as follows: + * <ul> + * <li>backslash -> slash</li> + * <li>ensure starting with slash</li> + * </ul> + * </p> + * <p> + * Even though Oracle's JarURLStreamHandler can handle backslashes and + * erroneous URIs w/ e.g. Windows file 'syntax', other may not (Netbeans).<br> + * See Bug 857 - http://jogamp.org/bugzilla/show_bug.cgi?id=857 + * </p> + */ + public static String encodeFilePathToURI(String filePath) { + if( !File.separator.equals("/") ) { + final String r = patternSingleBS.matcher(filePath).replaceAll("/"); + if( !r.startsWith("/") ) { + return "/" + r; + } else { + return r; + } + } + return filePath; + } + /** * Returns the connected URLConnection, or null if not url is not available */ diff --git a/src/java/com/jogamp/common/util/JarUtil.java b/src/java/com/jogamp/common/util/JarUtil.java index 1689d54..5411d66 100644 --- a/src/java/com/jogamp/common/util/JarUtil.java +++ b/src/java/com/jogamp/common/util/JarUtil.java @@ -449,9 +449,12 @@ public class JarUtil { throw new IllegalArgumentException("null jarFileURI"); } if(DEBUG) { - System.out.println("getJarFile: "+jarFileURI.toString()); + System.out.println("getJarFile.0: "+jarFileURI.toString()); } final URL jarFileURL = IOUtil.toURL(jarFileURI); + if(DEBUG) { + System.out.println("getJarFile.1: "+jarFileURL.toString()); + } // final URL jarFileURL = jarFileURI.toURL(); // doesn't work due to encoded path even w/ file schema! final URLConnection urlc = jarFileURL.openConnection(); if(urlc instanceof JarURLConnection) { diff --git a/src/junit/com/jogamp/common/net/TestURIQueryProps.java b/src/junit/com/jogamp/common/net/TestURIQueryProps.java new file mode 100644 index 0000000..09cef61 --- /dev/null +++ b/src/junit/com/jogamp/common/net/TestURIQueryProps.java @@ -0,0 +1,50 @@ +package com.jogamp.common.net; + +import static com.jogamp.common.net.URIDumpUtil.showURI; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.junit.util.JunitTracer; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestURIQueryProps extends JunitTracer { + + @Test + public void test() throws IOException, URISyntaxException { + final String SCHEME = "camera"; + final String HOST = "somewhere"; + final String PATH = "0"; + String[] args = new String[] { + SCHEME+"://"+HOST+"/"+PATH, + SCHEME+"://"+HOST+"/"+PATH+"?p1=1", + }; + for(int i=0; i<args.length-1; i+=2) { + String uri_s0 = args[i]; + String uri_s1 = args[i+1]; + URI uri0 = new URI(uri_s0); + URI uri1 = new URI(uri_s1); + showURI(uri0); + showURI(uri1); + URIQueryProps data = URIQueryProps.create(uri1, ';'); + if(null == data) { + System.err.println("Error: NULL: <"+uri_s1+"> -> "+uri1+" -> NULL"); + } else { + URI uri1T = data.appendQuery(uri0); + showURI(uri1T); + Assert.assertEquals(uri1, uri1T); + } + } + } + public static void main(String args[]) throws IOException { + String tstname = TestURIQueryProps.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/common/net/TestUrisWithAssetHandler.java b/src/junit/com/jogamp/common/net/TestUrisWithAssetHandler.java new file mode 100644 index 0000000..6e244eb --- /dev/null +++ b/src/junit/com/jogamp/common/net/TestUrisWithAssetHandler.java @@ -0,0 +1,50 @@ +package com.jogamp.common.net; + +import static com.jogamp.common.net.URIDumpUtil.showURX; + +import java.io.IOException; +import java.net.URISyntaxException; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import com.jogamp.junit.util.JunitTracer; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestUrisWithAssetHandler extends JunitTracer { + + @BeforeClass + public static void assetRegistration() throws Exception { + try { + System.err.println("******* Asset URL Stream Handler Registration: PRE"); + Assert.assertTrue("GenericURLStreamHandlerFactory.register() failed", AssetURLContext.registerHandler(TestUrisWithAssetHandler.class.getClassLoader())); + Assert.assertNotNull(AssetURLContext.getRegisteredHandler()); + System.err.println("******* Asset URL Stream Handler Registration: POST"); + } catch (Exception e) { + setTestSupported(false); + throw e; + } + } + + @Test + public void showURLComponents0() throws IOException, URISyntaxException { + showURX("file:///rootDir/file1.txt"); + showURX("file://host/rootDir/file1.txt"); + showURX("jar:file:/web1/file1.jar!/rootDir/file1.txt"); + showURX("asset:gluegen-test/info.txt"); + showURX("asset:/gluegen-test/info.txt"); + showURX("http://domain.com/web1/index.html?lala=23&lili=24#anchor"); + showURX("http://domain.com:1234/web1/index.html?lala=23&lili=24#anchor"); + showURX("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt"); + showURX("asset:jar:file:/web1/file1.jar!/rootDir/./file1.txt"); + showURX("asset:jar:file:/web1/file1.jar!/rootDir/dummyParent/../file1.txt"); + } + + public static void main(String args[]) throws IOException { + String tstname = TestUrisWithAssetHandler.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/common/net/URIDumpUtil.java b/src/junit/com/jogamp/common/net/URIDumpUtil.java new file mode 100644 index 0000000..132a19c --- /dev/null +++ b/src/junit/com/jogamp/common/net/URIDumpUtil.java @@ -0,0 +1,49 @@ +package com.jogamp.common.net; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +public class URIDumpUtil { + public static void showURX(String urx) throws MalformedURLException, URISyntaxException { + System.err.println("XXXXXX "+urx); + showURL(new URL(urx)); + showURI(new URI(urx)); + System.err.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); + } + + public static void showURL(URL url) { + System.err.println("YYYYYY URL "+url); + System.err.println("protocol: "+url.getProtocol()); + System.err.println("auth: "+url.getAuthority()); + System.err.println("host: "+url.getHost()); + System.err.println("port: "+url.getPort() + " ( " + url.getDefaultPort() + " ) " ); + System.err.println("file: "+url.getFile() + " ( path " + url.getPath() + ", query " + url.getQuery() + " ) " ); + System.err.println("ref: "+url.getRef()); + } + public static void showURI(URI uri) { + System.err.println("ZZZZZZ URI "+uri); + // 1 [scheme:]scheme-specific-part[#fragment] + System.err.println("1 scheme: "+uri.getScheme()); + System.err.println("1 scheme-part: "+uri.getRawSchemeSpecificPart()); + System.err.println("1 fragment: "+uri.getRawFragment()); + + // 2 [scheme:][//authority][path][?query][#fragment] + System.err.println("2 scheme: "+uri.getScheme()); + System.err.println("2 auth: "+uri.getRawAuthority()); + System.err.println("2 path: "+uri.getRawPath()); + System.err.println("2 query: "+uri.getRawQuery()); + System.err.println("2 fragment: "+uri.getRawFragment()); + + // 3 [scheme:][//authority][path][?query][#fragment] + // authority: [user-info@]host[:port] + System.err.println("3 scheme: "+uri.getScheme()); + System.err.println("3 user-info: "+uri.getRawUserInfo()); + System.err.println("3 host: "+uri.getHost()); + System.err.println("3 port: "+uri.getPort()); + System.err.println("3 path: "+uri.getRawPath()); + System.err.println("3 query: "+uri.getRawQuery()); + System.err.println("3 fragment: "+uri.getRawFragment()); + } +} diff --git a/src/junit/com/jogamp/common/net/URLCompositionTest.java b/src/junit/com/jogamp/common/net/URLCompositionTest.java deleted file mode 100644 index 3083fbd..0000000 --- a/src/junit/com/jogamp/common/net/URLCompositionTest.java +++ /dev/null @@ -1,241 +0,0 @@ -package com.jogamp.common.net; - -import java.io.File; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -import com.jogamp.common.util.IOUtil; -import com.jogamp.junit.util.JunitTracer; - -import org.junit.FixMethodOrder; -import org.junit.runners.MethodSorters; - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class URLCompositionTest extends JunitTracer { - - @BeforeClass - public static void assetRegistration() throws Exception { - try { - System.err.println("******* Asset URL Stream Handler Registration: PRE"); - Assert.assertTrue("GenericURLStreamHandlerFactory.register() failed", AssetURLContext.registerHandler(URLCompositionTest.class.getClassLoader())); - Assert.assertNotNull(AssetURLContext.getRegisteredHandler()); - System.err.println("******* Asset URL Stream Handler Registration: POST"); - } catch (Exception e) { - setTestSupported(false); - throw e; - } - } - - @Test - public void showURLComponents0() throws IOException, URISyntaxException { - showURX("file:///rootDir/file1.txt"); - showURX("file://host/rootDir/file1.txt"); - showURX("jar:file:/web1/file1.jar!/rootDir/file1.txt"); - showURX("asset:gluegen-test/info.txt"); - showURX("asset:/gluegen-test/info.txt"); - showURX("http://domain.com/web1/index.html?lala=23&lili=24#anchor"); - showURX("http://domain.com:1234/web1/index.html?lala=23&lili=24#anchor"); - showURX("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt"); - showURX("asset:jar:file:/web1/file1.jar!/rootDir/./file1.txt"); - showURX("asset:jar:file:/web1/file1.jar!/rootDir/dummyParent/../file1.txt"); - } - - static void showURX(String urx) throws MalformedURLException, URISyntaxException { - System.err.println("XXXXXX "+urx); - showURL(new URL(urx)); - showURI(new URI(urx)); - System.err.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); - } - - static void showURL(URL url) { - System.err.println("YYYYYY URL "+url); - System.err.println("protocol: "+url.getProtocol()); - System.err.println("auth: "+url.getAuthority()); - System.err.println("host: "+url.getHost()); - System.err.println("port: "+url.getPort() + " ( " + url.getDefaultPort() + " ) " ); - System.err.println("file: "+url.getFile() + " ( path " + url.getPath() + ", query " + url.getQuery() + " ) " ); - System.err.println("ref: "+url.getRef()); - } - static void showURI(URI uri) { - System.err.println("ZZZZZZ URI "+uri); - // 1 [scheme:]scheme-specific-part[#fragment] - System.err.println("1 scheme: "+uri.getScheme()); - System.err.println("1 scheme-part: "+uri.getRawSchemeSpecificPart()); - System.err.println("1 fragment: "+uri.getRawFragment()); - - // 2 [scheme:][//authority][path][?query][#fragment] - System.err.println("2 scheme: "+uri.getScheme()); - System.err.println("2 auth: "+uri.getRawAuthority()); - System.err.println("2 path: "+uri.getRawPath()); - System.err.println("2 query: "+uri.getRawQuery()); - System.err.println("2 fragment: "+uri.getRawFragment()); - - // 3 [scheme:][//authority][path][?query][#fragment] - // authority: [user-info@]host[:port] - System.err.println("3 scheme: "+uri.getScheme()); - System.err.println("3 user-info: "+uri.getRawUserInfo()); - System.err.println("3 host: "+uri.getHost()); - System.err.println("3 port: "+uri.getPort()); - System.err.println("3 path: "+uri.getRawPath()); - System.err.println("3 query: "+uri.getRawQuery()); - System.err.println("3 fragment: "+uri.getRawFragment()); - } - - - @Test - public void showURLComponents1() throws IOException, URISyntaxException { - testURI2URL("jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", - "jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", true); - - testURI2URL("jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/", - "jar:file:/usr/local/projects/JOGL/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/", true); - - testURI2URL("file:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar", - "file:/usr/local/projects/JOGL/gluegen/build-x86_64 öä lala/gluegen-rt.jar", true); - - testURI2URL("jar:http:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", - "jar:http:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", false); - - testURI2URL("jar:http:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/", - "jar:http:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/", false); - - testURI2URL("http:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar", - "http:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar", false); - - testURI2URL("jar:ftp:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", - "jar:ftp:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", false); - - testURI2URL("ftp:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar", - "ftp:/usr/local/projects/JOGL/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar", false); - } - - void testURI2URL(String source, String expected, boolean usesFile) throws IOException, URISyntaxException { - System.err.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); - final URI uri0 = new URI(source); - System.err.println("uri: "+uri0.toString()); - - final URL url0 = IOUtil.toURL(uri0); - final String actual = url0.toExternalForm(); - System.err.println("url: "+actual); - final String expectedOS; - if( usesFile ) { - final int jarSepI = expected.lastIndexOf("!"); - final int replB = 0 <= jarSepI ? jarSepI : expected.length(); - final String partA = expected.substring(0, replB).replace('/', File.separatorChar); - final String partB = expected.substring(replB); - expectedOS = partA+partB; - } else { - expectedOS = expected; - } - System.err.println("expected: orig "+expected+", OS "+expectedOS); - final boolean equalsA = expected.equals(actual); - final boolean equalsB = expectedOS.equals(actual); - Assert.assertTrue("No match, neither expected orig or OS w/ url", equalsA || equalsB); - System.err.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); - } - - @Test - public void showURLComponents2() throws IOException, URISyntaxException { - testURNCompositioning("file:///rootDir/file1.txt"); - testURNCompositioning("file://host/rootDir/file1.txt"); - testURNCompositioning("jar:file:/web1/file1.jar!/rootDir/file1.txt"); - testURNCompositioning("asset:gluegen-test/info.txt"); - testURNCompositioning("asset:/gluegen-test/info.txt"); - testURNCompositioning("http://domain.com/web1/index.html?lala=23&lili=24#anchor"); - testURNCompositioning("http://domain.com:1234/web1/index.html?lala=23&lili=24#anchor"); - - final URI file1URI = new URI("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt"); - testURICompositioning(file1URI); - testURICompositioning(file1URI, new URI("asset:jar:file:/web1/file1.jar!/rootDir/./file1.txt")); - testURICompositioning(file1URI, new URI("asset:jar:file:/web1/file1.jar!/rootDir/dummyParent/../file1.txt")); - - final URL file1URL = new URL("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt"); - testURLCompositioning(file1URL); - testURLCompositioning(file1URL, new URL("asset:jar:file:/web1/file1.jar!/rootDir/./file1.txt")); - testURLCompositioning(file1URL, new URL("asset:jar:file:/web1/file1.jar!/rootDir/dummyParent/../file1.txt")); - } - - static void testURNCompositioning(String urn) throws MalformedURLException, URISyntaxException { - testURICompositioning( new URI(urn) ); - testURLCompositioning( new URL(urn) ); - } - - static void testURICompositioning(URI uri) throws MalformedURLException, URISyntaxException { - testURICompositioning(uri, uri); - } - static void testURICompositioning(URI refURI, URI uri1) throws MalformedURLException, URISyntaxException { - final String scheme = uri1.getScheme(); - final String ssp = uri1.getRawSchemeSpecificPart(); - final String fragment = uri1.getRawFragment(); - - System.err.println("scheme <"+scheme+">, ssp <"+ssp+">, fragment <"+fragment+">"); - final URI uri2 = IOUtil.compose(scheme, ssp, null, fragment); - - System.err.println("URL-equals: "+refURI.equals(uri2)); - System.err.println("URL-ref : <"+refURI+">"); - System.err.println("URL-orig : <"+uri1+">"); - System.err.println("URL-comp : <"+uri2+">"); - Assert.assertEquals(refURI, uri2); - } - - static void testURLCompositioning(URL url) throws MalformedURLException, URISyntaxException { - testURLCompositioning(url, url); - } - static void testURLCompositioning(URL refURL, URL url1) throws MalformedURLException, URISyntaxException { - final URI uri1 = url1.toURI(); - final String scheme = uri1.getScheme(); - final String ssp = uri1.getRawSchemeSpecificPart(); - final String fragment = uri1.getRawFragment(); - - System.err.println("scheme <"+scheme+">, ssp <"+ssp+">, fragment <"+fragment+">"); - final URI uri2 = IOUtil.compose(scheme, ssp, null, fragment); - - System.err.println("URL-equals(1): "+refURL.toURI().equals(uri2)); - System.err.println("URL-equals(2): "+refURL.equals(uri2.toURL())); - System.err.println("URL-same : "+refURL.sameFile(uri2.toURL())); - System.err.println("URL-ref : <"+refURL+">"); - System.err.println("URL-orig : <"+url1+">"); - System.err.println("URL-comp : <"+uri2+">"); - Assert.assertEquals(refURL.toURI(), uri2); - Assert.assertEquals(refURL, uri2.toURL()); - Assert.assertTrue(refURL.sameFile(uri2.toURL())); - } - - @Test - public void testURLProps() throws IOException, URISyntaxException { - final String SCHEME = "camera"; - final String HOST = "somewhere"; - final String PATH = "0"; - String[] args = new String[] { - SCHEME+"://"+HOST+"/"+PATH, - SCHEME+"://"+HOST+"/"+PATH+"?p1=1", - }; - for(int i=0; i<args.length-1; i+=2) { - String uri_s0 = args[i]; - String uri_s1 = args[i+1]; - URI uri0 = new URI(uri_s0); - URI uri1 = new URI(uri_s1); - showURI(uri0); - showURI(uri1); - URIQueryProps data = URIQueryProps.create(uri1, ';'); - if(null == data) { - System.err.println("Error: NULL: <"+uri_s1+"> -> "+uri1+" -> NULL"); - } else { - URI uri1T = data.appendQuery(uri0); - showURI(uri1T); - Assert.assertEquals(uri1, uri1T); - } - } - } - public static void main(String args[]) throws IOException { - String tstname = URLCompositionTest.class.getName(); - org.junit.runner.JUnitCore.main(tstname); - } -} diff --git a/src/junit/com/jogamp/common/util/TestIOUtilURICompose.java b/src/junit/com/jogamp/common/util/TestIOUtilURICompose.java new file mode 100644 index 0000000..0809599 --- /dev/null +++ b/src/junit/com/jogamp/common/util/TestIOUtilURICompose.java @@ -0,0 +1,92 @@ +package com.jogamp.common.util; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.common.util.IOUtil; +import com.jogamp.junit.util.JunitTracer; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestIOUtilURICompose extends JunitTracer { + + @Test + public void test01URLCompositioning() throws IOException, URISyntaxException { + testURNCompositioning("file:///rootDir/file1.txt"); + testURNCompositioning("file://host/rootDir/file1.txt"); + testURNCompositioning("jar:file:/web1/file1.jar!/rootDir/file1.txt"); + testURNCompositioning("asset:gluegen-test/info.txt"); + testURNCompositioning("asset:/gluegen-test/info.txt"); + testURNCompositioning("http://domain.com/web1/index.html?lala=23&lili=24#anchor"); + testURNCompositioning("http://domain.com:1234/web1/index.html?lala=23&lili=24#anchor"); + + final URI file1URI = new URI("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt"); + testURICompositioning(file1URI); + testURICompositioning(file1URI, new URI("asset:jar:file:/web1/file1.jar!/rootDir/./file1.txt")); + testURICompositioning(file1URI, new URI("asset:jar:file:/web1/file1.jar!/rootDir/dummyParent/../file1.txt")); + + final URL file1URL = new URL("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt"); + testURLCompositioning(file1URL); + testURLCompositioning(file1URL, new URL("asset:jar:file:/web1/file1.jar!/rootDir/./file1.txt")); + testURLCompositioning(file1URL, new URL("asset:jar:file:/web1/file1.jar!/rootDir/dummyParent/../file1.txt")); + } + + static void testURNCompositioning(String urn) throws MalformedURLException, URISyntaxException { + testURICompositioning( new URI(urn) ); + testURLCompositioning( new URL(urn) ); + } + + static void testURICompositioning(URI uri) throws MalformedURLException, URISyntaxException { + testURICompositioning(uri, uri); + } + static void testURICompositioning(URI refURI, URI uri1) throws MalformedURLException, URISyntaxException { + final String scheme = uri1.getScheme(); + final String ssp = uri1.getRawSchemeSpecificPart(); + final String fragment = uri1.getRawFragment(); + + System.err.println("scheme <"+scheme+">, ssp <"+ssp+">, fragment <"+fragment+">"); + final URI uri2 = IOUtil.compose(scheme, ssp, null, fragment); + + System.err.println("URL-equals: "+refURI.equals(uri2)); + System.err.println("URL-ref : <"+refURI+">"); + System.err.println("URL-orig : <"+uri1+">"); + System.err.println("URL-comp : <"+uri2+">"); + Assert.assertEquals(refURI, uri2); + } + + static void testURLCompositioning(URL url) throws MalformedURLException, URISyntaxException { + testURLCompositioning(url, url); + } + static void testURLCompositioning(URL refURL, URL url1) throws MalformedURLException, URISyntaxException { + final URI uri1 = url1.toURI(); + final String scheme = uri1.getScheme(); + final String ssp = uri1.getRawSchemeSpecificPart(); + final String fragment = uri1.getRawFragment(); + + System.err.println("scheme <"+scheme+">, ssp <"+ssp+">, fragment <"+fragment+">"); + final URI uri2 = IOUtil.compose(scheme, ssp, null, fragment); + + System.err.println("URL-equals(1): "+refURL.toURI().equals(uri2)); + System.err.println("URL-equals(2): "+refURL.equals(uri2.toURL())); + System.err.println("URL-same : "+refURL.sameFile(uri2.toURL())); + System.err.println("URL-ref : <"+refURL+">"); + System.err.println("URL-orig : <"+url1+">"); + System.err.println("URL-comp : <"+uri2+">"); + Assert.assertEquals(refURL.toURI(), uri2); + Assert.assertEquals(refURL, uri2.toURL()); + Assert.assertTrue(refURL.sameFile(uri2.toURL())); + } + + public static void main(String args[]) throws IOException { + String tstname = TestIOUtilURICompose.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/common/util/TestIOUtilURIHandling.java b/src/junit/com/jogamp/common/util/TestIOUtilURIHandling.java new file mode 100644 index 0000000..73f6ce7 --- /dev/null +++ b/src/junit/com/jogamp/common/util/TestIOUtilURIHandling.java @@ -0,0 +1,150 @@ +package com.jogamp.common.util; + +import static com.jogamp.common.net.URIDumpUtil.showURX; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.common.os.Platform; +import com.jogamp.common.util.IOUtil; +import com.jogamp.junit.util.JunitTracer; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class TestIOUtilURIHandling extends JunitTracer { + + static final String[][] uriHttpSArray = new String[][] { + new String[] {"http://localhost/gluegen/build-x86_64/gluegen-rt.jar", + "http://localhost/gluegen/build-x86_64/gluegen-rt.jar"}, + + new String[] {"http://localhost/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar", + "http://localhost/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar" + // "http://localhost/gluegen/build-x86_64 öä lala/gluegen-rt.jar" + }, + + new String[] {"jar:http://localhost/gluegen/build-x86_64/gluegen-rt.jar!/", + "jar:http://localhost/gluegen/build-x86_64/gluegen-rt.jar!/" }, + + new String[] {"jar:http://localhost/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/", + "jar:http://localhost/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/" + // "jar:http://localhost/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/" + }, + + new String[] {"jar:http://localhost/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", + "jar:http://localhost/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class" }, + + new String[] {"jar:http://localhost/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", + "jar:http://localhost/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class" + // "jar:http://localhost/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class" + }, + }; + + static final String[][] uriFileSArrayUnix = new String[][] { + new String[] {"file:/gluegen/build-x86_64/gluegen-rt.jar", + "file:/gluegen/build-x86_64/gluegen-rt.jar"}, + + new String[] {"file:/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar", + "file:/gluegen/build-x86_64 öä lala/gluegen-rt.jar"}, + + new String[] {"jar:file:/gluegen/build-x86_64/gluegen-rt.jar!/", + "jar:file:/gluegen/build-x86_64/gluegen-rt.jar!/" }, + + new String[] {"jar:file:/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/", + "jar:file:/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/" }, + + new String[] {"jar:file:/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", + "jar:file:/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class" }, + + new String[] {"jar:file:/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", + "jar:file:/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class" }, + }; + + static final String[][] uriFileSArrayWindows = new String[][] { + new String[] {"file:/C:/gluegen/build-x86_64/gluegen-rt.jar", + "file:/C:/gluegen/build-x86_64/gluegen-rt.jar"}, + + new String[] {"file:/C:/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar", + "file:/C:/gluegen/build-x86_64 öä lala/gluegen-rt.jar"}, + + new String[] {"jar:file:/C:/gluegen/build-x86_64/gluegen-rt.jar!/", + "jar:file:/C:/gluegen/build-x86_64/gluegen-rt.jar!/" }, + + new String[] {"jar:file:/C:/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/", + "jar:file:/C:/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/" }, + + new String[] {"jar:file:/C:/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", + "jar:file:/C:/gluegen/build-x86_64/gluegen-rt.jar!/com/jogamp/common/os/Platform.class" }, + + new String[] {"jar:file:/C:/gluegen/build-x86_64%20%c3%b6%c3%a4%20lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class", + "jar:file:/C:/gluegen/build-x86_64 öä lala/gluegen-rt.jar!/com/jogamp/common/os/Platform.class" }, + }; + + @Test + public void test01HttpURI2URL() throws IOException, URISyntaxException { + testURI2URL(uriHttpSArray, false /*usesFile*/, false /*matchOS*/); + } + + @Test + public void test02FileUnixURI2URL() throws IOException, URISyntaxException { + testURI2URL(uriFileSArrayUnix, true /*usesFile*/, Platform.OSType.WINDOWS != Platform.getOSType() /*matchOS*/); + } + + @Test + public void test02FileWindowsURI2URL() throws IOException, URISyntaxException { + testURI2URL(uriFileSArrayWindows, true /*usesFile*/, Platform.OSType.WINDOWS == Platform.getOSType() /*matchOS*/); + } + + static void testURI2URL(String[][] uriSArray, boolean usesFile, boolean matchOS) throws IOException, URISyntaxException { + for(int i=0; i<uriSArray.length; i++) { + final String[] uriSPair = uriSArray[i]; + final String uriSource = uriSPair[0]; + final String uriExpected= uriSPair[1]; + System.err.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); + showURX(uriSource); + testURI2URL(uriSource, uriExpected, usesFile, matchOS); + System.err.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); + } + } + + static void testURI2URL(String source, String expected, boolean usesFile, boolean matchOS) throws IOException, URISyntaxException { + final URI uri0 = new URI(source); + System.err.println("uri: "+uri0.toString()); + + final URL actualUrl = IOUtil.toURL(uri0); + final String actualUrlS = actualUrl.toExternalForm(); + System.err.println("url: "+actualUrlS); + final boolean equalsA = expected.equals(actualUrlS); + System.err.println("expected___: "+expected+" - ok "+equalsA); + System.err.println("actual : "+actualUrlS); + Assert.assertTrue("No match, expected w/ orig url", equalsA); + + // now test open .. + Throwable t = null; + URLConnection con = null; + try { + con = actualUrl.openConnection(); + } catch (Throwable _t) { + t = _t; + } + if( null != t ) { + System.err.println("XXX: "+t.getClass().getName()+": "+t.getMessage()); + t.printStackTrace(); + } else { + System.err.println("XXX: No openConnection() failure"); + System.err.println("XXX: "+con); + } + } + + public static void main(String args[]) throws IOException { + String tstname = TestIOUtilURIHandling.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} |