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.

ResourceCopyTest.java 11KB

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