FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JSON-Java-unit-test/Util.java at master · FritzMock/JSON-Java-unit-test · GitHub
FritzMock
/
JSON-Java-unit-test
Public
forked from
stleary/JSON-Java-unit-test
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JSON-Java-unit-test
/
Util.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
109 lines (96 loc) · 4.45 KB
Breadcrumbs
JSON-Java-unit-test
/
Util.java
Copy path
File metadata and controls
109 lines (96 loc) · 4.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package
org
.
json
.
junit
;
import
static
org
.
junit
.
Assert
.*;
import
java
.
util
.*;
import
org
.
json
.*;
public
class
Util
{
/**
* Compares two json arrays for equality
* @param jsonArray created by the code to be tested
* @param expectedJsonArray created specifically for comparing
*/
public
static
void
compareActualVsExpectedJsonArrays
(
JSONArray
jsonArray
,
JSONArray
expectedJsonArray
) {
assertTrue
(
"jsonArray lengths should be equal"
,
jsonArray
.
length
() ==
expectedJsonArray
.
length
());
for
(
int
i
=
0
;
i
<
jsonArray
.
length
(); ++
i
) {
Object
value
=
jsonArray
.
get
(
i
);
Object
expectedValue
=
expectedJsonArray
.
get
(
i
);
compareActualVsExpectedObjects
(
value
,
expectedValue
);
}
}
/**
* Compares two json objects for equality
* @param jsonObject created by the code to be tested
* @param expectedJsonObject created specifically for comparing
*/
public
static
void
compareActualVsExpectedJsonObjects
(
JSONObject
jsonObject
,
JSONObject
expectedJsonObject
) {
assertTrue
(
"jsonObjects should have the same length"
,
jsonObject
.
length
() ==
expectedJsonObject
.
length
());
Iterator
<
String
>
keys
=
jsonObject
.
keys
();
while
(
keys
.
hasNext
()) {
String
key
=
keys
.
next
();
Object
value
=
jsonObject
.
get
(
key
);
Object
expectedValue
=
expectedJsonObject
.
get
(
key
);
compareActualVsExpectedObjects
(
value
,
expectedValue
);
}
}
/**
* Compare two objects for equality. Might be JSONArray, JSONObject,
* or something else.
* @param value created by the code to be tested
* @param expectedValue created specifically for comparing
* @param key key to the jsonObject entry to be compared
*/
private
static
void
compareActualVsExpectedObjects
(
Object
value
,
Object
expectedValue
) {
if
(
value
instanceof
JSONObject
&&
expectedValue
instanceof
JSONObject
) {
JSONObject
jsonObject
= (
JSONObject
)
value
;
JSONObject
expectedJsonObject
= (
JSONObject
)
expectedValue
;
compareActualVsExpectedJsonObjects
(
jsonObject
,
expectedJsonObject
);
}
else
if
(
value
instanceof
JSONArray
&&
expectedValue
instanceof
JSONArray
) {
JSONArray
jsonArray
= (
JSONArray
)
value
;
JSONArray
expectedJsonArray
= (
JSONArray
)
expectedValue
;
compareActualVsExpectedJsonArrays
(
jsonArray
,
expectedJsonArray
);
}
else
{
/**
* Certain helper classes (e.g. XML) may create Long instead of
* Integer for small int values. As long as both are Numbers,
* just compare the toString() values.
*/
if
(!(
value
instanceof
Number
&&
expectedValue
instanceof
Number
)) {
assertTrue
(
"object types should be equal for actual: "
+
value
.
toString
()+
" ("
+
value
.
getClass
().
toString
()+
") expected: "
+
expectedValue
.
toString
()+
" ("
+
expectedValue
.
getClass
().
toString
()+
")"
,
value
.
getClass
().
toString
().
equals
(
expectedValue
.
getClass
().
toString
()));
}
assertTrue
(
"string values should be equal for actual: "
+
value
.
toString
()+
" expected: "
+
expectedValue
.
toString
(),
value
.
toString
().
equals
(
expectedValue
.
toString
()));
}
}
public
static
void
compareActualVsExpectedStringArrays
(
String
[]
names
,
String
[]
expectedNames
) {
assertTrue
(
"Array lengths should be equal"
,
names
.
length
==
expectedNames
.
length
);
List
<
String
>
lNames
=
new
ArrayList
<
String
>(
Arrays
.
asList
(
names
));
for
(
int
i
=
0
;
i
<
expectedNames
.
length
; ++
i
) {
String
expectedName
=
expectedNames
[
i
];
assertTrue
(
"expected to find "
+
expectedName
,
lNames
.
contains
(
expectedName
));
lNames
.
remove
(
expectedName
);
}
}
public
static
void
compareXML
(
String
aXmlStr
,
String
bXmlStr
) {
// TODO For simple tests this may be adequate, but it won't work for
// elements with multiple attributes and possibly other cases as well.
// Should use XMLUnit or similar.
assertTrue
(
"expected equal XML strings
\n
aXmlStr: "
+
aXmlStr
+
"
\n
bXmlStr: "
+
bXmlStr
,
aXmlStr
.
equals
(
bXmlStr
));
}
}
Back
|
FazBrowse Home
|
New Git URL