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

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