update copyright
[fedora-idea.git] / plugins / junit_rt / src / com / intellij / rt / execution / junit / ComparisonDetailsExtractor.java
blob2d2c621853e0cf3249dc1c510bc43f818b2ca43a
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.rt.execution.junit;
18 import com.intellij.rt.execution.junit.segments.OutputObjectRegistryEx;
19 import com.intellij.rt.execution.junit.segments.Packet;
20 import com.intellij.rt.execution.junit.states.PoolOfTestStates;
21 import junit.framework.ComparisonFailure;
23 import java.lang.reflect.Field;
25 /**
26 * @noinspection HardCodedStringLiteral
28 public class ComparisonDetailsExtractor extends ExceptionPacketFactory {
29 private static Field EXPECTED_FIELD = null;
30 private static Field ACTUAL_FIELD = null;
31 protected String myActual = "";
32 protected String myExpected = "";
34 static {
35 try {
36 Class exceptionClass = ComparisonFailure.class;
37 exceptionClass.getDeclaredField("fExpected");
38 EXPECTED_FIELD = exceptionClass.getDeclaredField("fExpected");
39 EXPECTED_FIELD.setAccessible(true);
40 ACTUAL_FIELD = exceptionClass.getDeclaredField("fActual");
41 ACTUAL_FIELD.setAccessible(true);
42 } catch (Throwable e) {}
45 public ComparisonDetailsExtractor(Throwable assertion, String expected, String actual) {
46 super(PoolOfTestStates.COMPARISON_FAILURE, assertion);
47 myActual = actual;
48 myExpected = expected;
51 public static ExceptionPacketFactory create(Throwable assertion) {
52 try {
53 String expected;
54 if (assertion instanceof ComparisonFailure) {
55 expected = (String)EXPECTED_FIELD.get(assertion);
57 else {
58 Field field = assertion.getClass().getDeclaredField("fExpected");
59 field.setAccessible(true);
60 expected = (String)field.get(assertion);
62 String actual;
63 if (assertion instanceof ComparisonFailure) {
64 actual = (String)ACTUAL_FIELD.get(assertion);
66 else {
67 Field field = assertion.getClass().getDeclaredField("fActual");
68 field.setAccessible(true);
69 actual = (String)field.get(assertion);
71 return new ComparisonDetailsExtractor(assertion, expected, actual);
73 catch (Throwable e) {
74 return new ExceptionPacketFactory(PoolOfTestStates.FAILED_INDEX, assertion);
78 public Packet createPacket(OutputObjectRegistryEx registry, Object test) {
79 Packet packet = super.createPacket(registry, test);
80 packet.
81 addLimitedString(myExpected).
82 addLimitedString(myActual);
83 return packet;