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.

InpathTest.java 12KB

12 years ago
5 years ago
12 years ago
12 years ago
12 years ago
12 years ago
5 years ago
12 years ago
5 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
5 years ago
12 years ago
12 years ago
5 years ago
12 years ago
12 years ago
5 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* *******************************************************************
  2. * Copyright (c) 2003 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Mik Kersten initial implementation
  11. * Andy Clement Copied/changed for -inpath testing
  12. * Helen Hawkins Changed to use new ajde interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.ajde.core.tests;
  15. import java.io.File;
  16. import java.io.FileFilter;
  17. import java.io.FileInputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.util.HashSet;
  21. import java.util.Set;
  22. import java.util.jar.JarInputStream;
  23. import java.util.zip.ZipEntry;
  24. import org.aspectj.ajde.core.AjdeCoreTestCase;
  25. import org.aspectj.ajde.core.TestCompilerConfiguration;
  26. import org.aspectj.ajde.core.TestMessageHandler;
  27. import org.aspectj.util.FileUtil;
  28. public class InpathTest extends AjdeCoreTestCase {
  29. public static final FileFilter aspectjResourceFileFilter = new FileFilter() {
  30. @Override
  31. public boolean accept(File pathname) {
  32. String name = pathname.getName().toLowerCase();
  33. return (!name.endsWith(".class") && !name.endsWith(".java") && !name.endsWith(".aj"));
  34. }
  35. };
  36. public static final String indir1Name = "indir1";
  37. public static final String indir2Name = "indir2";
  38. public static final String injarName = "injar.jar";
  39. public static final String outjarName = "/bin/output.jar";
  40. private String[] build1 = new String[] { "src1" + File.separator + "Main.java" };
  41. private String[] build2 = new String[] { "src2" + File.separator + "Aspect.java" };
  42. private TestMessageHandler handler;
  43. private TestCompilerConfiguration compilerConfig;
  44. @Override
  45. protected void setUp() throws Exception {
  46. super.setUp();
  47. initialiseProject("InpathTest");
  48. handler = (TestMessageHandler) getCompiler().getMessageHandler();
  49. compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
  50. }
  51. @Override
  52. protected void tearDown() throws Exception {
  53. super.tearDown();
  54. handler = null;
  55. compilerConfig = null;
  56. }
  57. /**
  58. * Inputs to the compiler: inpath = 'indir1/' source = 'src' output = a jar file
  59. *
  60. * Expected result = output jar file contains contents of indir1 and class file for source that was in src
  61. */
  62. public void testInpathToOutjar() {
  63. Set<File> inpath = new HashSet<>();
  64. File indir1 = openFile(indir1Name);
  65. inpath.add(indir1);
  66. compilerConfig.setInpath(inpath);
  67. File outjar = openFile(outjarName);
  68. compilerConfig.setOutjar(outjar.getAbsolutePath());
  69. compilerConfig.setProjectSourceFiles(getSourceFileList(build1));
  70. doBuild(true);
  71. assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
  72. Set<String> expectedOutputJarContents = new HashSet<>();
  73. // From indir1
  74. // If we don't copy resources, these next three files won't make it.
  75. // expectedOutputJarContents.add("META-INF/MANIFEST.MF");
  76. // expectedOutputJarContents.add("META-INF/test.xml");
  77. // expectedOutputJarContents.add("test/test.props");
  78. expectedOutputJarContents.add("test/TestProperties.class");
  79. // From src
  80. expectedOutputJarContents.add("Main.class");
  81. compareJars(indir1, "src", outjar, expectedOutputJarContents);
  82. }
  83. /**
  84. * Similar to the first test but outputs to a directory rather than a jar.
  85. *
  86. */
  87. public void testInpathToBin() {
  88. Set<File> inpath = new HashSet<>();
  89. File indir1 = openFile(indir1Name);
  90. inpath.add(indir1);
  91. compilerConfig.setInpath(inpath);
  92. compilerConfig.setProjectSourceFiles(getSourceFileList(build1));
  93. doBuild(true);
  94. assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
  95. Set<String> expectedBindirContents = new HashSet<>();
  96. // From indir1
  97. // If we don't copy resources, these next three files won't make it
  98. // expectedBindirContents.add("META-INF/MANIFEST.MF");
  99. // expectedBindirContents.add("META-INF/test.xml");
  100. // expectedBindirContents.add("test/test.props");
  101. expectedBindirContents.add("test/TestProperties.class");
  102. // From src
  103. expectedBindirContents.add("Main.class");
  104. compareIndirToBin(indir1, "src", "bin", expectedBindirContents);
  105. }
  106. /**
  107. * Inputs to the compiler: inpath is 'indir2' that contains a helloworld source file and class file. source is 'src2' which
  108. * contains Aspect.java which weaves before advice into the HelloWorld code from 'indir2'
  109. *
  110. * Expected result: HelloWorld copied through to output jar and 'weaved'. Compiled version of Aspect.java put into the output
  111. * jar. The HelloWorld.java source file is also copied through to the output jar.
  112. *
  113. * An extra check is done at the end of this test to verify that HelloWorld has changed size (due to the weaving).
  114. */
  115. public void testInpathToOutjar2() {
  116. Set<File> inpath = new HashSet<>();
  117. File indir2 = openFile(indir2Name);
  118. inpath.add(indir2);
  119. compilerConfig.setInpath(inpath);
  120. File outjar = openFile(outjarName);
  121. compilerConfig.setOutjar(outjar.getAbsolutePath());
  122. compilerConfig.setProjectSourceFiles(getSourceFileList(build2));
  123. doBuild(true);
  124. assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
  125. Set<String> expectedOutputJarContents = new HashSet<>();
  126. // From indir1
  127. expectedOutputJarContents.add("example/HelloWorld.class");
  128. // If we don't copy resources, this file won't make it
  129. // expectedOutputJarContents.add("example/HelloWorld.java");
  130. // From src
  131. expectedOutputJarContents.add("Aspect.class");
  132. compareJars(indir2, "src", outjar, expectedOutputJarContents);
  133. // Extra test. The HelloWorld class from the input directory should have been woven
  134. // by the aspect - verify that the size of the HelloWorld class in the output directory
  135. // is a different size to the input version.
  136. int outputsize = fetchFromJar(outjar, "example/HelloWorld.class");
  137. try {
  138. FileInputStream fis = new FileInputStream(openFile(indir2Name + "/example/HelloWorld.class"));
  139. byte[] filedata = FileUtil.readAsByteArray(fis);
  140. int inputsize = filedata.length;
  141. assertTrue("Weaving of Aspect should have occurred but the input and output size for HelloWorld.class are the same",
  142. (inputsize != outputsize));
  143. } catch (Exception e) {
  144. e.printStackTrace();
  145. fail();
  146. }
  147. }
  148. /**
  149. * More complex inpath - a jar and a directory
  150. *
  151. * Inputs: -inpath injar.jar;indir2 source is 'src2' which contains Aspect.java
  152. *
  153. * Expected result: Result should be a directory containing the contents of injar.jar and indir2 and the Aspect.class file.
  154. *
  155. */
  156. public void testInpathAndInjarToBin() {
  157. Set<File> inpath = new HashSet<>();
  158. File indir2 = openFile(indir2Name);
  159. inpath.add(indir2);
  160. inpath.add(openFile(injarName));
  161. compilerConfig.setInpath(inpath);
  162. compilerConfig.setProjectSourceFiles(getSourceFileList(build2));
  163. doBuild(true);
  164. assertTrue("Expected no compiler errors or warnings but found " + handler.getMessages(), handler.getMessages().isEmpty());
  165. Set<String> expectedBindirContents = new HashSet<>();
  166. // From indir1
  167. expectedBindirContents.add("example/HelloWorld.class");
  168. // If we don't copy resources, this file won't make it
  169. // expectedBindirContents.add("example/HelloWorld.java");
  170. // From injar.jar
  171. expectedBindirContents.add("props/resources.properties");
  172. // From src
  173. expectedBindirContents.add("Aspect.class");
  174. compareIndirToBin(indir2, "src", "bin", expectedBindirContents);
  175. // Check the input and output versions of HelloWorld.class are different sizes
  176. try {
  177. FileInputStream fis1 = new FileInputStream(openFile("indir2/example/HelloWorld.class"));
  178. byte[] filedata1 = FileUtil.readAsByteArray(fis1);
  179. int inputsize = filedata1.length;
  180. FileInputStream fis2 = new FileInputStream(openFile("bin/example/HelloWorld.class"));
  181. byte[] filedata2 = FileUtil.readAsByteArray(fis2);
  182. int outputsize = filedata2.length;
  183. assertTrue("Weaving of Aspect should have occurred but the input and output size for HelloWorld.class are the same",
  184. (outputsize != inputsize));
  185. fis1.close();
  186. fis2.close();
  187. } catch (Exception e) {
  188. e.printStackTrace();
  189. fail();
  190. }
  191. }
  192. /*
  193. * Ensure -outjar contains all non-Java resouces from injars
  194. */
  195. public void compareJars(File dirFile, String sourceDir, File outjarFile, Set<String> expectedOutputJarContents) {
  196. try {
  197. assertTrue(
  198. "outjar older than injar: outjarLastMod=" + outjarFile.lastModified() + " injarLastMod="
  199. + dirFile.lastModified(), (outjarFile.lastModified() >= dirFile.lastModified()));
  200. // Go through the output jar file, for each element, remove it from
  201. // the expectedOutputJarContents - when we finish, the expectedOutputJarContents
  202. // set should be empty!
  203. JarInputStream outjar = new JarInputStream(new java.io.FileInputStream(outjarFile));
  204. ZipEntry entry;
  205. while (null != (entry = outjar.getNextEntry())) {
  206. String fileName = entry.getName();
  207. fileName = fileName.replace('\\', '/');
  208. if (!fileName.contains("CVS")) {
  209. boolean b = expectedOutputJarContents.remove(fileName);
  210. assertTrue("Unexpectedly found : " + fileName + " in outjar", b);
  211. }
  212. outjar.closeEntry();
  213. }
  214. outjar.close();
  215. assertTrue("Didnt make it into the output jar: " + expectedOutputJarContents.toString(),
  216. expectedOutputJarContents.isEmpty());
  217. } catch (IOException ex) {
  218. fail(ex.toString());
  219. }
  220. }
  221. /*
  222. * Ensure -outjar contains all non-Java resouces from source and injars
  223. */
  224. public void compareSourceToOutjar(String indirName, File outjarFile) {
  225. HashSet<String> resources = new HashSet<>();
  226. listSourceResources(indirName, resources);
  227. try {
  228. JarInputStream outjar = new JarInputStream(new java.io.FileInputStream(outjarFile));
  229. ZipEntry entry;
  230. while (null != (entry = outjar.getNextEntry())) {
  231. String fileName = entry.getName();
  232. if (!fileName.endsWith(".class")) {
  233. boolean b = resources.remove(fileName);
  234. assertTrue(fileName, b);
  235. }
  236. outjar.closeEntry();
  237. }
  238. outjar.close();
  239. assertTrue("Missing resources: " + resources.toString(), resources.isEmpty());
  240. } catch (IOException ex) {
  241. fail(ex.toString());
  242. }
  243. }
  244. /*
  245. * Ensure bin contains all non-Java resouces from source and injars
  246. */
  247. public void compareIndirToBin(File indirFile, String sourceDir, String outdirName, Set<String> expectedOutdirContents) {
  248. // byte[] inManifest = null;
  249. File binBase = openFile(outdirName);
  250. String[] toResources = FileUtil.listFiles(binBase);
  251. for (String fileName : toResources) {
  252. if (!fileName.contains("CVS")) {
  253. boolean b = expectedOutdirContents.remove(fileName);
  254. assertTrue("Extraneous resources: " + fileName, b);
  255. }
  256. }
  257. assertTrue("Missing resources: " + expectedOutdirContents.toString(), expectedOutdirContents.isEmpty());
  258. }
  259. private void listSourceResources(String indirName, Set<String> resources) {
  260. File srcBase = openFile(indirName);
  261. File[] fromResources = FileUtil.listFiles(srcBase, aspectjResourceFileFilter);
  262. for (File fromResource : fromResources) {
  263. String name = FileUtil.normalizedPath(fromResource, srcBase);
  264. // System.err.println("Checking "+name);
  265. if (!name.startsWith("CVS/") && (!name.contains("/CVS/")) && !name.endsWith("/CVS")) {
  266. resources.add(name);
  267. }
  268. }
  269. }
  270. // Return the size of specified entry from the output jar file
  271. public int fetchFromJar(File outjarFile, String filename) {
  272. int ret = -1;
  273. try {
  274. JarInputStream outjar;
  275. outjar = new JarInputStream(new java.io.FileInputStream(outjarFile));
  276. ZipEntry entry;
  277. while (null != (entry = outjar.getNextEntry())) {
  278. String zipentryname = entry.getName();
  279. if (zipentryname.equals(filename)) {
  280. byte[] filedata = FileUtil.readAsByteArray(outjar);
  281. ret = filedata.length;
  282. outjar.closeEntry();
  283. break;
  284. }
  285. outjar.closeEntry();
  286. }
  287. outjar.close();
  288. } catch (FileNotFoundException e) {
  289. e.printStackTrace();
  290. } catch (IOException e) {
  291. e.printStackTrace();
  292. }
  293. return ret;
  294. }
  295. }