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.

BuildModuleTests.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package org.aspectj.build;
  2. /* *******************************************************************
  3. * Copyright (c) 1999-2001 Xerox Corporation,
  4. * 2002 Palo Alto Research Center, Incorporated (PARC).
  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. // default package
  15. import org.aspectj.internal.tools.ant.taskdefs.Checklics;
  16. import org.aspectj.internal.tools.build.Builder;
  17. import org.aspectj.internal.tools.build.Util;
  18. import org.aspectj.internal.tools.build.UtilsTest;
  19. import org.aspectj.internal.build.BuildModuleTest;
  20. import org.aspectj.internal.build.ModulesTest;
  21. import java.io.File;
  22. import java.io.FileFilter;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Collections;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import java.util.StringTokenizer;
  29. import junit.framework.*;
  30. /**
  31. * Master suite for build module
  32. * and test of all source directories for correct licenses and known file types.
  33. */
  34. public class BuildModuleTests extends TestCase {
  35. /** if true, then replace old headers with new first */
  36. private static final boolean replacing = false; // XXX never to enable again...
  37. /** replace commented out below - if any replace failed, halt all */
  38. private static boolean replaceFailed = false;
  39. private static final String BASE_DIR = ".." + File.separator;
  40. private static final String[] JDT_SOURCE_DIRS = new String[] {};
  41. public static Test suite() {
  42. TestSuite suite = new TestSuite("Build module tests");
  43. suite.addTestSuite(BuildModuleTests.class);
  44. suite.addTestSuite(BuildModuleTest.class);
  45. suite.addTestSuite(ModulesTest.class);
  46. suite.addTestSuite(UtilsTest.class);
  47. return suite;
  48. }
  49. /** @return String tag of license if not default */
  50. public static String getLicense(String module) {
  51. return null; // use permissive default
  52. }
  53. final static List SOURCE_NAMES = Collections.unmodifiableList(
  54. Arrays.asList(new String[]{"src", "testsrc", "java5-src", "aspectj-src"}));
  55. /**
  56. * @param moduleDir
  57. * @return
  58. */
  59. private static File[] findSourceRoots(File moduleDir) {
  60. ArrayList result = new ArrayList();
  61. for (Iterator iter = SOURCE_NAMES.iterator(); iter.hasNext();) {
  62. String name = (String) iter.next();
  63. File srcDir = new File(moduleDir, name);
  64. if (srcDir.canRead() && srcDir.isDirectory()) {
  65. result.add(srcDir);
  66. }
  67. }
  68. return (File[]) result.toArray(new File[0]);
  69. }
  70. public BuildModuleTests(String name) { super(name); }
  71. public void testSuffixList() {
  72. if (!UnknownFileCheck.STATIC_ERRORS.isEmpty()) {
  73. fail("" + UnknownFileCheck.STATIC_ERRORS);
  74. }
  75. }
  76. public void testLicense_ajbrowser() {
  77. checkLicense("ajbrowser");
  78. }
  79. public void testLicense_ajde() {
  80. checkLicense("ajde");
  81. }
  82. public void testLicense_aspectj5rt() {
  83. checkLicense("aspectj5rt");
  84. }
  85. public void testLicense_asm() {
  86. checkLicense("asm");
  87. }
  88. public void testLicense_bridge() {
  89. checkLicense("bridge");
  90. }
  91. public void testLicense_build() {
  92. checkLicense("build");
  93. }
  94. public void testLicense_org_aspectj_ajdt_core() {
  95. checkLicense("org.aspectj.ajdt.core");
  96. }
  97. public void testLicense_org_aspectj_lib() {
  98. checkLicense("org.aspectj.lib");
  99. }
  100. public void testLicense_org_eclipse_jdt_core() {
  101. final String mod = "org.eclipse.jdt.core";
  102. final String pre = BASE_DIR + mod + File.separator;
  103. for (int i = 0; i < JDT_SOURCE_DIRS.length; i++) {
  104. checkSourceDirectory(new File(pre + JDT_SOURCE_DIRS[i]), mod);
  105. }
  106. }
  107. public void testLicense_runtime() {
  108. checkLicense("runtime");
  109. }
  110. public void testLicense_taskdefs() {
  111. checkLicense("taskdefs");
  112. }
  113. public void testLicense_testing() {
  114. checkLicense("testing");
  115. }
  116. public void testLicense_testing_client() {
  117. checkLicense("testing-client");
  118. }
  119. public void testLicense_testing_drivers() {
  120. checkLicense("testing-drivers");
  121. }
  122. public void testLicense_testing_util() {
  123. checkLicense("testing-util");
  124. }
  125. public void testLicense_util() {
  126. checkLicense("util");
  127. }
  128. public void testLicense_weaver() {
  129. String module = "weaver";
  130. checkSourceDirectory(new File(Util.path(new String[] {"..", module, "src"})), module);
  131. checkSourceDirectory(new File(Util.path(new String[] {"..", module, "testsrc", "org"})), module);
  132. }
  133. void checkLicense(String module) {
  134. File moduleDir = new File(Util.path("..", module));
  135. File[] srcDirs = findSourceRoots(moduleDir);
  136. for (int i = 0; i < srcDirs.length; i++) {
  137. checkSourceDirectory(srcDirs[i], module);
  138. }
  139. }
  140. void checkSourceDirectory(File srcDir, String module) {
  141. final String label = "source dir " + srcDir + " (module " + module + ")";
  142. assertTrue(label, (srcDir.exists() && srcDir.isDirectory()));
  143. String license = getLicense(module);
  144. // if (replacing) {
  145. // if (replacing && true) {
  146. // throw new Error("replacing done - code left for other replaces");
  147. // }
  148. // assertTrue("aborting - replace failed", !replaceFailed);
  149. // // do the replace
  150. // int fails = Checklics.runDirect(moduleDir.getPath(), "replace-headers");
  151. // replaceFailed = (0 != fails);
  152. // assertTrue(!replaceFailed);
  153. // license = Checklics.CPL_IBM_PARC_XEROX_TAG;
  154. // }
  155. int fails = Checklics.runDirect(srcDir.getPath(), license, true);
  156. if (0 != fails) {
  157. if (replacing) {
  158. BuildModuleTests.replaceFailed = true;
  159. }
  160. assertTrue(label + " fails", !BuildModuleTests.replaceFailed);
  161. }
  162. // separate check to verify all file types (suffixes) are known
  163. if (!"testsrc".equals(srcDir.getName())) {
  164. ArrayList unknownFiles = new ArrayList();
  165. UnknownFileCheck.SINGLETON.unknownFiles(srcDir, unknownFiles);
  166. if (!unknownFiles.isEmpty()) {
  167. String s = "unknown files (see readme-build-module.html to "
  168. + "update Builder.properties resource patterns): ";
  169. fail(s + unknownFiles);
  170. }
  171. }
  172. }
  173. /**
  174. * Check tree for files not managed by the build system
  175. * (either source files or managed as resources).
  176. * This should pick up situations where new kinds of resources are added
  177. * to the tree without updating the build script patterns to pick them
  178. * up.
  179. * @see Builder#BINARY_SOURCE_PATTERN
  180. * @see Builder#RESOURCE_PATTERN
  181. * @see org.aspectj.util.FileUtil#SOURCE_SUFFIXES
  182. */
  183. static class UnknownFileCheck implements FileFilter {
  184. private static final UnknownFileCheck SINGLETON = new UnknownFileCheck();
  185. private static final ArrayList STATIC_ERRORS = new ArrayList();
  186. // Builder.BINARY_SOURCE_PATTERN and Builder.RESOURCE_PATTERN
  187. public static final List KNOWN_SUFFIXES;
  188. static {
  189. List suffixes = new ArrayList();
  190. // sources from org.aspectj.util.FileUtil.SOURCE_SUFFIXES
  191. suffixes.add(".aj");
  192. suffixes.add(".java");
  193. // just because we know...
  194. suffixes.add(".html");
  195. // others from Builder
  196. final String input = Builder.BINARY_SOURCE_PATTERN
  197. + "," + Builder.RESOURCE_PATTERN;
  198. StringTokenizer st = new StringTokenizer(input, ",");
  199. while (st.hasMoreTokens()) {
  200. String token = st.nextToken().trim();
  201. if (0 == token.length()) {
  202. continue;
  203. }
  204. if (token.startsWith("**/*.")) {
  205. token = token.substring(4);
  206. } else if (token.startsWith("*.")) {
  207. token = token.substring(1);
  208. } else {
  209. String s = input + " at \"" + token + "\"";
  210. STATIC_ERRORS.add("unable to read pattern: " + s);
  211. }
  212. suffixes.add(token);
  213. }
  214. KNOWN_SUFFIXES = Collections.unmodifiableList(suffixes);
  215. }
  216. private UnknownFileCheck() {
  217. }
  218. /**
  219. * Return true if input File file is a valid path to a directory
  220. * or to a file
  221. * which is not hidden (starts with .)
  222. * and does not have a known suffix.
  223. * Caller is responsible for pruning CVS directories
  224. * @return true iff unknown or a directory
  225. */
  226. public boolean accept(File file) {
  227. if (null == file) {
  228. return false;
  229. }
  230. if (file.isDirectory()) {
  231. return file.canRead();
  232. }
  233. String name = file.getName();
  234. if ("CVS".equals(name) || name.startsWith(".")) {
  235. return false;
  236. }
  237. // to do not accepting uppercase suffixes...
  238. for (Iterator iter = KNOWN_SUFFIXES.iterator(); iter.hasNext();) {
  239. String suffix = (String) iter.next();
  240. if (name.endsWith(suffix)) {
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. void unknownFiles(File dir, ArrayList results) {
  247. File[] files = dir.listFiles(this);
  248. for (int j = 0; j < files.length; j++) {
  249. File file = files[j];
  250. if (file.isDirectory()) {
  251. String name = file.getName();
  252. if (!("CVS".equals(name))) {
  253. unknownFiles(file, results);
  254. }
  255. } else {
  256. results.add(file);
  257. }
  258. }
  259. }
  260. }
  261. }