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.

BuildModuleTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC),
  4. * 2005-2006 Contributors.
  5. * All rights reserved.
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Public License v1.0
  8. * which accompanies this distribution and is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. *
  11. * Contributors:
  12. * Xerox/PARC initial implementation
  13. * ******************************************************************/
  14. package org.aspectj.internal.build;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Enumeration;
  20. import java.util.Iterator;
  21. import java.util.zip.ZipEntry;
  22. import java.util.zip.ZipException;
  23. import java.util.zip.ZipFile;
  24. import junit.framework.TestCase;
  25. import org.apache.tools.ant.BuildException;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.taskdefs.Java;
  28. import org.apache.tools.ant.types.Path;
  29. import org.apache.tools.ant.types.Commandline.Argument;
  30. import org.aspectj.internal.tools.ant.taskdefs.BuildModule;
  31. import org.aspectj.internal.tools.ant.taskdefs.Checklics;
  32. import org.aspectj.internal.tools.build.Util;
  33. /**
  34. * Test our integrated taskdef build.
  35. * This responds to two environment variables:
  36. * (1) run.build.tests must be defined before
  37. * tests that build the tree (and hence take minutes)
  38. * will run;
  39. * (2) build.config takes the same form as it does for the
  40. * builder task, e.g., "useEclipseCompiles" will avoid
  41. * recompiling with Javac and adopt classes in the
  42. * {module}/bin directories.
  43. */
  44. public class BuildModuleTest extends TestCase {
  45. private static boolean printInfoMessages = false;
  46. private static boolean printedMessage;
  47. private static final boolean REMOVE_JARS_AFTER_DEBUGGING = true;
  48. // to just build one module verbosely
  49. private static final String[] DEBUGS
  50. = {};
  51. // skip those requiring ajdoc, which requires tools.jar
  52. // also skip those requiring java5 unless manually set up
  53. // also skip big ones to avoid slowing the build too much
  54. private static final String[] SKIPS
  55. //= {};
  56. = {"aspectjtools", "ajdoc", "aspectj5rt", "run-all-junit-tests",
  57. "ajbrowser", "testing", "testing-drivers", "org.aspectj.ajdt.core", "weaver"};
  58. private static final String SKIP_MESSAGE =
  59. "BuildModuleTest: Define \"run.build.tests\" as a system "
  60. + "property to run tests to build ";
  61. private static final String BUILD_CONFIG;
  62. static {
  63. String config = null;
  64. try {
  65. config = System.getProperty("build.config");
  66. } catch (Throwable t) {
  67. // ignore
  68. }
  69. BUILD_CONFIG = config;
  70. if (printInfoMessages) {
  71. System.out.println("BuildModuleTest build.config: " + config);
  72. }
  73. }
  74. ArrayList<File> tempFiles = new ArrayList<File>();
  75. private File jarDir;
  76. private boolean deleteJars;
  77. boolean building; // must be enabled for tests to run
  78. public BuildModuleTest(String name) {
  79. super(name);
  80. building = Boolean.getBoolean("run.build.tests");
  81. }
  82. protected void setUp() {
  83. // change to view whether prior output is used
  84. deleteJars = true; // todo
  85. }
  86. protected void tearDown() throws Exception {
  87. super.tearDown();
  88. if (debugging() && !REMOVE_JARS_AFTER_DEBUGGING) {
  89. if (0 < tempFiles.size()) {
  90. System.err.println("debugging files left: " + tempFiles);
  91. }
  92. return;
  93. }
  94. deleteTempFiles();
  95. }
  96. protected void deleteTempFiles() {
  97. for (File file : tempFiles) {
  98. if (!Util.delete(file)) {
  99. File[] list = file.listFiles();
  100. if (!Util.isEmpty(list)) {
  101. StringBuffer sb = new StringBuffer();
  102. sb.append("warning: BuildModuleTest unable to delete ");
  103. sb.append(file.toString());
  104. sb.append("\n"); // XXX platform
  105. for (File value : list) {
  106. sb.append(" ");
  107. sb.append(value.toString());
  108. sb.append("\n"); // XXX platform
  109. }
  110. System.err.println(sb.toString());
  111. }
  112. }
  113. }
  114. }
  115. public void testAllJunitTests() {
  116. checkBuild("run-all-junit-tests");
  117. }
  118. public void testBuild() {
  119. checkBuild("build",
  120. Checklics.class.getName(),
  121. new String[0], // help message
  122. true); // ant needed
  123. }
  124. public void testUtil() {
  125. checkBuild("util");
  126. }
  127. public void testAsm() {
  128. checkBuild("asm");
  129. }
  130. public void testRuntime() {
  131. checkBuild("runtime");
  132. }
  133. public void testAspectj5rt() {
  134. checkBuild("aspectj5rt");
  135. }
  136. // public void testLocalOutOfDate() {
  137. // Messager handler = new Messager();
  138. // File jarDir = new File("c:/home/ws/head/aj-build/jars");
  139. // File baseDir = new File("c:/home/ws/head");
  140. // Modules mods = new Modules(baseDir, jarDir, handler);
  141. // Module module = mods.getModule("ajbrowser");
  142. // Result r = module.getResult(Result.kind(true, true));
  143. // r.outOfDate();
  144. // }
  145. // public void testAspectj5rtRequired() {
  146. // File baseDir = new File("..");
  147. // Modules modules = new Modules(baseDir, getJarDir(), new Messager());
  148. // Module module = modules.getModule("aspectj5rt");
  149. // Result result = module.getResult(Result.kind(true, true));
  150. // Result[] results = result.getRequired();
  151. // System.out.println(result.toLongString());
  152. // System.out.println("results: " + Arrays.asList(results));
  153. // deleteTempFiles();
  154. // }
  155. public void xtestNoDuplicates() {
  156. File weaverAllJar = null;
  157. try {
  158. weaverAllJar = doTask("weaver",true, true, true);
  159. } catch (Throwable t) {
  160. System.err.println(getClass() + ".testNoDuplicates() incomplete");
  161. t.printStackTrace(System.err);
  162. return;
  163. }
  164. String dupError = duplicateEntryError(weaverAllJar);
  165. weaverAllJar.delete();
  166. if (null != dupError) {
  167. fail(dupError);
  168. }
  169. }
  170. public void testAjbrowser() {
  171. checkBuild("ajbrowser",
  172. "org.aspectj.tools.ajbrowser.Main",
  173. new String[] {"-noExit", "-version"}); // compiler version
  174. }
  175. public void testTestingUtils() {
  176. checkBuild("testing-util");
  177. }
  178. public void testAjdt() {
  179. checkBuild("org.aspectj.ajdt.core",
  180. "org.aspectj.tools.ajc.Main",
  181. new String[] { "-noExit", "-version" });
  182. }//
  183. public void testTesting() {
  184. checkBuild("testing",
  185. "org.aspectj.testing.util.LangUtilTest",
  186. new String[] {"ignored"});
  187. }
  188. public void testTestingDrivers() {
  189. checkBuild("testing-drivers",
  190. "org.aspectj.testing.drivers.Harness",
  191. new String[] {"-help"});
  192. }
  193. public void testWeaver() {
  194. checkBuild("weaver");
  195. }
  196. // ajdoc relies on tools.jar
  197. public void testAspectjtools() {
  198. if (!shouldBuild("aspectjtools")) {
  199. return;
  200. }
  201. File baseDir = new File("..");
  202. File tempBuildDir = new File(baseDir, "aj-build");
  203. File distDir = new File(tempBuildDir, "dist");
  204. File jarDir = new File(tempBuildDir, "jars");
  205. assertTrue(distDir.canWrite() || distDir.mkdirs());
  206. File productDir = new File(baseDir.getPath() + "/build/products/tools");
  207. assertTrue(""+productDir, productDir.canRead());
  208. checkBuildProduct(productDir, baseDir, distDir, jarDir);
  209. }
  210. void checkBuildProduct(File productDir, File baseDir, File distDir, File jarDir) {
  211. if (!shouldBuild(productDir.getPath())) {
  212. return;
  213. }
  214. assertTrue(null != productDir);
  215. assertTrue(productDir.canRead());
  216. checkJavac();
  217. BuildModule task = new BuildModule();
  218. Project project = new Project();
  219. task.setProject(project);
  220. assertTrue(jarDir.canWrite() || jarDir.mkdirs());
  221. tempFiles.add(jarDir);
  222. task.setJardir(new Path(project, jarDir.getAbsolutePath()));
  223. task.setProductdir(new Path(project, productDir.getAbsolutePath()));
  224. task.setBasedir(new Path(project, baseDir.getAbsolutePath()));
  225. task.setDistdir(new Path(project, distDir.getAbsolutePath()));
  226. task.setFailonerror(true);
  227. if (null != BUILD_CONFIG) {
  228. task.setBuildConfig(BUILD_CONFIG);
  229. }
  230. //task.setVerbose(true);
  231. task.setCreateinstaller(true);
  232. task.execute();
  233. // now run installer and do product tests?
  234. }
  235. File getAntJar() {
  236. return new File("../lib/ant/lib/ant.jar");
  237. }
  238. File getJUnitJar() {
  239. return new File("../lib/junit/junit.jar");
  240. }
  241. File getJarDir() {
  242. if (null == jarDir) {
  243. File baseDir = new File("../aj-build/");
  244. if (!baseDir.canWrite()) {
  245. baseDir = new File(".");
  246. }
  247. jarDir = new File(baseDir, "BuildModuleTest-jars");
  248. tempFiles.add(jarDir);
  249. }
  250. if (!jarDir.exists()) {
  251. assertTrue(jarDir.mkdirs());
  252. }
  253. return jarDir;
  254. }
  255. BuildModule getTask(String module) {
  256. BuildModule task = new BuildModule();
  257. Project project = new Project();
  258. task.setProject(project);
  259. File jarDir = getJarDir();
  260. assertTrue(jarDir.canWrite() || jarDir.mkdirs());
  261. tempFiles.add(jarDir);
  262. File moduleDir = new File(Util.path("..", module));
  263. assertTrue(moduleDir.canRead());
  264. task.setModuledir(new Path(project, moduleDir.getAbsolutePath()));
  265. task.setJardir(new Path(project, jarDir.getAbsolutePath()));
  266. task.setFailonerror(true);
  267. if (null != BUILD_CONFIG) {
  268. task.setBuildConfig(BUILD_CONFIG);
  269. }
  270. return task;
  271. }
  272. void checkBuild(String module) {
  273. checkBuild(module, null, null, false);
  274. }
  275. void checkBuild(String module,
  276. String classname,
  277. String[] args) {
  278. checkBuild(module, classname, args, true);
  279. }
  280. boolean shouldBuild(String target) {
  281. if (null == target) {
  282. return false;
  283. }
  284. if (!building && !printedMessage) {
  285. System.err.println(SKIP_MESSAGE + target + " (this is the only warning)");
  286. printedMessage = true;
  287. }
  288. if (debugging()) {
  289. for (String debug : DEBUGS) {
  290. if (target.equals(debug)) {
  291. return true;
  292. }
  293. }
  294. return false;
  295. } else {
  296. for (String skip : SKIPS) {
  297. if (skip.equals(target)) {
  298. if (printInfoMessages) {
  299. System.err.println(target + " skipped build test [" + getClass().getName() + ".shouldBuild(..)]");
  300. }
  301. return false;
  302. }
  303. }
  304. }
  305. return building;
  306. }
  307. private static boolean debugging() {
  308. return ((null != DEBUGS) && (0 < DEBUGS.length));
  309. }
  310. private static String duplicateEntryError(File weaverAllJar) {
  311. ZipFile zipFile = null;
  312. try {
  313. zipFile = new ZipFile(weaverAllJar);
  314. Enumeration e = zipFile.entries();
  315. ArrayList<String> entryNames = new ArrayList<String>();
  316. while (e.hasMoreElements()) {
  317. ZipEntry entry = (ZipEntry) e.nextElement();
  318. String name = entry.getName();
  319. if (entryNames.contains(name)) {
  320. return "duplicate entry: " + name;
  321. }
  322. entryNames.add(name);
  323. }
  324. } catch (ZipException e) {
  325. return "ZipException " + e;
  326. } catch (IOException e) {
  327. return "IOException " + e;
  328. } finally {
  329. if (null != zipFile) {
  330. try {
  331. zipFile.close();
  332. } catch (IOException e) {
  333. return "IOException closing " + zipFile + ": " + e;
  334. }
  335. }
  336. }
  337. return null;
  338. }
  339. private static String name(String module, boolean trimTesting, boolean assemble) {
  340. return module + (trimTesting?"":"-test") + (assemble?"-all":"");
  341. }
  342. private void deleteJar(File jar) {
  343. if (!deleteJars) {
  344. return ;
  345. }
  346. if (jar.exists()) {
  347. jar.delete();
  348. }
  349. if (jar.exists()) {
  350. try {
  351. Thread.sleep(5000);
  352. } catch (Throwable t) {
  353. }
  354. }
  355. if (jar.exists()) {
  356. assertTrue("cannot delete " + jar, jar.delete());
  357. }
  358. }
  359. void checkBuild(String module,
  360. String classname,
  361. String[] args,
  362. boolean addAnt) {
  363. if (!shouldBuild(module)) {
  364. return;
  365. }
  366. assertTrue(null != module);
  367. checkJavac();
  368. doTask(module, true, false);
  369. doTask(module, true, true);
  370. doTask(module, false, false);
  371. File jar = doTask(module, false, true, true);
  372. // verify if possible
  373. if (null != classname) {
  374. Java java = new Java();
  375. Project project = new Project();
  376. java.setProject(project);
  377. java.setFailonerror(true);
  378. Path cp = new Path(project);
  379. assertTrue(jar.canRead());
  380. cp.append(new Path(project, jar.getAbsolutePath()));
  381. if (addAnt) {
  382. cp.append(new Path(project, getAntJar().getAbsolutePath()));
  383. cp.append(new Path(project, getJUnitJar().getAbsolutePath()));
  384. }
  385. java.setClasspath(cp);
  386. java.setClassname(classname);
  387. if (null != args) {
  388. for (String s : args) {
  389. Argument arg = java.createArg();
  390. arg.setValue(s);
  391. }
  392. }
  393. try {
  394. java.execute();
  395. } catch (BuildException e) {
  396. e.printStackTrace(System.err);
  397. assertTrue("BuildException running " + classname, false);
  398. }
  399. }
  400. deleteJar(jar);
  401. }
  402. void doTask(String module, boolean trimTesting, boolean assembleAll) {
  403. doTask(module, trimTesting, assembleAll, false);
  404. }
  405. File doTask(String module, boolean trimTesting, boolean assembleAll, boolean keepJars) {
  406. BuildModule task = getTask(module);
  407. String name = name(module, trimTesting, assembleAll);
  408. File jar = new File(getJarDir(), name+ ".jar");
  409. task.setAssembleall(assembleAll);
  410. task.setTrimtesting(trimTesting);
  411. task.execute();
  412. if (!jar.canRead()) {
  413. File[] files = getJarDir().listFiles();
  414. fail("cannot read " + jar + " in " + Arrays.asList(files));
  415. }
  416. if (!keepJars && deleteJars) {
  417. deleteTempFiles();
  418. }
  419. return jar;
  420. }
  421. void checkJavac() {
  422. boolean result = false;
  423. try {
  424. result = (null != Class.forName("sun.tools.javac.Main"));
  425. } catch (Throwable t) {
  426. // ignore
  427. }
  428. if (! result) {
  429. assertTrue("add tools.jar to the classpath for Ant's use of javac", false);
  430. }
  431. }
  432. }