diff options
author | Sven Gothel <[email protected]> | 2012-03-17 21:15:49 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-03-17 21:15:49 +0100 |
commit | 235f8b1cbff8ed13071d5c19c0be492c0b25cb78 (patch) | |
tree | 659845e16bd69372bc7ddc3a42b3aa7130d18df5 /src/junit/com | |
parent | 0cfc7847c58b51c9a26b50d905b592d1fc4c8578 (diff) |
Add 'asset' URLConnection; IOUtil uses URLConnection / incr. effeciency; Android ClassLoaderUtil cleanup;
- Add 'asset' URLConnection
- Please read API doc 'PiggybackURLConnection' and 'AssetURLConnection'
- Solves generic resource handling where platform locations may differ,
ie ClassLoader lookup on Android in the 'assets/' subfolder.
- New Android 'AssetDexClassLoader' uses 'assets/' folder for findResource(..)
- aapt.signed (our APK ant task)
- uses 'assets/' folder
- adds the 'assetsdir' attribute allowing to copy other assets into the APK
- IOUtil uses URLConnection / incr. effeciency
- using URLConnection on all getResource(..) since URL
is connected anyways for validation and URLConnection can be used by caller right away
- String getRelativeOf(URL, String) -> URL getRelativeOf(URL, String)
- preserves scheme, authority, etc
- simple parentOf handling, more efficient
- reusing new 'asset' protocol impl.
- Android ClassLoaderUtil cleanup;
- Use createClassLoader(..) impl for build-in static jogamp and user APKs,
which removes code redundancy
Tests: New code path, especially 'assets' are covered by new unit tests, no regressions on Linux.
Diffstat (limited to 'src/junit/com')
5 files changed, 267 insertions, 15 deletions
diff --git a/src/junit/com/jogamp/common/net/AssetURLConnectionBase.java b/src/junit/com/jogamp/common/net/AssetURLConnectionBase.java new file mode 100644 index 0000000..80f9bd4 --- /dev/null +++ b/src/junit/com/jogamp/common/net/AssetURLConnectionBase.java @@ -0,0 +1,61 @@ +package com.jogamp.common.net; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.JarURLConnection; +import java.net.URLConnection; + +import org.junit.Assert; + +import com.jogamp.common.os.AndroidVersion; +import com.jogamp.common.util.IOUtil; +import com.jogamp.junit.util.JunitTracer; + +public abstract class AssetURLConnectionBase extends JunitTracer { + + /** In gluegen-rt.jar */ + protected static final String test_asset_rt_url = "asset:gluegen/info.txt"; + protected static final String test_asset_rt_entry = "gluegen/info.txt"; + + protected static final String test_asset_rt2_url = "asset:/gluegen/info.txt"; + + /** In gluegen.test.jar */ + protected static final String test_asset_test1_url = "asset:gluegen-test/info.txt"; + protected static final String test_asset_test1_entry = "gluegen-test/info.txt"; + protected static final String test_asset_test2_rel = "data/AssetURLConnectionTest.txt"; + protected static final String test_asset_test2a_url = "asset:com/jogamp/common/net/data/AssetURLConnectionTest.txt"; + protected static final String test_asset_test2b_url = "asset:/com/jogamp/common/net/data/AssetURLConnectionTest.txt"; + protected static final String test_asset_test2_entry = "com/jogamp/common/net/data/AssetURLConnectionTest.txt"; + protected static final String test_asset_test3_rel = "RelativeData.txt"; + protected static final String test_asset_test3a_url = "asset:com/jogamp/common/net/data/RelativeData.txt"; + protected static final String test_asset_test3b_url = "asset:/com/jogamp/common/net/data/RelativeData.txt"; + protected static final String test_asset_test3_entry = "com/jogamp/common/net/data/RelativeData.txt"; + + protected static void testAssetConnection(URLConnection c, String entry_name) throws IOException { + Assert.assertNotNull(c); + if(c instanceof AssetURLConnection) { + final AssetURLConnection ac = (AssetURLConnection) c; + Assert.assertEquals(entry_name, ac.getEntryName()); + } else if(c instanceof JarURLConnection) { + final JarURLConnection jc = (JarURLConnection) c; + if(AndroidVersion.isAvailable) { + Assert.assertEquals("assets/"+entry_name, jc.getEntryName()); + } else { + Assert.assertEquals(entry_name, jc.getEntryName()); + } + } + + final BufferedReader reader = new BufferedReader(new InputStreamReader(c.getInputStream())); + try { + String line = null; + int l = 0; + while ((line = reader.readLine()) != null) { + System.err.println(c.getURL()+":"+l+"> "+line); + l++; + } + } finally { + IOUtil.close(reader, false); + } + } +}
\ No newline at end of file diff --git a/src/junit/com/jogamp/common/net/AssetURLConnectionRegisteredTest.java b/src/junit/com/jogamp/common/net/AssetURLConnectionRegisteredTest.java new file mode 100644 index 0000000..7b8d1a4 --- /dev/null +++ b/src/junit/com/jogamp/common/net/AssetURLConnectionRegisteredTest.java @@ -0,0 +1,76 @@ +package com.jogamp.common.net; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.jogamp.common.util.IOUtil; + +public class AssetURLConnectionRegisteredTest extends AssetURLConnectionBase { + + @BeforeClass + public static void assetRegistration() throws Exception { + try { + System.err.println("******* Asset URL Stream Handler Registration: PRE"); + Assert.assertTrue("GenericURLStreamHandlerFactory.register() failed", AssetURLContext.registerHandler(AssetURLConnectionRegisteredTest.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 assetRegisteredURLConnection_RT() throws IOException { + testAssetConnection(createAssetURLConnection(test_asset_rt_url), test_asset_rt_entry); + } + + @Test + public void assetRegisteredURLConnection_Test() throws IOException { + testAssetConnection(createAssetURLConnection(test_asset_test1_url), test_asset_test1_entry); + } + + @Test + public void assetRegisteredIOUtilGetResourceRel1_RT() throws IOException { + final URLConnection urlConn0 = IOUtil.getResource(test_asset_test2a_url, this.getClass().getClassLoader()); + Assert.assertNotNull(urlConn0); + Assert.assertEquals(test_asset_test2a_url, urlConn0.getURL().toExternalForm()); + testAssetConnection(urlConn0, test_asset_test2_entry); + + final URL url1 = IOUtil.getRelativeOf(urlConn0.getURL(), test_asset_test3_rel); + Assert.assertNotNull(url1); + Assert.assertEquals(test_asset_test3a_url, url1.toExternalForm()); + testAssetConnection(url1.openConnection(), test_asset_test3_entry); + } + + @Test + public void assetRegisteredIOUtilGetResourceRel2_RT() throws IOException { + final URLConnection urlConn0 = IOUtil.getResource(test_asset_test2b_url, this.getClass().getClassLoader()); + Assert.assertNotNull(urlConn0); + Assert.assertEquals(test_asset_test2b_url, urlConn0.getURL().toExternalForm()); + testAssetConnection(urlConn0, test_asset_test2_entry); + + final URL url1 = IOUtil.getRelativeOf(urlConn0.getURL(), test_asset_test3_rel); + Assert.assertNotNull(url1); + Assert.assertEquals(test_asset_test3b_url, url1.toExternalForm()); + testAssetConnection(url1.openConnection(), test_asset_test3_entry); + } + + URLConnection createAssetURLConnection(String path) throws IOException { + URL url = AssetURLContext.createURL(path); + URLConnection c = url.openConnection(); + System.err.println("createAssetURL: "+path+" -> url: "+url+" -> conn: "+c+" / connURL "+(null!=c?c.getURL():null)); + return c; + + } + + public static void main(String args[]) throws IOException { + String tstname = AssetURLConnectionRegisteredTest.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/common/net/AssetURLConnectionUnregisteredTest.java b/src/junit/com/jogamp/common/net/AssetURLConnectionUnregisteredTest.java new file mode 100644 index 0000000..6db8c17 --- /dev/null +++ b/src/junit/com/jogamp/common/net/AssetURLConnectionUnregisteredTest.java @@ -0,0 +1,55 @@ +package com.jogamp.common.net; + +import java.io.IOException; +import java.net.URL; +import java.net.URLConnection; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.common.util.IOUtil; + +public class AssetURLConnectionUnregisteredTest extends AssetURLConnectionBase { + @Test + public void assetUnregisteredURLConnection_RT2() throws IOException { + testAssetConnection(createAssetURLConnection(test_asset_rt2_url, this.getClass().getClassLoader()), test_asset_rt_entry); + } + + @Test + public void assetUnregisteredURLConnection_RT() throws IOException { + testAssetConnection(createAssetURLConnection(test_asset_rt_url, this.getClass().getClassLoader()), test_asset_rt_entry); + } + + @Test + public void assetUnregisteredURLConnection_Test() throws IOException { + testAssetConnection(createAssetURLConnection(test_asset_test1_url, this.getClass().getClassLoader()), test_asset_test1_entry); + } + + @Test + public void assetUnregisteredIOUtilGetResourceAbs_RT() throws IOException { + URLConnection c = IOUtil.getResource(test_asset_rt_entry, this.getClass().getClassLoader()); + testAssetConnection(c, test_asset_rt_entry); + } + + @Test + public void assetUnregisteredIOUtilGetResourceRel0_RT() throws IOException { + final URLConnection urlConn0 = IOUtil.getResource(this.getClass(), test_asset_test2_rel); + testAssetConnection(urlConn0, test_asset_test2_entry); + + final URL url1 = IOUtil.getRelativeOf(urlConn0.getURL(), test_asset_test3_rel); + Assert.assertNotNull(url1); // JARFile URL .. + testAssetConnection(url1.openConnection(), test_asset_test3_entry); + } + + protected static URLConnection createAssetURLConnection(String path, ClassLoader cl) throws IOException { + URL url = AssetURLContext.createURL(path, cl); + URLConnection c = url.openConnection(); + System.err.println("createAssetURL: "+path+" -> url: "+url+" -> conn: "+c+" / connURL "+(null!=c?c.getURL():null)); + return c; + } + + public static void main(String args[]) throws IOException { + String tstname = AssetURLConnectionUnregisteredTest.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } +} diff --git a/src/junit/com/jogamp/common/net/URLCompositionTest.java b/src/junit/com/jogamp/common/net/URLCompositionTest.java new file mode 100644 index 0000000..fb6d298 --- /dev/null +++ b/src/junit/com/jogamp/common/net/URLCompositionTest.java @@ -0,0 +1,62 @@ +package com.jogamp.common.net; + +import java.io.IOException; +import java.net.MalformedURLException; +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; + +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 showURLComponents() throws IOException { + testURLCompositioning(new URL("file:///rootDir/file1.txt")); + testURLCompositioning(new URL("file://host/rootDir/file1.txt")); + testURLCompositioning(new URL("jar:file:/web1/file1.jar!/rootDir/file1.txt")); + testURLCompositioning(new URL("asset:gluegen-test/info.txt")); + testURLCompositioning(new URL("asset:/gluegen-test/info.txt")); + testURLCompositioning(new URL("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt")); + testURLCompositioning(new URL("http://domain.com:1234/web1/index.html?lala=23&lili=24#anchor")); + } + + static void testURLCompositioning(URL u) throws MalformedURLException { + final String scheme = u.getProtocol(); + final String auth = u.getAuthority(); + String path = u.getPath(); + String query = u.getQuery(); + String fragment = u.getRef(); + + System.err.println("scheme <"+scheme+">, auth <"+auth+">, path <"+path+">, query <"+query+">, fragment <"+fragment+">"); + URL u2 = IOUtil.compose(scheme, auth, path, null, query, fragment); + + System.err.println("URL-equals: "+u.equals(u2)); + System.err.println("URL-same : "+u.sameFile(u2)); + System.err.println("URL-orig : <"+u+">"); + System.err.println("URL-comp : <"+u2+">"); + Assert.assertEquals(u, u2); + Assert.assertTrue(u.sameFile(u2)); + } + + 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/TestIOUtil01.java b/src/junit/com/jogamp/common/util/TestIOUtil01.java index 47fa1e9..b75127c 100644 --- a/src/junit/com/jogamp/common/util/TestIOUtil01.java +++ b/src/junit/com/jogamp/common/util/TestIOUtil01.java @@ -35,13 +35,11 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.net.URL; +import java.net.URLConnection; import java.nio.ByteBuffer; import org.junit.Assert; -import org.junit.Before; import org.junit.BeforeClass; -import org.junit.AfterClass; import org.junit.Test; import com.jogamp.common.os.MachineDescription; @@ -69,9 +67,9 @@ public class TestIOUtil01 extends JunitTracer { @Test public void testCopyStream01Array() throws IOException { - URL url = IOUtil.getResource(this.getClass(), tfilename); - Assert.assertNotNull(url); - final BufferedInputStream bis = new BufferedInputStream( url.openStream() ); + URLConnection urlConn = IOUtil.getResource(this.getClass(), tfilename); + Assert.assertNotNull(urlConn); + final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() ); final byte[] bb; try { bb = IOUtil.copyStream2ByteArray( bis ); @@ -85,9 +83,9 @@ public class TestIOUtil01 extends JunitTracer { @Test public void testCopyStream02Buffer() throws IOException { - URL url = IOUtil.getResource(this.getClass(), tfilename); - Assert.assertNotNull(url); - final BufferedInputStream bis = new BufferedInputStream( url.openStream() ); + URLConnection urlConn = IOUtil.getResource(this.getClass(), tfilename); + Assert.assertNotNull(urlConn); + final BufferedInputStream bis = new BufferedInputStream( urlConn.getInputStream() ); final ByteBuffer bb; try { bb = IOUtil.copyStream2ByteBuffer( bis ); @@ -103,15 +101,15 @@ public class TestIOUtil01 extends JunitTracer { @Test public void testCopyStream03Buffer() throws IOException { final String tfilename2 = "./test2.bin" ; - URL url1 = IOUtil.getResource(this.getClass(), tfilename); - Assert.assertNotNull(url1); + URLConnection urlConn1 = IOUtil.getResource(this.getClass(), tfilename); + Assert.assertNotNull(urlConn1); File file2 = new File(tfilename2); - IOUtil.copyURL2File(url1, file2); - URL url2 = IOUtil.getResource(this.getClass(), tfilename2); - Assert.assertNotNull(url2); + IOUtil.copyURLConn2File(urlConn1, file2); + URLConnection urlConn2 = IOUtil.getResource(this.getClass(), tfilename2); + Assert.assertNotNull(urlConn2); - final BufferedInputStream bis = new BufferedInputStream( url2.openStream() ); + final BufferedInputStream bis = new BufferedInputStream( urlConn2.getInputStream() ); final ByteBuffer bb; try { bb = IOUtil.copyStream2ByteBuffer( bis ); |