1 19 20 package org.netbeans.modules.junit.output; 21 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import junit.framework.TestCase; 26 27 31 public class XmlOutputParserTest extends TestCase { 32 33 private final Constructor constructor; 34 private final Method methodGetStackTrace; 35 private XmlOutputParser instance; 36 37 public XmlOutputParserTest(String testName) throws NoSuchMethodException , 38 NoSuchFieldException , 39 IllegalAccessException { 40 super(testName); 41 constructor = XmlOutputParser.class.getDeclaredConstructor(new Class [0]); 43 constructor.setAccessible(true); 44 45 methodGetStackTrace = XmlOutputParser.class.getDeclaredMethod( 46 "getStackTrace", 47 new Class [] {String .class}); 48 methodGetStackTrace.setAccessible(true); 49 } 50 51 protected void setUp() throws Exception { 52 instance = (XmlOutputParser) constructor.newInstance((Object []) null); 53 } 54 55 public void testGetStackTrace() throws IllegalAccessException , 56 InvocationTargetException { 57 String stringToParse; 58 String [] expected, actual; 59 60 stringToParse = "abcdkockaprede\n" + 61 "pes pocita\n" + 62 " at Kukuku.bubububu(heheh)"; 63 expected = new String [] {"Kukuku.bubububu(heheh)"}; 64 actual = (String []) methodGetStackTrace.invoke( 65 instance, new Object [] {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 [] { 71 "javaaaplication2.NewBeanTest.testGetSampleProperty(NewBeanTest.java:54)"}; 72 actual = (String []) methodGetStackTrace.invoke( 73 instance, new Object [] {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 [] {"kuku.hehe", 81 "fuifiu.hyy(fuifiu.java)"}; 82 actual = (String []) methodGetStackTrace.invoke( 83 instance, new Object [] {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 [] {"toto.tamto(Tuhle.java)", 90 "jeste.tohle(Tamhle.java)", 91 "tadyhle.tu"}; 92 actual = (String []) methodGetStackTrace.invoke( 93 instance, new Object [] {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 []) methodGetStackTrace.invoke( 101 instance, new Object [] {stringToParse}); 102 assertArrayEquals(expected, actual); 103 } 104 105 private void assertArrayEquals(final Object [] expected, 106 final Object [] 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) { 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 exp = expected[i]; 120 Object 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; } 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 getNullStatus(Object o) { 137 return (o == null) ? "<null>" : "<non-null>"; 138 } 139 140 } 141 | Popular Tags |