1 package org.codehaus.classworlds.uberjar.protocol.jar;
2
3 import junit.framework.TestCase;
4
5 import java.io.File;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8
9 import org.codehaus.classworlds.uberjar.protocol.jar.JarUrlConnection;
10
11 public class JarUrlConnectionTest
12 extends TestCase
13 {
14
15 public void setUp()
16 {
17 System.setProperty( "java.protocol.handler.pkgs",
18 "org.codehaus.classworlds.uberjar.protocol" );
19 }
20
21 public void testConstruct()
22 throws Exception
23 {
24 URL url = buildUrl( "nested.jar",
25 "!/lib/a.jar!/a/A.class" );
26
27 JarUrlConnection connection = new JarUrlConnection( url );
28
29 String[] segments = connection.getSegments();
30
31 assertEquals( 2,
32 segments.length );
33
34 assertEquals( "/lib/a.jar",
35 segments[0] );
36
37 assertEquals( "/a/A.class",
38 segments[1] );
39
40 URL baseResource = connection.getBaseResource();
41
42 assertTrue( baseResource.toExternalForm().startsWith( "file:" ) );
43 assertTrue( baseResource.toExternalForm().endsWith( "nested.jar" ) );
44 }
45
46 public void testConnect_Simple()
47 throws Exception
48 {
49 URL url = buildUrl( "nested.jar", "" );
50
51 JarUrlConnection connection = new JarUrlConnection( url );
52
53 connection.connect();
54 }
55
56 protected URL buildUrl( String jarName,
57 String path )
58 throws Exception
59 {
60 File testDir = new File( System.getProperty( "basedir" ),
61 "target/test-data" );
62
63 File jarFile = new File( testDir,
64 jarName );
65
66 URL jarUrl = jarFile.toURL();
67
68 String urlText = "jar:" + jarUrl + path;
69
70 System.err.println( "url-text: " + urlText );
71
72 URL url = new URL( urlText );
73
74 System.err.println( "url: " + url );
75
76 return url;
77
78 }
79
80 public void testNormaliseURL() throws MalformedURLException
81 {
82 testNormaliseURL( "jar:http://localhost/ted.jar!/", "http://localhost/ted.jar" );
83
84 }
85
86 public void testNormaliseURL( String expected, String input ) throws MalformedURLException
87 {
88 assertEquals( "JarUrlConnection.normaliseURL(" + input + ")", new URL( expected ), JarUrlConnection.normaliseURL( new URL( input ) ) );
89 }
90
91 public void testConstructionMalformed( String expected, String input, Class exception ) throws Exception
92 {
93
94 String method = "JarUrlConnection.normaliseURL(" + input + ")";
95 try
96 {
97 new JarUrlConnection( new URL( input ) );
98 if ( exception != null )
99 {
100 fail( method + " should have thrown exception - " + exception.getName() );
101 }
102 }
103 catch ( Exception e )
104 {
105 if ( exception != null && exception.isInstance( e ) )
106 {
107
108 return;
109 }
110 throw e;
111 }
112 }
113
114 public void testMalformedURL() throws Exception
115 {
116 testConstructionMalformed( "", "http://!!!", MalformedURLException.class );
117 testConstructionMalformed( "", "jar://!!!/", MalformedURLException.class );
118 testConstructionMalformed( "", "jar:flan://!/", MalformedURLException.class );
119 testConstructionMalformed( "", "jar:file:///fred.jar!/", null );
120 }
121 }