選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

InpathTests.java 12KB

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