23
23
import org .junit .Test ;
24
24
25
25
import io .reactivex .rxjava3 .core .RxJavaTest ;
26
- import io .reactivex .rxjava3 .exceptions .CompositeException .CompositeExceptionCausalChain ;
27
26
28
27
public class CompositeExceptionTest extends RxJavaTest {
29
28
@@ -251,41 +250,6 @@ public void messageVarargs() {
251
250
assertEquals ("3 exceptions occurred. " , compositeException .getMessage ());
252
251
}
253
252
254
- @ Test
255
- public void complexCauses () {
256
- Throwable e1 = new Throwable ("1" );
257
- Throwable e2 = new Throwable ("2" );
258
- e1 .initCause (e2 );
259
-
260
- Throwable e3 = new Throwable ("3" );
261
- Throwable e4 = new Throwable ("4" );
262
- e3 .initCause (e4 );
263
-
264
- Throwable e5 = new Throwable ("5" );
265
- Throwable e6 = new Throwable ("6" );
266
- e5 .initCause (e6 );
267
-
268
- CompositeException compositeException = new CompositeException (e1 , e3 , e5 );
269
- assertTrue (compositeException .getCause () instanceof CompositeExceptionCausalChain );
270
-
271
- List <Throwable > causeChain = new ArrayList <Throwable >();
272
- Throwable cause = compositeException .getCause ().getCause ();
273
- while (cause != null ) {
274
- causeChain .add (cause );
275
- cause = cause .getCause ();
276
- }
277
- // The original relations
278
- //
279
- // e1 -> e2
280
- // e3 -> e4
281
- // e5 -> e6
282
- //
283
- // will be set to
284
- //
285
- // e1 -> e2 -> e3 -> e4 -> e5 -> e6
286
- assertEquals (Arrays .asList (e1 , e2 , e3 , e4 , e5 , e6 ), causeChain );
287
- }
288
-
289
253
@ Test
290
254
public void constructorWithNull () {
291
255
assertTrue (new CompositeException ((Throwable [])null ).getExceptions ().get (0 ) instanceof NullPointerException );
@@ -306,81 +270,96 @@ public void printStackTrace() {
306
270
}
307
271
308
272
@ Test
309
- public void cyclicRootCause () {
310
- RuntimeException te = new RuntimeException () {
311
-
312
- private static final long serialVersionUID = -8492568224555229753L ;
313
- Throwable cause ;
314
-
315
- @ Override
316
- public Throwable initCause (Throwable c ) {
317
- return this ;
318
- }
319
-
320
- @ Override
321
- public Throwable getCause () {
322
- return cause ;
323
- }
324
- };
325
-
326
- assertSame (te , new CompositeException (te ).getCause ().getCause ());
327
-
328
- assertSame (te , new CompositeException (new RuntimeException (te )).getCause ().getCause ().getCause ());
273
+ public void badException () {
274
+ Throwable e = new BadException ();
275
+ assertSame (e , new CompositeException (e ).getCause ().getCause ());
276
+ assertSame (e , new CompositeException (new RuntimeException (e )).getCause ().getCause ().getCause ());
329
277
}
330
278
331
279
@ Test
332
- public void nullRootCause () {
333
- RuntimeException te = new RuntimeException () {
280
+ public void exceptionOverview () {
281
+ CompositeException composite = new CompositeException (
282
+ new TestException ("ex1" ),
283
+ new TestException ("ex2" ),
284
+ new TestException ("ex3" , new TestException ("ex4" ))
285
+ );
286
+
287
+ String overview = composite .getCause ().getMessage ();
288
+
289
+ assertTrue (overview , overview .contains ("Multiple exceptions (3)" ));
290
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.TestException: ex1" ));
291
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.TestException: ex2" ));
292
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.TestException: ex3" ));
293
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.TestException: ex4" ));
294
+ assertTrue (overview , overview .contains ("at io.reactivex.rxjava3.exceptions.CompositeExceptionTest.exceptionOverview" ));
295
+ }
334
296
335
- private static final long serialVersionUID = -8492568224555229753L ;
297
+ @ Test
298
+ public void causeWithExceptionWithoutStacktrace () {
299
+ CompositeException composite = new CompositeException (
300
+ new TestException ("ex1" ),
301
+ new CompositeException .ExceptionOverview ("example" )
302
+ );
336
303
337
- @ Override
338
- public Throwable getCause () {
339
- return null ;
340
- }
341
- };
304
+ String overview = composite .getCause ().getMessage ();
342
305
343
- assertSame (te , new CompositeException (te ).getCause ().getCause ());
306
+ assertTrue (overview , overview .contains ("Multiple exceptions (2)" ));
307
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.TestException: ex1" ));
308
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.CompositeException.ExceptionOverview: example" ));
344
309
345
- assertSame ( te , new CompositeException ( new RuntimeException ( te )). getCause (). getCause (). getCause () );
310
+ assertEquals ( overview , 2 , overview . split ( "at \\ s" ). length );
346
311
}
347
312
348
313
@ Test
349
- public void badException () {
350
- Throwable e = new BadException ();
351
- assertSame (e , new CompositeException (e ).getCause ().getCause ());
352
- assertSame (e , new CompositeException (new RuntimeException (e )).getCause ().getCause ().getCause ());
314
+ public void reoccurringException () {
315
+ TestException ex0 = new TestException ("ex0" );
316
+ TestException ex1 = new TestException ("ex1" , ex0 );
317
+ CompositeException composite = new CompositeException (
318
+ ex1 ,
319
+ new TestException ("ex2" , ex1 )
320
+ );
321
+
322
+ String overview = composite .getCause ().getMessage ();
323
+ System .err .println (overview );
324
+
325
+ assertTrue (overview , overview .contains ("Multiple exceptions (2)" ));
326
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.TestException: ex0" ));
327
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.TestException: ex1" ));
328
+ assertTrue (overview , overview .contains ("io.reactivex.rxjava3.exceptions.TestException: ex2" ));
329
+ assertTrue (overview , overview .contains ("(cause not expanded again) io.reactivex.rxjava3.exceptions.TestException: ex0" ));
330
+ assertEquals (overview , 5 , overview .split ("at\\ s" ).length );
353
331
}
354
332
355
333
@ Test
356
- public void rootCauseEval () {
357
- final TestException ex0 = new TestException ();
358
- Throwable throwable = new Throwable () {
359
-
360
- private static final long serialVersionUID = 3597694032723032281L ;
361
-
362
- @ Override
363
- public synchronized Throwable getCause () {
364
- return ex0 ;
365
- }
366
- };
367
- CompositeException ex = new CompositeException (throwable );
368
- assertSame (ex0 , ex .getRootCause (ex ));
334
+ public void nestedMultilineMessage () {
335
+ TestException ex1 = new TestException ("ex1" );
336
+ TestException ex2 = new TestException ("ex2" );
337
+ CompositeException composite1 = new CompositeException (
338
+ ex1 ,
339
+ ex2
340
+ );
341
+ TestException ex3 = new TestException ("ex3" );
342
+ TestException ex4 = new TestException ("ex4" , composite1 );
343
+
344
+ CompositeException composite2 = new CompositeException (
345
+ ex3 ,
346
+ ex4
347
+ );
348
+
349
+ String overview = composite2 .getCause ().getMessage ();
350
+ System .err .println (overview );
351
+
352
+ assertTrue (overview , overview .contains (" Multiple exceptions (2)" ));
353
+ assertTrue (overview , overview .contains (" |-- io.reactivex.rxjava3.exceptions.TestException: ex1" ));
354
+ assertTrue (overview , overview .contains (" |-- io.reactivex.rxjava3.exceptions.TestException: ex2" ));
369
355
}
370
356
371
357
@ Test
372
- public void rootCauseSelf () {
373
- Throwable throwable = new Throwable () {
358
+ public void singleExceptionIsTheCause () {
359
+ TestException ex = new TestException ("ex1" );
360
+ CompositeException composite = new CompositeException (ex );
374
361
375
- private static final long serialVersionUID = -4398003222998914415L ;
376
-
377
- @ Override
378
- public synchronized Throwable getCause () {
379
- return this ;
380
- }
381
- };
382
- CompositeException tmp = new CompositeException (new TestException ());
383
- assertSame (throwable , tmp .getRootCause (throwable ));
362
+ assertSame (composite .getCause (), ex );
384
363
}
385
364
}
386
365
0 commit comments