1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.classic.jsonTest;
16
17 import ch.qos.logback.classic.spi.IThrowableProxy;
18 import ch.qos.logback.classic.spi.StackTraceElementProxy;
19
20 import java.util.Arrays;
21 import java.util.Objects;
22
23 public class ThrowableProxyComparator {
24
25
26 static public boolean areEqual(IThrowableProxy left, IThrowableProxy right) {
27
28 if(left == right)
29 return true;
30
31 if(left == null)
32 return false;
33
34 if(!left.getClassName().equals(right.getClassName()))
35 return false;
36
37
38
39 if(!left.getMessage().equals(right.getMessage()))
40 return false;
41
42 System.out.println("before equalsSTEPArray left.message="+left.getMessage()+", right.message="+right.getMessage());
43
44
45 StackTraceElementProxy[] leftStepArray = left.getStackTraceElementProxyArray();
46 StackTraceElementProxy[] rightStepArray = right.getStackTraceElementProxyArray();
47
48 if(left.getCommonFrames() != right.getCommonFrames()) {
49 return false;
50 }
51
52 if(!equalsSTEPArray(leftStepArray, rightStepArray, left.getCommonFrames()))
53 return false;
54
55 boolean causeComparaison = areEqual(left.getCause(), right.getCause());
56 if(!causeComparaison)
57 return causeComparaison;
58
59 if (!compareSuppressedThrowables(left, right))
60 return false;
61
62 return true;
63 }
64
65 private static boolean compareSuppressedThrowables(IThrowableProxy left, IThrowableProxy right) {
66 IThrowableProxy[] leftSuppressedThrowableArray = left.getSuppressed();
67 IThrowableProxy[] rightSuppressedThrowableArray = right.getSuppressed();
68
69
70
71
72
73 if(leftSuppressedThrowableArray == null && rightSuppressedThrowableArray == null) {
74 return true;
75 }
76 if(leftSuppressedThrowableArray.length == 0 && rightSuppressedThrowableArray == null) {
77 return true;
78 }
79
80 if(leftSuppressedThrowableArray.length != rightSuppressedThrowableArray.length) {
81 System.out.println("suppressed array length discrepancy");
82 return false;
83 }
84
85 for(int i = 0; i < leftSuppressedThrowableArray.length; i++) {
86 IThrowableProxy leftSuppressed = leftSuppressedThrowableArray[i];
87 IThrowableProxy rightSuppressed = rightSuppressedThrowableArray[i];
88
89 boolean suppressedComparison = areEqual(leftSuppressed, rightSuppressed);
90 if(!suppressedComparison) {
91 System.out.println("suppressed ITP comparison failed at position "+i);
92 return false;
93 }
94 }
95 return true;
96 }
97
98 static public boolean equalsSTEPArray( StackTraceElementProxy[] leftStepArray, StackTraceElementProxy[] rightStepArray, int commonFrames) {
99 if (leftStepArray==rightStepArray)
100 return true;
101 if (leftStepArray==null || rightStepArray==null)
102 return false;
103
104 int length = leftStepArray.length - commonFrames;
105 if (rightStepArray.length != length) {
106 System.out.println("length discrepancy");
107 return false;
108 }
109
110 System.out.println("checking ste array elements ");
111
112 for (int i=0; i< (length -commonFrames); i++) {
113 StackTraceElementProxy leftStep = leftStepArray[i];
114 StackTraceElementProxy rightStep = rightStepArray[i];
115
116 if (!equalsSTEP(leftStep, rightStep)) {
117 System.out.println("left "+leftStep);
118 System.out.println("right "+rightStep);
119 return false;
120 }
121 }
122 return true;
123 }
124
125 static public boolean equalsSTEP(StackTraceElementProxy left, StackTraceElementProxy right) {
126 if (left==right)
127 return true;
128
129 if (right == null)
130 return false;
131
132 StackTraceElement l = left.getStackTraceElement();
133 StackTraceElement r = right.getStackTraceElement();
134
135 if(!Objects.equals(l.getClassName(), (r.getClassName())))
136 return false;
137
138
139 if(!Objects.equals(l.getMethodName(), (r.getMethodName())))
140 return false;
141
142
143 if(!Objects.equals(l.getFileName(), (r.getFileName())))
144 return false;
145
146 if(l.getLineNumber() != r.getLineNumber())
147 return false;
148
149
150 return true;
151 }
152 }