aboutsummaryrefslogtreecommitdiffstats
path: root/build/src/test/java/org/aspectj/build/BuildModuleTests.java
blob: 181d84fcc26cde0f0c74aa450f4661e22d102de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package org.aspectj.build;
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation,
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v 2.0
 * which accompanies this distribution and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
 *
 * Contributors:
 *     Xerox/PARC     initial implementation
 * ******************************************************************/


// default package

import org.aspectj.internal.tools.ant.taskdefs.Checklics;
import org.aspectj.internal.tools.build.Builder;
import org.aspectj.internal.tools.build.Util;
import org.aspectj.internal.tools.build.UtilsTest;
import org.aspectj.internal.build.BuildModuleTest;
import org.aspectj.internal.build.ModulesTest;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import junit.framework.*;

/**
 * Master suite for build module
 * and test of all source directories for correct licenses and known file types.
 */
public class BuildModuleTests extends TestCase {

    /** if true, then replace old headers with new first */
    private static final boolean replacing = false; // XXX never to enable again...

    /** replace commented out below - if any replace failed, halt all */
    private static boolean replaceFailed = false;

    private static final String BASE_DIR = ".." + File.separator;
    private static final String[] JDT_SOURCE_DIRS = new String[] {};

    public static Test suite() {
        TestSuite suite = new TestSuite("Build module tests");
        suite.addTestSuite(BuildModuleTests.class);
        suite.addTestSuite(BuildModuleTest.class);
        suite.addTestSuite(ModulesTest.class);
        suite.addTestSuite(UtilsTest.class);
        return suite;
    }

    /** @return String tag of license if not default */
    public static String getLicense(String module) {
        return null; // use permissive default
    }

    final static List<String> SOURCE_NAMES = Collections.unmodifiableList(
            Arrays.asList(new String[]{"src/main/java", "src/test/java" }));

    /**
     * @param moduleDir
     * @return
     */
    private static File[] findSourceRoots(File moduleDir) {
        ArrayList<File> result = new ArrayList<>();
        for (String name: SOURCE_NAMES) {
            File srcDir = new File(moduleDir, name);
            if (srcDir.canRead() && srcDir.isDirectory()) {
                result.add(srcDir);
            }
        }
        return (File[]) result.toArray(new File[0]);
    }

    public BuildModuleTests(String name) { super(name); }

    public void testSuffixList() {
        if (!UnknownFileCheck.STATIC_ERRORS.isEmpty()) {
            fail("" + UnknownFileCheck.STATIC_ERRORS);
        }
    }
    public void testLicense_ajbrowser() {
        checkLicense("ajbrowser");
    }
    public void testLicense_ajde() {
        checkLicense("ajde");
    }
    public void testLicense_aspectj5rt() {
        checkLicense("aspectj5rt");
    }
    public void testLicense_asm() {
        checkLicense("asm");
    }

    public void testLicense_bridge() {
        checkLicense("bridge");
    }
    public void testLicense_build() {
        checkLicense("build");
    }
    public void testLicense_org_aspectj_ajdt_core() {
        checkLicense("org.aspectj.ajdt.core");
    }
    public void testLicense_org_aspectj_lib() {
        checkLicense("org.aspectj.lib");
    }
    public void testLicense_org_eclipse_jdt_core() {
        final String mod = "org.eclipse.jdt.core";
        final String pre = BASE_DIR + mod + File.separator;
		for (String jdtSourceDir : JDT_SOURCE_DIRS) {
			checkSourceDirectory(new File(pre + jdtSourceDir), mod);
		}
    }

    public void testLicense_runtime() {
        checkLicense("runtime");
    }
    public void testLicense_taskdefs() {
        checkLicense("taskdefs");
    }
    public void testLicense_testing() {
        checkLicense("testing");
    }
    public void testLicense_testing_client() {
        checkLicense("testing-client");
    }
    public void testLicense_testing_drivers() {
        checkLicense("testing-drivers");
    }
    public void testLicense_testing_util() {
        checkLicense("testing-util");
    }
    public void testLicense_util() {
        checkLicense("util");
    }

    public void testLicense_weaver() {
        String module = "weaver";
        // skip (testdata) packages fluffy, reflect
        checkSourceDirectory(new File(Util.path(new String[] {"..", module, "src","main","java"})), module);
        checkSourceDirectory(new File(Util.path(new String[] {"..", module, "src","test","java", "org"})), module);
    }

    public void testLicense_ajdoc() {
        checkLicense("ajdoc");
    }

    public void testLicense_loadtime() {
        checkLicense("loadtime");
    }

    public void testLicense_loadtime5() {
        checkLicense("loadtime5");
    }

    public void testLicense_weaver5() {
        checkLicense("weaver5");
    }

