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.

AjdeInteractionTestbed.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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.File;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Hashtable;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import junit.framework.AssertionFailedError;
  22. import junit.framework.TestCase;
  23. import org.aspectj.ajde.Ajde;
  24. import org.aspectj.ajde.BuildOptionsAdapter;
  25. import org.aspectj.ajde.BuildProgressMonitor;
  26. import org.aspectj.ajde.ErrorHandler;
  27. import org.aspectj.ajde.ProjectPropertiesAdapter;
  28. import org.aspectj.ajde.TaskListManager;
  29. import org.aspectj.ajdt.internal.core.builder.AjState;
  30. import org.aspectj.ajdt.internal.core.builder.IStateListener;
  31. import org.aspectj.ajdt.internal.core.builder.IncrementalStateManager;
  32. import org.aspectj.asm.AsmManager;
  33. import org.aspectj.bridge.IMessage;
  34. import org.aspectj.bridge.ISourceLocation;
  35. import org.aspectj.bridge.IMessage.Kind;
  36. /**
  37. * This class uses Ajde in the same way that an IDE (e.g. AJDT) does.
  38. *
  39. * The build is driven through 'build(projectName,configFile)' but the
  40. * build can be configured by the methods beginning 'configure***'.
  41. * Information about what happened during a build is accessible
  42. * through the get*, was*, print* public methods...
  43. *
  44. * There are many methods across the multiple listeners that communicate
  45. * info with Ajde - not all are implemented. Those that are are
  46. * task tagged DOESSOMETHING :)
  47. */
  48. public class AjdeInteractionTestbed extends TestCase {
  49. public static boolean VERBOSE = false; // do you want the gory details?
  50. public final static String testdataSrcDir = "../tests/multiIncremental";
  51. protected static File sandboxDir;
  52. private static final String SANDBOX_NAME = "ajcSandbox";
  53. private static boolean buildModel;
  54. // Methods for configuring the build
  55. public static void configureBuildStructureModel(boolean b) { buildModel = b;}
  56. public static void configureNewProjectDependency(String fromProject, String projectItDependsOn) {
  57. MyProjectPropertiesAdapter.addDependancy(fromProject,projectItDependsOn);
  58. }
  59. // End of methods for configuring the build
  60. protected File getWorkingDir() { return sandboxDir; }
  61. protected void setUp() throws Exception {
  62. super.setUp();
  63. // Create a sandbox in which to work
  64. createEmptySandbox();
  65. }
  66. /** Drives a build */
  67. public boolean build(String projectName,String configFile) {
  68. return AjdeManager.build(projectName,configFile);
  69. }
  70. /** Looks after communicating with the singleton Ajde instance */
  71. public static class AjdeManager {
  72. static {
  73. Ajde.init(null,
  74. MyTaskListManager.getInstance(),
  75. MyBuildProgressMonitor.getInstance(),
  76. MyProjectPropertiesAdapter.getInstance(),
  77. MyBuildOptionsAdapter.getInstance(),
  78. null,null,
  79. MyErrorHandler.getInstance());
  80. MyStateListener sl = MyStateListener.getInstance();
  81. AjState.stateListener = sl;
  82. }
  83. /**
  84. * Builds a specified project using a specified config file. Subsequent
  85. * calls to build the same project should result in incremental builds.
  86. */
  87. private static boolean build(String projectName,String configFile) {
  88. pause(1000); // delay to allow previous runs build stamps to be OK
  89. lognoln("Building project '"+projectName+"'");
  90. // Ajde.getDefault().enableLogging(System.out);
  91. //Ajde.getDefault().getBuildManager().setReportInfoMessages(true);
  92. // Configure the necessary providers and listeners for this compile
  93. MyBuildProgressMonitor.reset();
  94. MyTaskListManager.reset();
  95. MyStateListener.reset();
  96. MyProjectPropertiesAdapter.setActiveProject(projectName);
  97. AsmManager.attemptIncrementalModelRepairs=true;
  98. IncrementalStateManager.recordIncrementalStates=true;
  99. Ajde.getDefault().getBuildManager().setBuildModelMode(buildModel);
  100. // Do the compile
  101. Ajde.getDefault().getBuildManager().build(getFile(projectName,configFile));
  102. // Wait for it to complete
  103. while (!MyBuildProgressMonitor.hasFinished()) {
  104. lognoln(".");
  105. pause(100);
  106. }
  107. log("");
  108. // What happened?
  109. if (MyTaskListManager.hasErrorMessages()) {
  110. System.err.println("Build errors:");
  111. for (Iterator iter = MyTaskListManager.getErrorMessages().iterator(); iter.hasNext();) {
  112. IMessage element = (IMessage) iter.next();
  113. System.err.println(element);
  114. }
  115. System.err.println("---------");
  116. }
  117. log("Build finished, time taken = "+MyBuildProgressMonitor.getTimeTaken()+"ms");
  118. return true;
  119. }
  120. private static void pause(int millis) {
  121. try {
  122. Thread.sleep(millis);
  123. } catch (InterruptedException ie) {}
  124. }
  125. // public static boolean lastCompileDefaultedToBatch() {
  126. // return MyTaskListManager.defaultedToBatch();
  127. // }
  128. }
  129. // Methods for querying what happened during a build and accessing information
  130. // about the build:
  131. /**
  132. * Helper method for dumping info about which files were compiled and
  133. * woven during the last build.
  134. */
  135. public String printCompiledAndWovenFiles() {
  136. StringBuffer sb = new StringBuffer();
  137. if (getCompiledFiles().size()==0 && getWovenClasses().size()==0)
  138. sb.append("No files were compiled or woven\n");
  139. for (Iterator iter = getCompiledFiles().iterator(); iter.hasNext();) {
  140. Object element = (Object) iter.next();
  141. sb.append("compiled: "+element+"\n");
  142. }
  143. for (Iterator iter = getWovenClasses().iterator(); iter.hasNext();) {
  144. Object element = (Object) iter.next();
  145. sb.append("woven: "+element+"\n");
  146. }
  147. return sb.toString();
  148. }
  149. /**
  150. * Summary report on what happened in the most recent build
  151. */
  152. public void printBuildReport() {
  153. System.out.println("\n============== BUILD REPORT =================");
  154. System.out.println("Build took: "+getTimeTakenForBuild()+"ms");
  155. List compiled=getCompiledFiles();
  156. System.out.println("Compiled: "+compiled.size()+" files");
  157. for (Iterator iter = compiled.iterator(); iter.hasNext();) {
  158. System.out.println(" :"+iter.next());
  159. }
  160. List woven=getWovenClasses();
  161. System.out.println("Wove: "+woven.size()+" files");
  162. for (Iterator iter = woven.iterator(); iter.hasNext();) {
  163. System.out.println(" :"+iter.next());
  164. }
  165. if (wasFullBuild()) System.out.println("It was a batch (full) build");
  166. System.out.println("=============================================");
  167. }
  168. public boolean wasFullBuild() {
  169. return MyStateListener.wasFullBuild();
  170. }
  171. public long getTimeTakenForBuild() {
  172. return MyBuildProgressMonitor.getTimeTaken();
  173. }
  174. public List getCompiledFiles() {
  175. return MyBuildProgressMonitor.getCompiledFiles();
  176. }
  177. public List getWovenClasses() {
  178. return MyBuildProgressMonitor.getWovenClasses();
  179. }
  180. // Infrastructure below here
  181. private void createEmptySandbox() {
  182. String os = System.getProperty("os.name");
  183. File tempDir = null;
  184. // AMC - I did this rather than use the JDK default as I hate having to go look
  185. // in c:\documents and settings\......... for the results of a failed test.
  186. if (os.startsWith("Windows")) {
  187. //Alex: try D first since NTFS on mine while FAT leads to failure..
  188. tempDir = new File("D:\\temp");
  189. if (!tempDir.exists()) {
  190. tempDir = new File("C:\\temp");
  191. if (!tempDir.exists()) {
  192. tempDir.mkdir();
  193. }
  194. }
  195. } else {
  196. tempDir = new File("/tmp");
  197. }
  198. File sandboxRoot = new File(tempDir,SANDBOX_NAME);
  199. if (!sandboxRoot.exists()) {
  200. sandboxRoot.mkdir();
  201. }
  202. org.aspectj.util.FileUtil.deleteContents(sandboxRoot);
  203. try {
  204. sandboxDir = File.createTempFile("ajcTest",".tmp",sandboxRoot);
  205. sandboxDir.delete();
  206. sandboxDir.mkdir();
  207. } catch (IOException ioEx) {
  208. throw new AssertionFailedError("Unable to create sandbox directory for test");
  209. }
  210. }
  211. private static void log(String msg) {
  212. if (VERBOSE) System.out.println(msg);
  213. }
  214. private static void lognoln(String msg) {
  215. if (VERBOSE) System.out.print(msg);
  216. }
  217. /** Return the *full* path to this file which is taken relative to the project dir*/
  218. protected static String getFile(String projectName, String path) {
  219. return new File(sandboxDir,projectName+File.separatorChar + path).getAbsolutePath();
  220. }
  221. // Helper classes that communicate with Ajde
  222. static class MyErrorHandler implements ErrorHandler {
  223. static MyErrorHandler _instance = new MyErrorHandler();
  224. private MyErrorHandler() {}
  225. public static ErrorHandler getInstance() {
  226. return _instance;
  227. }
  228. public void handleWarning(String message) {
  229. log("ErrorHandler.handleWarning("+message+")");
  230. }
  231. public void handleError(String message) {
  232. log("ErrorHandler.handleWarning("+message+")");
  233. }
  234. public void handleError(String message, Throwable t) {
  235. log("ErrorHandler.handleError("+message+","+t+")");
  236. if (VERBOSE) t.printStackTrace();
  237. }
  238. }
  239. // -----------------
  240. static class MyProjectPropertiesAdapter implements ProjectPropertiesAdapter {
  241. private final static boolean VERBOSE = false;
  242. static MyProjectPropertiesAdapter _instance = new MyProjectPropertiesAdapter();
  243. private MyProjectPropertiesAdapter() {}
  244. public static ProjectPropertiesAdapter getInstance() {
  245. return _instance;
  246. }
  247. private String projectName = null;
  248. public static void setActiveProject(String n) {
  249. _instance.projectName = n;
  250. }
  251. private static Hashtable dependants = new Hashtable();
  252. public static void addDependancy(String project, String projectItDependsOn) {
  253. List l = (List)dependants.get(project);
  254. if (l == null) {
  255. List ps = new ArrayList();
  256. ps.add(projectItDependsOn);
  257. dependants.put(project,ps);
  258. } else {
  259. l.add(projectItDependsOn);
  260. }
  261. }
  262. // interface impl below
  263. // DOESSOMETHING
  264. public String getProjectName() {
  265. log("MyProjectProperties.getProjectName() [returning "+projectName+"]");
  266. return projectName;
  267. }
  268. // DOESSOMETHING
  269. public String getRootProjectDir() {
  270. String dir = testdataSrcDir+File.separatorChar+projectName;
  271. log("MyProjectProperties.getRootProjectDir() [returning "+dir+"]");
  272. return dir;
  273. }
  274. public List getBuildConfigFiles() {
  275. log("MyProjectProperties.getBuildConfigFiles()");
  276. return null;
  277. }
  278. public String getDefaultBuildConfigFile() {
  279. log("MyProjectProperties.getDefaultBuildConfigFile()");
  280. return null;
  281. }
  282. public String getLastActiveBuildConfigFile() {
  283. log("MyProjectProperties.getLastActiveBuildConfigFile()");
  284. return null;
  285. }
  286. public List getProjectSourceFiles() {
  287. log("MyProjectProperties.getProjectSourceFiles()");
  288. return null;
  289. }
  290. public String getProjectSourcePath() {
  291. log("MyProjectProperties.getProjectSourcePath()");
  292. return null;
  293. }
  294. // DOESSOMETHING
  295. public String getClasspath() {
  296. log("MyProjectProperties.getClasspath()");
  297. String cp =
  298. new File(testdataSrcDir) + File.pathSeparator +
  299. System.getProperty("sun.boot.class.path") +
  300. File.pathSeparator + "../runtime/bin" +
  301. File.pathSeparator + System.getProperty("aspectjrt.path") +
  302. File.pathSeparator+".."+File.separator+"lib" + File.separator+"test"+File.separator+"aspectjrt.jar";
  303. // look at dependant projects
  304. List projects = (List)dependants.get(projectName);
  305. if (projects!=null) {
  306. for (Iterator iter = projects.iterator(); iter.hasNext();) {
  307. cp = getFile((String)iter.next(),"bin")+File.pathSeparator+cp;
  308. }
  309. }
  310. //System.err.println("For project "+projectName+" getClasspath() returning "+cp);
  311. return cp;
  312. }
  313. public String getOutputPath() {
  314. String dir = getFile(projectName,"bin");
  315. log("MyProjectProperties.getOutputPath() [returning "+dir+"]");
  316. return dir;
  317. }
  318. public String getBootClasspath() {
  319. log("MyProjectProperties.getBootClasspath()");
  320. return null;
  321. }
  322. public String getClassToExecute() {
  323. log("MyProjectProperties.getClassToExecute()");
  324. return null;
  325. }
  326. public String getExecutionArgs() {
  327. log("MyProjectProperties.getExecutionArgs()");
  328. return null;
  329. }
  330. public String getVmArgs() {
  331. log("MyProjectProperties.getVmArgs()");
  332. return null;
  333. }
  334. public Set getInJars() {
  335. log("MyProjectProperties.getInJars()");
  336. return null;
  337. }
  338. public Set getInpath() {
  339. log("MyProjectProperties.getInPath()");
  340. return null;
  341. }
  342. public Map getSourcePathResources() {
  343. log("MyProjectProperties.getSourcePathResources()");
  344. return null;
  345. }
  346. public String getOutJar() {
  347. log("MyProjectProperties.getOutJar()");
  348. return null;
  349. }
  350. public Set getSourceRoots() {
  351. log("MyProjectProperties.getSourceRoots()");
  352. return null;
  353. }
  354. public Set getAspectPath() {
  355. log("MyProjectProperties.getAspectPath()");
  356. return null;
  357. }
  358. public static void log(String s) {
  359. if (VERBOSE) System.out.println(s);
  360. }
  361. }
  362. // -----------------------
  363. static class MyBuildProgressMonitor implements BuildProgressMonitor {
  364. public static boolean VERBOSE = false;
  365. private static MyBuildProgressMonitor _instance = new MyBuildProgressMonitor();
  366. private MyBuildProgressMonitor() {}
  367. private List compiledFiles=new ArrayList();
  368. private List wovenClasses=new ArrayList();
  369. public static BuildProgressMonitor getInstance() {
  370. return _instance;
  371. }
  372. public static void reset() {
  373. _instance.finished = false;
  374. _instance.compiledFiles.clear();
  375. _instance.wovenClasses.clear();
  376. }
  377. public static boolean hasFinished() {
  378. return _instance.finished;
  379. }
  380. public static List getCompiledFiles() { return _instance.compiledFiles;}
  381. public static List getWovenClasses() { return _instance.wovenClasses; }
  382. //---
  383. private long starttime = 0;
  384. private long totaltimetaken = 0;
  385. private boolean finished = false;
  386. public void start(String configFile) {
  387. starttime = System.currentTimeMillis();
  388. log("BuildProgressMonitor.start("+configFile+")");
  389. }
  390. public void setProgressText(String text) {
  391. log("BuildProgressMonitor.setProgressText("+text+")");
  392. if (text.startsWith("compiled: ")) {
  393. compiledFiles.add(text.substring(10));
  394. } else if (text.startsWith("woven class ")) {
  395. wovenClasses.add(text.substring(12));
  396. } else if (text.startsWith("woven aspect ")) {
  397. wovenClasses.add(text.substring(13));
  398. }
  399. }
  400. public void setProgressBarVal(int newVal) {
  401. log("BuildProgressMonitor.setProgressBarVal("+newVal+")");
  402. }
  403. public void incrementProgressBarVal() {
  404. log("BuildProgressMonitor.incrementProgressBarVal()");
  405. }
  406. public void setProgressBarMax(int maxVal) {
  407. log("BuildProgressMonitor.setProgressBarMax("+maxVal+")");
  408. }
  409. public int getProgressBarMax() {
  410. log("BuildProgressMonitor.getProgressBarMax() [returns 100]");
  411. return 100;
  412. }
  413. public void finish() {
  414. log("BuildProgressMonitor.finish()");
  415. _instance.finished=true;
  416. _instance.totaltimetaken=(System.currentTimeMillis()-starttime);
  417. }
  418. public static long getTimeTaken() {
  419. return _instance.totaltimetaken;
  420. }
  421. public static void log(String s) {
  422. if (VERBOSE) System.out.println(s);
  423. }
  424. }
  425. // ----
  426. static class MyTaskListManager implements TaskListManager {
  427. private static final String CANT_BUILD_INCREMENTAL_INDICATION = "Unable to perform incremental build";
  428. private static final String DOING_BATCH_BUILD_INDICATION = "Performing batch build for config";
  429. private final static boolean VERBOSE = false;
  430. static MyTaskListManager _instance = new MyTaskListManager();
  431. private MyTaskListManager() {}
  432. private boolean receivedNonIncrementalBuildMessage = false;
  433. private boolean receivedBatchBuildMessage = false;
  434. private List errorMessages = new ArrayList();
  435. public static void reset() {
  436. _instance.receivedNonIncrementalBuildMessage=false;
  437. _instance.receivedBatchBuildMessage=false;
  438. _instance.errorMessages.clear();
  439. }
  440. // public static boolean defaultedToBatch() {
  441. // return _instance.receivedNonIncrementalBuildMessage;
  442. // }
  443. //
  444. // public static boolean didBatchBuild() {
  445. // return _instance.receivedBatchBuildMessage;
  446. // }
  447. public static boolean hasErrorMessages() {
  448. return !_instance.errorMessages.isEmpty();
  449. }
  450. public static List/*IMessage*/ getErrorMessages() {
  451. return _instance.errorMessages;
  452. }
  453. public static TaskListManager getInstance() {
  454. return _instance;
  455. }
  456. public void addSourcelineTask(String message, ISourceLocation sourceLocation, Kind kind) {
  457. log("TaskListManager.addSourcelineTask("+message+","+sourceLocation+","+kind+")");
  458. }
  459. // DOESSOMETHING
  460. public void addSourcelineTask(IMessage message) {
  461. // if (message.getKind()==IMessage.INFO) {
  462. // if (message.getMessage().startsWith(CANT_BUILD_INCREMENTAL_INDICATION)) _instance.receivedNonIncrementalBuildMessage=true;
  463. // if (message.getMessage().startsWith(DOING_BATCH_BUILD_INDICATION)) _instance.receivedBatchBuildMessage=true;
  464. // }
  465. if (message.getKind()==IMessage.ERROR) {
  466. errorMessages.add(message);
  467. }
  468. log("TaskListManager.addSourcelineTask("+message+")");
  469. }
  470. public boolean hasWarning() {
  471. log("TaskListManager.hasWarning() [returning false]");
  472. return false;
  473. }
  474. public void addProjectTask(String message, Kind kind) {
  475. log("TaskListManager.addProjectTask("+message+","+kind+")");
  476. }
  477. public void clearTasks() {
  478. log("TaskListManager.clearTasks()");
  479. }
  480. public static void log(String s) {
  481. if (VERBOSE) System.out.println(s);
  482. }
  483. }
  484. // ----
  485. static class MyBuildOptionsAdapter implements BuildOptionsAdapter {
  486. static MyBuildOptionsAdapter _instance = new MyBuildOptionsAdapter();
  487. private MyBuildOptionsAdapter() {}
  488. public static BuildOptionsAdapter getInstance() {
  489. return _instance;
  490. }
  491. public Map getJavaOptionsMap() {
  492. return null;
  493. }
  494. public boolean getUseJavacMode() {
  495. return false;
  496. }
  497. public String getWorkingOutputPath() {
  498. return null;
  499. }
  500. public boolean getPreprocessMode() {
  501. return false;
  502. }
  503. public String getCharacterEncoding() {
  504. return null;
  505. }
  506. public boolean getSourceOnePointFourMode() {
  507. return false;
  508. }
  509. // DOESSOMETHING
  510. public boolean getIncrementalMode() {
  511. return true;
  512. }
  513. public boolean getLenientSpecMode() {
  514. return false;
  515. }
  516. public boolean getStrictSpecMode() {
  517. return false;
  518. }
  519. public boolean getPortingMode() {
  520. return false;
  521. }
  522. public String getNonStandardOptions() {
  523. return null;
  524. }
  525. public String getComplianceLevel() {
  526. return null;
  527. }
  528. public String getSourceCompatibilityLevel() {
  529. return "1.5";
  530. }
  531. public Set getWarnings() {
  532. return null;
  533. }
  534. public Set getDebugLevel() {
  535. return null;
  536. }
  537. public boolean getNoImportError() {
  538. return false;
  539. }
  540. public boolean getPreserveAllLocals() {
  541. return false;
  542. }
  543. }
  544. static class MyStateListener implements IStateListener {
  545. private static MyStateListener _instance = new MyStateListener();
  546. private MyStateListener() {reset();}
  547. public static MyStateListener getInstance() { return _instance;}
  548. public static boolean informedAboutKindOfBuild;
  549. public static boolean fullBuildOccurred;
  550. public static void reset() {
  551. informedAboutKindOfBuild=false;
  552. fullBuildOccurred=false;
  553. }
  554. public boolean pathChange = false;
  555. public void pathChangeDetected() {pathChange = true;}
  556. public void aboutToCompareClasspaths(List oldClasspath, List newClasspath) {}
  557. public void detectedClassChangeInThisDir(File f) {}
  558. public void buildSuccessful(boolean wasFullBuild) {
  559. informedAboutKindOfBuild= true;
  560. fullBuildOccurred=wasFullBuild;
  561. }
  562. public static boolean wasFullBuild() {
  563. if (!informedAboutKindOfBuild) throw new RuntimeException("I never heard about what kind of build it was!!");
  564. return fullBuildOccurred;
  565. }
  566. };
  567. }