source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/testcases/org/apache/tools/ant/taskdefs/JarTest.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 8.9 KB
Line 
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
18package org.apache.tools.ant.taskdefs;
19
20import java.io.BufferedReader;
21import java.io.File;
22import java.io.FileReader;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.io.IOException;
26import java.io.Reader;
27import java.util.Date;
28import java.util.Enumeration;
29import java.util.zip.ZipEntry;
30import java.util.zip.ZipFile;
31import org.apache.tools.ant.BuildFileTest;
32import org.apache.tools.ant.taskdefs.condition.Os;
33
34/**
35 */
36public 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 } // 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 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 } // end of try-catch
134 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 // bugzilla report 10262
204 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 // bugzilla report 16972
226 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}
Note: See TracBrowser for help on using the repository browser.