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

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