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.

MultiProjectIncrementalTests.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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.testing.util.FileUtil;
  25. /**
  26. * The superclass knows all about talking through Ajde to the compiler.
  27. * The superclass isn't in charge of knowing how to simulate overlays
  28. * for incremental builds, that is in here. As is the ability to
  29. * generate valid build configs based on a directory structure. To
  30. * support this we just need access to a sandbox directory - this
  31. * sandbox is managed by the superclass (it only assumes all builds occur
  32. * in <sandboxDir>/<projectName>/ )
  33. *
  34. * The idea is you can initialize multiple projects in the sandbox and
  35. * they can all be built independently, hopefully exploiting
  36. * incremental compilation. Between builds you can alter the contents
  37. * of a project using the alter() method that overlays some set of
  38. * new files onto the current set (adding new files/changing existing
  39. * ones) - you can then drive a new build and check it behaves as
  40. * expected.
  41. */
  42. public class MultiProjectIncrementalTests extends AjdeInteractionTestbed {
  43. private static boolean VERBOSE = false;
  44. protected void setUp() throws Exception {
  45. super.setUp();
  46. }
  47. // Compile a single simple project
  48. public void testTheBasics() {
  49. initialiseProject("P1");
  50. build("P1"); // This first build will be batch
  51. build("P1");
  52. checkWasntFullBuild();
  53. checkCompileWeaveCount(0,0);
  54. }
  55. // Make simple changes to a project, adding a class
  56. public void testSimpleChanges() {
  57. initialiseProject("P1");
  58. build("P1"); // This first build will be batch
  59. alter("P1","inc1"); // adds a single class
  60. build("P1");
  61. checkCompileWeaveCount(1,-1);
  62. build("P1");
  63. checkCompileWeaveCount(0,-1);
  64. }
  65. // Make simple changes to a project, adding a class and an aspect
  66. public void testAddingAnAspect() {
  67. initialiseProject("P1");
  68. build("P1");
  69. alter("P1","inc1"); // adds a class
  70. alter("P1","inc2"); // adds an aspect
  71. build("P1");
  72. long timeTakenForFullBuildAndWeave = getTimeTakenForBuild();
  73. checkWasntFullBuild();
  74. checkCompileWeaveCount(2,3);
  75. build("P1");
  76. long timeTakenForSimpleIncBuild = getTimeTakenForBuild();
  77. // I don't think this test will have timing issues as the times should be *RADICALLY* different
  78. // On my config, first build time is 2093ms and the second is 30ms
  79. assertTrue("Should not take longer for the trivial incremental build! first="+timeTakenForFullBuildAndWeave+
  80. "ms second="+timeTakenForSimpleIncBuild+"ms",
  81. timeTakenForSimpleIncBuild<timeTakenForFullBuildAndWeave);
  82. }
  83. public void testBuildingTwoProjectsInTurns() {
  84. configureBuildStructureModel(true);
  85. initialiseProject("P1");
  86. initialiseProject("P2");
  87. build("P1");
  88. build("P2");
  89. build("P1");
  90. checkWasntFullBuild();
  91. build("P2");
  92. checkWasntFullBuild();
  93. }
  94. /**
  95. * In order for this next test to run, I had to move the weaver/world pair we keep in the
  96. * AjBuildManager instance down into the state object - this makes perfect sense - otherwise
  97. * when reusing the state for another project we'd not be switching to the right weaver/world
  98. * for that project.
  99. */
  100. public void testBuildingTwoProjectsMakingSmallChanges() {
  101. configureBuildStructureModel(true);
  102. initialiseProject("P1");
  103. initialiseProject("P2");
  104. build("P1");
  105. build("P2");
  106. build("P1");
  107. checkWasntFullBuild();
  108. build("P2");
  109. checkWasntFullBuild();
  110. alter("P1","inc1"); // adds a class
  111. alter("P1","inc2"); // adds an aspect
  112. build("P1");
  113. checkWasntFullBuild();
  114. }
  115. /**
  116. * Setup up two simple projects and build them in turn - check the
  117. * structure model is right after each build
  118. */
  119. public void testBuildingTwoProjectsAndVerifyingModel() {
  120. configureBuildStructureModel(true);
  121. initialiseProject("P1");
  122. initialiseProject("P2");
  123. build("P1");
  124. checkForNode("pkg","C",true);
  125. build("P2");
  126. checkForNode("pkg","C",false);
  127. build("P1");
  128. checkForNode("pkg","C",true);
  129. build("P2");
  130. checkForNode("pkg","C",false);
  131. }
  132. // Setup up two simple projects and build them in turn - check the
  133. // structure model is right after each build
  134. public void testBuildingTwoProjectsAndVerifyingStuff() {
  135. configureBuildStructureModel(true);
  136. initialiseProject("P1");
  137. initialiseProject("P2");
  138. build("P1");
  139. checkForNode("pkg","C",true);
  140. build("P2");
  141. checkForNode("pkg","C",false);
  142. build("P1");
  143. checkForNode("pkg","C",true);
  144. build("P2");
  145. checkForNode("pkg","C",false);
  146. }
  147. /**
  148. * Complex. Here we are testing that a state object records structural changes since
  149. * the last full build correctly. We build a simple project from scratch - this will
  150. * be a full build and so the structural changes since last build count should be 0.
  151. * We then alter a class, adding a new method and check structural changes is 1.
  152. */
  153. public void testStateManagement1() {
  154. File binDirectoryForP1 = new File(getFile("P1","bin"));
  155. initialiseProject("P1");
  156. build("P1"); // full build
  157. AjState ajs = IncrementalStateManager.findStateManagingOutputLocation(binDirectoryForP1);
  158. assertTrue("There should be a state object for project P1",ajs!=null);
  159. assertTrue("Should be no structural changes as it was a full build but found: "+
  160. ajs.getNumberOfStructuralChangesSinceLastFullBuild(),
  161. ajs.getNumberOfStructuralChangesSinceLastFullBuild()==0);
  162. alter("P1","inc3"); // adds a method to the class C.java
  163. build("P1");
  164. checkWasntFullBuild();
  165. ajs = IncrementalStateManager.findStateManagingOutputLocation(new File(getFile("P1","bin")));
  166. assertTrue("There should be state for project P1",ajs!=null);
  167. checkWasntFullBuild();
  168. assertTrue("Should be one structural changes as it was a full build but found: "+
  169. ajs.getNumberOfStructuralChangesSinceLastFullBuild(),
  170. ajs.getNumberOfStructuralChangesSinceLastFullBuild()==1);
  171. }
  172. /**
  173. * Complex. Here we are testing that a state object records structural changes since
  174. * the last full build correctly. We build a simple project from scratch - this will
  175. * be a full build and so the structural changes since last build count should be 0.
  176. * We then alter a class, changing body of a method, not the structure and
  177. * check struc changes is still 0.
  178. */
  179. public void testStateManagement2() {
  180. File binDirectoryForP1 = new File(getFile("P1","bin"));
  181. initialiseProject("P1");
  182. alter("P1","inc3"); // need this change in here so 'inc4' can be applied without making
  183. // it a structural change
  184. build("P1"); // full build
  185. AjState ajs = IncrementalStateManager.findStateManagingOutputLocation(binDirectoryForP1);
  186. assertTrue("There should be state for project P1",ajs!=null);
  187. assertTrue("Should be no struc changes as its a full build: "+
  188. ajs.getNumberOfStructuralChangesSinceLastFullBuild(),
  189. ajs.getNumberOfStructuralChangesSinceLastFullBuild()==0);
  190. alter("P1","inc4"); // changes body of main() method but does *not* change the structure of C.java
  191. build("P1");
  192. checkWasntFullBuild();
  193. ajs = IncrementalStateManager.findStateManagingOutputLocation(new File(getFile("P1","bin")));
  194. assertTrue("There should be state for project P1",ajs!=null);
  195. checkWasntFullBuild();
  196. assertTrue("Shouldn't be any structural changes but there were "+
  197. ajs.getNumberOfStructuralChangesSinceLastFullBuild(),
  198. ajs.getNumberOfStructuralChangesSinceLastFullBuild()==0);
  199. }
  200. /**
  201. * Now the most complex test. Create a dependancy between two projects. Building
  202. * one may affect whether the other does an incremental or full build. The
  203. * structural information recorded in the state object should be getting used
  204. * to control whether a full build is necessary...
  205. */
  206. public void testBuildingDependantProjects() {
  207. initialiseProject("P1");
  208. initialiseProject("P2");
  209. configureNewProjectDependency("P2","P1");
  210. build("P1");
  211. build("P2"); // now everything is consistent and compiled
  212. alter("P1","inc1"); // adds a second class
  213. build("P1");
  214. build("P2"); // although a second class was added - P2 can't be using it, so we don't full build here :)
  215. checkWasntFullBuild();
  216. alter("P1","inc3"); // structurally changes one of the classes
  217. build("P1");
  218. build("P2"); // build notices the structural change
  219. checkWasFullBuild();
  220. alter("P1","inc4");
  221. build("P1");
  222. build("P2"); // build sees a change but works out its not structural
  223. checkWasntFullBuild();
  224. }
  225. public void testPr85132() {
  226. initialiseProject("PR85132");
  227. build("PR85132");
  228. alter("PR85132","inc1");
  229. build("PR85132");
  230. }
  231. public void testPr92837() {
  232. initialiseProject("PR92837");
  233. build("PR92837");
  234. alter("PR92837","inc1");
  235. build("PR92837");
  236. }
  237. /* public void testPr111779() {
  238. super.VERBOSE=true;
  239. initialiseProject("PR111779");
  240. build("PR111779");
  241. alter("PR111779","inc1");
  242. build("PR111779");
  243. }
  244. */
  245. // other possible tests:
  246. // - memory usage (freemem calls?)
  247. // - relationship map
  248. // ---------------------------------------------------------------------------------------------------
  249. /**
  250. * Check we compiled/wove the right number of files, passing '-1' indicates you don't care about
  251. * that number.
  252. */
  253. private void checkCompileWeaveCount(int expCompile,int expWoven) {
  254. if (expCompile!=-1 && getCompiledFiles().size()!=expCompile)
  255. fail("Expected compilation of "+expCompile+" files but compiled "+getCompiledFiles().size()+
  256. "\n"+printCompiledAndWovenFiles());
  257. if (expWoven!=-1 && getWovenClasses().size()!=expWoven)
  258. fail("Expected weaving of "+expWoven+" files but wove "+getWovenClasses().size()+
  259. "\n"+printCompiledAndWovenFiles());
  260. }
  261. private void checkWasntFullBuild() {
  262. assertTrue("Shouldn't have been a full (batch) build",!wasFullBuild());
  263. }
  264. private void checkWasFullBuild() {
  265. assertTrue("Should have been a full (batch) build",wasFullBuild());
  266. }
  267. private void checkForNode(String packageName,String typeName,boolean shouldBeFound) {
  268. IProgramElement ipe = AsmManager.getDefault().getHierarchy().findElementForType(packageName,typeName);
  269. if (shouldBeFound) {
  270. if (ipe==null) printModel();
  271. assertTrue("Should have been able to find '"+packageName+"."+typeName+"' in the asm",ipe!=null);
  272. } else {
  273. if (ipe!=null) printModel();
  274. assertTrue("Should have NOT been able to find '"+packageName+"."+typeName+"' in the asm",ipe==null);
  275. }
  276. }
  277. private void printModel() {
  278. try {
  279. AsmManager.dumptree(AsmManager.getDefault().getHierarchy().getRoot(),0);
  280. } catch (IOException e) {
  281. e.printStackTrace();
  282. }
  283. }
  284. public void build(String projectName) {
  285. constructUpToDateLstFile(projectName,"build.lst");
  286. build(projectName,"build.lst");
  287. if (AjdeInteractionTestbed.VERBOSE) printBuildReport();
  288. }
  289. private void constructUpToDateLstFile(String pname,String configname) {
  290. File projectBase = new File(sandboxDir,pname);
  291. File toConstruct = new File(projectBase,configname);
  292. List filesForCompilation = new ArrayList();
  293. collectUpFiles(projectBase,projectBase,filesForCompilation);
  294. try {
  295. FileOutputStream fos = new FileOutputStream(toConstruct);
  296. DataOutputStream dos = new DataOutputStream(fos);
  297. for (Iterator iter = filesForCompilation.iterator(); iter.hasNext();) {
  298. String file = (String) iter.next();
  299. dos.writeBytes(file+"\n");
  300. }
  301. dos.close();
  302. } catch (IOException ioe) {
  303. ioe.printStackTrace();
  304. }
  305. }
  306. private void collectUpFiles(File location,File base,List collectionPoint) {
  307. String contents[] = location.list();
  308. if (contents==null) return;
  309. for (int i = 0; i < contents.length; i++) {
  310. String string = contents[i];
  311. File f = new File(location,string);
  312. if (f.isDirectory()) {
  313. collectUpFiles(f,base,collectionPoint);
  314. } else if (f.isFile() && (f.getName().endsWith(".aj") || f.getName().endsWith(".java"))) {
  315. String fileFound;
  316. try {
  317. fileFound = f.getCanonicalPath();
  318. String toRemove = base.getCanonicalPath();
  319. if (!fileFound.startsWith(toRemove)) throw new RuntimeException("eh? "+fileFound+" "+toRemove);
  320. collectionPoint.add(fileFound.substring(toRemove.length()+1));//+1 captures extra separator
  321. } catch (IOException e) {
  322. e.printStackTrace();
  323. }
  324. }
  325. }
  326. }
  327. /**
  328. * Fill in the working directory with the project base files,
  329. * from the 'base' folder.
  330. */
  331. private void initialiseProject(String p) {
  332. File projectSrc=new File(testdataSrcDir+File.separatorChar+p+File.separatorChar+"base");
  333. File destination=new File(getWorkingDir(),p);
  334. if (!destination.exists()) {destination.mkdir();}
  335. copy(projectSrc,destination);//,false);
  336. }
  337. /*
  338. * Applies an overlay onto the project being tested - copying
  339. * the contents of the specified overlay directory.
  340. */
  341. private void alter(String projectName,String overlayDirectory) {
  342. File projectSrc =new File(testdataSrcDir+File.separatorChar+projectName+
  343. File.separatorChar+overlayDirectory);
  344. File destination=new File(getWorkingDir(),projectName);
  345. copy(projectSrc,destination);
  346. }
  347. /**
  348. * Copy the contents of some directory to another location - the
  349. * copy is recursive.
  350. */
  351. private void copy(File from, File to) {
  352. String contents[] = from.list();
  353. if (contents==null) return;
  354. for (int i = 0; i < contents.length; i++) {
  355. String string = contents[i];
  356. File f = new File(from,string);
  357. File t = new File(to,string);
  358. if (f.isDirectory() && !f.getName().startsWith("inc")) {
  359. t.mkdir();
  360. copy(f,t);
  361. } else if (f.isFile()) {
  362. StringBuffer sb = new StringBuffer();
  363. //if (VERBOSE) System.err.println("Copying "+f+" to "+t);
  364. FileUtil.copyFile(f,t,sb);
  365. if (sb.length()!=0) { System.err.println(sb.toString());}
  366. }
  367. }
  368. }
  369. private static void log(String msg) {
  370. if (VERBOSE) System.out.println(msg);
  371. }
  372. }