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.

ModulesTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. * Wes Isberg build tests
  14. * ******************************************************************/
  15. package org.aspectj.internal.build;
  16. import java.io.File;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import junit.framework.TestCase;
  23. import org.apache.tools.ant.BuildEvent;
  24. import org.apache.tools.ant.BuildListener;
  25. import org.apache.tools.ant.Project;
  26. import org.apache.tools.ant.Task;
  27. import org.apache.tools.ant.types.Path;
  28. import org.aspectj.internal.tools.ant.taskdefs.AntBuilder;
  29. import org.aspectj.internal.tools.ant.taskdefs.BuildModule;
  30. import org.aspectj.internal.tools.build.Messager;
  31. import org.aspectj.internal.tools.build.Module;
  32. import org.aspectj.internal.tools.build.Modules;
  33. import org.aspectj.internal.tools.build.Result;
  34. import org.aspectj.internal.tools.build.Util;
  35. import org.aspectj.internal.tools.build.Result.Kind;
  36. public class ModulesTest extends TestCase {
  37. public static final List<String> MODULE_NAMES;
  38. private static final File BASE_DIR = new File("..");
  39. static {
  40. String[] names = {
  41. "ajbrowser", "ajde", "ajdoc", "asm",
  42. "bridge", "loadtime", "org.aspectj.ajdt.core",
  43. "runtime", "taskdefs", "testing-client", "testing-util",
  44. "tests", "util", "weaver"};
  45. List<String> list = Arrays.asList(names);
  46. MODULE_NAMES = Collections.unmodifiableList(list);
  47. }
  48. private static boolean delete(File file) { // XXX Util
  49. if ((null == file) || !file.exists()) {
  50. return true;
  51. }
  52. if (file.isFile()) {
  53. return file.delete();
  54. } else {
  55. File[] files = file.listFiles();
  56. boolean result = true;
  57. for (File value : files) {
  58. if (!ModulesTest.delete(value)
  59. && result) {
  60. result = false;
  61. }
  62. }
  63. return (file.delete() && result);
  64. }
  65. }
  66. ArrayList<File> tempFiles = new ArrayList<File>();
  67. public ModulesTest(String name) {
  68. super(name);
  69. }
  70. protected void tearDown() throws Exception {
  71. super.tearDown();
  72. for (File file : tempFiles) {
  73. if (!ModulesTest.delete(file)) {
  74. System.err.println("warning: ModulesTest unable to delete " + file);
  75. }
  76. }
  77. }
  78. Modules getModules(Messager handler) {
  79. File jarDir = new File("../aj-build-test-jars");
  80. if (!jarDir.exists()) {
  81. assertTrue(jarDir.mkdirs());
  82. tempFiles.add(jarDir);
  83. }
  84. File baseDir = new File("..");
  85. if (null == handler) {
  86. handler = new Messager();
  87. }
  88. return new Modules(baseDir, jarDir, handler);
  89. }
  90. public void testAllModulesCreation() {
  91. ArrayList<Module> badModules = new ArrayList<Module>();
  92. for (String name: MODULE_NAMES) {
  93. File dir = new File(BASE_DIR, name);
  94. if (dir.isDirectory()) {
  95. File classpath = new File(dir, ".classpath");
  96. if (classpath.exists()) {
  97. Modules modules = getModules(null);
  98. Module module = modules.getModule(name);
  99. if (!module.valid) {
  100. badModules.add(module);
  101. }
  102. }
  103. }
  104. }
  105. if (!badModules.isEmpty()) {
  106. StringBuffer sb = new StringBuffer();
  107. for (Module module: badModules) {
  108. System.err.println(module.toLongString());
  109. sb.append("\n");
  110. sb.append(module);
  111. }
  112. fail(sb.toString());
  113. }
  114. }
  115. void checkModule(String s) {
  116. if ("docs".equals(s) || "lib".equals(s)) {
  117. return;
  118. }
  119. Modules modules = getModules(null);
  120. Module module = modules.getModule(s);
  121. if (!module.valid) {
  122. assertTrue(module.toLongString(), false);
  123. }
  124. }
  125. public void xtestClasspathCreation() {
  126. Modules modules = getModules(null);
  127. Module ajdt = modules.getModule("org.aspectj.ajdt.core");
  128. assertTrue(ajdt.valid);
  129. Project project = new Project();
  130. AntBuilder builder = getBuilder(project);
  131. Path classpath = new Path(project);
  132. Kind kind = Result.kind(Result.NORMAL, !Result.ASSEMBLE);
  133. Result result = ajdt.getResult(kind);
  134. boolean hasLibraries = builder.setupClasspath(result, classpath);
  135. assertTrue(hasLibraries);
  136. if ((null == classpath) || (2 > classpath.size())) {
  137. assertTrue(classpath.toString(), false);
  138. }
  139. }
  140. /**
  141. * This test requires two OSGI modules:
  142. * org.aspectj.util, which optionally depends on tempaspectjrt.
  143. * Currently, the Ant builder does not handle linked source folders,
  144. * and the OSGI support does not work around optional plugins.
  145. */
  146. public void skip_testOSGIModuleCreation() {
  147. final String MODULE = "org.aspectj.util";
  148. final String USES_MODULE = "tempaspectjrt";
  149. Modules modules = getModules(null);
  150. Module newutil = modules.getModule(MODULE);
  151. assertTrue(newutil.valid);
  152. Project project = new Project();
  153. AntBuilder builder = getBuilder(project);
  154. Path classpath = new Path(project);
  155. Kind kind = Result.kind(Result.NORMAL, !Result.ASSEMBLE);
  156. Result result = newutil.getResult(kind);
  157. builder.setupClasspath(result, classpath);
  158. System.out.println(newutil + " classpath: " + classpath);
  159. if ((1 != classpath.size())) {
  160. assertTrue(classpath.toString(), false);
  161. }
  162. String cpEntry = classpath.list()[0];
  163. if (!cpEntry.endsWith(USES_MODULE + ".jar")) {
  164. fail("expected " + classpath + " to end with " + USES_MODULE + ".jar");
  165. }
  166. }
  167. private AntBuilder getBuilder(Project project) {
  168. project.setBaseDir(new File("."));
  169. project.setName("testOSGIModuleCreation");
  170. File tempDir = new File(".");
  171. return (AntBuilder) AntBuilder.getBuilder("", project, tempDir);
  172. }
  173. /*********************************************************************
  174. * The following tests/code enable you to run the entire build in JUnit
  175. * to debug directly from Eclipse. To compile using Javac, you will
  176. * need to add tools.jar to the run classpath.
  177. */
  178. public void skip_testBuildingAspectJModule() {
  179. final String moduleName = "org.aspectj.lib";
  180. File modulesDir = new File("..").getAbsoluteFile();
  181. File buildDir = new File(modulesDir, "aj-build");
  182. File distDir = new File(buildDir, "dist");
  183. File jarDir = new File(buildDir, "jars");
  184. File moduleDir = new File(modulesDir, moduleName);
  185. File jar = new File(jarDir, "org.aspectj.lib.jar");
  186. jarDir.mkdirs();
  187. distDir.mkdirs();
  188. if (jar.canRead()) {
  189. assertTrue(jar.delete());
  190. }
  191. Project project = new Project();
  192. project.setBaseDir(modulesDir);
  193. project.setName("testAspectjbuild");
  194. BuildModule bm = new BuildModule();
  195. bm.setProject(project);
  196. bm.setAssembleall(true);
  197. bm.setBuildConfig("");
  198. bm.setModule(moduleName);
  199. bm.setBasedir(new Path(project, modulesDir.getPath()));
  200. bm.setDistdir(new Path(project, buildDir.getPath()));
  201. bm.setJardir(new Path(project, jarDir.getPath()));
  202. bm.setModuledir(new Path(project, moduleDir.getPath()));
  203. bm.setTrimtesting(true);
  204. bm.setBuildConfig("");
  205. bm.setVersion("1.2");
  206. bm.setVerbose(true);
  207. bm.setFailonerror(true);
  208. bm.execute();
  209. assertTrue(jar.canRead());
  210. }
  211. public void skip_testBuildingProduct() {
  212. final String productName = "tools";
  213. File modulesDir = new File("..").getAbsoluteFile();
  214. File buildDir = new File(modulesDir, "aj-build");
  215. File distDir = new File(buildDir, "dist");
  216. File jarDir = new File(buildDir, "jars");
  217. File productDir = new File(modulesDir,
  218. Util.path(new String[] {"build", "products", productName}));
  219. jarDir.mkdirs();
  220. distDir.mkdirs();
  221. Project project = new Project();
  222. project.setBaseDir(modulesDir);
  223. project.setName("testAspectjToolsbuild");
  224. project.addBuildListener(new EventBuildListener(Project.MSG_WARN));
  225. BuildModule bm = new BuildModule();
  226. bm.setProject(project);
  227. bm.setAssembleall(true);
  228. bm.setBuildConfig("");
  229. bm.setProductdir(new Path(project, productDir.getPath()));
  230. bm.setBasedir(new Path(project, modulesDir.getPath()));
  231. bm.setDistdir(new Path(project, distDir.getPath()));
  232. bm.setJardir(new Path(project, jarDir.getPath()));
  233. bm.setTrimtesting(true);
  234. bm.setBuildConfig("");
  235. bm.setVersion("1.2");
  236. bm.setFailonerror(true);
  237. bm.execute();
  238. File libDir = new File(distDir, "tools/lib");
  239. String[] jars = { "tools", "rt", "weaver", "lib"};
  240. for (String s : jars) {
  241. File jar = new File(libDir, "aspectj" + s + ".jar");
  242. assertTrue(jar.getPath(), jar.canRead());
  243. if (10 > jar.length()) {
  244. assertTrue(jar + " too small", false);
  245. }
  246. }
  247. }
  248. /**
  249. * Show messages from the task.
  250. * (normally shown by Ant's default listener)
  251. */
  252. static class EventBuildListener implements BuildListener {
  253. final int min;
  254. EventBuildListener(int min) {
  255. this.min = min;
  256. }
  257. public void buildFinished(BuildEvent event) {}
  258. public void buildStarted(BuildEvent event) { }
  259. public void messageLogged(BuildEvent event) {
  260. if (min <= event.getPriority()) {
  261. Task t = event.getTask();
  262. String src = (null == t ? "project" : t.getTaskName());
  263. System.out.println(src + ": " + event.getMessage());
  264. }
  265. }
  266. public void targetFinished(BuildEvent event) { }
  267. public void targetStarted(BuildEvent event) { }
  268. public void taskFinished(BuildEvent event) { }
  269. public void taskStarted(BuildEvent event) { }
  270. }
  271. }