001/*
002 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.mxtool.junit;
024
025import java.io.*;
026
027import org.junit.internal.*;
028import org.junit.runner.*;
029import org.junit.runner.notification.*;
030
031public class TextRunListener implements MxRunListener {
032
033    private final PrintStream fWriter;
034    protected Failure lastFailure;
035
036    public TextRunListener(JUnitSystem system) {
037        this(system.out());
038    }
039
040    public TextRunListener(PrintStream writer) {
041        fWriter = writer;
042    }
043
044    @Override
045    public PrintStream getWriter() {
046        return fWriter;
047    }
048
049    public Failure getLastFailure() {
050        return lastFailure;
051    }
052
053    @Override
054    public void testRunStarted(Description description) {
055    }
056
057    @Override
058    public void testRunFinished(Result result) {
059    }
060
061    @Override
062    public void testAssumptionFailure(Failure failure) {
063    }
064
065    @Override
066    public void testClassStarted(Class<?> clazz) {
067    }
068
069    @Override
070    public void testClassFinished(Class<?> clazz) {
071    }
072
073    @Override
074    public void testStarted(Description description) {
075        getWriter().print('.');
076    }
077
078    @Override
079    public void testFinished(Description description) {
080    }
081
082    @Override
083    public void testFailed(Failure failure) {
084        getWriter().print('E');
085        lastFailure = failure;
086    }
087
088    @Override
089    public void testSucceeded(Description description) {
090    }
091
092    @Override
093    public void testIgnored(Description description) {
094        getWriter().print('I');
095    }
096
097    @Override
098    public void testClassFinishedDelimiter() {
099    }
100
101    @Override
102    public void testClassStartedDelimiter() {
103    }
104
105    @Override
106    public void testStartedDelimiter() {
107    }
108
109    @Override
110    public void testFinishedDelimiter() {
111    }
112
113    public static RunListener createRunListener(MxRunListener l) {
114        return new TextListener(l.getWriter()) {
115            private Class<?> lastClass;
116            private boolean failed;
117
118            @Override
119            public final void testStarted(Description description) {
120                Class<?> currentClass = description.getTestClass();
121                if (currentClass != lastClass) {
122                    if (lastClass != null) {
123                        l.testClassFinished(lastClass);
124                        l.testClassFinishedDelimiter();
125                    }
126                    lastClass = currentClass;
127                    l.testClassStarted(currentClass);
128                    l.testClassStartedDelimiter();
129                }
130                failed = false;
131                l.testStarted(description);
132                l.testStartedDelimiter();
133            }
134
135            @Override
136            public final void testFailure(Failure failure) {
137                failed = true;
138                l.testFailed(failure);
139            }
140
141            @Override
142            public final void testFinished(Description description) {
143                // we have to do this because there is no callback for successful tests
144                if (!failed) {
145                    l.testSucceeded(description);
146                }
147                l.testFinished(description);
148                l.testFinishedDelimiter();
149            }
150
151            @Override
152            public void testIgnored(Description description) {
153                l.testStarted(description);
154                l.testStartedDelimiter();
155                l.testIgnored(description);
156                l.testFinished(description);
157                l.testFinishedDelimiter();
158            }
159
160            @Override
161            public void testRunStarted(Description description) {
162                l.testRunStarted(description);
163            }
164
165            @Override
166            public void testRunFinished(Result result) {
167                if (lastClass != null) {
168                    l.testClassFinished(lastClass);
169                }
170                l.testRunFinished(result);
171                super.testRunFinished(result);
172            }
173
174            @Override
175            public void testAssumptionFailure(Failure failure) {
176                l.testAssumptionFailure(failure);
177            }
178
179        };
180    }
181
182}