14
14
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
15
*/
16
16
17
- /*
18
- * $Id$
19
- */
17
+ package jakarta .json .bind ;
20
18
21
- package com . sun . ts . tests . jsonb ;
19
+ import static org . junit . Assert . fail ;
22
20
23
21
import java .io .ByteArrayInputStream ;
24
22
import java .io .ByteArrayOutputStream ;
28
26
import java .lang .reflect .Type ;
29
27
import java .nio .charset .StandardCharsets ;
30
28
31
- import javax .json .bind .Jsonb ;
32
- import javax .json .bind .JsonbBuilder ;
33
-
34
- import com .sun .javatest .Status ;
35
- import com .sun .ts .lib .harness .EETest .Fault ;
36
-
37
- import static com .sun .ts .tests .jsonb .MappingTester .combine ;
38
-
39
29
public class SimpleMappingTester <T > {
40
30
private final Jsonb jsonb = JsonbBuilder .create ();
41
31
@@ -45,103 +35,100 @@ public SimpleMappingTester(Class<T> typeClass) {
45
35
this .typeClass = typeClass ;
46
36
}
47
37
48
- public Status test (T value , String expectedRepresentationPattern ,
49
- String expectedRepresentation , Object expectedOutput ) throws Fault {
50
- return combine ( testMarshalling (value , expectedRepresentationPattern ),
51
- testMarshallingToStream (value , expectedRepresentationPattern ),
52
- testMarshallingToWriter (value , expectedRepresentationPattern ),
53
- testMarshallingByType (value , expectedRepresentationPattern ),
54
- testMarshallingByTypeToStream (value , expectedRepresentationPattern ),
55
- testMarshallingByTypeToWriter (value , expectedRepresentationPattern ),
56
- testUnmarshallingByClass (expectedRepresentation , expectedOutput ),
38
+ public void test (T value , String expectedRepresentationPattern ,
39
+ String expectedRepresentation , Object expectedOutput ) {
40
+ testMarshalling (value , expectedRepresentationPattern );
41
+ testMarshallingToStream (value , expectedRepresentationPattern );
42
+ testMarshallingToWriter (value , expectedRepresentationPattern );
43
+ testMarshallingByType (value , expectedRepresentationPattern );
44
+ testMarshallingByTypeToStream (value , expectedRepresentationPattern );
45
+ testMarshallingByTypeToWriter (value , expectedRepresentationPattern );
46
+ testUnmarshallingByClass (expectedRepresentation , expectedOutput );
57
47
testUnmarshallingByClassFromStream (expectedRepresentation ,
58
- expectedOutput ),
48
+ expectedOutput );
59
49
testUnmarshallingByClassFromReader (expectedRepresentation ,
60
- expectedOutput ),
61
- testUnmarshallingByType (expectedRepresentation , expectedOutput ),
50
+ expectedOutput );
51
+ testUnmarshallingByType (expectedRepresentation , expectedOutput );
62
52
testUnmarshallingByTypeFromStream (expectedRepresentation ,
63
- expectedOutput ),
53
+ expectedOutput );
64
54
testUnmarshallingByTypeFromReader (expectedRepresentation ,
65
- expectedOutput )) ;
55
+ expectedOutput );
66
56
}
67
57
68
- private Status testMarshalling (T value , String expectedRepresentation ) {
58
+ private void testMarshalling (T value , String expectedRepresentation ) {
69
59
String jsonString = jsonb .toJson (value );
70
60
if (jsonString .matches (expectedRepresentation )) {
71
- return Status . passed ( "OK" );
61
+ return ; // passed
72
62
} else {
73
- return Status . failed ("[testMarshalling] - Failed to correctly marshal "
63
+ fail ("[testMarshalling] - Failed to correctly marshal "
74
64
+ value .getClass ().getName () + " object" );
75
65
}
76
66
}
77
67
78
- private Status testMarshallingToStream (T value ,
68
+ private void testMarshallingToStream (T value ,
79
69
String expectedRepresentation ) {
80
70
try (ByteArrayOutputStream stream = new ByteArrayOutputStream ()) {
81
71
jsonb .toJson (value , stream );
82
72
String jsonString = new String (stream .toByteArray (),
83
73
StandardCharsets .UTF_8 );
84
74
if (jsonString .matches (expectedRepresentation )) {
85
- return Status . passed ( "OK" );
75
+ return ; // passed
86
76
} else {
87
- return Status
88
- .failed ("[testMarshallingToStream] - Failed to correctly marshal "
77
+ fail ("[testMarshallingToStream] - Failed to correctly marshal "
89
78
+ value .getClass ().getName () + " object" );
90
79
}
91
80
} catch (IOException e ) {
92
- return Status . error (e .getMessage ());
81
+ fail (e .getMessage ());
93
82
}
94
83
}
95
84
96
- private Status testMarshallingToWriter (T value ,
85
+ private void testMarshallingToWriter (T value ,
97
86
String expectedRepresentation ) {
98
87
try (ByteArrayOutputStream stream = new ByteArrayOutputStream ();
99
88
OutputStreamWriter writer = new OutputStreamWriter (stream )) {
100
89
jsonb .toJson (value , writer );
101
90
String jsonString = new String (stream .toByteArray (),
102
91
StandardCharsets .UTF_8 );
103
92
if (jsonString .matches (expectedRepresentation )) {
104
- return Status . passed ( "OK" );
93
+ return ; // passed
105
94
} else {
106
- return Status
107
- .failed ("[testMarshallingToWriter] - Failed to correctly marshal "
95
+ fail ("[testMarshallingToWriter] - Failed to correctly marshal "
108
96
+ value .getClass ().getName () + " object" );
109
97
}
110
98
} catch (IOException e ) {
111
- return Status . error (e .getMessage ());
99
+ fail (e .getMessage ());
112
100
}
113
101
}
114
102
115
- private Status testMarshallingByType (T value , String expectedRepresentation ) {
103
+ private void testMarshallingByType (T value , String expectedRepresentation ) {
116
104
String jsonString = jsonb .toJson (value , TypeContainer .class );
117
105
if (jsonString .matches (expectedRepresentation )) {
118
- return Status . passed ( "OK" );
106
+ return ; // passed
119
107
} else {
120
- return Status
121
- .failed ("[testMarshallingByType] - Failed to correctly marshal "
108
+ fail ("[testMarshallingByType] - Failed to correctly marshal "
122
109
+ value .getClass ().getName () + " object" );
123
110
}
124
111
}
125
112
126
- private Status testMarshallingByTypeToStream (T value ,
113
+ private void testMarshallingByTypeToStream (T value ,
127
114
String expectedRepresentation ) {
128
115
try (ByteArrayOutputStream stream = new ByteArrayOutputStream ()) {
129
116
jsonb .toJson (value , TypeContainer .class , stream );
130
117
String jsonString = new String (stream .toByteArray (),
131
118
StandardCharsets .UTF_8 );
132
119
if (jsonString .matches (expectedRepresentation )) {
133
- return Status . passed ( "OK" );
120
+ return ; // passed
134
121
} else {
135
- return Status . failed (
122
+ fail (
136
123
"[testMarshallingByTypeToStream] - Failed to correctly marshal "
137
124
+ value .getClass ().getName () + " object" );
138
125
}
139
126
} catch (IOException e ) {
140
- return Status . error (e .getMessage ());
127
+ fail (e .getMessage ());
141
128
}
142
129
}
143
130
144
- private Status testMarshallingByTypeToWriter (T value ,
131
+ private void testMarshallingByTypeToWriter (T value ,
145
132
String expectedRepresentation ) {
146
133
try (ByteArrayOutputStream stream = new ByteArrayOutputStream ();
147
134
OutputStreamWriter writer = new OutputStreamWriter (stream )) {
@@ -150,48 +137,47 @@ private Status testMarshallingByTypeToWriter(T value,
150
137
String jsonString = new String (stream .toByteArray (),
151
138
StandardCharsets .UTF_8 );
152
139
if (jsonString .matches (expectedRepresentation )) {
153
- return Status . passed ( "OK" );
140
+ return ; // passed
154
141
} else {
155
- return Status . failed (
142
+ fail (
156
143
"[testMarshallingByTypeToWriter] - Failed to correctly marshal "
157
144
+ value .getClass ().getName () + " object" );
158
145
}
159
146
} catch (IOException e ) {
160
- return Status . error (e .getMessage ());
147
+ fail (e .getMessage ());
161
148
}
162
149
}
163
150
164
- private Status testUnmarshallingByClass (String expectedRepresentation ,
151
+ private void testUnmarshallingByClass (String expectedRepresentation ,
165
152
Object value ) {
166
153
Object unmarshalledObject = jsonb .fromJson (expectedRepresentation ,
167
154
typeClass );
168
155
if (value .equals (unmarshalledObject )) {
169
- return Status . passed ( "OK" );
156
+ return ; // passed
170
157
} else {
171
- return Status
172
- .failed ("[testUnmarshallingByClass] - Failed to correctly unmarshal "
158
+ fail ("[testUnmarshallingByClass] - Failed to correctly unmarshal "
173
159
+ value .getClass ().getName () + " object" );
174
160
}
175
161
}
176
162
177
- private Status testUnmarshallingByClassFromStream (
163
+ private void testUnmarshallingByClassFromStream (
178
164
String expectedRepresentation , Object value ) {
179
165
try (ByteArrayInputStream stream = new ByteArrayInputStream (
180
166
expectedRepresentation .getBytes (StandardCharsets .UTF_8 ))) {
181
167
Object unmarshalledObject = jsonb .fromJson (stream , typeClass );
182
168
if (value .equals (unmarshalledObject )) {
183
- return Status . passed ( "OK" );
169
+ return ; // passed
184
170
} else {
185
- return Status . failed (
171
+ fail (
186
172
"[testUnmarshallingByClassFromStream] - Failed to correctly unmarshal "
187
173
+ value .getClass ().getName () + " object" );
188
174
}
189
175
} catch (IOException e ) {
190
- return Status . error (e .getMessage ());
176
+ fail (e .getMessage ());
191
177
}
192
178
}
193
179
194
- private Status testUnmarshallingByClassFromReader (
180
+ private void testUnmarshallingByClassFromReader (
195
181
String expectedRepresentation , Object value ) {
196
182
try (
197
183
ByteArrayInputStream stream = new ByteArrayInputStream (
@@ -200,48 +186,47 @@ private Status testUnmarshallingByClassFromReader(
200
186
201
187
Object unmarshalledObject = jsonb .fromJson (reader , typeClass );
202
188
if (value .equals (unmarshalledObject )) {
203
- return Status . passed ( "OK" );
189
+ return ; // passed
204
190
} else {
205
- return Status . failed (
191
+ fail (
206
192
"[testUnmarshallingByClassFromReader] - Failed to correctly unmarshal "
207
193
+ value .getClass ().getName () + " object" );
208
194
}
209
195
} catch (IOException e ) {
210
- return Status . error (e .getMessage ());
196
+ fail (e .getMessage ());
211
197
}
212
198
}
213
199
214
- private Status testUnmarshallingByType (String expectedRepresentation ,
200
+ private void testUnmarshallingByType (String expectedRepresentation ,
215
201
Object value ) {
216
202
Object unmarshalledObject = jsonb .fromJson (expectedRepresentation ,
217
203
(Type ) typeClass );
218
204
if (value .equals (unmarshalledObject )) {
219
- return Status . passed ( "OK" );
205
+ return ; // passed
220
206
} else {
221
- return Status
222
- .failed ("[testUnmarshallingByType] - Failed to correctly unmarshal "
207
+ fail ("[testUnmarshallingByType] - Failed to correctly unmarshal "
223
208
+ value .getClass ().getName () + " object" );
224
209
}
225
210
}
226
211
227
- private Status testUnmarshallingByTypeFromStream (
212
+ private void testUnmarshallingByTypeFromStream (
228
213
String expectedRepresentation , Object value ) {
229
214
try (ByteArrayInputStream stream = new ByteArrayInputStream (
230
215
expectedRepresentation .getBytes (StandardCharsets .UTF_8 ))) {
231
216
Object unmarshalledObject = jsonb .fromJson (stream , (Type ) typeClass );
232
217
if (value .equals (unmarshalledObject )) {
233
- return Status . passed ( "OK" );
218
+ return ; // passed
234
219
} else {
235
- return Status . failed (
220
+ fail (
236
221
"[testUnmarshallingByTypeFromStream] - Failed to correctly unmarshal "
237
222
+ value .getClass ().getName () + " object" );
238
223
}
239
224
} catch (IOException e ) {
240
- return Status . error (e .getMessage ());
225
+ fail (e .getMessage ());
241
226
}
242
227
}
243
228
244
- private Status testUnmarshallingByTypeFromReader (
229
+ private void testUnmarshallingByTypeFromReader (
245
230
String expectedRepresentation , Object value ) {
246
231
try (
247
232
ByteArrayInputStream stream = new ByteArrayInputStream (
@@ -250,14 +235,14 @@ private Status testUnmarshallingByTypeFromReader(
250
235
251
236
Object unmarshalledObject = jsonb .fromJson (reader , (Type ) typeClass );
252
237
if (value .equals (unmarshalledObject )) {
253
- return Status . passed ( "OK" );
238
+ return ; // passed
254
239
} else {
255
- return Status . failed (
240
+ fail (
256
241
"[testUnmarshallingByTypeFromReader] - Failed to correctly unmarshal "
257
242
+ value .getClass ().getName () + " object" );
258
243
}
259
244
} catch (IOException e ) {
260
- return Status . error (e .getMessage ());
245
+ fail (e .getMessage ());
261
246
}
262
247
}
263
248
}
0 commit comments