You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

JarTest.java 8.9 kB

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