1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.reuters.msgtest.tibrvfields;
33
34 import com.reuters.msgtest.ArrayUtility;
35 import com.tibco.tibrv.TibrvMsgField;
36 import java.util.List;
37 import java.util.Set;
38
39
40 /***
41 * @author Michael Ward
42 */
43 public class ArrayComparer extends AbstractFieldComparer {
44 public void handleCompare(TibrvMsgField expectedField,
45 TibrvMsgField actualField, StringBuffer differences,
46 List outerFieldNameList, Set ignoredFieldNames) {
47 Object[] exp = ArrayUtility.toArray(expectedField.data);
48 Object[] act = ArrayUtility.toArray(actualField.data);
49
50 if (exp.length != act.length) {
51
52
53 String fieldName = convertList(outerFieldNameList) +
54 expectedField.name;
55 String error = buildErrorMessage(fieldName, "" + exp.length,
56 "" + act.length);
57
58 differences.append(error);
59
60 int tmp_len = (exp.length > act.length) ? exp.length : act.length;
61
62 for (int i = 0; i < tmp_len; i++) {
63 differences.append("[").append(i).append("]").append("expected : ");
64
65 if (exp.length <= tmp_len) {
66 differences.append(exp[i]);
67 }
68
69 differences.append("but was : ");
70
71 if (act.length <= tmp_len) {
72 differences.append(act[i]);
73 }
74 }
75 } else {
76
77
78
79
80 boolean isFirstMismatch = false;
81
82 for (int i = 0; i < exp.length; i++) {
83 boolean valuesEqual = true;
84
85 if (exp[i] instanceof Double) {
86 double expectedDouble = ((Double) exp[i]).doubleValue();
87 double actualDouble = ((Double) act[i]).doubleValue();
88 double delta = expectedDouble - actualDouble;
89
90 if (delta < 0) {
91 delta = -delta;
92 }
93
94 if (delta > TOLERANCE) {
95 valuesEqual = false;
96 }
97 } else if (!(exp[i].equals(act[i]))) {
98 valuesEqual = false;
99 }
100
101
102
103 if (!valuesEqual) {
104 if (!isFirstMismatch) {
105 isFirstMismatch = true;
106
107 String outerFieldName = convertList(outerFieldNameList);
108 differences.append("FieldName: ").append(outerFieldName)
109 .append(expectedField.name).append("\n");
110 }
111
112 differences.append("[").append(i + 1).append("]")
113 .append(" expected <").append(exp[i])
114 .append("> but was <").append(act[i]).append(">\n");
115 }
116 }
117 }
118 }
119 }