001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.io.IOException;
027: import java.io.Reader;
028: import java.util.Enumeration;
029: import java.util.zip.ZipEntry;
030: import java.util.zip.ZipFile;
031: import org.apache.tools.ant.BuildFileTest;
032: import org.apache.tools.ant.util.FileUtils;
033:
034: /**
035: */
036: public class JarTest extends BuildFileTest {
037:
038: /** Utilities used for file operations */
039: private static final FileUtils FILE_UTILS = FileUtils
040: .getFileUtils();
041:
042: private static String tempJar = "tmp.jar";
043: private static String tempDir = "jartmp/";
044: private Reader r1, r2;
045:
046: public JarTest(String name) {
047: super (name);
048: }
049:
050: public void setUp() {
051: configureProject("src/etc/testcases/taskdefs/jar.xml");
052: }
053:
054: public void tearDown() {
055: if (r1 != null) {
056: try {
057: r1.close();
058: } catch (IOException e) {
059: }
060: }
061: if (r2 != null) {
062: try {
063: r2.close();
064: } catch (IOException e) {
065: }
066: }
067:
068: executeTarget("cleanup");
069: }
070:
071: public void test1() {
072: expectBuildException("test1", "required argument not specified");
073: }
074:
075: public void test2() {
076: expectBuildException("test2", "manifest file does not exist");
077: }
078:
079: public void test3() {
080: expectBuildException("test3",
081: "Unrecognized whenempty attribute: format C: /y");
082: }
083:
084: public void test4() {
085: executeTarget("test4");
086: File jarFile = new File(getProjectDir(), tempJar);
087: assertTrue(jarFile.exists());
088: }
089:
090: public void testNoRecreateWithoutUpdate() {
091: testNoRecreate("test4");
092: }
093:
094: public void testNoRecreateWithUpdate() {
095: testNoRecreate("testNoRecreateWithUpdate");
096: }
097:
098: private void testNoRecreate(String secondTarget) {
099: executeTarget("test4");
100: File jarFile = new File(getProjectDir(), tempJar);
101: long jarModifiedDate = jarFile.lastModified();
102: try {
103: Thread.sleep(2500);
104: } catch (InterruptedException e) {
105: } // end of try-catch
106: executeTarget(secondTarget);
107: assertEquals("jar has not been recreated in " + secondTarget,
108: jarModifiedDate, jarFile.lastModified());
109: }
110:
111: public void testRecreateWithoutUpdateAdditionalFiles() {
112: testRecreate("test4",
113: "testRecreateWithoutUpdateAdditionalFiles");
114: }
115:
116: public void testRecreateWithUpdateAdditionalFiles() {
117: testRecreate("test4", "testRecreateWithUpdateAdditionalFiles");
118: }
119:
120: public void testRecreateWithoutUpdateNewerFile() {
121: testRecreate("testRecreateNewerFileSetup",
122: "testRecreateWithoutUpdateNewerFile");
123: }
124:
125: public void testRecreateWithUpdateNewerFile() {
126: testRecreate("testRecreateNewerFileSetup",
127: "testRecreateWithUpdateNewerFile");
128: }
129:
130: private void testRecreate(String firstTarget, String secondTarget) {
131: executeTarget(firstTarget);
132: long sleeptime = 3000 + FILE_UTILS
133: .getFileTimestampGranularity();
134: try {
135: Thread.sleep(sleeptime);
136: } catch (InterruptedException e) {
137: } // end of try-catch
138: File jarFile = new File(getProjectDir(), tempJar);
139: long jarModifiedDate = jarFile.lastModified();
140: executeTarget(secondTarget);
141: jarFile = new File(getProjectDir(), tempJar);
142: assertTrue("jar has been recreated in " + secondTarget,
143: jarModifiedDate < jarFile.lastModified());
144: }
145:
146: public void testManifestStaysIntact() throws IOException,
147: ManifestException {
148: executeTarget("testManifestStaysIntact");
149:
150: r1 = new FileReader(getProject().resolveFile(
151: tempDir + "manifest"));
152: r2 = new FileReader(getProject().resolveFile(
153: tempDir + "META-INF/MANIFEST.MF"));
154: Manifest mf1 = new Manifest(r1);
155: Manifest mf2 = new Manifest(r2);
156: assertEquals(mf1, mf2);
157: }
158:
159: public void testNoRecreateBasedirExcludesWithUpdate() {
160: testNoRecreate("testNoRecreateBasedirExcludesWithUpdate");
161: }
162:
163: public void testNoRecreateBasedirExcludesWithoutUpdate() {
164: testNoRecreate("testNoRecreateBasedirExcludesWithoutUpdate");
165: }
166:
167: public void testNoRecreateZipfilesetExcludesWithUpdate() {
168: testNoRecreate("testNoRecreateZipfilesetExcludesWithUpdate");
169: }
170:
171: public void testNoRecreateZipfilesetExcludesWithoutUpdate() {
172: testNoRecreate("testNoRecreateZipfilesetExcludesWithoutUpdate");
173: }
174:
175: public void testRecreateZipfilesetWithoutUpdateAdditionalFiles() {
176: testRecreate("test4",
177: "testRecreateZipfilesetWithoutUpdateAdditionalFiles");
178: }
179:
180: public void testRecreateZipfilesetWithUpdateAdditionalFiles() {
181: testRecreate("test4",
182: "testRecreateZipfilesetWithUpdateAdditionalFiles");
183: }
184:
185: public void testRecreateZipfilesetWithoutUpdateNewerFile() {
186: testRecreate("testRecreateNewerFileSetup",
187: "testRecreateZipfilesetWithoutUpdateNewerFile");
188: }
189:
190: public void testRecreateZipfilesetWithUpdateNewerFile() {
191: testRecreate("testRecreateNewerFileSetup",
192: "testRecreateZipfilesetWithUpdateNewerFile");
193: }
194:
195: public void testCreateWithEmptyFileset() {
196: executeTarget("testCreateWithEmptyFilesetSetUp");
197: executeTarget("testCreateWithEmptyFileset");
198: executeTarget("testCreateWithEmptyFileset");
199: }
200:
201: public void testUpdateIfOnlyManifestHasChanged() {
202: executeTarget("testUpdateIfOnlyManifestHasChanged");
203: File jarXml = getProject().resolveFile(tempDir + "jar.xml");
204: assertTrue(jarXml.exists());
205: }
206:
207: // bugzilla report 10262
208: public void testNoDuplicateIndex() throws IOException {
209: ZipFile archive = null;
210: try {
211: executeTarget("testIndexTests");
212: archive = new ZipFile(getProject().resolveFile(tempJar));
213: Enumeration e = archive.entries();
214: int numberOfIndexLists = 0;
215: while (e.hasMoreElements()) {
216: ZipEntry ze = (ZipEntry) e.nextElement();
217: if (ze.getName().equals("META-INF/INDEX.LIST")) {
218: numberOfIndexLists++;
219: }
220: }
221: assertEquals(1, numberOfIndexLists);
222: } finally {
223: if (archive != null) {
224: archive.close();
225: }
226: }
227: }
228:
229: // bugzilla report 16972
230: public void testRootFilesInIndex() throws IOException {
231: ZipFile archive = null;
232: try {
233: executeTarget("testIndexTests");
234: archive = new ZipFile(getProject().resolveFile(tempJar));
235: ZipEntry ze = archive.getEntry("META-INF/INDEX.LIST");
236: InputStream is = archive.getInputStream(ze);
237: BufferedReader r = new BufferedReader(
238: new InputStreamReader(is, "UTF8"));
239: boolean foundSub = false;
240: boolean foundSubFoo = false;
241: boolean foundFoo = false;
242:
243: String line = r.readLine();
244: while (line != null) {
245: if (line.equals("foo")) {
246: foundFoo = true;
247: } else if (line.equals("sub")) {
248: foundSub = true;
249: } else if (line.equals("sub/foo")) {
250: foundSubFoo = true;
251: }
252: line = r.readLine();
253: }
254:
255: assertTrue(foundSub);
256: assertTrue(!foundSubFoo);
257: assertTrue(foundFoo);
258: } finally {
259: if (archive != null) {
260: archive.close();
261: }
262: }
263: }
264:
265: public void testManifestOnlyJar() {
266: expectLogContaining("testManifestOnlyJar",
267: "Building MANIFEST-only jar: ");
268: File manifestFile = getProject().resolveFile(
269: tempDir + "META-INF" + File.separator + "MANIFEST.MF");
270: assertTrue(manifestFile.exists());
271: }
272:
273: public void testIndexJarsPlusJarMarker() {
274: executeTarget("testIndexJarsPlusJarMarker");
275: }
276: }
|