1 43 44 package de.susebox.java.lang; 45 46 import java.text.MessageFormat ; 50 51 import junit.framework.Test; 52 import junit.framework.TestCase; 53 import junit.framework.TestSuite; 54 import junit.framework.Assert; 55 56 import de.susebox.TestUtilities; 57 58 59 63 72 public class TestThrowableList extends TestCase { 73 74 78 79 83 86 public static void main(String [] args) { 87 String [] tests = { TestThrowableList.class.getName() }; 88 89 TestUtilities.run(tests, args); 90 } 91 92 93 97 103 public static Test suite() { 104 TestSuite suite = new TestSuite(TestThrowableList.class.getName()); 105 106 suite.addTest(new TestThrowableList("testWrappedThrowable")); 107 suite.addTest(new TestThrowableList("testThrowableList")); 108 suite.addTest(new TestThrowableList("testMessageFormatting")); 109 return suite; 110 } 111 112 113 117 121 public TestThrowableList(String test) { 122 super(test); 123 } 124 125 126 130 134 protected void setUp() throws Exception {} 135 136 137 141 protected void tearDown() throws Exception {} 142 143 144 148 152 public void testWrappedThrowable() throws Throwable { 153 String msg = "This is an illegal argument."; 154 IllegalArgumentException argEx = new IllegalArgumentException (msg); 155 ExtRuntimeException rtEx = new ExtRuntimeException(argEx); 156 ExtIndexOutOfBoundsException indexEx = new ExtIndexOutOfBoundsException(argEx); 157 ExtIndexOutOfBoundsException bigEx = new ExtIndexOutOfBoundsException(rtEx); 158 159 assertTrue("rtEx: Wrapper exception not recognized.", rtEx.isWrapper()); 160 assertTrue("rtEx: Didn't retrieve the wrapped exception.", rtEx.getCause() == argEx); 161 assertTrue("rtEx: Messages not equal.", rtEx.getMessage().equals(argEx.getMessage())); 162 163 assertTrue("indexEx: Wrapper exception not recognized.", indexEx.isWrapper()); 164 assertTrue("indexEx: Didn't retrieve the wrapped exception.", indexEx.getCause() == argEx); 165 assertTrue("indexEx: Messages not equal.", indexEx.getMessage().equals(argEx.getMessage())); 166 167 assertTrue("bigEx: Wrapper exception not recognized.", bigEx.isWrapper()); 168 assertTrue("bigEx: Didn't retrieve the first wrapped exception.", bigEx.getCause() == rtEx); 169 assertTrue("bigEx: Didn't retrieve the second wrapped exception.", ((ThrowableList)bigEx.getCause()).getCause() == argEx); 170 assertTrue("bigEx: Messages not equal.", bigEx.getMessage().equals(argEx.getMessage())); 171 } 172 173 176 public void testThrowableList() throws Throwable { 177 String format = "This is exception no {0} of class {1}."; 178 String msg = "Message without format parameters."; 179 ExtRuntimeException rtEx1 = new ExtRuntimeException( format, 180 new Object [] { new Integer (1), ExtRuntimeException.class } ); 181 ExtRuntimeException rtEx2 = new ExtRuntimeException( rtEx1, 182 format, 183 new Object [] { new Integer (2), ExtRuntimeException.class } ); 184 ExtIndexOutOfBoundsException indexEx = new ExtIndexOutOfBoundsException(rtEx2, msg); 185 186 assertTrue("rtEx1: False wrapper exception.", ! rtEx1.isWrapper()); 187 assertTrue("rtEx2: False wrapper exception.", ! rtEx2.isWrapper()); 188 assertTrue("indexEx: False wrapper exception.", ! indexEx.isWrapper()); 189 assertTrue("rtEx2: Didn't retrieve the nested exception.", rtEx2.getCause() == rtEx1); 190 assertTrue("indexEx: Didn't retrieve the first nested exception.", indexEx.getCause() == rtEx2); 191 assertTrue("indexEx: Didn't retrieve the second nested exception.", ((ThrowableList)indexEx.getCause()).getCause() == rtEx1); 192 assertTrue("rtEx1: Format not found.", rtEx1.getFormat() == format); 193 assertTrue("rtEx2: Format not found.", rtEx2.getFormat() == format); 194 assertTrue("indexEx: Format not found.", indexEx.getFormat() == msg); 195 } 196 197 200 public void testMessageFormatting() throws Throwable { 201 String format = "Class {0}, reason \"{1}\", user {2}."; 202 Object [] paras = new Object [] { ExtRuntimeException.class, "bad weather", "myself" }; 203 ExtRuntimeException rtEx = new ExtRuntimeException(format, paras); 204 205 assertTrue("Format not found.", rtEx.getFormat() == format); 206 207 String str1 = MessageFormat.format(format, paras); 208 String str2 = rtEx.getMessage(); 209 assertTrue("Formating failed. Expected \"" + str1 + "\", got \"" + str2 + "\".", 210 str1.equals(str2)); 211 } 212 } 213 | Popular Tags |