1
+ /**
2
+ * Copyright 2013 Netflix, Inc.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ package rx .util ;
17
+
18
+ import static org .junit .Assert .*;
19
+
20
+ import java .util .ArrayList ;
21
+ import java .util .Arrays ;
22
+ import java .util .List ;
23
+
24
+ import org .junit .Test ;
25
+
26
+ public class CompositeExceptionTest {
27
+
28
+ private final Throwable ex1 = new Throwable ("Ex1" );
29
+ private final Throwable ex2 = new Throwable ("Ex2" , ex1 );
30
+ private final Throwable ex3 = new Throwable ("Ex3" , ex2 );
31
+
32
+ public CompositeExceptionTest () {
33
+ ex1 .initCause (ex2 );
34
+ }
35
+
36
+ private CompositeException getNewCompositeExceptionWithEx123 () {
37
+ List <Throwable > throwables = new ArrayList <Throwable >();
38
+ throwables .add (ex1 );
39
+ throwables .add (ex2 );
40
+ throwables .add (ex3 );
41
+ return new CompositeException (throwables );
42
+ }
43
+
44
+ @ Test (timeout = 1000 )
45
+ public void testMultipleWithSameCause () {
46
+ Throwable rootCause = new Throwable ("RootCause" );
47
+ Throwable e1 = new Throwable ("1" , rootCause );
48
+ Throwable e2 = new Throwable ("2" , rootCause );
49
+ Throwable e3 = new Throwable ("3" , rootCause );
50
+ CompositeException ce = new CompositeException ("3 failures with same root cause" , Arrays .asList (e1 , e2 , e3 ));
51
+ }
52
+
53
+ @ Test (timeout = 1000 )
54
+ public void testAttachCallingThreadStackParentThenChild () {
55
+ CompositeException .attachCallingThreadStack (ex1 , ex2 );
56
+ assertEquals ("Ex2" , ex1 .getCause ().getMessage ());
57
+ }
58
+
59
+ @ Test (timeout = 1000 )
60
+ public void testAttachCallingThreadStackChildThenParent () {
61
+ CompositeException .attachCallingThreadStack (ex2 , ex1 );
62
+ assertEquals ("Ex1" , ex2 .getCause ().getMessage ());
63
+ }
64
+
65
+ @ Test (timeout = 1000 )
66
+ public void testAttachCallingThreadStackAddComposite () {
67
+ CompositeException .attachCallingThreadStack (ex1 , getNewCompositeExceptionWithEx123 ());
68
+ assertEquals ("Ex2" , ex1 .getCause ().getMessage ());
69
+ }
70
+
71
+ @ Test (timeout = 1000 )
72
+ public void testAttachCallingThreadStackAddToComposite () {
73
+ CompositeException compositeEx = getNewCompositeExceptionWithEx123 ();
74
+ CompositeException .attachCallingThreadStack (compositeEx , ex1 );
75
+ assertEquals (CompositeException .CompositeExceptionCausalChain .MESSAGE , compositeEx .getCause ().getMessage ());
76
+ }
77
+
78
+ @ Test (timeout = 1000 )
79
+ public void testAttachCallingThreadStackAddCompositeToItself () {
80
+ CompositeException compositeEx = getNewCompositeExceptionWithEx123 ();
81
+ CompositeException .attachCallingThreadStack (compositeEx , compositeEx );
82
+ assertEquals (CompositeException .CompositeExceptionCausalChain .MESSAGE , compositeEx .getCause ().getMessage ());
83
+ }
84
+
85
+ @ Test (timeout = 1000 )
86
+ public void testAttachCallingThreadStackAddExceptionsToEachOther () {
87
+ CompositeException .attachCallingThreadStack (ex1 , ex2 );
88
+ CompositeException .attachCallingThreadStack (ex2 , ex1 );
89
+ assertEquals ("Ex2" , ex1 .getCause ().getMessage ());
90
+ assertEquals ("Ex1" , ex2 .getCause ().getMessage ());
91
+ }
92
+ }
0 commit comments