summaryrefslogtreecommitdiffstats
path: root/sandbox/axis2-1.4/modules/host-webapp-junit/src/main/java/org/apache/tuscany/sca/host/webapp/junit/JUnitServletFilter.java
blob: b2d3d0abf1f9d506fc6b443288bf4d6bc2d89765 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.host.webapp.junit;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import junit.framework.AssertionFailedError;

import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

/**
 * @version $Rev$ $Date$
 */
public class JUnitServletFilter implements Filter {
    private static final Logger logger = Logger.getLogger(JUnitServletFilter.class.getName());

    private static final String JUNIT_TESTS_PATTERN = "junit.tests.pattern";
    private static final String JUNIT_TESTS_PATH = "junit.tests.path";
    private static final String JUNIT_ENABLED = "junit.enabled";
    private static final String TESTCASE_PATTERN = ".*TestCase";
    private static final String TESTS_JAR = "/WEB-INF/test-lib/junit-tests.jar";

    private FilterConfig config;
    private boolean junitEnabled = true;
    private Set<String> allTestCases;
    private ClassLoader testClassLoader;

    private Set<String> findTestCases(String testJarPath) throws IOException {
        Pattern pattern = getTestCasePattern();
        if (testJarPath.endsWith(".jar")) {
            return findTestCasesInJar(testJarPath, pattern);
        } else {
            return findTestCasesInDir(testJarPath, pattern);
        }
    }

    /**
     * Search test cases in a JAR
     * @param testJarPath
     * @param pattern
     * @return
     * @throws IOException
     */
    private Set<String> findTestCasesInJar(String testJarPath, Pattern pattern) throws IOException {
        InputStream in = config.getServletContext().getResourceAsStream(testJarPath);
        Set<String> tests = new HashSet<String>();
        if (in != null) {
            JarInputStream jar = new JarInputStream(in);
            try {
                JarEntry entry = null;

                while ((entry = jar.getNextJarEntry()) != null) {
                    String name = entry.getName();

                    if (name.endsWith(".class")) {
                        String className = name.substring(0, name.length() - 6).replace('/', '.');
                        if (pattern.matcher(className).matches()) {
                            tests.add(className);
                        }
                    }
                }
            } catch (EOFException e) {
            } finally {
                if (jar != null) {
                    try {
                        jar.close();
                    } catch (IOException e) {
                    }
                }
            }

        }
        return tests;
    }

    private Pattern getTestCasePattern() {
        String filter = config.getInitParameter(JUNIT_TESTS_PATTERN);
        if (filter == null) {
            filter = TESTCASE_PATTERN;
        }
        Pattern pattern = Pattern.compile(filter);
        return pattern;
    }

    public void destroy() {
    }

    private void init() throws IOException {
        testClassLoader = Thread.currentThread().getContextClassLoader();
        allTestCases = new HashSet<String>();
        String testsPath = config.getInitParameter(JUNIT_TESTS_PATH);
        if (testsPath == null) {
            testsPath = TESTS_JAR;
        }
        URL url = config.getServletContext().getResource(testsPath);
        if (url != null) {
            allTestCases = findTestCases(testsPath);
            if (!(testsPath.startsWith("/WEB-INF/lib/") || testsPath.startsWith("/WEB-INF/classes/"))) {
                // Create a new ClassLoader to load the test jar
                testClassLoader = new URLClassLoader(new URL[] {url}, testClassLoader);
            }
        }
    }

    /**
     * Search test cases in a directory
     * @param classesPath
     * @param pattern
     * @return
     */
    private Set<String> findTestCasesInDir(String classesPath, Pattern pattern) {
        ServletContext context = config.getServletContext();
        Set<String> tests = new HashSet<String>();
        String dir = classesPath;
        findResources(context, pattern, tests, classesPath, dir);
        return tests;
    }

