Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

MultiProjectIncrementalTests.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* *******************************************************************
  2. * Copyright (c) 2005 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://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.systemtest.incremental.tools;
  13. import java.io.DataOutputStream;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import org.aspectj.ajdt.internal.core.builder.AjState;
  21. import org.aspectj.ajdt.internal.core.builder.IncrementalStateManager;
  22. import org.aspectj.asm.AsmManager;
  23. import org.aspectj.asm.IProgramElement;
  24. import org.aspectj.bridge.IMessage;
  25. import org.aspectj.testing.util.FileUtil;
  26. /**
  27. * The superclass knows all about talking through Ajde to the compiler.
  28. * The superclass isn't in charge of knowing how to simulate overlays
  29. * for incremental builds, that is in here. As is the ability to
  30. * generate valid build configs based on a directory structure. To
  31. * support this we just need access to a sandbox directory - this
  32. * sandbox is managed by the superclass (it only assumes all builds occur
  33. * in <sandboxDir>/<projectName>/ )
  34. *
  35. * The idea is you can initialize multiple projects in the sandbox and
  36. * they can all be built independently, hopefully exploiting
  37. * incremental compilation. Between builds you can alter the contents
  38. * of a project using the alter() method that overlays some set of
  39. * new files onto the current set (adding new files/changing existing
  40. * ones) - you can then drive a new build and check it behaves as
  41. * expected.
  42. */
  43. public class MultiProjectIncrementalTests extends AjdeInteractionTestbed {
  44. private static boolean VERBOSE = false;
  45. protected void setUp() throws Exception {
  46. super.setUp();
  47. AjdeInteractionTestbed.VERBOSE = VERBOSE;
  48. }
  49. // Compile a single simple project
  50. public void testTheBasics() {
  51. initialiseProject("P1");
  52. build("P1"); // This first build will be batch
  53. build("P1");
  54. checkWasntFullBuild();
  55. checkCompileWeaveCount(0,0);
  56. }
  57. // Make simple changes to a project, adding a class
  58. public void testSimpleChanges() {
  59. initialiseProject("P1");
  60. build("P1"); // This first build will be batch
  61. alter("P1","inc1"); // adds a single class
  62. build("P1");
  63. checkCompileWeaveCount(1,-1);
  64. build("P1");
  65. checkCompileWeaveCount(0,-1);
  66. }
  67. // Make simple changes to a project, adding a class and an aspect
  68. public void testAddingAnAspect() {
  69. initialiseProject("P1");
  70. build("P1");
  71. alter("P1","inc1"); // adds a class
  72. alter("P1","inc2"); // adds an aspect
  73. build("P1");
  74. long timeTakenForFullBuildAndWeave = getTimeTakenForBuild();
  75. checkWasntFullBuild();
  76. checkCompileWeaveCount(2,3);
  77. build("P1");
  78. long timeTakenForSimpleIncBuild = getTimeTakenForBuild();
  79. // I don't think this test will have timing issues as the times should be *RADICALLY* different
  80. // On my config, first build time is 2093ms and the second is 30ms
  81. assertTrue("Should not take longer for the trivial incremental build! first="+timeTakenForFullBuildAndWeave+
  82. "ms second="+timeTakenForSimpleIncBuild+"ms",
  83. timeTakenForSimpleIncBuild<timeTakenForFullBuildAndWeave);
  84. }
  85. public void testBuildingTwoProjectsInTurns() {
  86. configureBuildStructureModel(true);
  87. initialiseProject("P1");
  88. initialiseProject("P2");
  89. build("P1");
  90. build("P2");
  91. build("P1");
  92. checkWasntFullBuild();
  93. build("P2");
  94. checkWasntFullBuild();
  95. }
  96. /**
  97. * In order for this next test to run, I had to move the weaver/world pair we keep in the
  98. * AjBuildManager instance down into the state object - this makes perfect sense - otherwise
  99. * when reusing the state for another project we'd not be switching to the right weaver/world
  100. * for that project.
  101. */
  102. public void testBuildingTwoProjectsMakingSmallChanges() {
  103. configureBuildStructureModel(true);
  104. initialiseProject("P1");
  105. initialiseProject("P2");
  106. build("P1");
  107. build("P2");
  108. build("P1");
  109. checkWasntFullBuild();
  110. build("P2");
  111. checkWasntFullBuild();
  112. alter("P1","inc1"); // adds a class
  113. alter("P1","inc2"); // adds an aspect
  114. build("P1");
  115. checkWasntFullBuild();
  116. }
  117. /**
  118. * Setup up two simple projects and build them in turn - check the
  119. * structure model is right after each build
  120. */
  121. public void testBuildingTwoProjectsAndVerifyingModel() {
  122. configureBuildStructureModel(true);
  123. initialiseProject("P1");
  124. initialiseProject("P2");
  125. build("P1");
  126. checkForNode("pkg","C",true);
  127. build("P2");
  128. checkForNode("pkg","C",false);
  129. build("P1");
  130. checkForNode("pkg","C",true);
  131. build("P2");
  132. checkForNode("pkg","C",false);
  133. }
  134. // Setup up two simple projects and build them in turn - check the
  135. // structure model is right after each build
  136. public void testBuildingTwoProjectsAndVerifyingStuff() {
  137. configureBuildStructureModel(true);
  138. initialiseProject("P1");
  139. initialiseProject("P2");
  140. build("P1");
  141. checkForNode("pkg","C",true);
  142. build("P2");
  143. checkForNode("pkg","C",false);
  144. build("P1");
  145. checkForNode("pkg","C",true);
  146. build("P2");
  147. checkForNode("pkg","C",false);
  148. }
  149. /**
  150. * Complex. Here we are testing that a state object records structural changes since
  151. * the last full build correctly. We build a simple project from scratch - this will
  152. * be a full build and so the structural changes since last build count should be 0.
  153. * We then alter a class, adding a new method and check structural changes is 1.
  154. */
  155. public void testStateManagement1() {
  156. File binDirectoryForP1 = new File(getFile("P1","bin"));
  157. initialiseProject("P1");
  158. build("P1"); // full build
  159. AjState ajs = IncrementalStateManager.findStateManagingOutputLocation(binDirectoryForP1);
  160. assertTrue("There should be a state object for project P1",ajs!=null);
  161. assertTrue("Should be no structural changes as it was a full build but found: "+
  162. ajs.getNumberOfStructuralChangesSinceLastFullBuild(),
  163. ajs.getNumberOfStructuralChangesSinceLastFullBuild()==0);
  164. alter("P1","inc3"); // adds a method to the class C.java
  165. build("P1");
  166. checkWasntFullBuild();
  167. ajs = IncrementalStateManager.findStateManagingOutputLocation(new File(getFile("P1","bin")));
  168. assertTrue("There should be state for project P1",ajs!=null);
  169. checkWasntFullBuild();
  170. assertTrue("Should be one structural changes as it was a full build but found: "+
  171. ajs.getNumberOfStructuralChangesSinceLastFullBuild(),
  172. ajs.getNumberOfStructuralChangesSinceLastFullBuild()==1);
  173. }
  174. /**
  175. * Complex. Here we are testing that a state object records structural changes since
  176. * the last full build correctly. We build a simple project from scratch - this will
  177. * be a full build and so the structural changes since last build count should be 0.
  178. * We then alter a class, changing body of a method, not the structure and
  179. * check struc changes is still 0.
  180. */
  181. public void testStateManagement2() {
  182. File binDirectoryForP1 = new File(getFile("P1","bin"));
  183. initialiseProject("P1");
  184. alter("P1","inc3"); // need this change in here so 'inc4' can be applied without making
  185. // it a structural change
  186. build("P1"); // full build
  187. AjState ajs = IncrementalStateManager.findStateManagingOutputLocation(binDirectoryForP1);
  188. assertTrue("There should be state for project P1",ajs!=null);
  189. assertTrue("Should be no struc changes as its a full build: "+
  190. ajs.getNumberOfStructuralChangesSinceLastFullBuild(),
  191. ajs.getNumberOfStructuralChangesSinceLastFullBuild()==0);
  192. alter("P1","inc4"); // changes body of main() method but does *not* change the structure of C.java
  193. build("P1");
  194. checkWasntFullBuild();
  195. ajs = IncrementalStateManager.findStateManagingOutputLocation(new File(getFile("P1","bin")));
  196. assertTrue("There should be state for project P1",ajs!=null);
  197. checkWasntFullBuild();
  198. assertTrue("Shouldn't be any structural changes but there were "+
  199. ajs.getNumberOfStructuralChangesSinceLastFullBuild(),
  200. ajs.getNumberOfStructuralChangesSinceLastFullBuild()==0);
  201. }
  202. /**
  203. * Now the most complex test. Create a dependancy between two projects. Building
  204. * one may affect whether the other does an incremental or full build. The
  205. * structural information recorded in the state object should be getting used
  206. * to control whether a full build is necessary...
  207. */
  208. public void testBuildingDependantProjects() {
  209. initialiseProject("P1");
  210. initialiseProject("P2");
  211. configureNewProjectDependency("P2","P1");
  212. build("P1");
  213. build("P2"); // now everything is consistent and compiled
  214. alter("P1","inc1"); // adds a second class
  215. build("P1");
  216. build("P2"); // although a second class was added - P2 can't be using it, so we don't full build here :)
  217. checkWasntFullBuild();
  218. alter("P1","inc3"); // structurally changes one of the classes
  219. build("P1");
  220. build("P2"); // build notices the structural change
  221. checkWasFullBuild();
  222. alter("P1","inc4");
  223. build("P1");
  224. build("P2"); // build sees a change but works out its not structural
  225. checkWasntFullBuild();
  226. }
  227. public void testPr85132() {
  228. initialiseProject("PR85132");
  229. build("PR85132");
  230. alter("PR85132","inc1");
  231. build("PR85132");
  232. }
  233. public void testPr92837() {
  234. initialiseProject("PR92837");
  235. build("PR92837");
  236. alter("PR92837","inc1");
  237. build("PR92837");
  238. }
  239. public void testPr119570() {
  240. initialiseProject("PR119570");
  241. build("PR119570");
  242. assertTrue("Should be no errors, but got "+MyTaskListManager.getErrorMessages(),MyTaskListManager.getErrorMessages().size()==0);
  243. }
  244. public void testPr119570_2() {
  245. initialiseProject("PR119570_2");
  246. build("PR119570_2");
  247. List l = MyTaskListManager.getWarningMessages();
  248. assertTrue("Should be no warnings, but got "+l,l.size()==0);
  249. }
  250. // If you fiddle with the compiler options - you must manually reset the options at the end of the test
  251. public void testPr117209() {
  252. try {
  253. initialiseProject("pr117209");
  254. configureNonStandardCompileOptions("-proceedOnError");
  255. build("pr117209");
  256. checkCompileWeaveCount(6,6);
  257. } finally {
  258. MyBuildOptionsAdapter.reset();
  259. }
  260. }
  261. public void testPr114875() {
  262. initialiseProject("pr114875");
  263. build("pr114875");
  264. alter("pr114875","inc1");
  265. build("pr114875");
  266. checkWasntFullBuild();
  267. alter("pr114875","inc2");
  268. build("pr114875");
  269. checkWasntFullBuild();
  270. }
  271. public void testPr117882() {
  272. // AjdeInteractionTestbed.VERBOSE=true;
  273. // AjdeInteractionTestbed.configureBuildStructureModel(true);
  274. initialiseProject("PR117882");
  275. build("PR117882");
  276. checkWasFullBuild();
  277. alter("PR117882","inc1");
  278. build("PR117882");
  279. checkWasntFullBuild();
  280. // AjdeInteractionTestbed.VERBOSE=false;
  281. // AjdeInteractionTestbed.configureBuildStructureModel(false);
  282. }
  283. public void testPr117882_2() {
  284. // AjdeInteractionTestbed.VERBOSE=true;
  285. // AjdeInteractionTestbed.configureBuildStructureModel(true);
  286. initialiseProject("PR117882_2");
  287. build("PR117882_2");
  288. checkWasFullBuild();
  289. alter("PR117882_2","inc1");
  290. build("PR117882_2");
  291. checkWasntFullBuild();
  292. checkCompileWeaveCount(1,4);
  293. //fullBuild("PR117882_2");
  294. //checkWasFullBuild();
  295. // AjdeInteractionTestbed.VERBOSE=false;
  296. // AjdeInteractionTestbed.configureBuildStructureModel(false);
  297. }
  298. public void testPr115251() {
  299. //AjdeInteractionTestbed.VERBOSE=true;
  300. initialiseProject("PR115251");
  301. build("PR115251");
  302. checkWasFullBuild();
  303. alter("PR115251","inc1");
  304. build("PR115251");
  305. checkWasntFullBuild();
  306. }
  307. /* public void testPr111779() {
  308. super.VERBOSE=true;
  309. initialiseProject("PR111779");
  310. build("PR111779");
  311. alter("PR111779","inc1");
  312. build("PR111779");
  313. }
  314. */
  315. public void testPr93310_1() {
  316. initialiseProject("PR93310_1");
  317. build("PR93310_1");
  318. checkWasFullBuild();
  319. String fileC2 = getWorkingDir().getAbsolutePath() + File.separatorChar + "PR93310_1" + File.separatorChar + "src" + File.separatorChar + "pack" + File.separatorChar + "C2.java";
  320. (new File(fileC2)).delete();
  321. alter("PR93310_1","inc1");
  322. build("PR93310_1");
  323. checkWasFullBuild();
  324. int l = AjdeInteractionTestbed.MyStateListener.detectedDeletions.size();
  325. assertTrue("Expected one deleted file to be noticed, but detected: "+l,l==1);
  326. String name = (String)AjdeInteractionTestbed.MyStateListener.detectedDeletions.get(0);
  327. assertTrue("Should end with C2.java but is "+name,name.endsWith("C2.java"));
  328. }
  329. public void testPr93310_2() {
  330. initialiseProject("PR93310_2");
  331. build("PR93310_2");
  332. checkWasFullBuild();
  333. String fileC2 = getWorkingDir().getAbsolutePath() + File.separatorChar + "PR93310_2" + File.separatorChar + "src" + File.separatorChar + "pack" + File.separatorChar + "C2.java";
  334. (new File(fileC2)).delete();
  335. alter("PR93310_2","inc1");
  336. build("PR93310_2");
  337. checkWasFullBuild();
  338. int l = AjdeInteractionTestbed.MyStateListener.detectedDeletions.size();
  339. assertTrue("Expected one deleted file to be noticed, but detected: "+l,l==1);
  340. String name = (String)AjdeInteractionTestbed.MyStateListener.detectedDeletions.get(0);
  341. assertTrue("Should end with C2.java but is "+name,name.endsWith("C2.java"));
  342. }
  343. // Stage1: Compile two files, pack.A and pack.A1 - A1 sets a protected field in A.
  344. // Stage2: make the field private in class A > gives compile error
  345. // Stage3: Add a new aspect whilst there is a compile error !
  346. public void testPr113531() {
  347. initialiseProject("PR113531");
  348. build("PR113531");
  349. assertFalse("build should have compiled ok",
  350. MyTaskListManager.hasErrorMessages());
  351. alter("PR113531","inc1");
  352. build("PR113531");
  353. assertEquals("error message should be 'foo cannot be resolved' ",
  354. "foo cannot be resolved",
  355. ((IMessage)MyTaskListManager.getErrorMessages().get(0))
  356. .getMessage());
  357. alter("PR113531","inc2");
  358. build("PR113531");
  359. assertTrue("There should be no exceptions handled:\n"+MyErrorHandler.getErrorMessages(),
  360. MyErrorHandler.getErrorMessages().isEmpty());
  361. assertEquals("error message should be 'foo cannot be resolved' ",
  362. "foo cannot be resolved",
  363. ((IMessage)MyTaskListManager.getErrorMessages().get(0))
  364. .getMessage());
  365. }
  366. public void testPr112736() {
  367. AjdeInteractionTestbed.VERBOSE = true;
  368. initialiseProject("PR112736");
  369. build("PR112736");
  370. checkWasFullBuild();
  371. String fileC2 = getWorkingDir().getAbsolutePath() + File.separatorChar + "PR112736" + File.separatorChar + "src" + File.separatorChar + "pack" + File.separatorChar + "A.java";
  372. (new File(fileC2)).delete();
  373. alter("PR112736","inc1");
  374. build("PR112736");
  375. checkWasFullBuild();
  376. }
  377. // other possible tests:
  378. // - memory usage (freemem calls?)
  379. // - relationship map
  380. // ---------------------------------------------------------------------------------------------------
  381. /**
  382. * Check we compiled/wove the right number of files, passing '-1' indicates you don't care about
  383. * that number.
  384. */
  385. private void checkCompileWeaveCount(int expCompile,int expWoven) {
  386. if (expCompile!=-1 && getCompiledFiles().size()!=expCompile)
  387. fail("Expected compilation of "+expCompile+" files but compiled "+getCompiledFiles().size()+
  388. "\n"+printCompiledAndWovenFiles());
  389. if (expWoven!=-1 && getWovenClasses().size()!=expWoven)
  390. fail("Expected weaving of "+expWoven+" files but wove "+getWovenClasses().size()+
  391. "\n"+printCompiledAndWovenFiles());
  392. }
  393. private void checkWasntFullBuild() {
  394. assertTrue("Shouldn't have been a full (batch) build",!wasFullBuild());
  395. }
  396. private void checkWasFullBuild() {
  397. assertTrue("Should have been a full (batch) build",wasFullBuild());
  398. }
  399. private void checkForNode(String packageName,String typeName,boolean shouldBeFound) {
  400. IProgramElement ipe = AsmManager.getDefault().getHierarchy().findElementForType(packageName,typeName);
  401. if (shouldBeFound) {
  402. if (ipe==null) printModel();
  403. assertTrue("Should have been able to find '"+packageName+"."+typeName+"' in the asm",ipe!=null);
  404. } else {
  405. if (ipe!=null) printModel();
  406. assertTrue("Should have NOT been able to find '"+packageName+"."+typeName+"' in the asm",ipe==null);
  407. }
  408. }
  409. private void printModel() {
  410. try {
  411. AsmManager.dumptree(AsmManager.getDefault().getHierarchy().getRoot(),0);
  412. } catch (IOException e) {
  413. e.printStackTrace();
  414. }
  415. }
  416. public void build(String projectName) {
  417. constructUpToDateLstFile(projectName,"build.lst");
  418. build(projectName,"build.lst");
  419. if (AjdeInteractionTestbed.VERBOSE) printBuildReport();
  420. }
  421. public void fullBuild(String projectName) {
  422. constructUpToDateLstFile(projectName,"build.lst");
  423. fullBuild(projectName,"build.lst");
  424. if (AjdeInteractionTestbed.VERBOSE) printBuildReport();
  425. }
  426. private void constructUpToDateLstFile(String pname,String configname) {
  427. File projectBase = new File(sandboxDir,pname);
  428. File toConstruct = new File(projectBase,configname);
  429. List filesForCompilation = new ArrayList();
  430. collectUpFiles(projectBase,projectBase,filesForCompilation);
  431. try {
  432. FileOutputStream fos = new FileOutputStream(toConstruct);
  433. DataOutputStream dos = new DataOutputStream(fos);
  434. for (Iterator iter = filesForCompilation.iterator(); iter.hasNext();) {
  435. String file = (String) iter.next();
  436. dos.writeBytes(file+"\n");
  437. }
  438. dos.close();
  439. } catch (IOException ioe) {
  440. ioe.printStackTrace();
  441. }
  442. }
  443. private void collectUpFiles(File location,File base,List collectionPoint) {
  444. String contents[] = location.list();
  445. if (contents==null) return;
  446. for (int i = 0; i < contents.length; i++) {
  447. String string = contents[i];
  448. File f = new File(location,string);
  449. if (f.isDirectory()) {
  450. collectUpFiles(f,base,collectionPoint);
  451. } else if (f.isFile() && (f.getName().endsWith(".aj") || f.getName().endsWith(".java"))) {
  452. String fileFound;
  453. try {
  454. fileFound = f.getCanonicalPath();
  455. String toRemove = base.getCanonicalPath();
  456. if (!fileFound.startsWith(toRemove)) throw new RuntimeException("eh? "+fileFound+" "+toRemove);
  457. collectionPoint.add(fileFound.substring(toRemove.length()+1));//+1 captures extra separator
  458. } catch (IOException e) {
  459. e.printStackTrace();
  460. }
  461. }
  462. }
  463. }
  464. /**
  465. * Fill in the working directory with the project base files,
  466. * from the 'base' folder.
  467. */
  468. private void initialiseProject(String p) {
  469. File projectSrc=new File(testdataSrcDir+File.separatorChar+p+File.separatorChar+"base");
  470. File destination=new File(getWorkingDir(),p);
  471. if (!destination.exists()) {destination.mkdir();}
  472. copy(projectSrc,destination);//,false);
  473. }
  474. /*
  475. * Applies an overlay onto the project being tested - copying
  476. * the contents of the specified overlay directory.
  477. */
  478. private void alter(String projectName,String overlayDirectory) {
  479. File projectSrc =new File(testdataSrcDir+File.separatorChar+projectName+
  480. File.separatorChar+overlayDirectory);
  481. File destination=new File(getWorkingDir(),projectName);
  482. copy(projectSrc,destination);
  483. }
  484. /**
  485. * Copy the contents of some directory to another location - the
  486. * copy is recursive.
  487. */
  488. private void copy(File from, File to) {
  489. String contents[] = from.list();
  490. if (contents==null) return;
  491. for (int i = 0; i < contents.length; i++) {
  492. String string = contents[i];
  493. File f = new File(from,string);
  494. File t = new File(to,string);
  495. if (f.isDirectory() && !f.getName().startsWith("inc")) {
  496. t.mkdir();
  497. copy(f,t);
  498. } else if (f.isFile()) {
  499. StringBuffer sb = new StringBuffer();
  500. //if (VERBOSE) System.err.println("Copying "+f+" to "+t);
  501. FileUtil.copyFile(f,t,sb);
  502. if (sb.length()!=0) { System.err.println(sb.toString());}
  503. }
  504. }
  505. }
  506. private static void log(String msg) {
  507. if (VERBOSE) System.out.println(msg);
  508. }
  509. }