1 43 44 package de.susebox; 45 46 import java.lang.reflect.Constructor ; 50 import java.text.MessageFormat ; 51 52 import junit.framework.Test; 53 import junit.framework.TestCase; 54 import junit.framework.TestSuite; 55 import junit.framework.Assert; 56 57 import de.susebox.java.lang.ThrowableList; 58 59 60 64 73 public class TestExceptions extends TestCase { 74 75 79 82 public static final String [] EXCEPTIONS_TO_TEST = { 83 "de.susebox.java.lang.ExtRuntimeException", 84 "de.susebox.java.lang.ExtIndexOutOfBoundsException", 85 "de.susebox.java.lang.ExtIllegalArgumentException", 86 "de.susebox.java.lang.ExtNoSuchMethodException", 87 "de.susebox.java.lang.ExtNullPointerException", 88 "de.susebox.java.lang.ExtUnsupportedOperationException", 89 "de.susebox.java.io.ExtIOException", 90 "de.susebox.jtopas.TokenizerException", 91 }; 92 93 94 98 101 public static void main(String [] args) { 102 String [] tests = { TestExceptions.class.getName() }; 103 104 TestUtilities.run(tests, args); 105 } 106 107 108 112 119 public static Test suite() { 120 TestSuite suite = new TestSuite(TestExceptions.class.getName()); 121 122 for (int index = 0; index < EXCEPTIONS_TO_TEST.length; ++index) { 123 suite.addTest(new TestExceptions("testWrappedException", EXCEPTIONS_TO_TEST[index])); 124 suite.addTest(new TestExceptions("testNestedExceptions", EXCEPTIONS_TO_TEST[index])); 125 suite.addTest(new TestExceptions("testMessageFormatting", EXCEPTIONS_TO_TEST[index])); 126 } 127 return suite; 128 } 129 130 131 135 143 public TestExceptions(String test, String exToTest) { 144 super(test); 145 _classToTest = exToTest; 146 } 147 148 149 153 159 protected void setUp() throws Exception {} 160 161 162 168 protected void tearDown() throws Exception {} 169 170 171 175 181 public void testWrappedException() throws Throwable { 182 Class [] paraTypes = new Class [] { new Throwable ().getClass() }; 184 String msg = "This is an illegal argument."; 185 IllegalArgumentException argEx = new IllegalArgumentException (msg); 186 187 Constructor constr = Class.forName(_classToTest).getConstructor(paraTypes); 189 ThrowableList ex = (ThrowableList)constr.newInstance(new Object [] { argEx } ); 190 191 assertTrue("rtEx: Wrapper exception not recognized.", ex.isWrapper()); 193 assertTrue("rtEx: Didn't retrieve the wrapped exception.", ex.getCause() == argEx); 194 assertTrue("rtEx: Messages not equal.", ((Exception )ex).getMessage().equals(argEx.getMessage())); 195 } 196 197 200 public void testNestedExceptions() throws Throwable { 201 Object [] objArray = new Object [1]; 203 String format = "This is exception no {0} of class {1}."; 204 String msg = "Message without format parameters."; 205 IllegalArgumentException argEx = new IllegalArgumentException (msg); 206 Class [] paraTypes = new Class [] { new Throwable ().getClass(), msg.getClass(), objArray.getClass() }; 207 208 Constructor constr = Class.forName(_classToTest).getConstructor(paraTypes); 210 ThrowableList ex1 = (ThrowableList)constr.newInstance(new Object [] { argEx, format, new Object [] { new Integer (1), _classToTest } } ); 211 ThrowableList ex2 = (ThrowableList)constr.newInstance(new Object [] { ex1, format, new Object [] { new Integer (2), _classToTest } } ); 212 213 assertTrue("ex1: False wrapper exception.", ! ex1.isWrapper()); 215 assertTrue("ex2: False wrapper exception.", ! ex2.isWrapper()); 216 assertTrue("ex1: Didn't retrieve the nested exception.", ex1.getCause() == argEx); 217 assertTrue("ex2: Didn't retrieve the first nested exception.", ex2.getCause() == ex1); 218 assertTrue("ex2: Didn't retrieve the second nested exception.", ((ThrowableList)ex2.getCause()).getCause() == argEx); 219 assertTrue("ex1: Format not found.", ex1.getFormat() == format); 220 assertTrue("ex2: Format not found.", ex2.getFormat() == format); 221 } 222 223 226 public void testMessageFormatting() throws Throwable { 227 String format = "Class {0}, reason \"{1}\", user {2}."; 229 Object [] paras = new Object [] { _classToTest, "bad weather", "myself" }; 230 Class [] paraTypes = new Class [] { format.getClass(), paras.getClass() }; 231 232 Constructor constr = Class.forName(_classToTest).getConstructor(paraTypes); 234 ThrowableList ex = (ThrowableList)constr.newInstance(new Object [] { format, paras } ); 235 236 assertTrue("Format not found.", ex.getFormat() == format); 238 239 String str1 = MessageFormat.format(format, paras); 240 String str2 = ((Exception )ex).getMessage(); 241 assertTrue("Formating failed. Expected \"" + str1 + "\", got \"" + str2 + "\".", 242 str1.equals(str2)); 243 244 } 245 246 private String _classToTest = null; 250 } 251 | Popular Tags |