KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > JarTest


1 /*
2  * Copyright 2000-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.taskdefs;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.util.zip.ZipFile JavaDoc;
31 import org.apache.tools.ant.BuildFileTest;
32 import org.apache.tools.ant.taskdefs.condition.Os;
33
34 /**
35  */

36 public class JarTest extends BuildFileTest {
37
38     private static String JavaDoc tempJar = "tmp.jar";
39     private static String JavaDoc tempDir = "jartmp/";
40     private Reader JavaDoc r1, r2;
41
42     public JarTest(String JavaDoc 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 JavaDoc e) {
55             }
56         }
57         if (r2 != null) {
58             try {
59                 r2.close();
60             } catch (IOException JavaDoc 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 JavaDoc jarFile = new File JavaDoc(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 JavaDoc secondTarget) {
94         executeTarget("test4");
95         File JavaDoc jarFile = new File JavaDoc(getProjectDir(), tempJar);
96         long jarModifiedDate = jarFile.lastModified();
97         try {
98             Thread.currentThread().sleep(2500);
99         } catch (InterruptedException JavaDoc e) {
100         } // end of try-catch
101
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 JavaDoc firstTarget, String JavaDoc 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 JavaDoc e) {
133         } // end of try-catch
134
File JavaDoc jarFile = new File JavaDoc(getProjectDir(), tempJar);
135         long jarModifiedDate = jarFile.lastModified();
136         executeTarget(secondTarget);
137         jarFile = new File JavaDoc(getProjectDir(), tempJar);
138         assertTrue("jar has been recreated in " + secondTarget,
139                    jarModifiedDate < jarFile.lastModified());
140     }
141
142     public void testManifestStaysIntact()
143         throws IOException JavaDoc, ManifestException {
144         executeTarget("testManifestStaysIntact");
145
146         r1 = new FileReader JavaDoc(getProject()
147                             .resolveFile(tempDir + "manifest"));
148         r2 = new FileReader JavaDoc(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 JavaDoc jarXml = getProject().resolveFile(tempDir + "jar.xml");
200         assertTrue(jarXml.exists());
201     }
202
203     // bugzilla report 10262
204
public void testNoDuplicateIndex() throws IOException JavaDoc {
205         ZipFile JavaDoc archive = null;
206         try {
207             executeTarget("testIndexTests");
208             archive = new ZipFile JavaDoc(getProject().resolveFile(tempJar));
209             Enumeration JavaDoc e = archive.entries();
210             int numberOfIndexLists = 0;
211             while (e.hasMoreElements()) {
212                 ZipEntry JavaDoc ze = (ZipEntry JavaDoc) 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     // bugzilla report 16972
226
public void testRootFilesInIndex() throws IOException JavaDoc {
227         ZipFile JavaDoc archive = null;
228         try {
229             executeTarget("testIndexTests");
230             archive = new ZipFile JavaDoc(getProject().resolveFile(tempJar));
231             ZipEntry JavaDoc ze = archive.getEntry("META-INF/INDEX.LIST");
232             InputStream JavaDoc is = archive.getInputStream(ze);
233             BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is,
234                                                                         "UTF8"));
235             boolean foundSub = false;
236             boolean foundSubFoo = false;
237             boolean foundFoo = false;
238
239             String JavaDoc 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 JavaDoc manifestFile = getProject().resolveFile(tempDir + "META-INF" + File.separator + "MANIFEST.MF");
263         assertTrue(manifestFile.exists());
264     }
265 }
266
Popular Tags