View | Details | Raw Unified | Return to bug 38799
Collapse All | Expand All

(-)src/main/org/apache/tools/ant/AntClassLoader.java (-1 / +5 lines)
Lines 1-5 Link Here
1
/*
1
/*
2
 * Copyright  2000-2005 The Apache Software Foundation
2
 * Copyright  2000-2006 The Apache Software Foundation
3
 *
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
5
 *  you may not use this file except in compliance with the License.
Lines 1490-1493 Link Here
1490
        }
1490
        }
1491
    }
1491
    }
1492
1492
1493
    public String toString() {
1494
        return "AntClassLoader[" + getClasspath() + "]";
1495
    }
1496
1493
}
1497
}
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskMirror.java (+108 lines)
Line 0 Link Here
1
/*
2
 * Copyright  2006 The Apache Software Foundation
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 *
16
 */
17
18
package org.apache.tools.ant.taskdefs.optional.junit;
19
20
import java.io.IOException;
21
import java.io.OutputStream;
22
import org.apache.tools.ant.AntClassLoader;
23
import org.apache.tools.ant.types.Permissions;
24
25
/**
26
 * Handles the portions of {@link JUnitTask} which need to directly access
27
 * actual JUnit classes, so that junit.jar need not be on Ant's startup classpath.
28
 * Neither JUnitTask.java nor JUnitTaskMirror.java nor their transitive static
29
 * deps may import any junit.** classes!
30
 * Specifically, need to not refer to
31
 * - JUnitResultFormatter or its subclasses
32
 * - JUnitVersionHelper
33
 * - JUnitTestRunner
34
 * Cf. {@link JUnitTask.SplitLoader#isSplit}
35
 * 
36
 * @author refactoring tricks by Jesse Glick, real code by others
37
 * @since 1.7
38
 */
39
public interface JUnitTaskMirror {
40
    
41
    void addVmExit(JUnitTest test, JUnitResultFormatterMirror formatter,
42
            OutputStream out, final String message);
43
    
44
    JUnitTestRunnerMirror newJUnitTestRunner(JUnitTest test, boolean haltOnError,
45
            boolean filterTrace, boolean haltOnFailure, boolean showOutput,
46
            boolean logTestListenerEvents, AntClassLoader classLoader);
47
48
    SummaryJUnitResultFormatterMirror newSummaryJUnitResultFormatter();
49
50
    public interface JUnitResultFormatterMirror {
51
52
        void setOutput(OutputStream outputStream);
53
54
    }
55
56
    public interface SummaryJUnitResultFormatterMirror extends JUnitResultFormatterMirror {
57
58
        void setWithOutAndErr(boolean value);
59
60
    }
61
62
    public interface JUnitTestRunnerMirror {
63
64
        /**
65
         * Used in formatter arguments as a placeholder for the basename
66
         * of the output file (which gets replaced by a test specific
67
         * output file name later).
68
         *
69
         * @since Ant 1.6.3
70
         */
71
        String IGNORED_FILE_NAME = "IGNORETHIS";
72
73
        /**
74
         * No problems with this test.
75
         */
76
        int SUCCESS = 0;
77
    
78
        /**
79
         * Some tests failed.
80
         */
81
        int FAILURES = 1;
82
83
        /**
84
         * An error occurred.
85
         */
86
        int ERRORS = 2;
87
88
        void setPermissions(Permissions perm);
89
    
90
        void run();
91
92
        void addFormatter(JUnitResultFormatterMirror formatter);
93
    
94
        int getRetCode();
95
    
96
        void handleErrorFlush(String output);
97
    
98
        void handleErrorOutput(String output);
99
    
100
        void handleOutput(String output);
101
    
102
        int handleInput(byte[] buffer, int offset, int length) throws IOException;
103
    
104
        void handleFlush(String output);
105
106
    }
107
    
108
}
0
  + native
