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

ResourceCopyTests.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /********************************************************************
  2. * Copyright (c) 2003 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Mik Kersten initial implementation
  10. * Helen Hawkins Converted to new interface (bug 148190)
  11. *******************************************************************/
  12. package org.aspectj.ajde.core.tests;
  13. import java.io.File;
  14. import java.io.FileFilter;
  15. import java.io.IOException;
  16. import java.util.Arrays;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Set;
  20. import java.util.jar.JarInputStream;
  21. import java.util.zip.ZipEntry;
  22. import java.util.zip.ZipInputStream;
  23. import org.aspectj.ajde.core.AjdeCoreTestCase;
  24. import org.aspectj.ajde.core.TestCompilerConfiguration;
  25. import org.aspectj.ajde.core.TestMessageHandler;
  26. import org.aspectj.util.FileUtil;
  27. public class ResourceCopyTests extends AjdeCoreTestCase {
  28. public static final String PROJECT_DIR = "bug-36071a";
  29. public static final String srcDir = PROJECT_DIR + "/src";
  30. public static final String binDir = "bin";
  31. public static final String injar1Name = "input1.jar";
  32. public static final String injar2Name = "input2.jar";
  33. public static final String outjarName = "/bin/output.jar";
  34. private TestMessageHandler handler;
  35. private TestCompilerConfiguration compilerConfig;
  36. private String[] config1 = new String[] { "src" + File.separator + "Main.java",
  37. "src" + File.separator + "testsrc" + File.separator + "TestProperties.java" };
  38. private String[] config2 = new String[] { "src" + File.separator + "aspects" + File.separator + "Logging.java" };
  39. protected void setUp() throws Exception {
  40. super.setUp();
  41. initialiseProject(PROJECT_DIR);
  42. handler = (TestMessageHandler) getCompiler().getMessageHandler();
  43. compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
  44. }
  45. protected void tearDown() throws Exception {
  46. super.tearDown();
  47. handler = null;
  48. compilerConfig = null;
  49. }
  50. public void testSrcToBin() {
  51. assertTrue("Expected there to be no compiler messages but found " + handler.getMessages(), handler.getMessages().isEmpty());
  52. compilerConfig.setProjectSourceFiles(getSourceFileList(config1));
  53. doBuild(true);
  54. compareDirs("src", "bin");
  55. }
  56. public void testInjarsToOutjar() {
  57. Set<File> injars = new HashSet<File>();
  58. File injar1 = openFile(injar1Name);
  59. injars.add(injar1);
  60. compilerConfig.setInpath(injars);
  61. File outjar = openFile(outjarName);
  62. compilerConfig.setOutjar(outjar.getAbsolutePath());
  63. compilerConfig.setProjectSourceFiles(getSourceFileList(config2));
  64. doBuild(true);
  65. assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
  66. compareJars(injar1, "src", outjar);
  67. }
  68. public void testDuplicateResources() {
  69. Set<File> injars = new HashSet<File>();
  70. File injar1 = openFile(injar1Name);
  71. File injar2 = openFile(injar2Name);
  72. injars.add(injar1);
  73. injars.add(injar2);
  74. compilerConfig.setInpath(injars);
  75. File outjar = openFile(outjarName);
  76. compilerConfig.setOutjar(outjar.getAbsolutePath());
  77. compilerConfig.setProjectSourceFiles(getSourceFileList(config2));
  78. doBuild(true);
  79. assertFalse("Expected compiler errors or warnings but didn't find any", handler.getMessages().isEmpty());
  80. List msgs = handler.getMessages();
  81. String exp = "duplicate resource: ";
  82. String found = ((TestMessageHandler.TestMessage) msgs.get(0)).getContainedMessage().getMessage();
  83. assertTrue("Expected message to start with 'duplicate resource:' but found" + " message " + found, found.startsWith(exp));
  84. compareJars(injar1, "src", outjar);
  85. }
  86. public void testSrcToOutjar() {
  87. File outjar = openFile(outjarName);
  88. compilerConfig.setOutjar(outjar.getAbsolutePath());
  89. compilerConfig.setProjectSourceFiles(getSourceFileList(config1));
  90. doBuild(true);
  91. assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
  92. compareSourceToOutjar("src", outjar);
  93. }
  94. public void testInjarsToBin() {
  95. Set<File> injars = new HashSet<File>();
  96. File injar1 = openFile(injar1Name);
  97. injars.add(injar1);
  98. compilerConfig.setInpath(injars);
  99. compilerConfig.setProjectSourceFiles(getSourceFileList(config2));
  100. doBuild(true);
  101. assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
  102. compareInjarsToBin(injar1, "src", "bin");
  103. }
  104. // BAH! keeps whinging about CVS extraneous resources
  105. // public void testInjarsToOddBin () {
  106. // Set injars = new HashSet();
  107. // File injar1 = openFile(injar1Name);
  108. // injars.add(injar1);
  109. // ideManager.getProjectProperties().setOutputPath("crazy.jar");
  110. // ideManager.getProjectProperties().setInJars(injars);
  111. // assertTrue("Build failed",doSynchronousBuild("config2.lst"));
  112. // assertTrue("Build warnings",ideManager.getCompilationSourceLineTasks().isEmpty());
  113. // compareInjarsToBin(injar1,"src","crazy.jar");
  114. // }
  115. public void testInjarsToOutjarOddNames() {
  116. Set<File> injars = new HashSet<File>();
  117. File injar1 = openFile("input1");
  118. File outjar = openFile(outjarName + ".fozout");
  119. injars.add(injar1);
  120. compilerConfig.setInpath(injars);
  121. compilerConfig.setOutjar(outjar.getAbsolutePath());
  122. compilerConfig.setProjectSourceFiles(getSourceFileList(config2));
  123. doBuild(true);
  124. assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
  125. compareJars(injar1, "src", outjar);
  126. }
  127. /*
  128. * Ensure bin contains all non-Java resouces from source and injars
  129. */
  130. public void compareDirs(String indirName, String outdirName) {
  131. File binBase = openFile(outdirName);
  132. File[] toResources = FileUtil.listFiles(binBase, aspectjResourceFileFilter);
  133. HashSet resources = new HashSet();
  134. listSourceResources(indirName, resources);
  135. for (int i = 0; i < toResources.length; i++) {
  136. String fileName = FileUtil.normalizedPath(toResources[i], binBase);
  137. boolean b = resources.remove(fileName);
  138. assertTrue("Extraneous resources: " + fileName, b);
  139. }
  140. assertTrue("Missing resources: " + resources.toString(), resources.isEmpty());
  141. }
  142. private void listSourceResources(String indirName, Set resources) {
  143. File srcBase = openFile(indirName);
  144. File[] fromResources = FileUtil.listFiles(srcBase, aspectjResourceFileFilter);
  145. for (int i = 0; i < fromResources.length; i++) {
  146. String name = FileUtil.normalizedPath(fromResources[i], srcBase);
  147. if (!name.startsWith("CVS/") && (-1 == name.indexOf("/CVS/")) && !name.endsWith("/CVS")) {
  148. resources.add(name);
  149. }
  150. }
  151. }
  152. public static final FileFilter aspectjResourceFileFilter = new FileFilter() {
  153. public boolean accept(File pathname) {
  154. String name = pathname.getName().toLowerCase();
  155. boolean isCVSRelated = name.indexOf("/cvs/") != -1;
  156. return (!isCVSRelated && !name.endsWith(".class") && !name.endsWith(".java") && !name.endsWith(".aj"));
  157. }
  158. };
  159. /*
  160. * Ensure -outjar contains all non-Java resouces from injars
  161. */
  162. public void compareJars(File injarFile, String indirName, File outjarFile) {
  163. HashSet resources = new HashSet();
  164. try {
  165. assertTrue(
  166. "outjar older than injar: outjarLastMod=" + outjarFile.lastModified() + " injarLastMod="
  167. + injarFile.lastModified(), (outjarFile.lastModified() >= injarFile.lastModified()));
  168. byte[] inManifest = listJarResources(injarFile, resources, true);
  169. listSourceResources(indirName, resources);
  170. ZipInputStream outjar = new ZipInputStream(new java.io.FileInputStream(outjarFile));
  171. ZipEntry entry;
  172. while (null != (entry = outjar.getNextEntry())) {
  173. String fileName = entry.getName();
  174. if (!fileName.endsWith(".class")) {
  175. /* Ensure we copied right JAR manifest */
  176. if (fileName.equalsIgnoreCase("meta-inf/Manifest.mf")) {
  177. byte[] outManifest = FileUtil.readAsByteArray(outjar);
  178. assertTrue("Wrong manifest has been copied", Arrays.equals(inManifest, outManifest));
  179. }
  180. boolean b = resources.remove(fileName);
  181. assertTrue(fileName, b);
  182. }
  183. outjar.closeEntry();
  184. }
  185. outjar.close();
  186. resources.remove("META-INF/");
  187. assertTrue(resources.toString(), resources.isEmpty());
  188. } catch (IOException ex) {
  189. fail(ex.toString());
  190. }
  191. }
  192. /*
  193. * Ensure -outjar conatins all non-Java resouces from source and injars
  194. */
  195. public void compareSourceToOutjar(String indirName, File outjarFile) {
  196. HashSet resources = new HashSet();
  197. listSourceResources(indirName, resources);
  198. try {
  199. ZipInputStream outjar = new JarInputStream(new java.io.FileInputStream(outjarFile));
  200. ZipEntry entry;
  201. while (null != (entry = outjar.getNextEntry())) {
  202. String fileName = entry.getName();
  203. if (!fileName.endsWith(".class")) {
  204. boolean b = resources.remove(fileName);
  205. assertTrue(fileName, b);
  206. }
  207. outjar.closeEntry();
  208. }
  209. outjar.close();
  210. assertTrue("Missing resources: " + resources.toString(), resources.isEmpty());
  211. } catch (IOException ex) {
  212. fail(ex.toString());
  213. }
  214. }
  215. /*
  216. * Ensure bin contains all non-Java resouces from source and injars
  217. */
  218. public void compareInjarsToBin(File injarFile, String indirName, String outdirName) {
  219. HashSet resources = new HashSet();
  220. try {
  221. byte[] inManifest = listJarResources(injarFile, resources, false);
  222. listSourceResources(indirName, resources);
  223. File binBase = openFile(outdirName);
  224. File[] toResources = FileUtil.listFiles(binBase, aspectjResourceFileFilter);
  225. for (int i = 0; i < toResources.length; i++) {
  226. String fileName = FileUtil.normalizedPath(toResources[i], binBase);
  227. /* Ensure we copied the right JAR manifest */
  228. if (fileName.equalsIgnoreCase("meta-inf/Manifest.mf")) {
  229. byte[] outManifest = FileUtil.readAsByteArray(toResources[i]);
  230. assertTrue("Wrong manifest has been copied", Arrays.equals(inManifest, outManifest));
  231. }
  232. boolean b = resources.remove(fileName);
  233. assertTrue("Extraneous resources: " + fileName, b);
  234. }
  235. assertTrue("Missing resources: " + resources.toString(), resources.isEmpty());
  236. } catch (IOException ex) {
  237. fail(ex.toString());
  238. }
  239. }
  240. /**
  241. * Look in the specified jar file for resources (anything not .class) and add it the resources Set.
  242. *
  243. * @param injarFile jar file to open up
  244. * @param resources the set where resources should be accumulated
  245. * @param wantDirectories should any directories found in the jar be included
  246. * @return the byte data for any discovered manifest
  247. */
  248. private byte[] listJarResources(File injarFile, Set resources, boolean wantDirectories) {
  249. byte[] manifest = null;
  250. try {
  251. ZipInputStream injar = new ZipInputStream(new java.io.FileInputStream(injarFile));
  252. ZipEntry entry;
  253. while (null != (entry = injar.getNextEntry())) {
  254. String fileName = entry.getName();
  255. if (entry.isDirectory()) {
  256. if (wantDirectories) {
  257. resources.add(fileName);
  258. }
  259. } else if (!fileName.endsWith(".class")) {
  260. /* JAR manifests shouldn't be copied */
  261. if (fileName.equalsIgnoreCase("meta-inf/Manifest.mf")) {
  262. manifest = FileUtil.readAsByteArray(injar);
  263. }
  264. resources.add(fileName);
  265. }
  266. injar.closeEntry();
  267. }
  268. injar.close();
  269. } catch (IOException ex) {
  270. fail(ex.toString());
  271. }
  272. return manifest;
  273. }
  274. }