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 testNormaliseURL("jar:http://localhost/ted.jar!/", "http://localhost/ted.jar"); 82 83 } 84 85 public void testNormaliseURL(String expected, String input) throws MalformedURLException { 86 assertEquals("JarUrlConnection.normaliseURL(" + input + ")", new URL(expected), JarUrlConnection.normaliseURL(new URL(input))); 87 } 88 89 public void testConstructionMalformed(String expected, String input, Class exception) throws Exception { 90 91 String method = "JarUrlConnection.normaliseURL(" + input + ")"; 92 try { 93 new JarUrlConnection(new URL(input)); 94 if (exception != null) { 95 fail(method + " should have thrown exception - " + exception.getName()); 96 } 97 } catch (Exception e) { 98 if (exception != null && exception.isInstance(e)) { 99 //Success 100 return; 101 } 102 throw e; 103 } 104 } 105 106 public void testMalformedURL() throws Exception { 107 testConstructionMalformed("", "http://!!!", MalformedURLException.class); 108 testConstructionMalformed("", "jar://!!!/", MalformedURLException.class); 109 testConstructionMalformed("", "jar:flan://!/", MalformedURLException.class); 110 testConstructionMalformed("", "jar:file:///fred.jar!/", null); 111 } 112 }

This page was automatically generated by Maven