aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java
blob: 3046b75aae4fbd4a14c4cb818c9ab4664d1ee13a (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 *  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.tools.ant.taskdefs.optional.junit;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Vector;

import junit.framework.AssertionFailedError;
import junit.framework.Test;

import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildListener;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.util.FileUtils;

/**
 * <p>Collects all failing test <i>cases</i> and creates a new JUnit test class containing
 * a suite() method which calls these failed tests.</p>
 * <p>Having classes <i>A</i> ... <i>D</i> with each several testcases you could earn a new
 * test class like
 * <pre>
 * // generated on: 2007.08.06 09:42:34,555
 * import junit.framework.*;
 * public class FailedTests extends TestCase {
 *     public FailedTests(String testname) {
 *         super(testname);
 *     }
 *     public static Test suite() {
 *         TestSuite suite = new TestSuite();
 *         suite.addTest( new B("test04") );
 *         suite.addTest( new org.D("test10") );
 *         return suite;
 *     }
 * }
 * </pre>
 *
 * Because each running test case gets its own formatter, we collect
 * the failing test cases in a static list. Because we dont have a finalizer
 * method in the formatters "lifecycle", we register this formatter as
 * BuildListener and generate the new java source on taskFinished event.
 *
 * @since Ant 1.8.0
 */
public class FailureRecorder extends ProjectComponent implements JUnitResultFormatter, BuildListener {

    /**
     * This is the name of a magic System property ({@value}). The value of this
     * <b>System</b> property should point to the location where to store the
     * generated class (without suffix).
     * Default location and name is defined in DEFAULT_CLASS_LOCATION.
     * @see #DEFAULT_CLASS_LOCATION
     */
    public static final String MAGIC_PROPERTY_CLASS_LOCATION
        = "ant.junit.failureCollector";

    /** Default location and name for the generated JUnit class file,
     *  in the temp directory + FailedTests */
    public static final String DEFAULT_CLASS_LOCATION
        = System.getProperty("java.io.tmpdir") + "FailedTests";

    /** Prefix for logging. {@value} */
    private static final String LOG_PREFIX = "    [junit]";

    /** Class names of failed tests without duplicates. */
    private static SortedSet/*<TestInfos>*/ failedTests = new TreeSet();

    /** A writer for writing the generated source to. */
    private BufferedWriter writer;

    /**
     * Location and name of the generated JUnit class.
     * Lazy instantiated via getLocationName().
     */
    private static String locationName;

    /**
     * Returns the (lazy evaluated) location for the collector class.
     * Order for evaluation: System property > Ant property > default value
     * @return location for the collector class
     * @see #MAGIC_PROPERTY_CLASS_LOCATION
     * @see #DEFAULT_CLASS_LOCATION
     */
    private String getLocationName() {
        if (locationName == null) {
            String syspropValue = System.getProperty(MAGIC_PROPERTY_CLASS_LOCATION);
            String antpropValue = getProject().getProperty(MAGIC_PROPERTY_CLASS_LOCATION);

            if (syspropValue != null) {
                locationName = syspropValue;
                verbose("System property '" + MAGIC_PROPERTY_CLASS_LOCATION + "' set, so use "
                        + "its value '" + syspropValue + "' as location for collector class.");
            } else if (antpropValue != null) {
                locationName = antpropValue;
                verbose("Ant property '" + MAGIC_PROPERTY_CLASS_LOCATION + "' set, so use "
                        + "its value '" + antpropValue + "' as location for collector class.");
            } else {
                locationName = DEFAULT_CLASS_LOCATION;
                verbose("System property '" + MAGIC_PROPERTY_CLASS_LOCATION + "' not set, so use "
                        + "value as location for collector class: '"
                        + DEFAULT_CLASS_LOCATION + "'");
            }

            File locationFile = new File(locationName);
            if (!locationFile.isAbsolute()) {
                File f = new File(getProject().getBaseDir(), locationName);
                locationName = f.getAbsolutePath();
                verbose("Location file is relative (" + locationFile + ")"
                        + " use absolute path instead (" + locationName + ")");
            }
        }

        return locationName;
    }

    /**
     * This method is called by the Ant runtime by reflection. We use the project reference for
     * registration of this class as BuildListener.
     *
     * @param project
     *            project reference
     */
    public void setProject(Project project) {
        // store project reference for logging
        super.setProject(project);
        // check if already registered
        boolean alreadyRegistered = false;
        Vector allListeners = project.getBuildListeners();
        final int size = allListeners.size();
        for (int i = 0; i < size; i++) {
            Object listener = allListeners.get(i);
            if (listener instanceof FailureRecorder) {
                alreadyRegistered = true;
                break;
            }
        }
        // register if needed
        if (!alreadyRegistered) {
            verbose("Register FailureRecorder (@" + this.hashCode() + ") as BuildListener");
            project.addBuildListener(this);
        }
    }

    // ===== JUnitResultFormatter =====

    /**
     * Not used
     * {@inheritDoc}
     */
    public void endTestSuite(JUnitTest suite) throws BuildException {
    }

    /**
     * Add the failed test to the list.
     * @param test the test that errored.
     * @param throwable the reason it errored.
     * @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable)
     */
    public void addError(Test test, Throwable throwable) {
        failedTests.add(new TestInfos(test));
    }

    // CheckStyle:LineLengthCheck OFF - @see is long
    /**
     * Add the failed test to the list.
     * @param test the test that failed.
     * @param error the assertion that failed.
     * @see junit.framework.TestListener#addFailure(junit.framework.Test, junit.framework.AssertionFailedError)
     */
    // CheckStyle:LineLengthCheck ON
    public void addFailure(Test test, AssertionFailedError error) {
        failedTests.add(new TestInfos(test));
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void setOutput(OutputStream out) {
        // unused, close output file so it can be deleted before the VM exits
        if (out != System.out) {
            FileUtils.close(out);
        }
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void setSystemError(String err) {
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void setSystemOutput(String out) {
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void startTestSuite(JUnitTest suite) throws BuildException {
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void endTest(Test test) {
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void startTest(Test test) {
    }

    // ===== "Templates" for generating the JUnit class =====

    private void writeJavaClass() {
        try {
            File sourceFile = new File((getLocationName() + ".java"));
            verbose("Write collector class to '" + sourceFile.getAbsolutePath() + "'");

            if (sourceFile.exists() && !sourceFile.delete()) {
                throw new IOException("could not delete " + sourceFile);
            }
            writer = new BufferedWriter(new FileWriter(sourceFile));

            createClassHeader();
            createSuiteMethod();
            createClassFooter();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            FileUtils.close(writer);
        }
    }

    private void createClassHeader() throws IOException {
        String className = getLocationName().replace('\\', '/');
        if (className.indexOf('/') > -1) {
            className = className.substring(className.lastIndexOf('/') + 1);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss,SSS");
        writer.write("// generated on: ");
        writer.write(sdf.format(new Date()));
        writer.newLine();
        writer.write("import junit.framework.*;");
        writer.newLine();
        writer.write("public class ");
        writer.write(className);
        // If this class does not extend TC, Ant doesn't run these
        writer.write(" extends TestCase {");
        writer.newLine();
        // standard String-constructor
        writer.write("    public ");
        writer.write(className);
        writer.write("(String testname) {");
        writer.newLine();
        writer.write("        super(testname);");
        writer.newLine();
        writer.write("    }");
        writer.newLine();
    }

    private void createSuiteMethod() throws IOException {
        writer.write("    public static Test suite() {");
        writer.newLine();
        writer.write("        TestSuite suite = new TestSuite();");
        writer.newLine();
        for (Iterator iter = failedTests.iterator(); iter.hasNext();) {
            TestInfos testInfos = (TestInfos) iter.next();
            writer.write("        suite.addTest(");
            writer.write(String.valueOf(testInfos));
            writer.write(");");
            writer.newLine();
        }
        writer.write("        return suite;");
        writer.newLine();
        writer.write("    }");
        writer.newLine();
    }

    private void createClassFooter() throws IOException {
        writer.write("}");
        writer.newLine();
    }

    // ===== Helper classes and methods =====

    /**
     * Logging facade in INFO-mode.
     * @param message Log-message
     */
    public void log(String message) {
        getProject().log(LOG_PREFIX + " " + message, Project.MSG_INFO);
    }

    /**
     * Logging facade in VERBOSE-mode.
     * @param message Log-message
     */
    public void verbose(String message) {
        getProject().log(LOG_PREFIX + " " + message, Project.MSG_VERBOSE);
    }

    /**
     * TestInfos holds information about a given test for later use.
     */
    public static class TestInfos implements Comparable {

        /** The class name of the test. */
        private final String className;

        /** The method name of the testcase. */
        private final String methodName;

        /**
         * This constructor extracts the needed information from the given test.
         * @param test Test to analyze
         */
        public TestInfos(Test test) {
            className = test.getClass().getName();
            String _methodName = test.toString();
            methodName = _methodName.substring(0, _methodName.indexOf('('));
        }

        /**
         * This String-Representation can directly be used for instantiation of
         * the JUnit testcase.
         * @return the string representation.
         * @see java.lang.Object#toString()
         * @see FailureRecorder#createSuiteMethod()
         */
        public String toString() {
            return "new " + className + "(\"" + methodName + "\")";
        }

        /**
         * The SortedMap needs comparable elements.
         * @param other the object to compare to.
         * @return the result of the comparison.
         * @see java.lang.Comparable#compareTo
         * @see SortedSet#comparator()
         */
        public int compareTo(Object other) {
            if (other instanceof TestInfos) {
                TestInfos otherInfos = (TestInfos) other;
                return toString().compareTo(otherInfos.toString());
            } else {
                return -1;
            }
        }
        public boolean equals(Object obj) {
            return obj instanceof TestInfos && toString().equals(obj.toString());
        }
        public int hashCode() {
            return toString().hashCode();
        }
    }

    // ===== BuildListener =====

    /**
     * Not used
     * {@inheritDoc}
     */
    public void buildFinished(BuildEvent event) {
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void buildStarted(BuildEvent event) {
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void messageLogged(BuildEvent event) {
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void targetFinished(BuildEvent event) {
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void targetStarted(BuildEvent event) {
    }

    /**
     * The task outside of this JUnitResultFormatter is the <junit> task. So all tests passed
     * and we could create the new java class.
     * @param event  not used
     * @see org.apache.tools.ant.BuildListener#taskFinished(org.apache.tools.ant.BuildEvent)
     */
    public void taskFinished(BuildEvent event) {
        if (!failedTests.isEmpty()) {
            writeJavaClass();
        }
    }

    /**
     * Not used
     * {@inheritDoc}
     */
    public void taskStarted(BuildEvent event) {
    }

}