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.

ShowWeaveMessagesTests.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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.BufferedReader;
  15. import java.io.File;
  16. import java.io.FileReader;
  17. import java.io.FileWriter;
  18. import java.util.ArrayList;
  19. import java.util.HashSet;
  20. import java.util.Hashtable;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.aspectj.ajde.core.AjdeCoreTestCase;
  26. import org.aspectj.ajde.core.JavaOptions;
  27. import org.aspectj.ajde.core.TestCompilerConfiguration;
  28. import org.aspectj.ajde.core.TestMessageHandler;
  29. import org.aspectj.bridge.IMessage;
  30. import org.aspectj.util.LangUtil;
  31. /**
  32. * Weaving messages are complicated things. There are multiple places where weaving takes place and the places vary depending on
  33. * whether we are doing a binary weave or going from source. All places that output weaving messages are tagged: // TAG:
  34. * WeavingMessage so you can easily find them!
  35. *
  36. * Advice is the simplest to deal with as that is advice weaving is always done in the weaver.
  37. *
  38. * Next is intertype declarations. These are also always done in the weaver but in the case of a binary weave we don't know the
  39. * originating source line for the ITD.
  40. *
  41. * Finally, declares. Declare Parents: extends Can only be done when going from source, if attempted by a binary weave then an error
  42. * message (compiler limitation) is produced. Declare Parents: implements Is (currently!) done at both compile time and weave time.
  43. * If going from source then the message is produced by the code in the compiler. if going from binary then the message is produced
  44. * by the weaver. Declare Soft: Comes out with 'advice' as a special kind of advice: softener advice
  45. *
  46. *
  47. * Q: Where are the messages turned on/off? A: It is a bit messy. See BuildArgParser.genBuildConfig(). Basically that method is the
  48. * first time we parse the option set. Whether weaving messages are on or off is stored in the build config. As soon as we have
  49. * parser the options and determined that weave messages are on, we grab the top level message handler and tell it not to ignore
  50. * WeaveInfo messages.
  51. *
  52. *
  53. * TODO - Other forms of declare? Do they need messages? e.g. declare precedence *
  54. */
  55. public class ShowWeaveMessagesTests extends AjdeCoreTestCase {
  56. private static boolean regenerate;
  57. private static boolean debugTests = false;
  58. static {
  59. // Switch this to true for a single iteration if you want to reconstruct the
  60. // 'expected weaving messages' files.
  61. regenerate = false;
  62. }
  63. public static final String PROJECT_DIR = "WeaveInfoMessagesTest";
  64. public static final String binDir = "bin";
  65. public static final String expectedResultsDir = "expected";
  66. public String[] one = new String[] { "AspectAdvice.aj", "Simple.java" };
  67. public String[] two = new String[] { "AspectITD.aj", "Simple.java" };
  68. public String[] three = new String[] { "AspectDeclare.aj", "Simple.java" };
  69. public String[] four = new String[] { "AspectDeclareExtends.aj", "Simple.java" };
  70. public String[] five = new String[] { "Simple.java", "AspectDeclareSoft.aj" };
  71. public String[] six = new String[] { "AspectDeclareAnnotations.aj" };
  72. public String[] seven = new String[] { "AspectDeclareAnnotations.aj" };
  73. public String[] empty = new String[] {};
  74. private TestMessageHandler handler;
  75. private TestCompilerConfiguration compilerConfig;
  76. protected void setUp() throws Exception {
  77. super.setUp();
  78. initialiseProject(PROJECT_DIR);
  79. handler = (TestMessageHandler) getCompiler().getMessageHandler();
  80. handler.dontIgnore(IMessage.WEAVEINFO);
  81. compilerConfig = (TestCompilerConfiguration) getCompiler().getCompilerConfiguration();
  82. compilerConfig.setNonStandardOptions("-showWeaveInfo");
  83. }
  84. protected void tearDown() throws Exception {
  85. super.tearDown();
  86. handler = null;
  87. compilerConfig = null;
  88. }
  89. /**
  90. * Weave all the possible kinds of advice and verify the messages that come out.
  91. */
  92. public void testWeaveMessagesAdvice() {
  93. if (debugTests)
  94. System.out.println("testWeaveMessagesAdvice: Building with One.lst");
  95. compilerConfig.setProjectSourceFiles(getSourceFileList(one));
  96. doBuild();
  97. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  98. verifyWeavingMessages("advice", true);
  99. }
  100. /**
  101. * Weave field and method ITDs and check the weave messages that come out.
  102. */
  103. public void testWeaveMessagesITD() {
  104. if (debugTests)
  105. System.out.println("\ntestWeaveMessagesITD: Building with Two.lst");
  106. compilerConfig.setProjectSourceFiles(getSourceFileList(two));
  107. doBuild();
  108. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  109. verifyWeavingMessages("itd", true);
  110. }
  111. /**
  112. * Weave "declare parents: implements" and check the weave messages that come out.
  113. */
  114. public void testWeaveMessagesDeclare() {
  115. if (debugTests)
  116. System.out.println("\ntestWeaveMessagesDeclare: Building with Three.lst");
  117. compilerConfig.setProjectSourceFiles(getSourceFileList(three));
  118. doBuild();
  119. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  120. verifyWeavingMessages("declare1", true);
  121. }
  122. /**
  123. * Weave "declare parents: extends" and check the weave messages that come out. Can't do equivalent binary test - as can't do
  124. * extends in binary.
  125. */
  126. public void testWeaveMessagesDeclareExtends() {
  127. if (debugTests)
  128. System.out.println("\ntestWeaveMessagesDeclareExtends: Building with Four.lst");
  129. compilerConfig.setProjectSourceFiles(getSourceFileList(four));
  130. doBuild();
  131. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  132. verifyWeavingMessages("declare.extends", true);
  133. }
  134. /**
  135. * Weave "declare soft: type: pointcut" and check the weave messages that come out.
  136. */
  137. public void testWeaveMessagesDeclareSoft() {
  138. if (debugTests)
  139. System.out.println("\ntestWeaveMessagesDeclareSoft: Building with Five.lst");
  140. compilerConfig.setProjectSourceFiles(getSourceFileList(five));
  141. doBuild();
  142. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  143. verifyWeavingMessages("declare.soft", true);
  144. }
  145. /**
  146. * Weave 'declare @type, @constructor, @method and @field' and check the weave messages that come out.
  147. */
  148. public void testWeaveMessagesDeclareAnnotation() {
  149. if (!LangUtil.is15VMOrGreater())
  150. return; // annotation classes won't be about pre 15
  151. if (debugTests)
  152. System.out.println("\ntestWeaveMessagesDeclareAnnotation: Building with Six.lst");
  153. compilerConfig.setProjectSourceFiles(getSourceFileList(six));
  154. setRunIn15Mode();
  155. compilerConfig.setNonStandardOptions("-showWeaveInfo -1.5");
  156. doBuild();
  157. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  158. verifyWeavingMessages("declare.annotation", true);
  159. }
  160. /**
  161. * Weave 'declare @type, @constructor, @method and @field' and check the weave messages don't come out without the
  162. * -showWeaveInfo arg.
  163. */
  164. public void testWeaveMessagesDeclareAnnotationWeaveInfoOff() {
  165. if (debugTests)
  166. System.out.println("\ntestWeaveMessagesDeclareAnnotation: Building with Seven.lst");
  167. compilerConfig.setProjectSourceFiles(getSourceFileList(seven));
  168. compilerConfig.setNonStandardOptions("");
  169. setRunIn15Mode();
  170. doBuild();
  171. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  172. verifyWeavingMessages("declare.annotationNoWeaveInfo", true);
  173. }
  174. // BINARY WEAVING TESTS
  175. /**
  176. * Binary weave variant of the advice weaving test above - to check messages are ok for binary weave. Unlike the source level
  177. * weave, in this test we are using an aspect on the aspectpath - which means it has already had its necessary parts woven - so
  178. * the list of weaving messages we expect is less.
  179. */
  180. public void testWeaveMessagesBinaryAdvice() {
  181. if (debugTests)
  182. System.out.println("\ntestWeaveMessagesBinaryAdvice: Simple.jar + AspectAdvice.jar");
  183. Set<File> inpath = new HashSet<File>();
  184. inpath.add(openFile("Simple.jar"));
  185. compilerConfig.setInpath(inpath);
  186. Set<File> aspectpath = new HashSet<File>();
  187. aspectpath.add(openFile("AspectAdvice.jar"));
  188. compilerConfig.setAspectPath(aspectpath);
  189. doBuild();
  190. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  191. verifyWeavingMessages("advice.binary", true);
  192. }
  193. public void testWeaveMessagesBinaryITD() {
  194. if (debugTests)
  195. System.out.println("\ntestWeaveMessagesBinaryITD: Simple.jar + AspectITD.jar");
  196. Set<File> inpath = new HashSet<File>();
  197. inpath.add(openFile("Simple.jar"));
  198. compilerConfig.setInpath(inpath);
  199. Set<File> aspectpath = new HashSet<File>();
  200. aspectpath.add(openFile("AspectITD.jar"));
  201. compilerConfig.setAspectPath(aspectpath);
  202. doBuild();
  203. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  204. verifyWeavingMessages("itd", false);
  205. }
  206. public void testWeaveMessagesBinaryDeclare() {
  207. if (debugTests)
  208. System.out.println("\ntestWeaveMessagesBinaryDeclare: Simple.jar + AspectDeclare.jar");
  209. Set<File> inpath = new HashSet<File>();
  210. inpath.add(openFile("Simple.jar"));
  211. compilerConfig.setInpath(inpath);
  212. Set<File> aspectpath = new HashSet<File>();
  213. aspectpath.add(openFile("AspectDeclare.jar"));
  214. compilerConfig.setAspectPath(aspectpath);
  215. doBuild();
  216. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  217. verifyWeavingMessages("declare1", false);
  218. }
  219. /**
  220. * Weave "declare soft: type: pointcut" and check the weave messages that come out.
  221. */
  222. public void testWeaveMessagesBinaryDeclareSoft() {
  223. if (debugTests)
  224. System.out.println("\ntestWeaveMessagesBinaryDeclareSoft: Simple.jar + AspectDeclareSoft.jar");
  225. Set<File> inpath = new HashSet<File>();
  226. inpath.add(openFile("Simple.jar"));
  227. compilerConfig.setInpath(inpath);
  228. Set<File> aspectpath = new HashSet<File>();
  229. aspectpath.add(openFile("AspectDeclareSoft.jar"));
  230. compilerConfig.setAspectPath(aspectpath);
  231. doBuild();
  232. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  233. verifyWeavingMessages("declare.soft.binary", true);
  234. }
  235. public void testWeaveMessagesBinaryAdviceInPackageFromJar() {
  236. if (debugTests)
  237. System.out.println("\ntestWeaveMessagesBinaryAdviceInPackageFromJar: Simple.jar + AspectInPackage.jar");
  238. Set<File> inpath = new HashSet<File>();
  239. inpath.add(openFile("Simple.jar"));
  240. compilerConfig.setInpath(inpath);
  241. Set<File> aspectpath = new HashSet<File>();
  242. aspectpath.add(openFile("AspectInPackage.jar"));
  243. compilerConfig.setAspectPath(aspectpath);
  244. doBuild();
  245. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  246. verifyWeavingMessages("advice.binary.package.jar", true);
  247. }
  248. public void testWeaveMessagesBinaryAdviceInPackage() {
  249. if (debugTests)
  250. System.out.println("\ntestWeaveMessagesBinaryAdviceInPackage: Simple.jar + AspectInPackage.jar");
  251. Set<File> inpath = new HashSet<File>();
  252. inpath.add(openFile("Simple.jar"));
  253. compilerConfig.setInpath(inpath);
  254. Set<File> aspectpath = new HashSet<File>();
  255. aspectpath.add(openFile("pkg"));
  256. compilerConfig.setAspectPath(aspectpath);
  257. doBuild();
  258. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  259. verifyWeavingMessages("advice.binary.package", true);
  260. }
  261. // BINARY WEAVING WHEN WE'VE LOST THE SOURCE POINTERS
  262. public void testWeaveMessagesBinaryAdviceNoDebugInfo() {
  263. if (debugTests)
  264. System.out.println("\ntestWeaveMessagesBinaryAdvice: Simple.jar + AspectAdvice.jar");
  265. Set<File> inpath = new HashSet<File>();
  266. inpath.add(openFile("Simple_nodebug.jar"));
  267. compilerConfig.setInpath(inpath);
  268. Set<File> aspectpath = new HashSet<File>();
  269. aspectpath.add(openFile("AspectAdvice_nodebug.jar"));
  270. compilerConfig.setAspectPath(aspectpath);
  271. doBuild();
  272. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  273. verifyWeavingMessages("advice.binary.nodebug", true);
  274. }
  275. public void testWeaveMessagesBinaryITDNoDebugInfo() {
  276. if (debugTests)
  277. System.out.println("\ntestWeaveMessagesBinaryITD: Simple.jar + AspectITD.jar");
  278. Set inpath = new HashSet();
  279. inpath.add(openFile("Simple_nodebug.jar"));
  280. compilerConfig.setInpath(inpath);
  281. Set aspectpath = new HashSet();
  282. aspectpath.add(openFile("AspectITD_nodebug.jar"));
  283. compilerConfig.setAspectPath(aspectpath);
  284. doBuild();
  285. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  286. verifyWeavingMessages("itd.nodebug", true);
  287. }
  288. public void testWeaveMessagesBinaryDeclareNoDebugInfo() {
  289. if (debugTests)
  290. System.out.println("\ntestWeaveMessagesBinaryDeclareNoDebugInfo: Simple.jar + AspectDeclare.jar");
  291. Set<File> inpath = new HashSet<File>();
  292. inpath.add(openFile("Simple_nodebug.jar"));
  293. compilerConfig.setInpath(inpath);
  294. Set<File> aspectpath = new HashSet<File>();
  295. aspectpath.add(openFile("AspectDeclare_nodebug.jar"));
  296. compilerConfig.setAspectPath(aspectpath);
  297. doBuild();
  298. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  299. verifyWeavingMessages("declare1.nodebug", true);
  300. }
  301. /**
  302. * Weave "declare soft: type: pointcut" and check the weave messages that come out.
  303. */
  304. public void testWeaveMessagesBinaryDeclareSoftNoDebugInfo() {
  305. if (debugTests)
  306. System.out.println("\ntestWeaveMessagesBinaryDeclareSoftNoDebugInfo: Simple.jar + AspectDeclareSoft.jar");
  307. Set<File> inpath = new HashSet<File>();
  308. inpath.add(openFile("Simple_nodebug.jar"));
  309. compilerConfig.setInpath(inpath);
  310. Set<File> aspectpath = new HashSet<File>();
  311. aspectpath.add(openFile("AspectDeclareSoft_nodebug.jar"));
  312. compilerConfig.setAspectPath(aspectpath);
  313. doBuild();
  314. assertTrue("Expected no compiler errors but found " + handler.getErrors(), handler.getErrors().isEmpty());
  315. verifyWeavingMessages("declare.soft.binary.nodebug", true);
  316. }
  317. public void verifyWeavingMessages(String testid, boolean source) {
  318. File expectedF = openFile(expectedResultsDir + File.separator + testid + ".txt");
  319. if (regenerate && source) {
  320. // Create the file
  321. saveWeaveMessages(expectedF);
  322. } else {
  323. // Verify the file matches what we have
  324. compareWeaveMessages(expectedF);
  325. }
  326. }
  327. /**
  328. * Compare weaving messages with what is in the file
  329. */
  330. private void compareWeaveMessages(File f) {
  331. List fileContents = new ArrayList();
  332. BufferedReader fr;
  333. try {
  334. // Load the file in
  335. fr = new BufferedReader(new FileReader(f));
  336. String line = null;
  337. while ((line = fr.readLine()) != null)
  338. fileContents.add(line);
  339. List originalFileContents = new ArrayList();
  340. originalFileContents.addAll(fileContents);
  341. // See if the messages match
  342. int msgCount = 0;
  343. List l = handler.getMessages();
  344. for (Iterator iter = l.iterator(); iter.hasNext();) {
  345. IMessage msg = ((TestMessageHandler.TestMessage) iter.next()).getContainedMessage();
  346. if (debugTests)
  347. System.out.println("Looking at [" + msg + "]");
  348. if (msg.getKind().equals(IMessage.WEAVEINFO)) {
  349. if (!fileContents.contains(msg.getMessage())) {
  350. fail("Could not find message '" + msg.getMessage() + "' in the expected results. Expected results are:\n"
  351. + stringify(originalFileContents));
  352. } else {
  353. fileContents.remove(msg.getMessage());
  354. }
  355. msgCount++;
  356. }
  357. }
  358. assertTrue("Didn't get these expected messages: " + fileContents, fileContents.size() == 0);
  359. if (debugTests)
  360. System.out.println("Successfully verified " + msgCount + " weaving messages");
  361. } catch (Exception e) {
  362. fail("Unexpected exception saving weaving messages:" + e);
  363. }
  364. }
  365. private String stringify(List l) {
  366. StringBuffer result = new StringBuffer();
  367. for (Iterator iter = l.iterator(); iter.hasNext();) {
  368. String str = (String) iter.next();
  369. result.append(str);
  370. result.append("\n");
  371. }
  372. return result.toString();
  373. }
  374. /**
  375. * Store the weaving messages in the specified file.
  376. */
  377. private void saveWeaveMessages(File f) {
  378. System.out.println("Saving weave messages into " + f.getName());
  379. FileWriter fw;
  380. try {
  381. fw = new FileWriter(f);
  382. List l = handler.getMessages();
  383. for (Iterator iter = l.iterator(); iter.hasNext();) {
  384. IMessage msg = ((TestMessageHandler.TestMessage) iter.next()).getContainedMessage();
  385. if (msg.getKind().equals(IMessage.WEAVEINFO)) {
  386. fw.write(msg.getMessage() + "\n");
  387. }
  388. }
  389. fw.close();
  390. } catch (Exception e) {
  391. fail("Unexpected exception saving weaving messages:" + e);
  392. }
  393. }
  394. private void setRunIn15Mode() {
  395. Map m = new Hashtable();
  396. m.put(JavaOptions.COMPLIANCE_LEVEL, JavaOptions.VERSION_15);
  397. m.put(JavaOptions.SOURCE_COMPATIBILITY_LEVEL, JavaOptions.VERSION_15);
  398. m.put(JavaOptions.TARGET_COMPATIBILITY_LEVEL, JavaOptions.VERSION_15);
  399. compilerConfig.setJavaOptions(m);
  400. }
  401. }