JUnitTestHelper.java 10.5 KB
Newer Older
Dominik Vayhinger's avatar
Dominik Vayhinger committed
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
package de.hftstuttgart.junitlauncher.utils;

import com.google.common.io.Files;
import de.hftstuttgart.junitlauncher.models.TestResult;
import de.hftstuttgart.junitlauncher.models.UserResult;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class JUnitTestHelper {

    private static final Logger LOG = LoggerFactory.getLogger(JUnitTestHelper.class);
    private static final String COMPILER_OUTPUT_FOLDER_PREFIX = "compiledFiles_";

    private final String junitLibDirPath;
    private final String parentPath;
    private final String testFolderName;

    private final List<Diagnostic> compilationErrors = new ArrayList<>();
    public File compileOutputDir;

    public JUnitTestHelper(@Value("${junitlauncher.dir.junit}") String junitLibDirPath,
                           @Value("${junitlauncher.dir.parent}") String parentPath,
                           @Value("${junitlauncher.dir.test.folder.name}") String testFolderName) {
        this.junitLibDirPath = junitLibDirPath;
        this.parentPath = parentPath;
        this.testFolderName = testFolderName;
    }

    /**
     * Runs the JUnit tests and return the result
     *
     * @return {@link UserResult} with a list of the {@link TestResult} of the executed UnitTests
     *  and a List of {@link Diagnostic} with the not compilable task files
     */
    public UserResult runUnitTests() throws IOException, ClassNotFoundException {

        List<File> unitTestFiles = getAllJavaFilesInFolder(new File(parentPath + File.separator + testFolderName));

        // Create list of all files we need to compile i.e. UnitTests and TaskFiles
        List<File> filesToCompile = new ArrayList<>();
        filesToCompile.addAll(getAllJavaFilesInFolder(new File(parentPath)));
        filesToCompile.addAll(unitTestFiles);

        // create temp folder for the compilation output
        compileOutputDir = new File(parentPath + File.separator + COMPILER_OUTPUT_FOLDER_PREFIX + UUID.randomUUID().toString());
        compile(filesToCompile, compileOutputDir);
        LOG.info("compileOutputDir: " + compileOutputDir.getAbsolutePath());

        // Load compiled classes into classloader
        URL url = compileOutputDir.toURI().toURL();
        URL[] urls = {url};
        // It's important to set the context loader as parent, otherwise the test runs will fail
        ClassLoader classLoader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());

        // Run JUnit tests
        JUnitCore junit = new JUnitCore();
        List<TestResult> testResults = new ArrayList<>();
        for (File testFile : unitTestFiles) {
            boolean currentTestCompiled = true;
            // Check if the current test was successfully compiled. If not, continue with the next unit test
            for (Diagnostic error : compilationErrors) {
                File failedCompilationFile = new File((((JavaFileObject) error.getSource()).toUri().getPath()));
                if (failedCompilationFile.getAbsolutePath().equals(testFile.getAbsolutePath())) {
                    currentTestCompiled = false;
                }
            }
            if (!currentTestCompiled) {
                continue;
            }

            String testName = Files.getNameWithoutExtension(testFile.getPath());
            LOG.info("Running JUnit test " + testName);
            Class<?> junitTestClass = classLoader.loadClass(testName);

            // Add the run listener to JUnit in order to be able to see the failures
            MyRunListener runListener = new MyRunListener();
            junit.addListener(runListener);
            Result junitResult = junit.run(junitTestClass);
            junit.removeListener(runListener);

            // Build the result
            List<String> successfulTestNames = runListener.getSuccessFullTestNames();
            TestResult testResult = new TestResult();
            testResult.setTestName(testName);
            testResult.setTestCount(junitResult.getRunCount());
            testResult.setFailureCount(junitResult.getFailureCount());
            testResult.setTestFailures(junitResult.getFailures());
            testResult.setSuccessfulTests(successfulTestNames);

            testResults.add(testResult);
        }

        // Add the failed compilations to the result
        UserResult userResult = new UserResult(testResults);
        userResult.setCompilationErrors(new ArrayList<>(compilationErrors));
        compilationErrors.clear();
        if (compileOutputDir.exists()) {
            FileUtil.deleteFolderRecursively(compileOutputDir);
        }
        return userResult;
    }

    /**
     * @param files         Files to compile
     * @param outputDir     Output folder where the .class files are stored.
     */
    private void compile(List<File> files, File outputDir) {
        // Create the compiler and add a diagnostic listener to get the compilation errors
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        MyDiagnosticListener listener = new MyDiagnosticListener();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(listener, null, StandardCharsets.UTF_8);
        File[] fileArray = new File[files.size()];
        fileArray = files.toArray(fileArray);
        Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects(fileArray);

        if (!outputDir.exists()) {
            outputDir.mkdir();
        }

        // Set the compiler option for a specific output path
        List<String> options = new ArrayList<>();
        options.add("-d"); // output dir
        options.add(outputDir.getAbsolutePath());
        options.add("-cp"); // custom classpath
        String cp = buildClassPath(junitLibDirPath, compileOutputDir.getAbsolutePath());
        LOG.debug("Classpath for compilation: " + cp);
        options.add(cp);

        // compile it
        JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, listener, options, null, fileObjects);
        Boolean compileResult = task.call();

        // If the compilation failed, remove the failed file from the pathsToCompile list and try to compile again without this file
        if (!compileResult) {
            File currentFile = new File(((JavaFileObject) compilationErrors.get(compilationErrors.size() - 1).getSource()).toUri().getPath());
            LOG.warn("Compilation of file '" + currentFile.getPath() + "' failed");
            files.removeIf(file -> file.getPath().equalsIgnoreCase(currentFile.getPath()));
            if (files.size() > 0) {
                compile(files, outputDir);
            }
        }
    }

    /**
     * This function builds a classpath from the passed Strings.
     * We need this because the JUnit4 and Hamcrest libraries needs to be added.
     *
     * @param paths classpath elements
     * @return returns the complete classpath with wildcards expanded
     */
    private static String buildClassPath(String... paths) {
        StringBuilder sb = new StringBuilder();
        for (String path : paths) {
            if (path.endsWith("*")) {
                path = path.substring(0, path.length() - 1);
                File pathFile = new File(path);
                for (File file : Objects.requireNonNull(pathFile.listFiles())) {
                    if (file.isFile() && file.getName().endsWith(".jar")) {
                        sb.append(path);
                        sb.append(file.getName());
                        sb.append(System.getProperty("path.separator"));
                    }
                }
            } else {
                sb.append(path);
                sb.append(System.getProperty("path.separator"));
            }
        }
        return sb.toString();
    }

    private List<File> getAllJavaFilesInFolder(File path) {
        // check if provided path is a directory, otherwise throw a IllegalArgumentException
        if(!path.isDirectory()) {
            String error = path.getAbsolutePath() + " is not a path";
            LOG.error(error);
            throw new IllegalArgumentException(error);
        }

        // Return a list of all java files in the given directory
        return Stream.of(Objects.requireNonNull(path.listFiles()))
                .filter(file -> file.getAbsolutePath().endsWith(".java"))
                .collect(Collectors.toList());
    }

    /**
     * Custom DiagnosticListener to get the compilation errors
     */
    private class MyDiagnosticListener implements DiagnosticListener {
        public void report(Diagnostic diagnostic) {
            compilationErrors.add(diagnostic);
        }
    }

    /**
     * Custom JUnit RunListener which enables us to record the successful and the failed tests.
     */
    private static class MyRunListener extends RunListener {
        private final Set<Description> allTests = new LinkedHashSet<>();
        private final Set<Description> failedTests = new LinkedHashSet<>();

        @Override
        public void testFinished(Description description) throws Exception {
            super.testFinished(description);
            if (description.isTest()) {
                allTests.add(description);
            }
        }

        @Override
        public void testFailure(Failure failure) throws Exception {
            super.testFailure(failure);
            Description description = failure.getDescription();
            if (description != null && description.isTest()) {
                failedTests.add(description);
            }
        }

        List<String> getSuccessFullTestNames() {
            Set<Description> tmp = new LinkedHashSet<>(allTests);
            tmp.removeAll(failedTests);
            return tmp.stream().map(Description::getMethodName).collect(Collectors.toList());
        }
    }
}