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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. "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 testTestingUtils() {
  168. checkBuild("testing-util");
  169. }
  170. public void testAjdt() {
  171. checkBuild("org.aspectj.ajdt.core",
  172. "org.aspectj.tools.ajc.Main",
  173. new String[] { "-noExit", "-version" });
  174. }//
  175. public void testTesting() {
  176. checkBuild("testing",
  177. "org.aspectj.testing.util.LangUtilTest",
  178. new String[] {"ignored"});
  179. }
  180. public void testTestingDrivers() {
  181. checkBuild("testing-drivers",
  182. "org.aspectj.testing.drivers.Harness",
  183. new String[] {"-help"});
  184. }
  185. public void testWeaver() {
  186. checkBuild("weaver");
  187. }
  188. // ajdoc relies on tools.jar
  189. public void testAspectjtools() {
  190. if (!shouldBuild("aspectjtools")) {
  191. return;
  192. }
  193. File baseDir = new File("..");
  194. File tempBuildDir = new File(baseDir, "aj-build");
  195. File distDir = new File(tempBuildDir, "dist");
  196. File jarDir = new File(tempBuildDir, "jars");
  197. assertTrue(distDir.canWrite() || distDir.mkdirs());
  198. File productDir = new File(baseDir.getPath() + "/build/products/tools");
  199. assertTrue(""+productDir, productDir.canRead());
  200. checkBuildProduct(productDir, baseDir, distDir, jarDir);
  201. }
  202. void checkBuildProduct(File productDir, File baseDir, File distDir, File jarDir) {
  203. if (!shouldBuild(productDir.getPath())) {
  204. return;
  205. }
  206. assertTrue(null != productDir);
  207. assertTrue(productDir.canRead());
  208. checkJavac();
  209. BuildModule task = new BuildModule();
  210. Project project = new Project();
  211. task.setProject(project);
  212. assertTrue(jarDir.canWrite() || jarDir.mkdirs());
  213. tempFiles.add(jarDir);
  214. task.setJardir(new Path(project, jarDir.getAbsolutePath()));
  215. task.setProductdir(new Path(project, productDir.getAbsolutePath()));
  216. task.setBasedir(new Path(project, baseDir.getAbsolutePath()));
  217. task.setDistdir(new Path(project, distDir.getAbsolutePath()));
  218. task.setFailonerror(true);
  219. if (null != BUILD_CONFIG) {
  220. task.setBuildConfig(BUILD_CONFIG);
  221. }
  222. //task.setVerbose(true);
  223. task.setCreateinstaller(true);
  224. task.execute();
  225. // now run installer and do product tests?
  226. }
  227. File getAntJar() {
  228. return new File("../lib/ant/lib/ant.jar");
  229. }
  230. File getJUnitJar() {
  231. return new File("../lib/junit/junit.jar");
  232. }
  233. File getJarDir() {
  234. if (null == jarDir) {
  235. File baseDir = new File("../aj-build/");
  236. if (!baseDir.canWrite()) {
  237. baseDir = new File(".");
  238. }
  239. jarDir = new File(baseDir, "BuildModuleTest-jars");
  240. tempFiles.add(jarDir);
  241. }
  242. if (!jarDir.exists()) {
  243. assertTrue(jarDir.mkdirs());
  244. }
  245. return jarDir;
  246. }
  247. BuildModule getTask(String module) {
  248. BuildModule task = new BuildModule();
  249. Project project = new Project();
  250. task.setProject(project);
  251. File jarDir = getJarDir();
  252. assertTrue(jarDir.canWrite() || jarDir.mkdirs());
  253. tempFiles.add(jarDir);
  254. File moduleDir = new File(Util.path("..", module));
  255. assertTrue(moduleDir.canRead());
  256. task.setModuledir(new Path(project, moduleDir.getAbsolutePath()));
  257. task.setJardir(new Path(project, jarDir.getAbsolutePath()));
  258. task.setFailonerror(true);
  259. if (null != BUILD_CONFIG) {
  260. task.setBuildConfig(BUILD_CONFIG);
  261. }
  262. return task;
  263. }
  264. void checkBuild(String module) {
  265. checkBuild(module, null, null, false);
  266. }
  267. void checkBuild(String module,
  268. String classname,
  269. String[] args) {
  270. checkBuild(module, classname, args, true);
  271. }
  272. boolean shouldBuild(String target) {
  273. if (null == target) {
  274. return false;
  275. }
  276. if (!building && !printedMessage) {
  277. System.err.println(SKIP_MESSAGE + target + " (this is the only warning)");
  278. printedMessage = true;
  279. }
  280. if (debugging()) {
  281. for (String debug : DEBUGS) {
  282. if (target.equals(debug)) {
  283. return true;
  284. }
  285. }
  286. return false;
  287. } else {
  288. for (String skip : SKIPS) {
  289. if (skip.equals(target)) {
  290. if (printInfoMessages) {
  291. System.err.println(target + " skipped build test [" + getClass().getName() + ".shouldBuild(..)]");
  292. }
  293. return false;
  294. }
  295. }
  296. }
  297. return building;
  298. }
  299. private static boolean debugging() {
  300. return ((null != DEBUGS) && (0 < DEBUGS.length));
  301. }
  302. private static String duplicateEntryError(File weaverAllJar) {
  303. ZipFile zipFile = null;
  304. try {
  305. zipFile = new ZipFile(weaverAllJar);
  306. Enumeration e = zipFile.entries();
  307. List<String> entryNames = new ArrayList<>();
  308. while (e.hasMoreElements()) {
  309. ZipEntry entry = (ZipEntry) e.nextElement();
  310. String name = entry.getName();
  311. if (entryNames.contains(name)) {
  312. return "duplicate entry: " + name;
  313. }
  314. entryNames.add(name);
  315. }
  316. } catch (ZipException e) {
  317. return "ZipException " + e;
  318. } catch (IOException e) {
  319. return "IOException " + e;
  320. } finally {
  321. if (null != zipFile) {
  322. try {
  323. zipFile.close();
  324. } catch (IOException e) {
  325. return "IOException closing " + zipFile + ": " + e;
  326. }
  327. }
  328. }
  329. return null;
  330. }
  331. private static String name(String module, boolean trimTesting, boolean assemble) {
  332. return module + (trimTesting?"":"-test") + (assemble?"-all":"");
  333. }
  334. private void deleteJar(File jar) {
  335. if (!deleteJars) {
  336. return ;
  337. }
  338. if (jar.exists()) {
  339. jar.delete();
  340. }
  341. if (jar.exists()) {
  342. try {
  343. Thread.sleep(5000);
  344. } catch (Throwable t) {
  345. }
  346. }
  347. if (jar.exists()) {
  348. assertTrue("cannot delete " + jar, jar.delete());
  349. }
  350. }
  351. void checkBuild(String module,
  352. String classname,
  353. String[] args,
  354. boolean addAnt) {
  355. if (!shouldBuild(module)) {
  356. return;
  357. }
  358. assertTrue(null != module);
  359. checkJavac();
  360. doTask(module, true, false);
  361. doTask(module, true, true);
  362. doTask(module, false, false);
  363. File jar = doTask(module, false, true, true);
  364. // verify if possible
  365. if (null != classname) {
  366. Java java = new Java();
  367. Project project = new Project();
  368. java.setProject(project);
  369. java.setFailonerror(true);
  370. Path cp = new Path(project);
  371. assertTrue(jar.canRead());
  372. cp.append(new Path(project, jar.getAbsolutePath()));
  373. if (addAnt) {
  374. cp.append(new Path(project, getAntJar().getAbsolutePath()));
  375. cp.append(new Path(project, getJUnitJar().getAbsolutePath()));
  376. }
  377. java.setClasspath(cp);
  378. java.setClassname(classname);
  379. if (null != args) {
  380. for (String s : args) {
  381. Argument arg = java.createArg();
  382. arg.setValue(s);
  383. }
  384. }
  385. try {
  386. java.execute();
  387. } catch (BuildException e) {
  388. e.printStackTrace(System.err);
  389. assertTrue("BuildException running " + classname, false);
  390. }
  391. }
  392. deleteJar(jar);
  393. }
  394. void doTask(String module, boolean trimTesting, boolean assembleAll) {
  395. doTask(module, trimTesting, assembleAll, false);
  396. }
  397. File doTask(String module, boolean trimTesting, boolean assembleAll, boolean keepJars) {
  398. BuildModule task = getTask(module);
  399. String name = name(module, trimTesting, assembleAll);
  400. File jar = new File(getJarDir(), name+ ".jar");
  401. task.setAssembleall(assembleAll);
  402. task.setTrimtesting(trimTesting);
  403. task.execute();
  404. if (!jar.canRead()) {
  405. File[] files = getJarDir().listFiles();
  406. fail("cannot read " + jar + " in " + Arrays.asList(files));
  407. }
  408. if (!keepJars && deleteJars) {
  409. deleteTempFiles();
  410. }
  411. return jar;
  412. }
  413. void checkJavac() {
  414. boolean result = false;
  415. try {
  416. result = (null != Class.forName("sun.tools.javac.Main"));
  417. } catch (Throwable t) {
  418. // ignore
  419. }
  420. if (! result) {
  421. assertTrue("add tools.jar to the classpath for Ant's use of javac", false);
  422. }
  423. }
  424. }