1
+ package net .minidev .json .test .reader ;
2
+
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
6
+
7
+ import java .io .IOException ;
8
+ import java .io .StringWriter ;
9
+ import net .minidev .json .JSONStyle ;
10
+ import net .minidev .json .reader .BeansWriter ;
11
+ import org .junit .jupiter .api .BeforeEach ;
12
+ import org .junit .jupiter .api .Test ;
13
+
14
+ public class BeansWriterTest {
15
+
16
+ private BeansWriter beansWriter ;
17
+
18
+ @ BeforeEach
19
+ public void setUp () {
20
+ beansWriter = new BeansWriter ();
21
+ }
22
+
23
+ public static class SimpleBean {
24
+ public String name ;
25
+ public int age ;
26
+ private String city ;
27
+
28
+ public SimpleBean () {}
29
+
30
+ public SimpleBean (String name , int age , String city ) {
31
+ this .name = name ;
32
+ this .age = age ;
33
+ this .city = city ;
34
+ }
35
+
36
+ public String getCity () {
37
+ return city ;
38
+ }
39
+
40
+ public void setCity (String city ) {
41
+ this .city = city ;
42
+ }
43
+ }
44
+
45
+ public static class BooleanBean {
46
+ private boolean active ;
47
+ private Boolean enabled ;
48
+
49
+ public BooleanBean (boolean active , Boolean enabled ) {
50
+ this .active = active ;
51
+ this .enabled = enabled ;
52
+ }
53
+
54
+ public boolean isActive () {
55
+ return active ;
56
+ }
57
+
58
+ public Boolean getEnabled () {
59
+ return enabled ;
60
+ }
61
+ }
62
+
63
+ public static class BeanWithStaticFields {
64
+ public static final String CONSTANT = "test" ;
65
+ public static int staticField = 42 ;
66
+ public String normalField = "normal" ;
67
+ private transient String transientField = "transient" ;
68
+ }
69
+
70
+ public static class BeanWithNullValues {
71
+ public String name ;
72
+ public String nullField ;
73
+
74
+ public BeanWithNullValues (String name , String nullField ) {
75
+ this .name = name ;
76
+ this .nullField = nullField ;
77
+ }
78
+ }
79
+
80
+ public static class InheritedBean extends SimpleBean {
81
+ public double salary ;
82
+
83
+ public InheritedBean (String name , int age , String city , double salary ) {
84
+ super (name , age , city );
85
+ this .salary = salary ;
86
+ }
87
+ }
88
+
89
+ @ Test
90
+ public void testWriteSimpleBean () throws IOException {
91
+ SimpleBean bean = new SimpleBean ("John" , 30 , "New York" );
92
+ StringWriter writer = new StringWriter ();
93
+
94
+ beansWriter .writeJSONString (bean , writer , JSONStyle .NO_COMPRESS );
95
+
96
+ String result = writer .toString ();
97
+ assertTrue (result .contains ("\" name\" :\" John\" " ));
98
+ assertTrue (result .contains ("\" age\" :30" ));
99
+ assertTrue (result .contains ("\" city\" :\" New York\" " ));
100
+ }
101
+
102
+ @ Test
103
+ public void testWriteBooleanBean () throws IOException {
104
+ BooleanBean bean = new BooleanBean (true , false );
105
+ StringWriter writer = new StringWriter ();
106
+
107
+ beansWriter .writeJSONString (bean , writer , JSONStyle .NO_COMPRESS );
108
+
109
+ String result = writer .toString ();
110
+ assertTrue (result .contains ("\" active\" :true" ));
111
+ assertTrue (result .contains ("\" enabled\" :false" ));
112
+ }
113
+
114
+ @ Test
115
+ public void testWriteBeanWithStaticAndTransientFields () throws IOException {
116
+ BeanWithStaticFields bean = new BeanWithStaticFields ();
117
+ StringWriter writer = new StringWriter ();
118
+
119
+ beansWriter .writeJSONString (bean , writer , JSONStyle .NO_COMPRESS );
120
+
121
+ String result = writer .toString ();
122
+ assertTrue (result .contains ("\" normalField\" :\" normal\" " ));
123
+ assertTrue (!result .contains ("CONSTANT" ));
124
+ assertTrue (!result .contains ("staticField" ));
125
+ assertTrue (!result .contains ("transientField" ));
126
+ }
127
+
128
+ @ Test
129
+ public void testWriteBeanWithNullValuesIgnoreNull () throws IOException {
130
+ BeanWithNullValues bean = new BeanWithNullValues ("Test" , null );
131
+ StringWriter writer = new StringWriter ();
132
+
133
+ beansWriter .writeJSONString (bean , writer , JSONStyle .MAX_COMPRESS );
134
+
135
+ String result = writer .toString ();
136
+ assertTrue (result .contains ("name:Test" ));
137
+ assertTrue (!result .contains ("nullField" ));
138
+ }
139
+
140
+ @ Test
141
+ public void testWriteBeanWithNullValuesIncludeNull () throws IOException {
142
+ BeanWithNullValues bean = new BeanWithNullValues ("Test" , null );
143
+ StringWriter writer = new StringWriter ();
144
+
145
+ beansWriter .writeJSONString (bean , writer , JSONStyle .NO_COMPRESS );
146
+
147
+ String result = writer .toString ();
148
+ assertTrue (result .contains ("\" name\" :\" Test\" " ));
149
+ assertTrue (result .contains ("\" nullField\" :null" ));
150
+ }
151
+
152
+ @ Test
153
+ public void testWriteInheritedBean () throws IOException {
154
+ InheritedBean bean = new InheritedBean ("Alice" , 25 , "Boston" , 75000.0 );
155
+ StringWriter writer = new StringWriter ();
156
+
157
+ beansWriter .writeJSONString (bean , writer , JSONStyle .NO_COMPRESS );
158
+
159
+ String result = writer .toString ();
160
+ assertTrue (result .contains ("\" name\" :\" Alice\" " ));
161
+ assertTrue (result .contains ("\" age\" :25" ));
162
+ assertTrue (result .contains ("\" city\" :\" Boston\" " ));
163
+ assertTrue (result .contains ("\" salary\" :75000.0" ));
164
+ }
165
+
166
+ @ Test
167
+ public void testWriteEmptyBean () throws IOException {
168
+ SimpleBean bean = new SimpleBean ();
169
+ StringWriter writer = new StringWriter ();
170
+
171
+ beansWriter .writeJSONString (bean , writer , JSONStyle .NO_COMPRESS );
172
+
173
+ String result = writer .toString ();
174
+ assertTrue (result .contains ("\" name\" :null" ));
175
+ assertTrue (result .contains ("\" age\" :0" ));
176
+ assertTrue (result .contains ("\" city\" :null" ));
177
+ }
178
+
179
+ @ Test
180
+ public void testWriteCompressed () throws IOException {
181
+ SimpleBean bean = new SimpleBean ("John" , 30 , "New York" );
182
+ StringWriter writer = new StringWriter ();
183
+
184
+ beansWriter .writeJSONString (bean , writer , JSONStyle .MAX_COMPRESS );
185
+
186
+ String result = writer .toString ();
187
+ assertTrue (result .contains ("name:John" ));
188
+ assertTrue (result .contains ("age:30" ));
189
+ assertTrue (result .contains ("city:New York" ));
190
+ assertTrue (!result .contains ("\" name\" " ));
191
+ }
192
+
193
+ @ Test
194
+ public void testWriteNullObject () {
195
+ assertThrows (RuntimeException .class , () -> {
196
+ beansWriter .writeJSONString (null , new StringWriter (), JSONStyle .NO_COMPRESS );
197
+ });
198
+ }
199
+ }
0 commit comments