junit: do not stop if wasn't start (IDEADEV-41262)
[fedora-idea.git] / plugins / junit_rt / src / com / intellij / junit4 / JUnit4TestResultsSender.java
blob2684a98a30816ee8edfe06dae6ced7fa77b2b30b
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.junit4;
18 import com.intellij.rt.execution.junit.*;
19 import com.intellij.rt.execution.junit.segments.OutputObjectRegistryEx;
20 import com.intellij.rt.execution.junit.segments.Packet;
21 import com.intellij.rt.execution.junit.segments.PacketProcessor;
22 import com.intellij.rt.execution.junit.states.PoolOfTestStates;
23 import junit.framework.ComparisonFailure;
24 import org.junit.Ignore;
25 import org.junit.runner.Description;
26 import org.junit.runner.notification.Failure;
27 import org.junit.runner.notification.RunListener;
29 public class JUnit4TestResultsSender extends RunListener {
30 private final OutputObjectRegistryEx myRegistry;
31 private final PacketProcessor myErr;
32 private TestMeter myCurrentTestMeter;
33 private Description myCurrentTest;
35 public JUnit4TestResultsSender(OutputObjectRegistryEx packetFactory, PacketProcessor segmentedErr) {
36 myRegistry = packetFactory;
37 myErr = segmentedErr;
40 public synchronized void testFailure(Failure failure) throws Exception {
41 final Description description = failure.getDescription();
42 final Throwable throwable = failure.getException();
44 if (throwable instanceof AssertionError) {
45 // junit4 makes no distinction between errors and failures
46 doAddFailure(description, (Error)throwable);
49 else {
50 if (description.equals(myCurrentTest)) { //exceptions thrown from @BeforeClass
51 stopMeter(description);
53 prepareDefectPacket(description, throwable).send();
57 public synchronized void testIgnored(Description description) throws Exception {
58 final Ignore ignoredAnnotation = (Ignore)description.getAnnotation(Ignore.class);
59 final String val = ignoredAnnotation != null ? ignoredAnnotation.value() : null;
60 testStarted(description);
61 stopMeter(description);
62 prepareIgnoredPacket(description, val).send();
65 private void doAddFailure(final Description test, final Throwable assertion) {
66 if (test.equals(myCurrentTest)) {
67 stopMeter(test);
69 createExceptionNotification(assertion).createPacket(myRegistry, test).send();
72 private static PacketFactory createExceptionNotification(Throwable assertion) {
73 if (assertion instanceof KnownException) return ((KnownException)assertion).getPacketFactory();
74 if (assertion instanceof ComparisonFailure || assertion.getClass().getName().equals("org.junit.ComparisonFailure")) {
75 return ComparisonDetailsExtractor.create(assertion);
77 return new ExceptionPacketFactory(PoolOfTestStates.FAILED_INDEX, assertion);
80 private Packet prepareDefectPacket(Description test, Throwable assertion) {
81 return myRegistry.createPacket().
82 setTestState(test, PoolOfTestStates.ERROR_INDEX).
83 addThrowable(assertion);
85 private Packet prepareIgnoredPacket(Description test, String val) {
86 return myRegistry.createPacket().setTestState(test, PoolOfTestStates.IGNORED_INDEX).addObject(test).addLimitedString(val != null ? val : "");
89 public void testFinished(Description description) throws Exception {
90 stopMeter(description);
91 Packet packet = myRegistry.createPacket().setTestState(description, PoolOfTestStates.COMPLETE_INDEX);
92 myCurrentTestMeter.writeTo(packet);
93 packet.send();
94 myRegistry.forget(description);
97 private void stopMeter(Description test) {
98 if (!test.equals(myCurrentTest)) {
99 myCurrentTestMeter = new TestMeter();
100 //noinspection HardCodedStringLiteral
101 System.err.println("Wrong test finished. Last started: " + myCurrentTest+" stopped: " + test+"; "+test.getClass());
103 myCurrentTestMeter.stop();
106 private void switchOutput(Packet switchPacket) {
107 switchPacket.send();
108 switchPacket.sendThrough(myErr);
112 public synchronized void testStarted(Description description) throws Exception {
113 myCurrentTest = description;
114 myRegistry.createPacket().setTestState(description, PoolOfTestStates.RUNNING_INDEX).send();
115 switchOutput(myRegistry.createPacket().switchInputTo(description));
116 myCurrentTestMeter = new TestMeter();