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

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