1 20 package org.apache.cactus.integration.ant.deployment; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.StringReader ; 28 29 import junit.framework.TestCase; 30 31 36 public final class TestJarArchive extends TestCase 37 { 38 39 41 47 public void testConstructorWithNullFile() throws Exception 48 { 49 try 50 { 51 new DefaultJarArchive((File ) null); 52 fail("NullPointerException expected"); 53 } 54 catch (NullPointerException expected) 55 { 56 } 58 } 59 60 66 public void testConstructorWithNullInputStream() throws Exception 67 { 68 try 69 { 70 new DefaultJarArchive((InputStream ) null); 71 fail("NullPointerException expected"); 72 } 73 catch (NullPointerException expected) 74 { 75 } 77 } 78 79 84 public void testRandomAccess() throws Exception 85 { 86 JarArchive jar = new DefaultJarArchive(getTestInput( 87 "org/apache/cactus/integration/ant/deployment/randomaccess.jar")); 88 assertContains(jar.getResource("firstEntry.txt"), "firstEntry"); 89 assertContains(jar.getResource("secondEntry.txt"), "secondEntry"); 90 assertContains(jar.getResource("secondEntry.txt"), "secondEntry"); 91 assertContains(jar.getResource("firstEntry.txt"), "firstEntry"); 92 } 93 94 100 public void testContainsClass() throws Exception 101 { 102 JarArchive jar = new DefaultJarArchive(getTestInput( 103 "org/apache/cactus/integration/ant/deployment/containsclass.jar")); 104 assertTrue(jar.containsClass("test.Test")); 105 } 106 107 113 public void testContainsClassEmpty() throws Exception 114 { 115 JarArchive jar = new DefaultJarArchive(getTestInput( 116 "org/apache/cactus/integration/ant/deployment/empty.jar")); 117 assertTrue(!jar.containsClass("test.Test")); 118 } 119 120 122 130 private void assertContains(InputStream theInput, String theExpectedString) 131 throws IOException 132 { 133 try 134 { 135 BufferedReader inReader = 136 new BufferedReader (new InputStreamReader (theInput)); 137 BufferedReader stringReader = 138 new BufferedReader (new StringReader (theExpectedString)); 139 String line = null; 140 while ((line = inReader.readLine()) != null) 141 { 142 assertEquals(stringReader.readLine(), line); 143 } 144 } 145 finally 146 { 147 if (theInput != null) 148 { 149 theInput.close(); 150 } 151 } 152 } 153 154 162 private File getTestInput(String theFileName) 163 { 164 String testInputDirProperty = System.getProperty("testinput.dir"); 165 assertTrue("The system property 'testinput.dir' must be set", 166 testInputDirProperty != null); 167 File testInputDir = new File (testInputDirProperty); 168 assertTrue("The system property 'testinput.dir' must point to an " 169 + "existing directory", testInputDir.isDirectory()); 170 File testInputFile = new File (testInputDir, theFileName); 171 assertTrue("The test input " + theFileName + " does not exist", 172 testInputFile.exists()); 173 return testInputFile; 174 } 175 176 } 177 | Popular Tags |