diff options
author | Sven Gothel <[email protected]> | 2014-09-07 19:02:48 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-09-07 19:02:48 +0200 |
commit | 6322991799268166e46aeeacb142c11d45950f48 (patch) | |
tree | 1a7c724f30f1f586fbea5aa25661729f545d9367 | |
parent | 2d615932505d4870f3fd0dd37f21fb3d308ab536 (diff) |
Bug 1063 - Refine Uri and Uri.Encode ; Fix unit tests (passing on GNU/Linux, OSX and Windows)
- Refine Uri
- remove System.err.print*
- scheme is of type Encode
- Refine Uri.Encode
- substring(..) shall return type Encode
- Fix unit tests (passing on GNU/Linux, OSX and Windows)
-rwxr-xr-x | make/scripts/runtest-x64.bat | 4 | ||||
-rwxr-xr-x | make/scripts/runtest.sh | 4 | ||||
-rw-r--r-- | src/java/com/jogamp/common/net/Uri.java | 50 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/net/TestUri01.java | 8 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/net/TestUri03Resolving.java | 75 |
5 files changed, 59 insertions, 82 deletions
diff --git a/make/scripts/runtest-x64.bat b/make/scripts/runtest-x64.bat index f88e795..f6dda45 100755 --- a/make/scripts/runtest-x64.bat +++ b/make/scripts/runtest-x64.bat @@ -10,6 +10,6 @@ REM scripts\java-win64.bat com.jogamp.common.net.TestUrisWithAssetHandler REM scripts\java-win64.bat com.jogamp.common.net.TestURIQueryProps REM scripts\java-win64.bat com.jogamp.common.net.TestUri01 REM scripts\java-win64.bat com.jogamp.common.net.TestUri02Composing -REM scripts\java-win64.bat com.jogamp.common.net.TestUri03Resolving -scripts\java-win64.bat com.jogamp.common.net.TestUri99LaunchOnReservedCharPathBug908 +scripts\java-win64.bat com.jogamp.common.net.TestUri03Resolving +REM scripts\java-win64.bat com.jogamp.common.net.TestUri99LaunchOnReservedCharPathBug908 diff --git a/make/scripts/runtest.sh b/make/scripts/runtest.sh index 5d1e7d7..c996305 100755 --- a/make/scripts/runtest.sh +++ b/make/scripts/runtest.sh @@ -112,8 +112,8 @@ function onetest() { #onetest com.jogamp.common.net.TestURIQueryProps 2>&1 | tee -a $LOG #onetest com.jogamp.common.net.TestUri01 2>&1 | tee -a $LOG #onetest com.jogamp.common.net.TestUri02Composing 2>&1 | tee -a $LOG -#onetest com.jogamp.common.net.TestUri03Resolving 2>&1 | tee -a $LOG -onetest com.jogamp.common.net.TestUri99LaunchOnReservedCharPathBug908 2>&1 | tee -a $LOG +onetest com.jogamp.common.net.TestUri03Resolving 2>&1 | tee -a $LOG +#onetest com.jogamp.common.net.TestUri99LaunchOnReservedCharPathBug908 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.junit.sec.TestSecIOUtil01 2>&1 | tee -a $LOG diff --git a/src/java/com/jogamp/common/net/Uri.java b/src/java/com/jogamp/common/net/Uri.java index e1363ed..8c641dd 100644 --- a/src/java/com/jogamp/common/net/Uri.java +++ b/src/java/com/jogamp/common/net/Uri.java @@ -292,6 +292,7 @@ public class Uri { public boolean isASCII() { return false; } + /** Returns the encoded String */ public final String get() { return s; } /** @@ -315,6 +316,12 @@ public class Uri { // Basic Object / Identity // + /** + * {@inheritDoc} + * <p> + * Returns the encoded String, same as {@link #get()}. + * </p> + */ @Override public final String toString() { return s; } @@ -366,9 +373,9 @@ public class Uri { public Encoded concat(final Encoded encoded) { return new Encoded(s.concat(encoded.s)); } /** See {@link String#substring(int)}. */ - public final String substring(final int start) { return s.substring(start); } + public final Encoded substring(final int start) { return new Encoded(s.substring(start)); } /** See {@link String#substring(int, int)}. */ - public final String substring(final int start, final int end) { return s.substring(start, end); } + public final Encoded substring(final int start, final int end) { return new Encoded(s.substring(start, end)); } /** See {@link String#indexOf(int)}. */ public final int indexOf(final int ch) { return s.indexOf(ch); } @@ -864,28 +871,19 @@ public class Uri { if( !reencode) { return new Uri(new Encoded(uri.toString())); } - System.err.println("Uri.createByURI(reencode):"); - System.err.println(" source: "+uri.toString()); final Uri recomposedUri; if( uri.isOpaque()) { // opaque, without host validation - System.err.println(" opaq source: ssp: "+uri.getSchemeSpecificPart()); - System.err.println(" opaq source: frg: "+uri.getFragment()); recomposedUri = Uri.create(uri.getScheme(), uri.getSchemeSpecificPart(), uri.getFragment()); } else if( null != uri.getHost() ) { // with host validation - System.err.println(" host source: ssp: "+uri.getSchemeSpecificPart()); - System.err.println(" host source: frg: "+uri.getFragment()); recomposedUri = Uri.create(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); } else { // without host validation - System.err.println(" source: ssp: "+uri.getSchemeSpecificPart()); - System.err.println(" source: frg: "+uri.getFragment()); recomposedUri = Uri.create(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(), uri.getFragment()); } - System.err.println(" result: "+recomposedUri.toString()); return recomposedUri; } @@ -919,8 +917,8 @@ public class Uri { private int hash; - /** Plain {@code scheme}, {@code null} if undefined. */ - public final String scheme; + /** Encoded {@code scheme}, {@code null} if undefined. */ + public final Encoded scheme; /** Encoded {@code scheme-specific-part}, never {@code null}. */ public final Encoded schemeSpecificPart; @@ -970,7 +968,7 @@ public class Uri { /** Returns true, if this instance is a {@code file} {@code scheme}, otherwise false. */ public final boolean isFileScheme() { - return IOUtil.FILE_SCHEME.equals( scheme ); + return IOUtil.FILE_SCHEME.equals( scheme.get() ); } /** @@ -1134,7 +1132,7 @@ public class Uri { if (0 > idx) { throw new URISyntaxException(input.get(), "missing jar separator"); } - sb.append( schemeSpecificPart.substring(0, idx) ); // exclude '!/' + sb.append( schemeSpecificPart.get().substring(0, idx) ); // exclude '!/' } else { sb.append( schemeSpecificPart.get() ); } @@ -1184,7 +1182,7 @@ public class Uri { if( e < pl - 1 ) { // path is file or has a query try { - return new Uri(new Encoded( scheme+IOUtil.SCHEME_SEPARATOR+schemeSpecificPart.substring(0, e+1) )); + return new Uri( new Encoded( scheme.get()+IOUtil.SCHEME_SEPARATOR+schemeSpecificPart.get().substring(0, e+1) ) ); } catch (final URISyntaxException ue) { // not complete, hence removed authority, or even root folder -> return null } @@ -1193,7 +1191,7 @@ public class Uri { final int p = schemeSpecificPart.lastIndexOf("/", e-1); if( p > 0 ) { try { - return new Uri(new Encoded( scheme+IOUtil.SCHEME_SEPARATOR+schemeSpecificPart.substring(0, p+1) )); + return new Uri( new Encoded( scheme.get()+IOUtil.SCHEME_SEPARATOR+schemeSpecificPart.get().substring(0, p+1) ) ); } catch (final URISyntaxException ue) { // not complete, hence removed authority, or even root folder -> return null } @@ -1256,7 +1254,7 @@ public class Uri { return compose(scheme, schemeSpecificPart, relativePath, fragment); } - static Uri compose(final String scheme, final Encoded schemeSpecificPart, final Encoded relativePath, final Encoded fragment) throws URISyntaxException { + static Uri compose(final Encoded scheme, final Encoded schemeSpecificPart, final Encoded relativePath, final Encoded fragment) throws URISyntaxException { String schemeSpecificPartS = schemeSpecificPart.get(); // cut off optional query in scheme-specific-part @@ -1276,7 +1274,7 @@ public class Uri { } schemeSpecificPartS = IOUtil.cleanPathString( schemeSpecificPartS ); final StringBuilder uri = new StringBuilder(); - uri.append(scheme); + uri.append(scheme.get()); uri.append(':'); uri.append(schemeSpecificPartS); if ( null != query ) { @@ -1430,16 +1428,16 @@ public class Uri { while ( ( index = first.indexOf('%', previndex) ) != -1 && second.indexOf('%', previndex) == index ) { - if( !first.substring(previndex, index).equals( second.substring(previndex, index) ) ) { + if( !first.get().substring(previndex, index).equals( second.get().substring(previndex, index) ) ) { return false; } - if( !first.substring(index + 1, index + 3).equalsIgnoreCase( second.substring(index + 1, index + 3) ) ) { + if( !first.get().substring(index + 1, index + 3).equalsIgnoreCase( second.get().substring(index + 1, index + 3) ) ) { return false; } index += 3; previndex = index; } - return first.substring(previndex).equals( second.substring(previndex) ); + return first.get().substring(previndex).equals( second.get().substring(previndex) ); } /* @@ -1450,7 +1448,7 @@ public class Uri { private String getHashString() { final StringBuilder result = new StringBuilder(); if (scheme != null) { - result.append(scheme.toLowerCase()); + result.append(scheme.get().toLowerCase()); result.append(IOUtil.SCHEME_SEPARATOR); } if (opaque) { @@ -1531,7 +1529,7 @@ public class Uri { ) { // the characters up to the first ':' comprise the scheme absolute = true; - scheme = temp.substring(0, indexSchemeSep); + scheme = new Encoded( temp.substring(0, indexSchemeSep) ); if (scheme.length() == 0) { failExpecting(input, "scheme", indexSchemeSep); } @@ -1696,13 +1694,13 @@ public class Uri { } } - private static void validateScheme(final Encoded uri, final String scheme, final int index) throws URISyntaxException { + private static void validateScheme(final Encoded uri, final Encoded scheme, final int index) throws URISyntaxException { // first char needs to be an alpha char final char ch = scheme.charAt(0); if ( !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) ) { fail(uri, "invalid scheme", index); } - final int errIdx = validateAlphaNum(scheme, "+-."); + final int errIdx = validateAlphaNum(scheme.get(), "+-."); if( 0 <= errIdx ) { fail(uri, "invalid scheme", index+errIdx); } diff --git a/src/junit/com/jogamp/common/net/TestUri01.java b/src/junit/com/jogamp/common/net/TestUri01.java index 361304c..652caa6 100644 --- a/src/junit/com/jogamp/common/net/TestUri01.java +++ b/src/junit/com/jogamp/common/net/TestUri01.java @@ -28,7 +28,7 @@ public class TestUri01 extends JunitTracer { 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_MIN_LEGAL); + final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL); System.err.println("esc1 "+esc1); Assert.assertEquals(escaped, esc1); @@ -40,7 +40,7 @@ public class TestUri01 extends JunitTracer { 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_MIN_LEGAL); + final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL); System.err.println("esc1 "+esc1); Assert.assertEquals(escaped, esc1); @@ -63,9 +63,9 @@ public class TestUri01 extends JunitTracer { 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%7E"); + 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_MIN_LEGAL); + final Uri.Encoded esc1 = new Uri.Encoded(vanilla, Uri.PATH_LEGAL); System.err.println("esc1 "+esc1); Assert.assertEquals(escaped, esc1); diff --git a/src/junit/com/jogamp/common/net/TestUri03Resolving.java b/src/junit/com/jogamp/common/net/TestUri03Resolving.java index 7c1dadf..9445100 100644 --- a/src/junit/com/jogamp/common/net/TestUri03Resolving.java +++ b/src/junit/com/jogamp/common/net/TestUri03Resolving.java @@ -2,7 +2,6 @@ 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; @@ -146,7 +145,7 @@ public class TestUri03Resolving extends JunitTracer { "C:\\gluegen\\build-x86_64 öä lala\\gluegen-rt.jar"}, new String[] {"\\\\filehost\\gluegen\\build-x86_64 öä lala\\gluegen-rt.jar", - "file:////filehost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar", + "file://filehost/gluegen/build-x86_64%20öä%20lala/gluegen-rt.jar", "\\\\filehost\\gluegen\\build-x86_64 öä lala\\gluegen-rt.jar"}, new String[] {"C:/gluegen/A$/B^/C~/D#/E[/F]/gluegen-rt.jar", @@ -167,71 +166,51 @@ public class TestUri03Resolving extends JunitTracer { System.err.println("file.path.dec "+file.getPath()); System.err.println("file.path.abs "+file.getAbsolutePath()); System.err.println("file.path.can "+file.getCanonicalPath()); - final URI uri0 = file.toURI(); - System.err.println("uri0.string: "+uri0.toString()); - System.err.println("uri0.path : "+uri0.getPath()); - System.err.println("uri0.ascii : "+uri0.toASCIIString()); + final Uri uri0 = Uri.valueOf(file); + URIDumpUtil.showUri(uri0); + URIDumpUtil.showReencodedURIOfUri(uri0); + boolean ok = true; { - final URI uri1 = Uri.valueOf(file).toURI(); - final boolean equalString= uri0.toString().equals(uri1.toString()); - final boolean equalPath = uri0.getPath().equals(uri1.getPath()); - final boolean equalASCII= uri0.toASCIIString().equals(uri1.toASCIIString()); - System.err.println("uri1.string: "+uri1.toString()+" - "+(equalString?"OK":"ERROR")); - System.err.println("uri1.path : "+uri1.getPath()+" - "+(equalPath?"OK":"ERROR")); - System.err.println("uri1.ascii : "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR")); - ok = equalString && equalPath && equalASCII && ok; - } - { final String s2 = IOUtil.slashify(file.getAbsolutePath(), true /* startWithSlash */, file.isDirectory() /* endWithSlash */); System.err.println("uri2.slashify: "+s2); - { - final URI uri1 = new URI(IOUtil.FILE_SCHEME, null, s2, null); - final boolean equalString= uri0.toString().equals(uri1.toString()); - final boolean equalPath = uri0.getPath().equals(uri1.getPath()); - final boolean equalASCII= uri0.toASCIIString().equals(uri1.toASCIIString()); - System.err.println("uri2.string: "+uri1.toString()+" - "+(equalString?"OK":"ERROR")); - System.err.println("uri2.path : "+uri1.getPath()+" - "+(equalPath?"OK":"ERROR")); - System.err.println("uri2.ascii : "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR")); - ok = equalString && equalPath && equalASCII && ok; - } - final URI uri1 = new URI(IOUtil.FILE_SCHEME, null, s2, null); - final boolean equalString= uri0.toString().equals(uri1.toString()); - final boolean equalPath = uri0.getPath().equals(uri1.getPath()); - final boolean equalASCII= uri0.toASCIIString().equals(uri1.toASCIIString()); - System.err.println("uri2.string: "+uri1.toString()+" - "+(equalString?"OK":"ERROR")); - System.err.println("uri2.path : "+uri1.getPath()+" - "+(equalPath?"OK":"ERROR")); - System.err.println("uri2.ascii : "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR")); - ok = equalString && equalPath && equalASCII && ok; + final Uri uri1 = Uri.create(IOUtil.FILE_SCHEME, null, s2, null); + final boolean equalEncoded= uri0.getEncoded().equals(uri1.getEncoded()); + final boolean equalPath = uri0.path.decode().equals(uri1.path.decode()); + final boolean equalASCII= uri0.toASCIIString().equals(uri1.toASCIIString().get()); + System.err.println("uri2.enc : "+uri1.getEncoded()+" - "+(equalEncoded?"OK":"ERROR")); + System.err.println("uri2.pathD : "+uri1.path.decode()+" - "+(equalPath?"OK":"ERROR")); + System.err.println("uri2.asciiE: "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR")); + ok = equalEncoded && equalPath && equalASCII && ok; } { final String s2 = "/"+string; System.err.println("uri3.orig: "+s2); - final URI uri1 = new URI(IOUtil.FILE_SCHEME, s2, null); + final Uri uri1 = Uri.create(IOUtil.FILE_SCHEME, s2, null); final String rString = "file:/Hallo%20Welt%20öä"; final String rPath = s2; final String rASCII = "file:/Hallo%20Welt%20%C3%B6%C3%A4"; - final boolean equalString= rString.equals(uri1.toString()); - final boolean equalPath = rPath.equals(uri1.getPath()); - final boolean equalASCII= rASCII.equals(uri1.toASCIIString()); - System.err.println("uri3.string: "+uri1.toString()+" - "+(equalString?"OK":"ERROR")); - System.err.println("uri3.path : "+uri1.getPath()+" - "+(equalPath?"OK":"ERROR")); - System.err.println("uri3.ascii : "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR")); - ok = equalString && equalPath && equalASCII && ok; + final boolean equalEncoded = rString.equals(uri1.toString()); + final boolean equalPath = rPath.equals(uri1.path.decode()); + final boolean equalASCII= rASCII.equals(uri1.toASCIIString().get()); + System.err.println("uri3.enc : "+uri1.toString()+" - "+(equalEncoded?"OK":"ERROR")); + System.err.println("uri3.pathD : "+uri1.path.decode()+" - "+(equalPath?"OK":"ERROR")); + System.err.println("uri3.asciiE: "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR")); + ok = equalEncoded && equalPath && equalASCII && ok; } { final String s2 = "//lala.org/"+string; System.err.println("uri4.orig: "+s2); - final URI uri1 = new URI(IOUtil.HTTP_SCHEME, s2, null); + final Uri uri1 = Uri.create(IOUtil.HTTP_SCHEME, s2, null); final String rString = "http://lala.org/Hallo%20Welt%20öä"; final String rPath = "/"+string; final String rASCII = "http://lala.org/Hallo%20Welt%20%C3%B6%C3%A4"; final boolean equalString= rString.equals(uri1.toString()); - final boolean equalPath = rPath.equals(uri1.getPath()); - final boolean equalASCII= rASCII.equals(uri1.toASCIIString()); - System.err.println("uri4.string: "+uri1.toString()+" - "+(equalString?"OK":"ERROR")); - System.err.println("uri4.path : "+uri1.getPath()+" - "+(equalPath?"OK":"ERROR")); - System.err.println("uri4.ascii : "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR")); + final boolean equalPath = rPath.equals(uri1.path.decode()); + final boolean equalASCII= rASCII.equals(uri1.toASCIIString().get()); + System.err.println("uri4.enc : "+uri1.toString()+" - "+(equalString?"OK":"ERROR")); + System.err.println("uri4.pathD : "+uri1.path.decode()+" - "+(equalPath?"OK":"ERROR")); + System.err.println("uri4.asciiE: "+uri1.toASCIIString()+" - "+(equalASCII?"OK":"ERROR")); ok = equalString && equalPath && equalASCII && ok; } Assert.assertTrue("One or more errors occured see stderr above", ok); |