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

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