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 16KB

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<>();
  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 (Iterator iter = tempFiles.iterator(); iter.hasNext();) {
  98. File file = (File) iter.next();
  99. if (!Util.delete(file)) {
  100. File[] list = file.listFiles();
  101. if (!Util.isEmpty(list)) {
  102. StringBuffer sb = new StringBuffer();
  103. sb.append("warning: BuildModuleTest unable to delete ");
  104. sb.append(file.toString());
  105. sb.append("\n"); // XXX platform
  106. for (int i = 0; i < list.length; i++) {
  107. sb.append(" ");
  108. sb.append(list[i].toString());
  109. sb.append("\n"); // XXX platform
  110. }
  111. System.err.println(sb.toString());
  112. }
  113. }
  114. }
  115. }
  116. public void testAllJunitTests() {
  117. checkBuild("run-all-junit-tests");
  118. }
  119. public void testBuild() {
  120. checkBuild("build",
  121. Checklics.class.getName(),
  122. new String[0], // help message
  123. true); // ant needed
  124. }
  125. public void testUtil() {
  126. checkBuild("util");
  127. }
  128. public void testAsm() {
  129. checkBuild("asm");
  130. }
  131. public void testRuntime() {
  132. checkBuild("runtime");
  133. }
  134. public void testAspectj5rt() {
  135. checkBuild("aspectj5rt");
  136. }
  137. // public void testLocalOutOfDate() {
  138. // Messager handler = new Messager();
  139. // File jarDir = new File("c:/home/ws/head/aj-build/jars");
  140. // File baseDir = new File("c:/home/ws/head");
  141. // Modules mods = new Modules(baseDir, jarDir, handler);
  142. // Module module = mods.getModule("ajbrowser");
  143. // Result r = module.getResult(Result.kind(true, true));
  144. // r.outOfDate();
  145. // }
  146. // public void testAspectj5rtRequired() {
  147. // File baseDir = new File("..");
  148. // Modules modules = new Modules(baseDir, getJarDir(), new Messager());
  149. // Module module = modules.getModule("aspectj5rt");
  150. // Result result = module.getResult(Result.kind(true, true));
  151. // Result[] results = result.getRequired();
  152. // System.out.println(result.toLongString());
  153. // System.out.println("results: " + Arrays.asList(results));
  154. // deleteTempFiles();
  155. // }
  156. public void testNoDuplicates() {
  157. File weaverAllJar = null;
  158. try {
  159. weaverAllJar = doTask("weaver",true, true, true);
  160. } catch (Throwable t) {
  161. System.err.println(getClass() + ".testNoDuplicates() incomplete");
  162. t.printStackTrace(System.err);
  163. return;
  164. }
  165. String dupError = duplicateEntryError(weaverAllJar);
  166. weaverAllJar.delete();
  167. if (null != dupError) {
  168. fail(dupError);
  169. }
  170. }
  171. public void testAjbrowser() {
  172. checkBuild("ajbrowser",
  173. "org.aspectj.tools.ajbrowser.Main",
  174. new String[] {"-noExit", "-version"}); // compiler version
  175. }
  176. public void testTestingUtils() {
  177. checkBuild("testing-util");
  178. }
  179. public void testAjdt() {
  180. checkBuild("org.aspectj.ajdt.core",
  181. "org.aspectj.tools.ajc.Main",
  182. new String[] { "-noExit", "-version" });
  183. }//
  184. public void testTesting() {
  185. checkBuild("testing",
  186. "org.aspectj.testing.util.LangUtilTest",
  187. new String[] {"ignored"});
  188. }
  189. public void testTestingDrivers() {
  190. checkBuild("testing-drivers",
  191. "org.aspectj.testing.drivers.Harness",
  192. new String[] {"-help"});
  193. }
  194. public void testWeaver() {
  195. checkBuild("weaver");
  196. }
  197. // ajdoc relies on tools.jar
  198. public void testAspectjtools() {
  199. if (!shouldBuild("aspectjtools")) {
  200. return;
  201. }
  202. File baseDir = new File("..");
  203. File tempBuildDir = new File(baseDir, "aj-build");
  204. File distDir = new File(tempBuildDir, "dist");
  205. File jarDir = new File(tempBuildDir, "jars");
  206. assertTrue(distDir.canWrite() || distDir.mkdirs());
  207. File productDir = new File(baseDir.getPath() + "/build/products/tools");
  208. assertTrue(""+productDir, productDir.canRead());
  209. checkBuildProduct(productDir, baseDir, distDir, jarDir);
  210. }
  211. void checkBuildProduct(File productDir, File baseDir, File distDir, File jarDir) {
  212. if (!shouldBuild(productDir.getPath())) {
  213. return;
  214. }
  215. assertTrue(null != productDir);
  216. assertTrue(productDir.canRead());
  217. checkJavac();
  218. BuildModule task = new BuildModule();
  219. Project project = new Project();
  220. task.setProject(project);
  221. assertTrue(jarDir.canWrite() || jarDir.mkdirs());
  222. tempFiles.add(jarDir);
  223. task.setJardir(new Path(project, jarDir.getAbsolutePath()));
  224. task.setProductdir(new Path(project, productDir.getAbsolutePath()));
  225. task.setBasedir(new Path(project, baseDir.getAbsolutePath()));
  226. task.setDistdir(new Path(project, distDir.getAbsolutePath()));
  227. task.setFailonerror(true);
  228. if (null != BUILD_CONFIG) {
  229. task.setBuildConfig(BUILD_CONFIG);
  230. }
  231. //task.setVerbose(true);
  232. task.setCreateinstaller(true);
  233. task.execute();
  234. // now run installer and do product tests?
  235. }
  236. File getAntJar() {
  237. return new File("../lib/ant/lib/ant.jar");
  238. }
  239. File getJUnitJar() {
  240. return new File("../lib/junit/junit.jar");
  241. }
  242. File getJarDir() {
  243. if (null == jarDir) {
  244. File baseDir = new File("../aj-build/");
  245. if (!baseDir.canWrite()) {
  246. baseDir = new File(".");
  247. }
  248. jarDir = new File(baseDir, "BuildModuleTest-jars");
  249. tempFiles.add(jarDir);
  250. }
  251. if (!jarDir.exists()) {
  252. assertTrue(jarDir.mkdirs());
  253. }
  254. return jarDir;
  255. }
  256. BuildModule getTask(String module) {
  257. BuildModule task = new BuildModule();
  258. Project project = new Project();
  259. task.setProject(project);
  260. File jarDir = getJarDir();
  261. assertTrue(jarDir.canWrite() || jarDir.mkdirs());
  262. tempFiles.add(jarDir);
  263. File moduleDir = new File(Util.path("..", module));
  264. assertTrue(moduleDir.canRead());
  265. task.setModuledir(new Path(project, moduleDir.getAbsolutePath()));
  266. task.setJardir(new Path(project, jarDir.getAbsolutePath()));
  267. task.setFailonerror(true);
  268. if (null != BUILD_CONFIG) {
  269. task.setBuildConfig(BUILD_CONFIG);
  270. }
  271. return task;
  272. }
  273. void checkBuild(String module) {
  274. checkBuild(module, null, null, false);
  275. }
  276. void checkBuild(String module,
  277. String classname,
  278. String[] args) {
  279. checkBuild(module, classname, args, true);
  280. }
  281. boolean shouldBuild(String target) {
  282. if (null == target) {
  283. return false;
  284. }
  285. if (!building && !printedMessage) {
  286. System.err.println(SKIP_MESSAGE + target + " (this is the only warning)");
  287. printedMessage = true;
  288. }
  289. if (debugging()) {
  290. for (int i = 0; i < DEBUGS.length; i++) {
  291. if (target.equals(DEBUGS[i])) {
  292. return true;
  293. }
  294. }
  295. return false;
  296. } else {
  297. for (int i = 0; i < SKIPS.length; i++) {
  298. if (SKIPS[i].equals(target)) {
  299. if (printInfoMessages) {
  300. System.err.println(target + " skipped build test [" + getClass().getName() + ".shouldBuild(..)]");
  301. }
  302. return false;
  303. }
  304. }
  305. }
  306. return building;
  307. }
  308. private static boolean debugging() {
  309. return ((null != DEBUGS) && (0 < DEBUGS.length));
  310. }
  311. private static String duplicateEntryError(File weaverAllJar) {
  312. ZipFile zipFile = null;
  313. try {
  314. zipFile = new ZipFile(weaverAllJar);
  315. Enumeration e = zipFile.entries();
  316. ArrayList<String> entryNames = new ArrayList<>();
  317. while (e.hasMoreElements()) {
  318. ZipEntry entry = (ZipEntry) e.nextElement();
  319. String name = entry.getName();
  320. if (entryNames.contains(name)) {
  321. return "duplicate entry: " + name;
  322. }
  323. entryNames.add(name);
  324. }
  325. } catch (ZipException e) {
  326. return "ZipException " + e;
  327. } catch (IOException e) {
  328. return "IOException " + e;
  329. } finally {
  330. if (null != zipFile) {
  331. try {
  332. zipFile.close();
  333. } catch (IOException e) {
  334. return "IOException closing " + zipFile + ": " + e;
  335. }
  336. }
  337. }
  338. return null;
  339. }
  340. private static String name(String module, boolean trimTesting, boolean assemble) {
  341. return module + (trimTesting?"":"-test") + (assemble?"-all":"");
  342. }
  343. private void deleteJar(File jar) {
  344. if (!deleteJars) {
  345. return ;
  346. }
  347. if (jar.exists()) {
  348. jar.delete();
  349. }
  350. if (jar.exists()) {
  351. try {
  352. Thread.sleep(5000);
  353. } catch (Throwable t) {
  354. }
  355. }
  356. if (jar.exists()) {
  357. assertTrue("cannot delete " + jar, jar.delete());
  358. }
  359. }
  360. void checkBuild(String module,
  361. String classname,
  362. String[] args,
  363. boolean addAnt) {
  364. if (!shouldBuild(module)) {
  365. return;
  366. }
  367. assertTrue(null != module);
  368. checkJavac();
  369. doTask(module, true, false);
  370. doTask(module, true, true);
  371. doTask(module, false, false);
  372. File jar = doTask(module, false, true, true);
  373. // verify if possible
  374. if (null != classname) {
  375. Java java = new Java();
  376. Project project = new Project();
  377. java.setProject(project);
  378. java.setFailonerror(true);
  379. Path cp = new Path(project);
  380. assertTrue(jar.canRead());
  381. cp.append(new Path(project, jar.getAbsolutePath()));
  382. if (addAnt) {
  383. cp.append(new Path(project, getAntJar().getAbsolutePath()));
  384. cp.append(new Path(project, getJUnitJar().getAbsolutePath()));
  385. }
  386. java.setClasspath(cp);
  387. java.setClassname(classname);
  388. if (null != args) {
  389. for (int i = 0; i < args.length; i++) {
  390. Argument arg = java.createArg();
  391. arg.setValue(args[i]);
  392. }
  393. }
  394. try {
  395. java.execute();
  396. } catch (BuildException e) {
  397. e.printStackTrace(System.err);
  398. assertTrue("BuildException running " + classname, false);
  399. }
  400. }
  401. deleteJar(jar);
  402. }
  403. void doTask(String module, boolean trimTesting, boolean assembleAll) {
  404. doTask(module, trimTesting, assembleAll, false);
  405. }
  406. File doTask(String module, boolean trimTesting, boolean assembleAll, boolean keepJars) {
  407. BuildModule task = getTask(module);
  408. String name = name(module, trimTesting, assembleAll);
  409. File jar = new File(getJarDir(), name+ ".jar");
  410. task.setAssembleall(assembleAll);
  411. task.setTrimtesting(trimTesting);
  412. task.execute();
  413. if (!jar.canRead()) {
  414. File[] files = getJarDir().listFiles();
  415. fail("cannot read " + jar + " in " + Arrays.asList(files));
  416. }
  417. if (!keepJars && deleteJars) {
  418. deleteTempFiles();
  419. }
  420. return jar;
  421. }
  422. void checkJavac() {
  423. boolean result = false;
  424. try {
  425. result = (null != Class.forName("sun.tools.javac.Main"));
  426. } catch (Throwable t) {
  427. // ignore
  428. }
  429. if (! result) {
  430. assertTrue("add tools.jar to the classpath for Ant's use of javac", false);
  431. }
  432. }
  433. }