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

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