    @SuppressWarnings("unchecked")
    private void findResources(ServletContext context, Pattern pattern, Set<String> tests, String root, String dir) {
        Set<String> paths = context.getResourcePaths(dir);
        if (paths != null) {
            for (String name : paths) {
                if (name.endsWith("/")) {
                    findResources(context, pattern, tests, root, name);
                }
                if (name.endsWith(".class")) {
                    String className = name.substring(root.length(), name.length() - 6).replace('/', '.');
                    if (pattern.matcher(className).matches()) {
                        tests.add(className);
                    }
                }
            }
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
        ServletException {

        if (!junitEnabled) {
            chain.doFilter(request, response);
            return;
        }

        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;

        if (!req.getRequestURI().equals(req.getContextPath() + "/junit")) {
            // Intercept the /junit call
            chain.doFilter(request, response);
            return;
        }

        String query = req.getQueryString();
        PrintStream ps = new PrintStream(response.getOutputStream());

        Set<String> testCases = null;
        // ClassLoader cl = Thread.currentThread().getContextClassLoader();
        String op = req.getParameter("op");
        if (query == null || op == null || "list".equalsIgnoreCase(op)) {
            response.setContentType("text/html");
            resp.setStatus(HttpServletResponse.SC_OK);
            ps.println("<html><body>");
            ps.println("<h2>Available Test Cases</h2><p>");
            ps.println("<form method=\"get\" action=\"junit\">");
            ps.println("<table border=\"1\">");
            for (String s : this.allTestCases) {
                ps.print("<tr><td>");
                ps.print("<input type=\"checkbox\" name=\"test\" value=\"" + s
                    + "\"/><a href=\"junit?op=runSelected&test="
                    + s
                    + "\">"
                    + s
                    + "</a>");
                ps.println("</td></tr>");
            }
            ps.println("</table>");
            ps.println("<p><input type=\"submit\" name=\"op\" value=\"RunSelected\"/>");
            ps.println("<input type=\"submit\" name=\"op\" value=\"RunAll\"/>");
            ps.println("</form></body></html>");
            resp.flushBuffer();
            return;
        } else {
            if ("runAll".equalsIgnoreCase(op)) {
                testCases = this.allTestCases;
            } else {
                String[] tests = req.getParameterValues("test");
                if (tests == null) {
                    tests = new String[0];
                }
                testCases = new HashSet<String>(Arrays.asList(tests));
            }
        }

        response.setContentType("application/xml");
        ps.println("<?xml version=\"1.0\" encoding=\"" + "UTF-8" + "\"?>");

        ServletContext context = config.getServletContext();
        Object domain = context.getAttribute("org.apache.tuscany.sca.SCADomain");
        URL contribution = context.getResource("/META-INF/sca-contribution.xml");

        long duration = 0L;
        int errors = 0;
        int failures = 0;
        int runs = 0;
        List<Class<?>> testClasses = new ArrayList<Class<?>>();
        List<Result> results = new ArrayList<Result>();
        for (String testClass : testCases) {
            Class<?> test = null;
            try {
                test = Class.forName(testClass, false, testClassLoader);
                if (domain != null && contribution != null) {
                    // Inject the SCADomain
                    inject(test, domain);
                }
                testClasses.add(test);
            } catch (ClassNotFoundException e) {
                String st = XMLFormatter.exceptionToString(e);
                st = XMLFormatter.escape(st);
                ps.println(st);
                // ps.close();
                throw new ServletException(e);
            }

            JUnitCore core = new JUnitCore();
            Result result = core.run(Request.aClass(test));
            results.add(result);

            duration += result.getRunTime();
            runs += result.getRunCount();

            for (Failure f : result.getFailures()) {
                if (f.getException() instanceof AssertionFailedError) {
                    failures++;
                } else {
                    errors++;
                }
            }
        }

        ps.println("<" + XMLFormatter.TESTSUITE
            + " "
            + XMLFormatter.ATTR_TESTS
            + "=\""
            + runs
            + "\" "
            + XMLFormatter.ATTR_FAILURES
            + "=\""
            + failures
            + "\" "
            + XMLFormatter.ATTR_ERRORS
            + "=\""
            + errors
            + "\" "
            + XMLFormatter.ATTR_TIME
            + "=\""
            + XMLFormatter.getDurationAsString(duration)
            + "\">");
        for (int i = 0; i < testClasses.size(); i++) {
            ps.println(XMLFormatter.toXML(results.get(i), testClasses.get(i)));
        }
        ps.println("</" + XMLFormatter.TESTSUITE + ">");

        resp.addIntHeader("junit.errors", errors);
        resp.addIntHeader("junit.failures", failures);
        resp.addIntHeader("junit.runs", runs);
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.flushBuffer();
        
        ps.close();    
    }

    private boolean inject(Class<?> cls, Object target) {
        for (Field f : cls.getDeclaredFields()) {
            if (Modifier.isStatic(f.getModifiers()) && f.getType().isInstance(target)) {
                f.setAccessible(true);
                try {
                    f.set(null, target);
                    return true;
                } catch (IllegalArgumentException e) {
                    return false;
                } catch (IllegalAccessException e) {
                    return false;
                }
            }
        }
        return false;
    }

    public void init(FilterConfig config) throws ServletException {
        this.config = config;
        // Check if the /junit path should be allowed
        String param = config.getInitParameter(JUNIT_ENABLED);
        if (param != null && param.trim().equals("false")) {
            junitEnabled = false;
            return;
        }
        try {
            init();
        } catch (IOException e) {
            throw new ServletException(e);
        }
    }

    public boolean isJunitEnabled() {
        return junitEnabled;
    }

}