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

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