KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > output > XmlOutputParserTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit.output;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import junit.framework.TestCase;
26
27 /**
28  *
29  * @author Marian Petras
30  */

31 public class XmlOutputParserTest extends TestCase {
32
33     private final Constructor JavaDoc constructor;
34     private final Method JavaDoc methodGetStackTrace;
35     private XmlOutputParser instance;
36
37     public XmlOutputParserTest(String JavaDoc testName) throws NoSuchMethodException JavaDoc,
38                                                        NoSuchFieldException JavaDoc,
39                                                        IllegalAccessException JavaDoc {
40         super(testName);
41         // XXX the following belongs in setUp:
42
constructor = XmlOutputParser.class.getDeclaredConstructor(new Class JavaDoc[0]);
43         constructor.setAccessible(true);
44         
45         methodGetStackTrace = XmlOutputParser.class.getDeclaredMethod(
46                                     "getStackTrace",
47                                     new Class JavaDoc[] {String JavaDoc.class});
48         methodGetStackTrace.setAccessible(true);
49     }
50     
51     protected void setUp() throws Exception JavaDoc {
52         instance = (XmlOutputParser) constructor.newInstance((Object JavaDoc[]) null);
53     }
54
55     public void testGetStackTrace() throws IllegalAccessException JavaDoc,
56                                            InvocationTargetException JavaDoc {
57         String JavaDoc stringToParse;
58         String JavaDoc[] expected, actual;
59         
60         stringToParse = "abcdkockaprede\n" +
61                         "pes pocita\n" +
62                         " at Kukuku.bubububu(heheh)";
63         expected = new String JavaDoc[] {"Kukuku.bubububu(heheh)"};
64         actual = (String JavaDoc[]) methodGetStackTrace.invoke(
65                             instance, new Object JavaDoc[] {stringToParse});
66         assertArrayEquals(expected, actual);
67                           
68         stringToParse = "junit.framework.AssertionFailedError: The test case is empty.\n" +
69                         " at javaaaplication2.NewBeanTest.testGetSampleProperty(NewBeanTest.java:54)\n";
70         expected = new String JavaDoc[] {
71             "javaaaplication2.NewBeanTest.testGetSampleProperty(NewBeanTest.java:54)"};
72         actual = (String JavaDoc[]) methodGetStackTrace.invoke(
73                             instance, new Object JavaDoc[] {stringToParse});
74         assertArrayEquals(expected, actual);
75                           
76         stringToParse = "\tat kuku.hehe\n" +
77                         "\t\tat fuifiu.hyy(fuifiu.java)\n" +
78                         "fuska\n" +
79                         "\t\tat fsdfas.oio(fsdfas.java)";
80         expected = new String JavaDoc[] {"kuku.hehe",
81                                  "fuifiu.hyy(fuifiu.java)"};
82         actual = (String JavaDoc[]) methodGetStackTrace.invoke(
83                             instance, new Object JavaDoc[] {stringToParse});
84         assertArrayEquals(expected, actual);
85                           
86         stringToParse = " at toto.tamto(Tuhle.java)\n" +
87                         " at jeste.tohle(Tamhle.java)\n" +
88                         " at tadyhle.tu";
89         expected = new String JavaDoc[] {"toto.tamto(Tuhle.java)",
90                                  "jeste.tohle(Tamhle.java)",
91                                  "tadyhle.tu"};
92         actual = (String JavaDoc[]) methodGetStackTrace.invoke(
93                             instance, new Object JavaDoc[] {stringToParse});
94         assertArrayEquals(expected, actual);
95                           
96         stringToParse = "toto.tamto(Tuhle.java)\n" +
97                         "jeste.tohle(Tamhle.java)\n" +
98                         "tadyhle.tu";
99         expected = null;
100         actual = (String JavaDoc[]) methodGetStackTrace.invoke(
101                             instance, new Object JavaDoc[] {stringToParse});
102         assertArrayEquals(expected, actual);
103     }
104     
105     private void assertArrayEquals(final Object JavaDoc[] expected,
106                                    final Object JavaDoc[] actual) {
107         if ((expected == null) != (actual == null)) {
108             fail("Object arrays differ - expected: " + getNullStatus(expected)
109                  + ", but actual was " + getNullStatus(actual) + '.');
110         }
111         if (expected == null) { //i.e. actual is <null>, too
112
return;
113         }
114         if (expected.length != actual.length) {
115             fail("Different array lengths - expected: " + expected.length
116                  + ", but was: " + actual.length);
117         }
118         for (int i = 0; i < expected.length; i++) {
119             Object JavaDoc exp = expected[i];
120             Object JavaDoc act = actual[i];
121             
122             if ((exp == null) != (act == null)) {
123                 fail("Items at index " + i + " differ - expected: " + getNullStatus(exp)
124                      + ", but was " + getNullStatus(act) + '.');
125             }
126             if (exp == null) {
127                 continue; //i.e. act is <null>, too
128
}
129             if (!exp.equals(act)) {
130                 fail("Items at index " + i + " differ - expected: " + exp
131                      + ", but was " + act + '.');
132             }
133         }
134     }
135     
136     private String JavaDoc getNullStatus(Object JavaDoc o) {
137         return (o == null) ? "<null>" : "<non-null>";
138     }
139     
140 }
141
Popular Tags