109
  + native
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java (-32 / +111 lines)
Lines 24-29 Link Here
24
import java.io.IOException;
24
import java.io.IOException;
25
import java.io.OutputStream;
25
import java.io.OutputStream;
26
import java.io.PrintWriter;
26
import java.io.PrintWriter;
27
import java.lang.reflect.Constructor;
27
import java.util.ArrayList;
28
import java.util.ArrayList;
28
import java.util.Collection;
29
import java.util.Collection;
29
import java.util.Enumeration;
30
import java.util.Enumeration;
Lines 34-42 Link Here
34
import java.util.Map;
35
import java.util.Map;
35
import java.util.Properties;
36
import java.util.Properties;
36
import java.util.Vector;
37
import java.util.Vector;
37
import junit.framework.AssertionFailedError;
38
import junit.framework.Test;
39
import junit.framework.TestResult;
40
import org.apache.tools.ant.AntClassLoader;
38
import org.apache.tools.ant.AntClassLoader;
41
import org.apache.tools.ant.BuildException;
39
import org.apache.tools.ant.BuildException;
42
import org.apache.tools.ant.Project;
40
import org.apache.tools.ant.Project;
Lines 134-140 Link Here
134
    private boolean summary = false;
132
    private boolean summary = false;
135
    private boolean reloading = true;
133
    private boolean reloading = true;
136
    private String summaryValue = "";
134
    private String summaryValue = "";
137
    private JUnitTestRunner runner = null;
135
    private JUnitTaskMirror.JUnitTestRunnerMirror runner = null;
138
136
139
    private boolean newEnvironment = false;
137
    private boolean newEnvironment = false;
140
    private Environment env = new Environment();
138
    private Environment env = new Environment();
Lines 148-153 Link Here
148
    private Permissions perm = null;
146
    private Permissions perm = null;
149
    private ForkMode forkMode = new ForkMode("perTest");
147
    private ForkMode forkMode = new ForkMode("perTest");
150
148
149
    private boolean splitJunit = false;
150
    private JUnitTaskMirror delegate;
151
151
    private static final int STRING_BUFFER_SIZE = 128;
152
    private static final int STRING_BUFFER_SIZE = 128;
152
    /**
153
    /**
153
     * @since Ant 1.7
154
     * @since Ant 1.7
Lines 632-643 Link Here
632
     */
633
     */
633
    public void init() {
634
    public void init() {
634
        antRuntimeClasses = new Path(getProject());
635
        antRuntimeClasses = new Path(getProject());
635
        addClasspathEntry("/junit/framework/TestCase.class");
636
        splitJunit = !addClasspathEntry("/junit/framework/TestCase.class");
636
        addClasspathEntry("/org/apache/tools/ant/launch/AntMain.class");
637
        addClasspathEntry("/org/apache/tools/ant/launch/AntMain.class");
637
        addClasspathEntry("/org/apache/tools/ant/Task.class");
638
        addClasspathEntry("/org/apache/tools/ant/Task.class");
638
        addClasspathEntry("/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.class");
639
        addClasspathEntry("/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.class");
639
    }
640
    }
640
641
642
    private static JUnitTaskMirror createMirror(JUnitTask task, ClassLoader loader) {
643
        try {
644
            loader.loadClass("junit.framework.Test"); // sanity check
645
        } catch (ClassNotFoundException e) {
646
            throw new BuildException(
647
                    "The <classpath> for <junit> must include junit.jar if not in Ant's own classpath",
648
                    e, task.getLocation());
649
        }
650
        try {
651
            Class c = loader.loadClass(JUnitTaskMirror.class.getName() + "Impl");
652
            if (c.getClassLoader() != loader) {
653
                throw new BuildException("Overdelegating loader", task.getLocation());
654
            }
655
            Constructor cons = c.getConstructor(new Class[] {JUnitTask.class});
656
            return (JUnitTaskMirror) cons.newInstance(new Object[] {task});
657
        } catch (Exception e) {
658
            throw new BuildException(e, task.getLocation());
659
        }
660
    }
