jaulib v1.3.0
Jau Support Library (C++, Java, ..)
TestUri02Composing.java
Go to the documentation of this file.
1package jau.test.net;
2
3import java.io.IOException;
4import java.net.MalformedURLException;
5import java.net.URISyntaxException;
6import java.net.URL;
7
8import org.jau.net.AssetURLContext;
9import org.jau.net.Uri;
10import org.junit.Assert;
11import org.junit.BeforeClass;
12import org.junit.FixMethodOrder;
13import org.junit.Test;
14import org.junit.runners.MethodSorters;
15
16import jau.test.junit.util.JunitTracer;
17
18@FixMethodOrder(MethodSorters.NAME_ASCENDING)
19public class TestUri02Composing extends JunitTracer {
20
21 @BeforeClass
22 public static void assetRegistration() throws Exception {
23 try {
24 System.err.println("******* Asset URL Stream Handler Registration: PRE");
25 Assert.assertTrue("GenericURLStreamHandlerFactory.register() failed", AssetURLContext.registerHandler(TestAssetURLConnectionRegistered.class.getClassLoader()));
26 Assert.assertNotNull(AssetURLContext.getRegisteredHandler());
27 System.err.println("******* Asset URL Stream Handler Registration: POST");
28 } catch (final Exception e) {
29 setTestSupported(false);
30 throw e;
31 }
32 }
33
34 @Test
35 public void test01URLCompositioning() throws IOException, URISyntaxException {
36 testURNCompositioning("file:///rootDir/file1.txt");
37 testURNCompositioning("file://host/rootDir/file1.txt");
38 testURNCompositioning("jar:file:/web1/file1.jar!/rootDir/file1.txt");
39 testURNCompositioning("asset:gluegen-test/info.txt");
40 testURNCompositioning("asset:/gluegen-test/info.txt");
41 testURNCompositioning("http://domain.com/web1/index.html?lala=23&lili=24#anchor");
42 testURNCompositioning("http://domain.com:1234/web1/index.html?lala=23&lili=24#anchor");
43
44 final Uri file1URI = Uri.cast("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt");
45 testURICompositioning(file1URI);
46 testUriCompositioning(file1URI, Uri.cast("asset:jar:file:/web1/file1.jar!/rootDir/./file1.txt"));
47 testUriCompositioning(file1URI, Uri.cast("asset:jar:file:/web1/file1.jar!/rootDir/dummyParent/../file1.txt"));
48
49 final URL file1URL = new URL("asset:jar:file:/web1/file1.jar!/rootDir/file1.txt");
50 testURLCompositioning(file1URL);
51 testURLCompositioning(file1URL, new URL("asset:jar:file:/web1/file1.jar!/rootDir/./file1.txt"));
52 testURLCompositioning(file1URL, new URL("asset:jar:file:/web1/file1.jar!/rootDir/dummyParent/../file1.txt"));
53 }
54
55 static void testURNCompositioning(final String urn) throws MalformedURLException, URISyntaxException {
56 testURICompositioning( Uri.cast(urn) );
57 testURLCompositioning( new URL(urn) );
58 }
59
60 static void testURICompositioning(final Uri uri) throws MalformedURLException, URISyntaxException {
61 testUriCompositioning(uri, uri);
62 }
63 static void testUriCompositioning(final Uri refURI, final Uri uri1) throws MalformedURLException, URISyntaxException {
64 System.err.println("scheme <"+uri1.scheme+">, ssp <"+uri1.schemeSpecificPart+">, fragment <"+uri1.fragment+">");
65 final Uri uri2 = uri1.getRelativeOf(null);
66
67 System.err.println("URL-equals: "+refURI.equals(uri2));
68 System.err.println("URL-ref : <"+refURI+">");
69 System.err.println("URL-orig : <"+uri1+">");
70 System.err.println("URL-comp : <"+uri2+">");
71 Assert.assertEquals(refURI, uri2);
72 }
73
74 static void testURLCompositioning(final URL url) throws MalformedURLException, URISyntaxException {
75 testURLCompositioning(url, url);
76 }
77 static void testURLCompositioning(final URL refURL, final URL url1) throws MalformedURLException, URISyntaxException {
78 final Uri uri1 = Uri.valueOf(url1);
79 System.err.println("scheme <"+uri1.scheme+">, ssp <"+uri1.schemeSpecificPart+">, fragment <"+uri1.fragment+">");
80 final Uri uri2 = uri1.getRelativeOf(null);
81
82 System.err.println("URL-equals(2): "+refURL.equals(uri2.toURL()));
83 System.err.println("URL-same : "+refURL.sameFile(uri2.toURL()));
84 System.err.println("URL-ref : <"+refURL+">");
85 System.err.println("URL-orig : <"+url1+">");
86 System.err.println("URL-comp : <"+uri2+">");
87 Assert.assertEquals(Uri.valueOf(refURL), uri2);
88 Assert.assertEquals(refURL, uri2.toURL());
89 Assert.assertTrue(refURL.sameFile(uri2.toURL()));
90 }
91
92 public static void main(final String args[]) throws IOException {
93 final String tstname = TestUri02Composing.class.getName();
94 org.junit.runner.JUnitCore.main(tstname);
95 }
96}
static void main(final String args[])
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.
This class implements an immutable Uri as defined by RFC 2396.
Definition: Uri.java:162
static Uri cast(final String encodedUri)
Casts the given encoded String to a new Encoded instance used to create the resulting Uri instance vi...
Definition: Uri.java:1073