    void checkLicense(String module) {
        File moduleDir = new File(Util.path("..", module));
        File[] srcDirs = findSourceRoots(moduleDir);
		for (File srcDir : srcDirs) {
			System.out.println(srcDir);
			checkSourceDirectory(srcDir, module);
		}
    }

    void checkSourceDirectory(File srcDir, String module) {
        final String label = "source dir " + srcDir + " (module " + module + ")";
        assertTrue(label, srcDir.isDirectory());
        String license = getLicense(module);
//        if (replacing) {
//            if (replacing && true) {
//                throw new Error("replacing done - code left for other replaces");
//            }
//            assertTrue("aborting - replace failed", !replaceFailed);
//            // do the replace
//            int fails = Checklics.runDirect(moduleDir.getPath(), "replace-headers");
//            replaceFailed = (0 != fails);
//            assertTrue(!replaceFailed);
//            license = Checklics.CPL_IBM_PARC_XEROX_TAG;
//        }
        int fails = Checklics.runDirect(srcDir.getPath(), license, true);
        if (0 != fails) {
            if (replacing) {
                BuildModuleTests.replaceFailed = true;
            }
            assertTrue(label + " fails", !BuildModuleTests.replaceFailed);
        }

        // separate check to verify all file types (suffixes) are known
        if (!isTestFolder(srcDir)) {
            ArrayList<File> unknownFiles = new ArrayList<>();
            UnknownFileCheck.SINGLETON.unknownFiles(srcDir, unknownFiles);
            System.out.println(unknownFiles);
            if (!unknownFiles.isEmpty()) {
                String s = "unknown files (see readme-build-module.html to "
                    + "update Builder.properties resource patterns): ";
                fail(s + unknownFiles);
            }
        }
    }

    private boolean isTestFolder(File dir) {
    	return dir.toString().contains("src"+File.separator+"test"+File.separator+"java");
    }

    /**
     * Check tree for files not managed by the build system
     * (either source files or managed as resources).
     * This should pick up situations where new kinds of resources are added
     * to the tree without updating the build script patterns to pick them
     * up.
     * @see Builder#BINARY_SOURCE_PATTERN
     * @see Builder#RESOURCE_PATTERN
     * @see org.aspectj.util.FileUtil#SOURCE_SUFFIXES
     */
    static class UnknownFileCheck implements FileFilter {
        private static final UnknownFileCheck SINGLETON = new UnknownFileCheck();
        private static final List<String> STATIC_ERRORS = new ArrayList<>();
        // Builder.BINARY_SOURCE_PATTERN and Builder.RESOURCE_PATTERN
        public static final List<String> KNOWN_SUFFIXES;

        static {
            List<String> suffixes = new ArrayList<>();
            // sources from org.aspectj.util.FileUtil.SOURCE_SUFFIXES
            suffixes.add(".aj");
            suffixes.add(".java");

            // just because we know...
            suffixes.add(".html");

            // others from Builder
            final String input = Builder.BINARY_SOURCE_PATTERN
                + "," + Builder.RESOURCE_PATTERN;
            StringTokenizer st = new StringTokenizer(input, ",");
            while (st.hasMoreTokens()) {
                String token = st.nextToken().trim();
                if (0 == token.length()) {
                    continue;
                }
                if (token.startsWith("**/*.")) {
                    token = token.substring(4);
                } else if (token.startsWith("*.")) {
                    token = token.substring(1);
                } else {
                    String s = input + " at \"" + token + "\"";
                    STATIC_ERRORS.add("unable to read pattern: " + s);
                }
                suffixes.add(token);
            }
            KNOWN_SUFFIXES = Collections.unmodifiableList(suffixes);
        }

        private UnknownFileCheck() {

        }
        /**
         * Return true if input File file is a valid path to a directory
         * or to a file
         * which is not hidden (starts with .)
         * and does not have a known suffix.
         * Caller is responsible for pruning CVS directories
         * @return true iff unknown or a directory
         */
        public boolean accept(File file) {
            if (null == file) {
                return false;
            }
            if (file.isDirectory()) {
                return file.canRead();
            }

            String name = file.getName();
            if ("CVS".equals(name) || name.startsWith(".")) {
                return false;
            }
            // to do not accepting uppercase suffixes...
            for (String suffix: KNOWN_SUFFIXES) {
                if (name.endsWith(suffix)) {
                    return false;
                }
            }
            return true;

        }
        void unknownFiles(File dir, ArrayList<File> results) {
            File[] files = dir.listFiles(this);
			for (File file : files) {
				if (file.isDirectory()) {
					String name = file.getName();
					if (!("CVS".equals(name))) {
						unknownFiles(file, results);
					}
				} else {
					results.add(file);
				}
			}
        }

    }
}