1+ package net .minidev .json .test ;
2+
3+ import net .minidev .json .JSONArray ;
4+ import net .minidev .json .JSONObject ;
5+ import org .junit .jupiter .api .Assertions ;
6+ import org .junit .jupiter .api .Test ;
7+
8+ class JSONObjectTest {
9+
10+ @ Test
11+ void mergeIntegerFailed () {
12+ JSONObject jsonObject1 = new JSONObject ();
13+ jsonObject1 .appendField ("k1" , 1 );
14+ Assertions .assertEquals ("{\" k1\" :1}" , jsonObject1 .toJSONString ());
15+
16+ // replace with new value by Map merge
17+ jsonObject1 .merge ("k1" , 11 , (oldValue , newValue ) -> newValue );
18+ Assertions .assertEquals ("{\" k1\" :11}" , jsonObject1 .toJSONString ());
19+
20+ JSONObject jsonObject3 = new JSONObject ();
21+ jsonObject3 .appendField ("k2" , 2 );
22+ jsonObject1 .merge (jsonObject3 );
23+ Assertions .assertEquals ("{\" k1\" :11,\" k2\" :2}" , jsonObject1 .toJSONString ());
24+
25+ // replace with new value by JSONObject merge will fail
26+ Exception exception = Assertions .assertThrows (RuntimeException .class , () -> {
27+ JSONObject jsonObject2 = new JSONObject ();
28+ jsonObject2 .appendField ("k1" , 101 );
29+ jsonObject1 .merge (jsonObject2 );
30+ });
31+
32+ Assertions .assertEquals (exception .getMessage (), "JSON merge can not merge two java.lang.Integer Object together" );
33+ }
34+
35+ @ Test
36+ void mergeStringFailed () {
37+ JSONObject jsonObject1 = new JSONObject ();
38+ jsonObject1 .appendField ("k1" , "v1" );
39+ Assertions .assertEquals ("{\" k1\" :\" v1\" }" , jsonObject1 .toJSONString ());
40+
41+ // replace with new value by Map merge
42+ jsonObject1 .merge ("k1" , "vNew1" , (oldValue , newValue ) -> newValue );
43+ Assertions .assertEquals ("{\" k1\" :\" vNew1\" }" , jsonObject1 .toJSONString ());
44+
45+ JSONObject jsonObject3 = new JSONObject ();
46+ jsonObject3 .appendField ("k2" , "v2" );
47+ jsonObject1 .merge (jsonObject3 );
48+ Assertions .assertEquals ("{\" k1\" :\" vNew1\" ,\" k2\" :\" v2\" }" , jsonObject1 .toJSONString ());
49+
50+ // replace with new value by JSONObject merge will fail
51+ Exception exception = Assertions .assertThrows (RuntimeException .class , () -> {
52+ JSONObject jsonObject2 = new JSONObject ();
53+ jsonObject2 .appendField ("k1" , "vNew2" );
54+ jsonObject1 .merge (jsonObject2 );
55+ System .out .println (jsonObject1 .toJSONString ());
56+ });
57+
58+ Assertions .assertEquals (exception .getMessage (), "JSON merge can not merge two java.lang.String Object together" );
59+ }
60+
61+ @ Test
62+ void mergeJsonObjectFailed () {
63+ JSONObject jsonObject1 = new JSONObject ();
64+ jsonObject1 .appendField ("k1" , "v1" );
65+ Assertions .assertEquals ("{\" k1\" :\" v1\" }" , jsonObject1 .toJSONString ());
66+
67+ JSONObject jsonObject2 = new JSONObject ();
68+ jsonObject2 .appendField ("k2" , jsonObject1 );
69+ Assertions .assertEquals ("{\" k2\" :{\" k1\" :\" v1\" }}" , jsonObject2 .toJSONString ());
70+
71+ // replace with new value by JSONObject merge will fail
72+ JSONObject jsonObject3 = new JSONObject ();
73+ jsonObject3 .appendField ("k1" , "vNew1" );
74+
75+ JSONObject jsonObject4 = new JSONObject ();
76+ jsonObject4 .appendField ("k2" , jsonObject3 );
77+ Assertions .assertEquals ("{\" k2\" :{\" k1\" :\" vNew1\" }}" , jsonObject4 .toJSONString ());
78+
79+ Exception exception = Assertions .assertThrows (RuntimeException .class , () -> {
80+ jsonObject4 .merge (jsonObject2 );
81+ });
82+
83+ Assertions .assertEquals (exception .getMessage (), "JSON merge can not merge two java.lang.String Object together" );
84+ }
85+
86+ @ Test
87+ void mergeJsonArraySuccess () {
88+ JSONObject jsonObject1 = new JSONObject ();
89+ jsonObject1 .appendField ("k1" , "v1" );
90+ JSONObject jsonObject2 = new JSONObject ();
91+ jsonObject2 .appendField ("k2" , "v2" );
92+
93+ JSONArray jsonArray1 = new JSONArray ();
94+ jsonArray1 .add (jsonObject1 );
95+ jsonArray1 .add (jsonObject2 );
96+ Assertions .assertEquals ("[{\" k1\" :\" v1\" },{\" k2\" :\" v2\" }]" , jsonArray1 .toJSONString ());
97+
98+ // replace with new value by JSONObject merge will fail
99+ JSONObject jsonObject3 = new JSONObject ();
100+ jsonObject3 .appendField ("k1" , "vNew1" );
101+ JSONObject jsonObject4 = new JSONObject ();
102+ jsonObject4 .appendField ("k2" , "vNew2" );
103+
104+ JSONArray jsonArray2 = new JSONArray ();
105+ jsonArray2 .add (jsonObject3 );
106+ jsonArray2 .add (jsonObject4 );
107+ Assertions .assertEquals ("[{\" k1\" :\" vNew1\" },{\" k2\" :\" vNew2\" }]" , jsonArray2 .toJSONString ());
108+
109+ jsonArray2 .merge (jsonArray1 );
110+ Assertions .assertEquals ("[{\" k1\" :\" vNew1\" },{\" k2\" :\" vNew2\" },{\" k1\" :\" v1\" },{\" k2\" :\" v2\" }]" , jsonArray2 .toJSONString ());
111+ }
112+
113+ @ Test
114+ void mergeIntegerWithOverwriteSuccess () {
115+ JSONObject jsonObject1 = new JSONObject ();
116+ jsonObject1 .appendField ("k1" , 1 );
117+ Assertions .assertEquals ("{\" k1\" :1}" , jsonObject1 .toJSONString ());
118+
119+ // replace with new value by Map merge
120+ jsonObject1 .merge ("k1" , 11 , (oldValue , newValue ) -> newValue );
121+ Assertions .assertEquals ("{\" k1\" :11}" , jsonObject1 .toJSONString ());
122+
123+ JSONObject jsonObject3 = new JSONObject ();
124+ jsonObject3 .appendField ("k2" , 2 );
125+ jsonObject1 .merge (jsonObject3 );
126+ Assertions .assertEquals ("{\" k1\" :11,\" k2\" :2}" , jsonObject1 .toJSONString ());
127+
128+ // replace with new value by JSONObject merge with override success
129+ JSONObject jsonObject2 = new JSONObject ();
130+ jsonObject2 .appendField ("k1" , 101 );
131+ jsonObject1 .merge (jsonObject2 , true );
132+ Assertions .assertEquals ("{\" k1\" :101,\" k2\" :2}" , jsonObject1 .toJSONString ());
133+ }
134+
135+ @ Test
136+ void mergeStringWithOverwriteSuccess () {
137+ JSONObject jsonObject1 = new JSONObject ();
138+ jsonObject1 .appendField ("k1" , "v1" );
139+ Assertions .assertEquals ("{\" k1\" :\" v1\" }" , jsonObject1 .toJSONString ());
140+
141+ // replace with new value by Map merge
142+ jsonObject1 .merge ("k1" , "vNew1" , (oldValue , newValue ) -> newValue );
143+ Assertions .assertEquals ("{\" k1\" :\" vNew1\" }" , jsonObject1 .toJSONString ());
144+
145+ JSONObject jsonObject3 = new JSONObject ();
146+ jsonObject3 .appendField ("k2" , "v2" );
147+ jsonObject1 .merge (jsonObject3 );
148+ Assertions .assertEquals ("{\" k1\" :\" vNew1\" ,\" k2\" :\" v2\" }" , jsonObject1 .toJSONString ());
149+
150+ // replace with new value by JSONObject merge with override success
151+ JSONObject jsonObject2 = new JSONObject ();
152+ jsonObject2 .appendField ("k1" , "vNew2" );
153+ jsonObject1 .merge (jsonObject2 , true );
154+ Assertions .assertEquals ("{\" k1\" :\" vNew2\" ,\" k2\" :\" v2\" }" , jsonObject1 .toJSONString ());
155+ }
156+
157+ @ Test
158+ void mergeJsonObjectWithOverwriteSuccess () {
159+ JSONObject jsonObject1 = new JSONObject ();
160+ jsonObject1 .appendField ("k1" , "v1" );
161+ Assertions .assertEquals ("{\" k1\" :\" v1\" }" , jsonObject1 .toJSONString ());
162+
163+ JSONObject jsonObject2 = new JSONObject ();
164+ jsonObject2 .appendField ("k2" , jsonObject1 );
165+ Assertions .assertEquals ("{\" k2\" :{\" k1\" :\" v1\" }}" , jsonObject2 .toJSONString ());
166+
167+ // JSONObject merge will overwrite jsonObject3 by jsonObject2
168+ JSONObject jsonObject3 = new JSONObject ();
169+ jsonObject3 .appendField ("k1" , "vNew1" );
170+
171+ JSONObject jsonObject4 = new JSONObject ();
172+ jsonObject4 .appendField ("k2" , jsonObject3 );
173+ Assertions .assertEquals ("{\" k2\" :{\" k1\" :\" vNew1\" }}" , jsonObject4 .toJSONString ());
174+
175+ jsonObject4 .merge (jsonObject2 , true );
176+ Assertions .assertEquals ("{\" k2\" :{\" k1\" :\" v1\" }}" , jsonObject4 .toJSONString ());
177+ }
178+ }
0 commit comments