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.

ReweavableTests.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* *******************************************************************
  2. * Copyright (c) 2004 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. * Andy Clement Initial version
  11. * Helen Hawkins Converted to new interface (bug 148190)
  12. * ******************************************************************/
  13. package org.aspectj.ajde.core.tests;
  14. import java.io.File;
  15. import java.util.ArrayList;
  16. import java.util.HashSet;
  17. import java.util.Set;
  18. import org.aspectj.ajde.core.AjdeCoreTestCase;
  19. import org.aspectj.ajde.core.TestCompilerConfiguration;
  20. import org.aspectj.ajde.core.TestMessageHandler;
  21. import org.aspectj.bridge.IMessage;
  22. public class ReweavableTests extends AjdeCoreTestCase {
  23. private static final boolean debugTests = false;
  24. public static final String binDir = "bin";
  25. public static final String indir1Name = "indir1";
  26. public static final String indir2Name = "indir2";
  27. public static final String injarName = "injar.jar";
  28. public static final String outjarName = "/bin/output.jar";
  29. private TestMessageHandler handler;
  30. private TestCompilerConfiguration compilerConfig;
  31. @Override
  32. protected void setUp() throws Exception {
  33. super.setUp();
  34. initialiseProject("ReweavableTest");
  35. handler = (TestMessageHandler) getCompiler().getMessageHandler();
  36. handler.dontIgnore(IMessage.INFO);
  37. compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
  38. }
  39. @Override
  40. protected void tearDown() throws Exception {
  41. super.tearDown();
  42. handler = null;
  43. compilerConfig = null;
  44. }
  45. /**
  46. * Aim: Check we haven't damaged 'normal compilation' when not supplying -Xreweavable. Also determines baseline sizes for the
  47. * compiled class files for later comparison.
  48. *
  49. * Inputs to the compiler: NonReweavable1.lst -> CalculatePI.java -> Logger.aj -> -verbose -> -noExit
  50. *
  51. * Expected result = Compile successful, the types will not be reweavable and the weaver should not report it is running in
  52. * reweavable mode.
  53. */
  54. public void testNonReweavableCompile() {
  55. if (debugTests)
  56. System.out.println("testNonReweavableCompile: Building with NonReweavable1.lst");
  57. String[] files = new String[] { "CalculatePI.java", "Logger.aj" };
  58. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  59. compilerConfig.setNonStandardOptions("-verbose -noExit -XnotReweavable");
  60. doBuild(true);
  61. assertFalse("Did not expect to find a message about the weaver operating " + "in reweavable mode",
  62. checkFor("weaver operating in reweavable mode"));
  63. File fCalc = openFile("bin/CalculatePI.class");
  64. File fLog = openFile("bin/Logger.class");
  65. assertTrue("bin/CalculatePI.class should exist?!?", fCalc.exists());
  66. assertTrue("bin/Logger.class should exist?!?", fLog.exists());
  67. if (debugTests)
  68. System.out.println("CalculatePI.class is of size: " + fCalc.length());
  69. if (debugTests)
  70. System.out.println("Logger.class is of size: " + fLog.length());
  71. if (debugTests)
  72. System.out.println("\n\n\n");
  73. /* nonreweavesize_CalculatePI = (int) */fCalc.length();
  74. /* nonreweavesize_Logger = (int) */fLog.length();
  75. }
  76. /**
  77. * Aim: Basic call to -Xreweavable. Weaver should report it is in reweavable mode and the classes produced should be much larger
  78. * than normal classes (those produced in the first test).
  79. *
  80. * Inputs to the compiler: Reweavable1.lst -> CalculatePI.java -> Logger.aj -> -Xreweavable -> -verbose -> -noExit
  81. *
  82. * Expected result = Compile successful, the types will be reweavable and the weaver should report it is running in reweavable
  83. * mode. The files produced should be larger than those created during the last test.
  84. */
  85. public void testReweavableCompile() {
  86. if (debugTests)
  87. System.out.println("testReweavableCompile: Building with Reweavable1.lst");
  88. String[] files = new String[] { "CalculatePI.java", "Logger.aj" };
  89. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  90. compilerConfig.setNonStandardOptions("-verbose -noExit");
  91. doBuild(true);
  92. assertTrue("Expected a message about operating in reweavable mode, but " + "didn't get one",
  93. checkFor("weaver operating in reweavable mode"));
  94. File fCalc = openFile("bin/CalculatePI.class");
  95. File fLog = openFile("bin/Logger.class");
  96. assertTrue("bin/CalculatePI.class should exist?!?", fCalc.exists());
  97. assertTrue("bin/Logger.class should exist?!?", fLog.exists());
  98. if (debugTests)
  99. System.out.println("CalculatePI.class is of size: " + fCalc.length());
  100. if (debugTests)
  101. System.out.println("Logger.class is of size: " + fLog.length());
  102. // Temporarily remove these tests - it seems the order in which the
  103. // testXXX methods are run cannot be relied upon
  104. // so reweavablesize_XXX fields might not have been set yet.
  105. // assertTrue("Reweavable version should be larger than non-reweavable
  106. // version of CalculatePI",
  107. // fCalc.length()>nonreweavesize_CalculatePI);
  108. // assertTrue("Reweavable version should be larger than non-reweavable
  109. // version of Logger",
  110. // fLog.length()>nonreweavesize_Logger);
  111. /* reweavablesize_CalculatePI = (int) */fCalc.length();
  112. /* reweavablesize_Logger = (int) */fLog.length();
  113. if (debugTests)
  114. System.out.println("\n\n\n");
  115. }
  116. /**
  117. * Aim: Use the optional ':compress' modifier on -Xreweavable. This causes some of the meta-data for use in reweaving to be
  118. * compressed. It should succeed and produce class files smaller than straight -Xreweavable but larger than without specifying
  119. * -Xreweavable.
  120. *
  121. * Inputs to the compiler: ReweavableCompress1.lst -> CalculatePI.java -> Logger.aj -> -Xreweavable:compress -> -verbose ->
  122. * -noExit
  123. *
  124. * Expected result = Compile successful, the types will be reweavable and the weaver should report it is running in reweavable
  125. * mode. The files created should have a size between the non-reweavable versions and the reweavable (without compression)
  126. * versions.
  127. */
  128. public void testReweavableCompressCompile() {
  129. if (debugTests)
  130. System.out.println("testReweavableCompressCompile: Building with ReweavableCompress1.lst");
  131. String[] files = new String[] { "CalculatePI.java", "Logger.aj" };
  132. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  133. compilerConfig.setNonStandardOptions("-Xreweavable:compress -verbose -noExit");
  134. doBuild(true);
  135. assertTrue("Expected a message about operating in reweavable mode, but didn't get one",
  136. checkFor("weaver operating in reweavable mode"));
  137. File fCalc = openFile("bin/CalculatePI.class");
  138. File fLog = openFile("bin/Logger.class");
  139. assertTrue("bin/CalculatePI.class should exist?!?", fCalc.exists());
  140. assertTrue("bin/Logger.class should exist?!?", fLog.exists());
  141. int calclen = (int) fCalc.length();
  142. int loglen = (int) fLog.length();
  143. if (debugTests)
  144. System.out.println("CalculatePI.class is of size: " + calclen);
  145. if (debugTests)
  146. System.out.println("Logger.class is of size: " + loglen);
  147. // Temporarily remove these tests - it seems the order in which the
  148. // testXXX methods are run cannot be relied upon
  149. // so reweavablesize_XXX fields might not have been set yet.
  150. // assertTrue("Reweavable version should be larger than non-reweavable
  151. // version of CalculatePI",
  152. // calclen>nonreweavesize_CalculatePI);
  153. // assertTrue("Reweavable version should be larger than non-reweavable
  154. // version of Logger",
  155. // loglen>nonreweavesize_Logger);
  156. // Temporarily remove these tests - it seems the order in which the
  157. // testXXX methods are run cannot be relied upon
  158. // so reweavablesize_XXX fields might not have been set yet.
  159. // assertTrue("Reweavable (with compression) version should be smaller
  160. // than reweavable (without compression) version of CalculatePI:" +
  161. // " Compressed version:"+calclen+"bytes Non-compressed
  162. // version:"+reweavablesize_CalculatePI+"bytes",
  163. // calclen<reweavablesize_CalculatePI);
  164. // assertTrue("Reweavable (with compression) version should be smaller
  165. // than reweavable (without compression) version of Logger"+
  166. // " Compressed version:"+loglen+"bytes Non-compressed
  167. // version:"+reweavablesize_Logger+"bytes",
  168. // loglen<reweavablesize_Logger);
  169. if (debugTests)
  170. System.out.println("\n\n\n");
  171. }
  172. /**
  173. * Aim: The tests above have determined that reweaving appears to be behaving in terms of the .class files it is creating. Now
  174. * lets actually attempt a reweave. For this, we build two files as reweavable and then build a single file whilst specifying an
  175. * inpath that contains the .class files from the first compile. This should succeed.
  176. *
  177. * Inputs to the first compile: Reweavable1.lst -> CalculatePI.java -> Logger.aj -> -Xreweavable -> -verbose -> -noExit
  178. *
  179. * Input to the second compile: Reweavable2.lst -> SecondAspect.aj -> -Xreweavable -> -verbose -> -noExit -inpath bin\.
  180. *
  181. * Expected result = Both compiles will succeed.
  182. */
  183. public void testReweavableSimpleCompile() {
  184. if (debugTests)
  185. System.out.println("testReweavableSimpleCompile: Building with Reweavable1.lst");
  186. String[] files = new String[] { "CalculatePI.java", "Logger.aj" };
  187. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  188. compilerConfig.setNonStandardOptions("-verbose -noExit");
  189. doBuild(true);
  190. assertTrue("Expected a message about operating in reweavable mode, but didn't get one",
  191. checkFor("weaver operating in reweavable mode"));
  192. if (debugTests)
  193. System.out.println("\ntestReweavableSimpleCompile: Building with Reweavable2.lst");
  194. Set<File> paths = new HashSet<>();
  195. paths.add(openFile(binDir));
  196. compilerConfig.setInpath(paths);
  197. String[] newFiles = new String[] { "SecondAspect.aj" };
  198. compilerConfig.setProjectSourceFiles(getSourceFileList(newFiles));
  199. doBuild(true);
  200. String expMessage = "successfully verified type Logger exists";
  201. assertTrue("Expected message '" + expMessage + "' but did not find it", checkFor(expMessage));
  202. File fCalc = openFile("bin/CalculatePI.class");
  203. File fLog = openFile("bin/Logger.class");
  204. File fSec = openFile("bin/SecondAspect.class");
  205. assertTrue("bin/CalculatePI.class should exist?!?", fCalc.exists());
  206. assertTrue("bin/Logger.class should exist?!?", fLog.exists());
  207. assertTrue("bin/SecondAspect.class should exist?!?", fSec.exists());
  208. if (debugTests)
  209. System.out.println("\n\n\n");
  210. }
  211. /**
  212. * Aim: Based on the test above, if we delete Logger.class between the first and second compiles the second compile should fail
  213. * because there is not enough information to reweave CalculatePI
  214. *
  215. * Inputs to the first compile: Reweavable1.lst -> CalculatePI.java -> Logger.aj -> -Xreweavable -> -verbose -> -noExit
  216. *
  217. * Input to the second compile: Reweavable2.lst -> SecondAspect.aj -> -Xreweavable -> -verbose -> -noExit -inpath bin\.
  218. *
  219. * Expected result = Second compile will fail - reporting that Logger is missing (it 'touched' in the first compile CalculatePI)
  220. */
  221. public void testForReweavableSimpleErrorCompile() {
  222. if (debugTests)
  223. System.out.println("testForReweavableSimpleErrorCompile: Building with Reweavable2.lst");
  224. String[] files = new String[] { "CalculatePI.java", "Logger.aj" };
  225. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  226. compilerConfig.setNonStandardOptions("-verbose -noExit");
  227. doBuild(true);
  228. assertTrue("Expected a message about operating in reweavable mode, but didn't get one",
  229. checkFor("weaver operating in reweavable mode"));
  230. assertTrue("Could not delete bin/Logger.class??", openFile("bin/Logger.class").delete());
  231. if (debugTests)
  232. System.out.println("\ntestForReweavableSimpleErrorCompile: Building with Reweavable2.lst");
  233. Set<File> paths = new HashSet<>();
  234. paths.add(openFile(binDir));
  235. compilerConfig.setInpath(paths);
  236. String[] newFiles = new String[] { "SecondAspect.aj" };
  237. compilerConfig.setProjectSourceFiles(getSourceFileList(newFiles));
  238. doBuild(true);
  239. String expMessage = "aspect Logger cannot be found when reweaving CalculatePI";
  240. assertTrue("Expected message '" + expMessage + "' but did not find it", checkFor(expMessage));
  241. File fCalc = openFile("bin/CalculatePI.class");
  242. File fLog = openFile("bin/Logger.class");
  243. File fSec = openFile("bin/SecondAspect.class");
  244. assertTrue("bin/CalculatePI.class should exist!", fCalc.exists());
  245. assertTrue("bin/Logger.class should not exist!", !fLog.exists());
  246. assertTrue("bin/SecondAspect.class should not exist!", fSec.exists());
  247. if (debugTests)
  248. System.out.println("\n\n\n");
  249. }
  250. /**
  251. * Aim: Based on the test above, if we delete Logger.class between the first and second compiles the second compile should fail
  252. * because there is not enough information to reweave CalculatePI
  253. *
  254. * Inputs to the first compile: TJP1.lst -> tjp/Demo.java -> tjp/GetInfo.java -> -Xreweavable -> -verbose -> -noExit
  255. *
  256. * Now, delete bin\tjp\GetInfo.class and do a compile with: TJP2.lst -> -Xreweavable -> -verbose -> -noExit -inpath bin\.
  257. *
  258. * Expected result = Second compile will fail - reporting that tjp.GetInfo is missing (it 'touched' in the first compile
  259. * tjp.Demo)
  260. */
  261. public void testErrorScenario2Compile() {
  262. if (debugTests)
  263. System.out.println("testErrorScenario2: Building with TJP1.lst");
  264. String[] files = new String[] { "tjp" + File.separator + "Demo.java", "tjp" + File.separator + "GetInfo.java" };
  265. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  266. compilerConfig.setNonStandardOptions("-verbose -noExit");
  267. doBuild(true);
  268. assertTrue("Expected a message about operating in reweavable mode, but didn't get one",
  269. checkFor("weaver operating in reweavable mode"));
  270. assertTrue("Could not delete bin/tjp/GetInfo.class??", openFile("bin/tjp/GetInfo.class").delete());
  271. if (debugTests)
  272. System.out.println("\ntestErrorScenario2: Building with TJP2.lst");
  273. Set<File> paths = new HashSet<>();
  274. paths.add(openFile(binDir));
  275. compilerConfig.setInpath(paths);
  276. compilerConfig.setProjectSourceFiles(new ArrayList<String>());
  277. doBuild(true);
  278. String expMessage = "aspect tjp.GetInfo cannot be found when reweaving tjp.Demo";
  279. assertTrue("Expected message '" + expMessage + "' but did not find it", checkFor(expMessage));
  280. File fDemo = openFile("bin/tjp/Demo.class");
  281. File fGetInfo = openFile("bin/tjp/GetInfo.class");
  282. assertTrue("bin/tjp/Demo.class should exist!", fDemo.exists());
  283. assertTrue("bin/tjp/GetInfo.class should not exist!", !fGetInfo.exists());
  284. if (debugTests)
  285. System.out.println("\n\n\n");
  286. }
  287. public void testWorkingScenario2Compile() {
  288. if (debugTests)
  289. System.out.println("testWorkingScenario2: Building with TJP1.lst");
  290. String[] files = new String[] { "tjp" + File.separator + "Demo.java", "tjp" + File.separator + "GetInfo.java" };
  291. compilerConfig.setProjectSourceFiles(getSourceFileList(files));
  292. compilerConfig.setNonStandardOptions("-Xreweavable:compress -verbose -noExit");
  293. doBuild(true);
  294. assertTrue("Expected a message about operating in reweavable mode, but didn't get one",
  295. checkFor("weaver operating in reweavable mode"));
  296. if (debugTests)
  297. System.out.println("\ntestWorkingScenario2: Building with TJP2.lst");
  298. Set<File> paths = new HashSet<>();
  299. paths.add(openFile(binDir));
  300. compilerConfig.setInpath(paths);
  301. compilerConfig.setProjectSourceFiles(new ArrayList<String>());
  302. doBuild(true);
  303. String expMessage = "successfully verified type tjp.GetInfo exists";
  304. assertTrue("Expected message '" + expMessage + "' but did not find it", checkFor(expMessage));
  305. File fGetInfo = openFile("bin/tjp/GetInfo.class");
  306. File fDemo = openFile("bin/tjp/Demo.class");
  307. assertTrue("bin/tjp/GetInfo.class should exist!", fGetInfo.exists());
  308. assertTrue("bin/tjp/Demo.class should not exist!", fDemo.exists());
  309. if (debugTests)
  310. System.out.println("\n\n\n");
  311. }
  312. }