1 17 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileReader ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.io.IOException ; 26 import java.io.Reader ; 27 import java.util.Date ; 28 import java.util.Enumeration ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipFile ; 31 import org.apache.tools.ant.BuildFileTest; 32 import org.apache.tools.ant.taskdefs.condition.Os; 33 34 36 public class JarTest extends BuildFileTest { 37 38 private static String tempJar = "tmp.jar"; 39 private static String tempDir = "jartmp/"; 40 private Reader r1, r2; 41 42 public JarTest(String name) { 43 super(name); 44 } 45 46 public void setUp() { 47 configureProject("src/etc/testcases/taskdefs/jar.xml"); 48 } 49 50 public void tearDown() { 51 if (r1 != null) { 52 try { 53 r1.close(); 54 } catch (IOException e) { 55 } 56 } 57 if (r2 != null) { 58 try { 59 r2.close(); 60 } catch (IOException e) { 61 } 62 } 63 64 executeTarget("cleanup"); 65 } 66 67 public void test1() { 68 expectBuildException("test1", "required argument not specified"); 69 } 70 71 public void test2() { 72 expectBuildException("test2", "manifest file does not exist"); 73 } 74 75 public void test3() { 76 expectBuildException("test3", "Unrecognized whenempty attribute: format C: /y"); 77 } 78 79 public void test4() { 80 executeTarget("test4"); 81 File jarFile = new File (getProjectDir(), tempJar); 82 assertTrue(jarFile.exists()); 83 } 84 85 public void testNoRecreateWithoutUpdate() { 86 testNoRecreate("test4"); 87 } 88 89 public void testNoRecreateWithUpdate() { 90 testNoRecreate("testNoRecreateWithUpdate"); 91 } 92 93 private void testNoRecreate(String secondTarget) { 94 executeTarget("test4"); 95 File jarFile = new File (getProjectDir(), tempJar); 96 long jarModifiedDate = jarFile.lastModified(); 97 try { 98 Thread.currentThread().sleep(2500); 99 } catch (InterruptedException e) { 100 } executeTarget(secondTarget); 102 assertEquals("jar has not been recreated in " + secondTarget, 103 jarModifiedDate, jarFile.lastModified()); 104 } 105 106 public void testRecreateWithoutUpdateAdditionalFiles() { 107 testRecreate("test4", "testRecreateWithoutUpdateAdditionalFiles"); 108 } 109 110 public void testRecreateWithUpdateAdditionalFiles() { 111 testRecreate("test4", "testRecreateWithUpdateAdditionalFiles"); 112 } 113 114 public void testRecreateWithoutUpdateNewerFile() { 115 testRecreate("testRecreateNewerFileSetup", 116 "testRecreateWithoutUpdateNewerFile"); 117 } 118 119 public void testRecreateWithUpdateNewerFile() { 120 testRecreate("testRecreateNewerFileSetup", 121 "testRecreateWithUpdateNewerFile"); 122 } 123 124 private void testRecreate(String firstTarget, String secondTarget) { 125 executeTarget(firstTarget); 126 int sleeptime = 2500; 127 if (Os.isFamily("windows")) { 128 sleeptime += 2500; 129 } 130 try { 131 Thread.currentThread().sleep(sleeptime); 132 } catch (InterruptedException e) { 133 } File jarFile = new File (getProjectDir(), tempJar); 135 long jarModifiedDate = jarFile.lastModified(); 136 executeTarget(secondTarget); 137 jarFile = new File (getProjectDir(), tempJar); 138 assertTrue("jar has been recreated in " + secondTarget, 139 jarModifiedDate < jarFile.lastModified()); 140 } 141 142 public void testManifestStaysIntact() 143 throws IOException , ManifestException { 144 executeTarget("testManifestStaysIntact"); 145 146 r1 = new FileReader (getProject() 147 .resolveFile(tempDir + "manifest")); 148 r2 = new FileReader (getProject() 149 .resolveFile(tempDir + "META-INF/MANIFEST.MF")); 150 Manifest mf1 = new Manifest(r1); 151 Manifest mf2 = new Manifest(r2); 152 assertEquals(mf1, mf2); 153 } 154 155 public void testNoRecreateBasedirExcludesWithUpdate() { 156 testNoRecreate("testNoRecreateBasedirExcludesWithUpdate"); 157 } 158 159 public void testNoRecreateBasedirExcludesWithoutUpdate() { 160 testNoRecreate("testNoRecreateBasedirExcludesWithoutUpdate"); 161 } 162 163 public void testNoRecreateZipfilesetExcludesWithUpdate() { 164 testNoRecreate("testNoRecreateZipfilesetExcludesWithUpdate"); 165 } 166 167 public void testNoRecreateZipfilesetExcludesWithoutUpdate() { 168 testNoRecreate("testNoRecreateZipfilesetExcludesWithoutUpdate"); 169 } 170 171 public void testRecreateZipfilesetWithoutUpdateAdditionalFiles() { 172 testRecreate("test4", 173 "testRecreateZipfilesetWithoutUpdateAdditionalFiles"); 174 } 175 176 public void testRecreateZipfilesetWithUpdateAdditionalFiles() { 177 testRecreate("test4", 178 "testRecreateZipfilesetWithUpdateAdditionalFiles"); 179 } 180 181 public void testRecreateZipfilesetWithoutUpdateNewerFile() { 182 testRecreate("testRecreateNewerFileSetup", 183 "testRecreateZipfilesetWithoutUpdateNewerFile"); 184 } 185 186 public void testRecreateZipfilesetWithUpdateNewerFile() { 187 testRecreate("testRecreateNewerFileSetup", 188 "testRecreateZipfilesetWithUpdateNewerFile"); 189 } 190 191 public void testCreateWithEmptyFileset() { 192 executeTarget("testCreateWithEmptyFilesetSetUp"); 193 executeTarget("testCreateWithEmptyFileset"); 194 executeTarget("testCreateWithEmptyFileset"); 195 } 196 197 public void testUpdateIfOnlyManifestHasChanged() { 198 executeTarget("testUpdateIfOnlyManifestHasChanged"); 199 File jarXml = getProject().resolveFile(tempDir + "jar.xml"); 200 assertTrue(jarXml.exists()); 201 } 202 203 public void testNoDuplicateIndex() throws IOException { 205 ZipFile archive = null; 206 try { 207 executeTarget("testIndexTests"); 208 archive = new ZipFile (getProject().resolveFile(tempJar)); 209 Enumeration e = archive.entries(); 210 int numberOfIndexLists = 0; 211 while (e.hasMoreElements()) { 212 ZipEntry ze = (ZipEntry ) e.nextElement(); 213 if (ze.getName().equals("META-INF/INDEX.LIST")) { 214 numberOfIndexLists++; 215 } 216 } 217 assertEquals(1, numberOfIndexLists); 218 } finally { 219 if (archive != null) { 220 archive.close(); 221 } 222 } 223 } 224 225 public void testRootFilesInIndex() throws IOException { 227 ZipFile archive = null; 228 try { 229 executeTarget("testIndexTests"); 230 archive = new ZipFile (getProject().resolveFile(tempJar)); 231 ZipEntry ze = archive.getEntry("META-INF/INDEX.LIST"); 232 InputStream is = archive.getInputStream(ze); 233 BufferedReader r = new BufferedReader (new InputStreamReader (is, 234 "UTF8")); 235 boolean foundSub = false; 236 boolean foundSubFoo = false; 237 boolean foundFoo = false; 238 239 String line = r.readLine(); 240 while (line != null) { 241 if (line.equals("foo")) { 242 foundFoo = true; 243 } else if (line.equals("sub")) { 244 foundSub = true; 245 } else if (line.equals("sub/foo")) { 246 foundSubFoo = true; 247 } 248 line = r.readLine(); 249 } 250 251 assertTrue(foundSub); 252 assertTrue(!foundSubFoo); 253 assertTrue(foundFoo); 254 } finally { 255 if (archive != null) { 256 archive.close(); 257 } 258 } 259 } 260 public void testManifestOnlyJar() { 261 expectLogContaining("testManifestOnlyJar", "Building MANIFEST-only jar: "); 262 File manifestFile = getProject().resolveFile(tempDir + "META-INF" + File.separator + "MANIFEST.MF"); 263 assertTrue(manifestFile.exists()); 264 } 265 } 266 | Popular Tags |