661
662
    private final class SplitLoader extends AntClassLoader {
663
664
        public SplitLoader(ClassLoader parent, Path path) {
665
            super(parent, getProject(), path, true);
666
        }
667
668
        // forceLoadClass is not convenient here since it would not
669
        // properly deal with inner classes of these classes.
670
        protected synchronized Class loadClass(String classname, boolean resolve)
671
        throws ClassNotFoundException {
672
            Class theClass = findLoadedClass(classname);
673
            if (theClass != null) {
674
                return theClass;
675
            }
676
            if (isSplit(classname)) {
677
                theClass = findClass(classname);
678
                if (resolve) {
679
                    resolveClass(theClass);
680
                }
681
                return theClass;
682
            } else {
683
                return super.loadClass(classname, resolve);
684
            }
685
        }
686
687
        private final String[] SPLIT_CLASSES = {
688
            "BriefJUnitResultFormatter",
689
            "JUnitResultFormatter",
690
            "JUnitTaskMirrorImpl",
691
            "JUnitTestRunner",
692
            "JUnitVersionHelper",
693
            "OutErrSummaryJUnitResultFormatter",
694
            "PlainJUnitResultFormatter",
695
            "SummaryJUnitResultFormatter",
696
            "XMLJUnitResultFormatter",
697
        };
698
699
        private boolean isSplit(String classname) {
700
            String simplename = classname.substring(classname.lastIndexOf('.') + 1);
701
            for (int i = 0; i < SPLIT_CLASSES.length; i++) {
702
                if (simplename.equals(SPLIT_CLASSES[i]) || simplename.startsWith(SPLIT_CLASSES[i] + '$')) {
703
                    return true;
704
                }
705
            }
706
            return false;
707
        }
708
709
    }
710
    
641
    /**
711
    /**
642
     * Runs the testcase.
712
     * Runs the testcase.
643
     *
713
     *
Lines 645-650 Link Here
645
     * @since Ant 1.2
715
     * @since Ant 1.2
646
     */
716
     */
647
    public void execute() throws BuildException {
717
    public void execute() throws BuildException {
718
        ClassLoader myLoader = JUnitTask.class.getClassLoader();
719
        ClassLoader mirrorLoader;
720
        if (splitJunit) {
721
            Path path = new Path(getProject());
722
            path.add(antRuntimeClasses);
723
            path.add(getCommandline().getClasspath());
724
            mirrorLoader = new SplitLoader(myLoader, path);
725
        } else {
726
            mirrorLoader = myLoader;
727
        }
728
        delegate = createMirror(this, mirrorLoader);
729
648
        List testLists = new ArrayList();
730
        List testLists = new ArrayList();
649
731
650
        boolean forkPerTest = forkMode.getValue().equals(ForkMode.PER_TEST);
732
        boolean forkPerTest = forkMode.getValue().equals(ForkMode.PER_TEST);
Lines 672-677 Link Here
672
            }
754
            }
673
        } finally {
755
        } finally {
674
            deleteClassLoader();
756
            deleteClassLoader();
757
            if (mirrorLoader instanceof SplitLoader) {
758
                ((SplitLoader) mirrorLoader).cleanup();
759
            }
760
            delegate = null;
675
        }
761
        }
676
    }
762
    }
