1 | |
package uk.co.datumedge.hamcrest.json; |
2 | |
|
3 | |
import static uk.co.datumedge.hamcrest.json.JSONArrayComparatorFactory.jsonArrayComparison; |
4 | |
import static uk.co.datumedge.hamcrest.json.JSONAssertComparator.modalComparatorFor; |
5 | |
import static uk.co.datumedge.hamcrest.json.JSONObjectComparatorFactory.jsonObjectComparison; |
6 | |
import static uk.co.datumedge.hamcrest.json.StringComparatorFactory.stringComparison; |
7 | |
|
8 | |
import java.io.PrintWriter; |
9 | |
import java.io.StringWriter; |
10 | |
|
11 | |
import org.hamcrest.Description; |
12 | |
import org.hamcrest.Factory; |
13 | |
import org.hamcrest.TypeSafeDiagnosingMatcher; |
14 | |
import org.json.JSONArray; |
15 | |
import org.json.JSONException; |
16 | |
import org.json.JSONObject; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public final class SameJSONAs<T> extends TypeSafeDiagnosingMatcher<T> { |
25 | |
private final T expected; |
26 | |
private final JSONModalComparator<T> comparator; |
27 | |
|
28 | 33 | public SameJSONAs(T expected, JSONModalComparator<T> comparator) { |
29 | 33 | this.expected = expected; |
30 | 33 | this.comparator = comparator; |
31 | 33 | } |
32 | |
|
33 | |
@Override |
34 | |
public void describeTo(Description description) { |
35 | 1 | description.appendValue(expected.toString()); |
36 | 1 | } |
37 | |
|
38 | |
@Override |
39 | |
protected boolean matchesSafely(T actual, Description mismatchDescription) { |
40 | |
try { |
41 | 25 | JSONComparisonResult result = comparator.compare(expected, actual); |
42 | 22 | if (result.failed()) { |
43 | 13 | mismatchDescription.appendDescriptionOf(result); |
44 | |
} |
45 | 22 | return result.passed(); |
46 | 3 | } catch (JSONException e) { |
47 | 3 | StringWriter out = new StringWriter(); |
48 | 3 | e.printStackTrace(new PrintWriter(out)); |
49 | 3 | mismatchDescription.appendText(out.toString()); |
50 | 3 | return false; |
51 | |
} |
52 | |
} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public SameJSONAs<T> allowingAnyArrayOrdering() { |
61 | 6 | return new SameJSONAs<T>(expected, comparator.butAllowingAnyArrayOrdering()); |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public SameJSONAs<T> allowingExtraUnexpectedFields() { |
96 | 5 | return new SameJSONAs<T>(expected, comparator.butAllowingExtraUnexpectedFields()); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
@Factory |
106 | |
public static SameJSONAs<JSONObject> sameJSONObjectAs(JSONObject expected) { |
107 | 6 | return new SameJSONAs<JSONObject>(expected, modalComparatorFor(jsonObjectComparison())); |
108 | |
} |
109 | |
|
110 | |
@Factory |
111 | |
public static SameJSONAs<JSONObject> sameJSONObjectAs(JSONObject expected, JSONModalComparator<JSONObject> comparator) { |
112 | 1 | return new SameJSONAs<JSONObject>(expected, comparator); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
@Factory |
122 | |
public static SameJSONAs<JSONArray> sameJSONArrayAs(JSONArray expected) { |
123 | 8 | return new SameJSONAs<JSONArray>(expected, modalComparatorFor(jsonArrayComparison())); |
124 | |
} |
125 | |
|
126 | |
@Factory |
127 | |
public static SameJSONAs<? super JSONArray> sameJSONArrayAs(JSONArray expected, JSONModalComparator<JSONArray> comparator) { |
128 | 1 | return new SameJSONAs<JSONArray>(expected, comparator); |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
@Factory |
138 | |
public static SameJSONAs<? super String> sameJSONAs(String expected) { |
139 | 3 | return new SameJSONAs<String>(expected, modalComparatorFor(stringComparison())); |
140 | |
} |
141 | |
|
142 | |
@Factory |
143 | |
public static SameJSONAs<? super String> sameJSONAs(String expected, JSONModalComparator<String> comparator) { |
144 | 1 | return new SameJSONAs<String>(expected, comparator); |
145 | |
} |
146 | |
} |