1 16 package org.apache.commons.lang.exception; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.EOFException ; 20 import java.io.PrintStream ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 32 public class NestableRuntimeExceptionTestCase extends AbstractNestableTestCase { 33 34 40 public NestableRuntimeExceptionTestCase(String name) 41 { 42 super(name); 43 } 44 45 48 public void setUp() 49 { 50 } 51 52 57 public static Test suite() 58 { 59 return new TestSuite(NestableRuntimeExceptionTestCase.class); 60 } 61 62 65 public void tearDown() 66 { 67 } 68 69 74 public static void main(String args[]) 75 { 76 TestRunner.run(suite()); 77 } 78 79 82 public Nestable getNestable() 83 { 84 return new NestableRuntimeException(); 85 } 86 87 90 public Nestable getNestable(Nestable n) 91 { 92 return new NestableRuntimeException((Throwable ) n); 93 } 94 95 98 public Nestable getNestable(String msg) 99 { 100 return new NestableRuntimeException(msg); 101 } 102 103 106 public Nestable getNestable(Throwable t) 107 { 108 return new NestableRuntimeException(t); 109 } 110 111 114 public Nestable getNestable(String msg, Throwable t) 115 { 116 return new NestableRuntimeException(msg, t); 117 } 118 119 122 public Nestable getNestable(String msg, Nestable n) 123 { 124 return new NestableRuntimeException(msg, (Throwable ) n); 125 } 126 127 130 public Nestable getTester1(Throwable t) 131 { 132 return new NestableRuntimeExceptionTester1(t); 133 } 134 135 138 public Nestable getTester1(Nestable n) 139 { 140 return new NestableRuntimeExceptionTester1((Throwable ) n); 141 } 142 143 146 public Nestable getTester1(String msg, Throwable t) 147 { 148 return new NestableRuntimeExceptionTester1(msg, t); 149 } 150 151 154 public Nestable getTester1(String msg, Nestable n) 155 { 156 return new NestableRuntimeExceptionTester1(msg, (Throwable ) n); 157 } 158 159 162 public Class getTester1Class() 163 { 164 return NestableRuntimeExceptionTester1.class; 165 } 166 167 170 public Nestable getTester2(String msg, Throwable t) 171 { 172 return new NestableRuntimeExceptionTester2(msg, t); 173 } 174 175 178 public Nestable getTester2(String msg, Nestable n) 179 { 180 return new NestableRuntimeExceptionTester2(msg, (Throwable ) n); 181 } 182 183 186 public Class getTester2Class() 187 { 188 return NestableRuntimeExceptionTester2.class; 189 } 190 191 194 public Throwable getThrowable(String msg) 195 { 196 return new EOFException (msg); 197 } 198 199 202 public Class getThrowableClass() 203 { 204 return EOFException .class; 205 } 206 207 210 public Class getBaseThrowableClass() 211 { 212 return RuntimeException .class; 213 } 214 215 public void testSpecificPrintStackTrace() 216 { 217 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 221 PrintStream ps = new PrintStream (baos); 222 NestableRuntimeException ne = new NestableRuntimeException("outer", new NestableRuntimeException("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.NestableRuntimeException: outer")); 244 assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: 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.NestableRuntimeException: outer") < 248 msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner")); 249 assertTrue( "printStackTrace() cause message after inner message", 250 msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner") < 251 msg.indexOf("Caused by: java.lang.Exception: another exception")); 252 } 253 254 } 255 256 259 class NestableRuntimeExceptionTester1 extends NestableRuntimeException 260 { 261 public NestableRuntimeExceptionTester1() 262 { 263 super(); 264 } 265 266 public NestableRuntimeExceptionTester1(String reason, Throwable cause) 267 { 268 super(reason, cause); 269 } 270 271 public NestableRuntimeExceptionTester1(String reason) 272 { 273 super(reason); 274 } 275 276 public NestableRuntimeExceptionTester1(Throwable cause) 277 { 278 super(cause); 279 } 280 281 } 282 283 286 class NestableRuntimeExceptionTester2 extends NestableRuntimeException 287 { 288 public NestableRuntimeExceptionTester2() 289 { 290 super(); 291 } 292 293 public NestableRuntimeExceptionTester2(String reason, Throwable cause) 294 { 295 super(reason, cause); 296 } 297 298 public NestableRuntimeExceptionTester2(String reason) 299 { 300 super(reason); 301 } 302 303 public NestableRuntimeExceptionTester2(Throwable cause) 304 { 305 super(cause); 306 } 307 308 } 309 310 | Popular Tags |