677
763
Lines 1061-1078 Link Here
1061
        try {
1147
        try {
1062
            log("Using System properties " + System.getProperties(),
1148
            log("Using System properties " + System.getProperties(),
1063
                Project.MSG_VERBOSE);
1149
                Project.MSG_VERBOSE);
1064
            createClassLoader();
1150
            if (splitJunit) {
1151
                classLoader = (AntClassLoader) delegate.getClass().getClassLoader();
1152
            } else {
1153
                createClassLoader();
1154
            }
1065
            if (classLoader != null) {
1155
            if (classLoader != null) {
1066
                classLoader.setThreadContextLoader();
1156
                classLoader.setThreadContextLoader();
1067
            }
1157
            }
1068
            runner = new JUnitTestRunner(test, test.getHaltonerror(),
1158
            runner = delegate.newJUnitTestRunner(test, test.getHaltonerror(),
1069
                                         test.getFiltertrace(),
1159
                                         test.getFiltertrace(),
1070
                                         test.getHaltonfailure(), false,
1160
                                         test.getHaltonfailure(), false,
1071
                                         true, classLoader);
1161
                                         true, classLoader);
1072
            if (summary) {
1162
            if (summary) {
1073
1163
1074
                SummaryJUnitResultFormatter f =
1164
                JUnitTaskMirror.SummaryJUnitResultFormatterMirror f =
1075
                    new SummaryJUnitResultFormatter();
1165
                    delegate.newSummaryJUnitResultFormatter();
1076
                f.setWithOutAndErr("withoutanderr"
1166
                f.setWithOutAndErr("withoutanderr"
1077
                                   .equalsIgnoreCase(summaryValue));
1167
                                   .equalsIgnoreCase(summaryValue));
1078
                f.setOutput(getDefaultOutput());
1168
                f.setOutput(getDefaultOutput());
Lines 1186-1192 Link Here
1186
        if (fe.getUseFile()) {
1276
        if (fe.getUseFile()) {
1187
            String base = test.getOutfile();
1277
            String base = test.getOutfile();
1188
            if (base == null) {
1278
            if (base == null) {
1189
                base = JUnitTestRunner.IGNORED_FILE_NAME;
1279
                base = JUnitTaskMirror.JUnitTestRunnerMirror.IGNORED_FILE_NAME;
1190
            }
1280
            }
1191
            String filename = base + fe.getExtension();
1281
            String filename = base + fe.getExtension();
1192
            File destFile = new File(test.getTodir(), filename);
1282
            File destFile = new File(test.getTodir(), filename);
Lines 1204-1212 Link Here
1204
     * getResource doesn't contain the name of the archive.</p>
1294
     * getResource doesn't contain the name of the archive.</p>
1205
     *
1295
     *
1206
     * @param resource resource that one wants to lookup
1296
     * @param resource resource that one wants to lookup
1297
     * @return true if something was in fact added
1207
     * @since Ant 1.4
1298
     * @since Ant 1.4
1208
     */
1299
     */
1209
    protected void addClasspathEntry(String resource) {
1300
    protected boolean addClasspathEntry(String resource) {
1210
        /*
1301
        /*
1211
         * pre Ant 1.6 this method used to call getClass().getResource
1302
         * pre Ant 1.6 this method used to call getClass().getResource
1212
         * while Ant 1.6 will call ClassLoader.getResource().
1303
         * while Ant 1.6 will call ClassLoader.getResource().
Lines 1228-1235 Link Here
1228
        if (f != null) {
1319
        if (f != null) {
1229
            log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG);
1320
            log("Found " + f.getAbsolutePath(), Project.MSG_DEBUG);
1230
            antRuntimeClasses.createPath().setLocation(f);
1321
            antRuntimeClasses.createPath().setLocation(f);
1322
            return true;
1231
        } else {
1323
        } else {
1232
            log("Couldn\'t find " + resource, Project.MSG_DEBUG);
1324
            log("Couldn\'t find " + resource, Project.MSG_DEBUG);
1325
            return false;
1233
        }
1326
        }
1234
    }
1327
    }
1235
1328
Lines 1269-1275 Link Here
1269
        for (int i = 0; i < feArray.length; i++) {
1362
        for (int i = 0; i < feArray.length; i++) {
1270
            FormatterElement fe = feArray[i];
1363
            FormatterElement fe = feArray[i];
1271
            File outFile = getOutput(fe, test);
1364
            File outFile = getOutput(fe, test);
1272
            JUnitResultFormatter formatter = fe.createFormatter(classLoader);
1365
            JUnitTaskMirror.JUnitResultFormatterMirror formatter = fe.createFormatter(classLoader);
1273
            if (outFile != null && formatter != null) {
1366
            if (outFile != null && formatter != null) {
1274
                try {
1367
                try {
1275
                    OutputStream out = new FileOutputStream(outFile);
1368
                    OutputStream out = new FileOutputStream(outFile);
Lines 1280-1286 Link Here
1280
            }
1373
            }
1281
        }
1374
        }
1282
        if (summary) {
1375
        if (summary) {
1283
            SummaryJUnitResultFormatter f = new SummaryJUnitResultFormatter();
1376
            JUnitTaskMirror.SummaryJUnitResultFormatterMirror f = delegate.newSummaryJUnitResultFormatter();
1284
            f.setWithOutAndErr("withoutanderr".equalsIgnoreCase(summaryValue));
1377
            f.setWithOutAndErr("withoutanderr".equalsIgnoreCase(summaryValue));
1285
            addVmExit(test, f, getDefaultOutput(), message);
1378
            addVmExit(test, f, getDefaultOutput(), message);
1286
        }
1379
        }
Lines 1291-1313 Link Here
1291
     * Only used from the logVmExit method.
1384
     * Only used from the logVmExit method.
1292
     * @since Ant 1.7
1385
     * @since Ant 1.7
1293
     */
1386
     */
1294
    private void addVmExit(JUnitTest test, JUnitResultFormatter formatter,
1387
    private void addVmExit(JUnitTest test, JUnitTaskMirror.JUnitResultFormatterMirror formatter,
1295
                           OutputStream out, final String message) {
1388
                           OutputStream out, final String message) {
1296
        formatter.setOutput(out);
1389
        delegate.addVmExit(test, formatter, out, message);
1297
        formatter.startTestSuite(test);
1298
1299
        //the trick to integrating test output to the formatter, is to
1300
        //create a special test class that asserts an error
1301
        //and tell the formatter that it raised.
1302
        Test t = new Test() {
1303
            public int countTestCases() { return 1; }
1304
            public void run(TestResult r) {
1305
                throw new AssertionFailedError(message);
1306
            }
1307
        };
1308
        formatter.startTest(t);
1309
        formatter.addError(t, new AssertionFailedError(message));
1310
        formatter.endTestSuite(test);
1311
    }
1390
    }
1312
1391
1313
    /**
1392
    /**
Lines 1535-1543 Link Here
1535
        // everything otherwise just log a statement
1614
        // everything otherwise just log a statement
1536
        boolean fatal = result.timedOut || result.crashed;
1615
        boolean fatal = result.timedOut || result.crashed;
1537
        boolean errorOccurredHere =
1616
        boolean errorOccurredHere =
1538
            result.exitCode == JUnitTestRunner.ERRORS || fatal;
1617
            result.exitCode == JUnitTaskMirror.JUnitTestRunnerMirror.ERRORS || fatal;
1539
        boolean failureOccurredHere =
1618
        boolean failureOccurredHere =
1540
            result.exitCode != JUnitTestRunner.SUCCESS || fatal;
1619
            result.exitCode != JUnitTaskMirror.JUnitTestRunnerMirror.SUCCESS || fatal;
1541
        if (errorOccurredHere || failureOccurredHere) {
1620
        if (errorOccurredHere || failureOccurredHere) {
1542
            if ((errorOccurredHere && test.getHaltonerror())
1621
            if ((errorOccurredHere && test.getHaltonerror())
1543
                || (failureOccurredHere && test.getHaltonfailure())) {
1622
                || (failureOccurredHere && test.getHaltonfailure())) {
Lines 1559-1565 Link Here
1559
    }
1638
    }
1560
1639
1561
    protected class TestResultHolder {
1640
    protected class TestResultHolder {
1562
        public int exitCode = JUnitTestRunner.ERRORS;
1641
        public int exitCode = JUnitTaskMirror.JUnitTestRunnerMirror.ERRORS;
1563
        public boolean timedOut = false;
1642
        public boolean timedOut = false;
1564
        public boolean crashed = false;
1643
        public boolean crashed = false;
1565
    }
1644
    }
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitResultFormatter.java (-2 / +2 lines)
Lines 1-5 Link Here
1
/*
1
/*
2
 * Copyright  2001-2002,2004 The Apache Software Foundation
2
 * Copyright  2001-2002,2004,2006 The Apache Software Foundation
3
 *
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
5
 *  you may not use this file except in compliance with the License.
Lines 26-32 Link Here
26
 * testrun.
26
 * testrun.
27
 *
27
 *
28
 */
28
 */
29
public interface JUnitResultFormatter extends TestListener {
29
public interface JUnitResultFormatter extends TestListener, JUnitTaskMirror.JUnitResultFormatterMirror {
30
    /**
30
    /**
31
     * The whole testsuite started.
31
     * The whole testsuite started.
32
     */
32
     */
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/FormatterElement.java (-7 / +8 lines)
Lines 1-5 Link Here
1
/*
1
/*
2
 * Copyright  2001-2004 The Apache Software Foundation
2
 * Copyright  2001-2004,2006 The Apache Software Foundation
3
 *
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
5
 *  you may not use this file except in compliance with the License.
Lines 188-201 Link Here
188
    /**
188
    /**
189
     * @since Ant 1.2
189
     * @since Ant 1.2
190
     */
190
     */
191
    JUnitResultFormatter createFormatter() throws BuildException {
191
    JUnitTaskMirror.JUnitResultFormatterMirror createFormatter() throws BuildException {
192
        return createFormatter(null);
192
        return createFormatter(null);
193
    }
193
    }
194
194
195
    /**
195
    /**
196
     * @since Ant 1.6
196
     * @since Ant 1.6
197
     */
197
     */
198
    JUnitResultFormatter createFormatter(ClassLoader loader)
198
    JUnitTaskMirror.JUnitResultFormatterMirror createFormatter(ClassLoader loader)
199
        throws BuildException {
199
        throws BuildException {
200
200
201
        if (classname == null) {
201
        if (classname == null) {
Lines 210-216 Link Here
210
                f = Class.forName(classname, true, loader);
210
                f = Class.forName(classname, true, loader);
211
            }
211
            }
212
        } catch (ClassNotFoundException e) {
212
        } catch (ClassNotFoundException e) {
213
            throw new BuildException(e);
213
            throw new BuildException("Using loader " + loader + " on class " + classname + ": " + e, e);
214
        } catch (NoClassDefFoundError e) {
215
            throw new BuildException("Using loader " + loader + " on class " + classname + ": " + e, e);
214
        }
216
        }
215
217
216
        Object o = null;
218
        Object o = null;
Lines 222-234 Link Here
222
            throw new BuildException(e);
224
            throw new BuildException(e);
223
        }
225
        }
224
226
225
        if (!(o instanceof JUnitResultFormatter)) {
227
        if (!(o instanceof JUnitTaskMirror.JUnitResultFormatterMirror)) {
226
            throw new BuildException(classname
228
            throw new BuildException(classname
227
                + " is not a JUnitResultFormatter");
229
                + " is not a JUnitResultFormatter");
228
        }
230
        }
231
        JUnitTaskMirror.JUnitResultFormatterMirror r = (JUnitTaskMirror.JUnitResultFormatterMirror) o;
229
232
230
        JUnitResultFormatter r = (JUnitResultFormatter) o;
231
232
        if (useFile && outFile != null) {
233
        if (useFile && outFile != null) {
233
            try {
234
            try {
234
                out = new FileOutputStream(outFile);
235
                out = new FileOutputStream(outFile);
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java (-32 / +12 lines)
Lines 1-5 Link Here
1
/*
1
/*
2
 * Copyright  2000-2005 The Apache Software Foundation
2
 * Copyright  2000-2006 The Apache Software Foundation
3
 *
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
5
 *  you may not use this file except in compliance with the License.
Lines 62-94 Link Here
62
 * @since Ant 1.2
62
 * @since Ant 1.2
63
 */
63
 */
64
64
65
public class JUnitTestRunner implements TestListener {
65
public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestRunnerMirror {
66
66
67
    /**
67
    /**
68
     * No problems with this test.
69
     */
70
    public static final int SUCCESS = 0;
71
72
    /**
73
     * Some tests failed.
74
     */
75
    public static final int FAILURES = 1;
76
77
    /**
78
     * An error occurred.
79
     */
80
    public static final int ERRORS = 2;
81
82
    /**
83
     * Used in formatter arguments as a placeholder for the basename
84
     * of the output file (which gets replaced by a test specific
85
     * output file name later).
86
     *
87
     * @since Ant 1.6.3
88
     */
89
    public static final String IGNORED_FILE_NAME = "IGNORETHIS";
90
91
    /**
92
     * Holds the registered formatters.
68
     * Holds the registered formatters.
93
     */
69
     */
94
    private Vector formatters = new Vector();
70
    private Vector formatters = new Vector();
Lines 441-447 Link Here
441
        perm = permissions;
417
        perm = permissions;
442
    }
418
    }
443
419
444
    protected void handleOutput(String output) {
420
    public void handleOutput(String output) {
445
        if (!logTestListenerEvents && output.startsWith(JUnitTask.TESTLISTENER_PREFIX))
421
        if (!logTestListenerEvents && output.startsWith(JUnitTask.TESTLISTENER_PREFIX))
446
            ; // ignore
422
            ; // ignore
447
        else if (systemOut != null) {
423
        else if (systemOut != null) {
Lines 454-477 Link Here
454
     *
430
     *
455
     * @since Ant 1.6
431
     * @since Ant 1.6
456
     */
432
     */
457
    protected int handleInput(byte[] buffer, int offset, int length)
433
    public int handleInput(byte[] buffer, int offset, int length)
458
        throws IOException {
434
        throws IOException {
459
        return -1;
435
        return -1;
460
    }
436
    }
461
437
462
    protected void handleErrorOutput(String output) {
438
    public void handleErrorOutput(String output) {
463
        if (systemError != null) {
439
        if (systemError != null) {
464
            systemError.print(output);
440
            systemError.print(output);
465
        }
441
        }
466
    }
442
    }
467
443
468
    protected void handleFlush(String output) {
444
    public void handleFlush(String output) {
469
        if (systemOut != null) {
445
        if (systemOut != null) {
470
            systemOut.print(output);
446
            systemOut.print(output);
471
        }
447
        }
472
    }
448
    }
473
449
474
    protected void handleErrorFlush(String output) {
450
    public void handleErrorFlush(String output) {
475
        if (systemError != null) {
451
        if (systemError != null) {
476
            systemError.print(output);
452
            systemError.print(output);
477
        }
453
        }
Lines 505-510 Link Here
505
        formatters.addElement(f);
481
        formatters.addElement(f);
506
    }
482
    }
507
483
484
    public void addFormatter(JUnitTaskMirror.JUnitResultFormatterMirror f) {
485
        formatters.addElement((JUnitResultFormatter) f);
486
    }
487
508
    /**
488
    /**
509
     * Entry point for standalone (forked) mode.
489
     * Entry point for standalone (forked) mode.
510
     *
490
     *
Lines 645-651 Link Here
645
                             test.getOutfile() + fe.getExtension());
625
                             test.getOutfile() + fe.getExtension());
646
                fe.setOutfile(destFile);
626
                fe.setOutfile(destFile);
647
            }
627
            }
648
            runner.addFormatter(fe.createFormatter());
628
            runner.addFormatter((JUnitResultFormatter) fe.createFormatter());
649
        }
629
        }
650
    }
630
    }
651
631
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskMirrorImpl.java (+69 lines)
Line 0 Link Here
1
/*
2
 * Copyright  2006 The Apache Software Foundation
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 *
16
 */
17
18
package org.apache.tools.ant.taskdefs.optional.junit;
19
20
import java.io.OutputStream;
21
import junit.framework.AssertionFailedError;
22
import junit.framework.Test;
23
import junit.framework.TestResult;
24
import org.apache.tools.ant.AntClassLoader;
25
26
/**
27
 * Implementation of the part of the junit task which can directly refer to junit.* classes.
28
 * @see JUnitTaskMirror
29
 */
30
public final class JUnitTaskMirrorImpl implements JUnitTaskMirror {
31
    
32
    private final JUnitTask task;
33
    
34
    public JUnitTaskMirrorImpl(JUnitTask task) {
35
        this.task = task;
36
    }
37
    
38
    public void addVmExit(JUnitTest test, JUnitResultFormatterMirror _formatter,
39
            OutputStream out, final String message) {
40
        JUnitResultFormatter formatter = (JUnitResultFormatter) _formatter;
41
        formatter.setOutput(out);
42
        formatter.startTestSuite(test);
43
        
44
        //the trick to integrating test output to the formatter, is to
45
        //create a special test class that asserts an error
46
        //and tell the formatter that it raised.
47
        Test t = new Test() {
48
            public int countTestCases() { return 1; }
49
            public void run(TestResult r) {
50
                throw new AssertionFailedError(message);
51
            }
52
        };
53
        formatter.startTest(t);
54
        formatter.addError(t, new AssertionFailedError(message));
55
        formatter.endTestSuite(test);
56
    }
57
58
    public JUnitTaskMirror.JUnitTestRunnerMirror newJUnitTestRunner(JUnitTest test,
59
            boolean haltOnError, boolean filterTrace, boolean haltOnFailure,
60
            boolean showOutput, boolean logTestListenerEvents, AntClassLoader classLoader) {
61
        return new JUnitTestRunner(test, haltOnError, filterTrace, haltOnFailure,
62
                showOutput, logTestListenerEvents, classLoader);
63
    }
64
65
    public JUnitTaskMirror.SummaryJUnitResultFormatterMirror newSummaryJUnitResultFormatter() {
66
        return new SummaryJUnitResultFormatter();
67
    }
68
    
69
}
0
  + native
70
  + native
(-)src/main/org/apache/tools/ant/taskdefs/optional/junit/SummaryJUnitResultFormatter.java (-1 / +1 lines)
Lines 29-35 Link Here
29
 *
29
 *
30
 */
30
 */
31
31
32
public class SummaryJUnitResultFormatter implements JUnitResultFormatter {
32
public class SummaryJUnitResultFormatter implements JUnitResultFormatter, JUnitTaskMirror.SummaryJUnitResultFormatterMirror {
33
33
34
    /**
34
    /**
35
     * Formatter for timings.
35
     * Formatter for timings.

Return to bug 38799