jaulib v1.3.0
Jau Support Library (C++, Java, ..)
TestAssetURLConnectionRegistered.java
Go to the documentation of this file.
1package jau.test.net;
2
3import java.io.IOException;
4import java.net.URISyntaxException;
5import java.net.URL;
6import java.net.URLConnection;
7
8import org.jau.io.IOUtil;
9import org.jau.net.AssetURLContext;
10import org.jau.net.Uri;
11import org.junit.Assert;
12import org.junit.BeforeClass;
13import org.junit.FixMethodOrder;
14import org.junit.Test;
15import org.junit.runners.MethodSorters;
16
17@FixMethodOrder(MethodSorters.NAME_ASCENDING)
19
20 @BeforeClass
21 public static void assetRegistration() throws Exception {
22 try {
23 System.err.println("******* Asset URL Stream Handler Registration: PRE");
24 Assert.assertTrue("GenericURLStreamHandlerFactory.register() failed", AssetURLContext.registerHandler(TestAssetURLConnectionRegistered.class.getClassLoader()));
25 Assert.assertNotNull(AssetURLContext.getRegisteredHandler());
26 System.err.println("******* Asset URL Stream Handler Registration: POST");
27 } catch (final Exception e) {
28 setTestSupported(false);
29 throw e;
30 }
31 }
32
33 @Test
34 public void assetRegisteredURLConnection_RT() throws IOException {
35 testAssetConnection(createAssetURLConnection(test_asset_rt_url), test_asset_rt_entry);
36 }
37
38 @Test
39 public void assetRegisteredURLConnection_Test() throws IOException {
40 testAssetConnection(createAssetURLConnection(test_asset_test1_url), test_asset_test1_entry);
41 }
42
43 @Test
44 public void assetRegisteredIOUtilGetResourceRel1_RT() throws IOException, URISyntaxException {
45 final URLConnection urlConn0 = IOUtil.getResource(test_asset_test2a_url, this.getClass().getClassLoader());
46 Assert.assertNotNull(urlConn0);
47 Assert.assertEquals(test_asset_test2a_url, urlConn0.getURL().toExternalForm());
48 testAssetConnection(urlConn0, test_asset_test2_entry);
49
50 final Uri uri1 = Uri.valueOf(urlConn0.getURL()).getRelativeOf(test_asset_test3_rel);
51 Assert.assertNotNull(uri1);
52 Assert.assertEquals(test_asset_test3a_url, uri1.toString());
53 testAssetConnection(uri1.toURL().openConnection(), test_asset_test3_entry);
54
55 final Uri uri2 = Uri.valueOf(urlConn0.getURL()).getRelativeOf(test_asset_test4_rel);
56 Assert.assertNotNull(uri2);
57 Assert.assertEquals(test_asset_test4a_url, uri2.toString());
58 testAssetConnection(uri2.toURL().openConnection(), test_asset_test4_entry);
59 }
60
61 @Test
62 public void assetRegisteredIOUtilGetResourceRel2_RT() throws IOException, URISyntaxException {
63 final URLConnection urlConn0 = IOUtil.getResource(test_asset_test2b_url, this.getClass().getClassLoader());
64 Assert.assertNotNull(urlConn0);
65 Assert.assertEquals(test_asset_test2b_url, urlConn0.getURL().toExternalForm());
66 testAssetConnection(urlConn0, test_asset_test2_entry);
67
68 final Uri uri1 = Uri.valueOf(urlConn0.getURL()).getRelativeOf(test_asset_test3_rel);
69 Assert.assertNotNull(uri1);
70 Assert.assertEquals(test_asset_test3b_url, uri1.toString());
71 testAssetConnection(uri1.toURL().openConnection(), test_asset_test3_entry);
72
73 final Uri uri2 = Uri.valueOf(urlConn0.getURL()).getRelativeOf(test_asset_test4_rel);
74 Assert.assertNotNull(uri2);
75 Assert.assertEquals(test_asset_test4b_url, uri2.toString());
76 testAssetConnection(uri2.toURL().openConnection(), test_asset_test4_entry);
77 }
78
79 URLConnection createAssetURLConnection(final String path) throws IOException {
80 final URL url = AssetURLContext.createURL(path);
81 final URLConnection c = url.openConnection();
82 System.err.println("createAssetURL: "+path+" -> url: "+url+" -> conn: "+c+" / connURL "+(null!=c?c.getURL():null));
83 return c;
84
85 }
86
87 public static void main(final String args[]) throws IOException {
88 final String tstname = TestAssetURLConnectionRegistered.class.getName();
89 org.junit.runner.JUnitCore.main(tstname);
90 }
91}
static URLConnection getResource(final String resourcePath, final ClassLoader classLoader, final Class<?> relContext)
Locating a resource using getResource(String, ClassLoader):
Definition: IOUtil.java:512
See PiggybackURLConnection for description and examples.
static boolean registerHandler(final ClassLoader cl)
Registers the generic URLStreamHandlerFactory via GenericURLStreamHandlerFactory#register() and if su...
static URLStreamHandler getRegisteredHandler()
Returns the asset handler previously set via registerHandler(ClassLoader), or null if none was set.
static URL createURL(final String path, final ClassLoader cl)
Create an asset URL, suitable even w/o the registered asset URLStreamHandler.
This class implements an immutable Uri as defined by RFC 2396.
Definition: Uri.java:162
final String toString()
Returns the encoded input as String, never null, same as getEncoded().
Definition: Uri.java:1263
final java.net.URL toURL()
Returns a new URL instance using the encoded input string, new URL(uri.input), i.e.
Definition: Uri.java:1331
static Uri valueOf(final File file)
Creates a new Uri instance using the given File instance.
Definition: Uri.java:1126
Uri getRelativeOf(final Encoded appendPath)
Returns a new Uri appending the given appendPath to this instance's directory.
Definition: Uri.java:1686