1 16 package org.apache.commons.lang.exception; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.EOFException ; 21 import java.io.ObjectInputStream ; 22 import java.io.ObjectOutputStream ; 23 import java.io.PrintStream ; 24 25 import junit.framework.Test; 26 import junit.framework.TestSuite; 27 import junit.textui.TestRunner; 28 29 35 public class NestableExceptionTestCase extends AbstractNestableTestCase { 36 37 43 public NestableExceptionTestCase(String name) 44 { 45 super(name); 46 } 47 48 51 public void setUp() 52 { 53 } 54 55 60 public static Test suite() 61 { 62 return new TestSuite(NestableExceptionTestCase.class); 63 } 64 65 68 public void tearDown() 69 { 70 } 71 72 77 public static void main(String args[]) 78 { 79 TestRunner.run(suite()); 80 } 81 82 85 public Nestable getNestable() 86 { 87 return new NestableException(); 88 } 89 90 93 public Nestable getNestable(Nestable n) 94 { 95 return new NestableException((Throwable ) n); 96 } 97 98 101 public Nestable getNestable(String msg) 102 { 103 return new NestableException(msg); 104 } 105 106 109 public Nestable getNestable(Throwable t) 110 { 111 return new NestableException(t); 112 } 113 114 117 public Nestable getNestable(String msg, Throwable t) 118 { 119 return new NestableException(msg, t); 120 } 121 122 125 public Nestable getNestable(String msg, Nestable n) 126 { 127 return new NestableException(msg, (Throwable ) n); 128 } 129 130 133 public Nestable getTester1(Throwable t) 134 { 135 return new NestableExceptionTester1(t); 136 } 137 138 141 public Nestable getTester1(Nestable n) 142 { 143 return new NestableExceptionTester1((Throwable ) n); 144 } 145 146 149 public Nestable getTester1(String msg, Throwable t) 150 { 151 return new NestableExceptionTester1(msg, t); 152 } 153 154 157 public Nestable getTester1(String msg, Nestable n) 158 { 159 return new NestableExceptionTester1(msg, (Throwable ) n); 160 } 161 162 165 public Class getTester1Class() 166 { 167 return NestableExceptionTester1.class; 168 } 169 170 173 public Nestable getTester2(String msg, Throwable t) 174 { 175 return new NestableExceptionTester2(msg, t); 176 } 177 178 181 public Nestable getTester2(String msg, Nestable n) 182 { 183 return new NestableExceptionTester2(msg, (Throwable ) n); 184 } 185 186 189 public Class getTester2Class() 190 { 191 return NestableExceptionTester2.class; 192 } 193 194 197 public Throwable getThrowable(String msg) 198 { 199 return new EOFException (msg); 200 } 201 202 205 public Class getThrowableClass() 206 { 207 return EOFException .class; 208 } 209 210 213 public Class getBaseThrowableClass() 214 { 215 return Exception .class; 216 } 217 218 public void testSpecificPrintStackTrace() 219 { 220 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 221 PrintStream ps = new PrintStream (baos); 222 NestableException ne = new NestableException("outer", new NestableException("inner", new Exception ("another exception"))); 223 for(int i = 0; i < 2; i++) 224 { 225 if(i == 0) 226 { 227 PrintStream err = System.err; 231 System.setErr(ps); 232 ne.printStackTrace(); 233 System.setErr(err); 235 } 236 else 237 { 238 ne.printStackTrace(ps); 240 } 241 } 242 String msg = baos.toString(); 243 assertTrue( "printStackTrace() starts with outer message", msg.startsWith("org.apache.commons.lang.exception.NestableException: outer")); 244 assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") >= 0); 245 assertTrue( "printStackTrace() contains 2nd nested message", msg.indexOf("Caused by: java.lang.Exception: another exception") >= 0); 246 assertTrue( "printStackTrace() inner message after outer message", 247 msg.indexOf("org.apache.commons.lang.exception.NestableException: outer") < 248 msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner")); 249 assertTrue( "printStackTrace() cause message after inner message", 250 msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") < 251 msg.indexOf("Caused by: java.lang.Exception: another exception")); 252 } 253 254 public void testSerialization() 255 throws java.io.IOException , ClassNotFoundException 256 { 257 RuntimeException nestedEx = new RuntimeException ("nested exception message"); 258 NestableExceptionTester1 ex = new NestableExceptionTester1("serialization test", nestedEx); 259 260 assertTrue( "implements java.io.Serializable", nestedEx instanceof java.io.Serializable ); 261 262 assertTrue( "implements java.io.Serializable", ex instanceof java.io.Serializable ); 263 264 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 265 ByteArrayInputStream bais = null; 266 ObjectOutputStream oos = null; 267 ObjectInputStream ois = null; 268 269 try 270 { 271 oos = new ObjectOutputStream (baos); 272 oos.writeObject(ex); 273 oos.flush(); 274 bais = new ByteArrayInputStream (baos.toByteArray()); 275 ois = new ObjectInputStream (bais); 276 NestableExceptionTester1 deserializedEx = (NestableExceptionTester1) ois.readObject(); 277 assertEquals( 278 "getThrowableCount() return value", 279 ex.getThrowableCount(), 280 deserializedEx.getThrowableCount()); 281 282 for (int i = 0; i < ex.getThrowableCount(); i++) 283 { 284 Throwable t = ex.getThrowable(i); 285 Throwable deserializedThrowable = deserializedEx.getThrowable(i); 286 287 assertEquals( t.getClass(), 288 deserializedThrowable.getClass()); 289 290 assertEquals( 291 t.getMessage(), 292 deserializedThrowable.getMessage()); 293 } 294 } 295 finally 296 { 297 if (null != oos) 298 { 299 try 300 { 301 oos.close(); 302 } 303 catch (Exception ignored) 304 { 305 } 307 } 308 } 309 310 } 311 } 312 313 316 class NestableExceptionTester1 extends NestableException 317 { 318 public NestableExceptionTester1() 319 { 320 super(); 321 } 322 323 public NestableExceptionTester1(String reason, Throwable cause) 324 { 325 super(reason, cause); 326 } 327 328 public NestableExceptionTester1(String reason) 329 { 330 super(reason); 331 } 332 333 public NestableExceptionTester1(Throwable cause) 334 { 335 super(cause); 336 } 337 338 } 339 340 343 class NestableExceptionTester2 extends NestableException 344 { 345 public NestableExceptionTester2() 346 { 347 super(); 348 } 349 350 public NestableExceptionTester2(String reason, Throwable cause) 351 { 352 super(reason, cause); 353 } 354 355 public NestableExceptionTester2(String reason) 356 { 357 super(reason); 358 } 359 360 public NestableExceptionTester2(Throwable cause) 361 { 362 super(cause); 363 } 364 365 } 366 367 | Popular Tags |