Debian Codegraph @

src/test/java/ezvcard/io/json/JCardRawWriterTest.java

raw symbols SCIP
1 package ezvcard.io.json;
2
3 import static ezvcard.util.StringUtils.NEWLINE;
4 import static org.junit.Assert.assertEquals;
5
6 import java.io.StringWriter;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.LinkedHashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import org.junit.Test;
14
15 import ezvcard.VCardDataType;
16 import ezvcard.parameter.VCardParameters;
17
18 /*
19 Copyright (c) 2012-2023, Michael Angstadt
20 All rights reserved.
21
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24
25 1. Redistributions of source code must retain the above copyright notice, this
26 list of conditions and the following disclaimer.
27 2. Redistributions in binary form must reproduce the above copyright notice,
28 this list of conditions and the following disclaimer in the documentation
29 and/or other materials provided with the distribution.
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
35 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 /**
44 * @author Michael Angstadt
45 */
46 @SuppressWarnings("resource")
47 public class JCardRawWriterTest {
48 @Test
49 public void write_multiple() throws Throwable {
50 StringWriter sw = new StringWriter();
51 try (JCardRawWriter writer = new JCardRawWriter(sw, true)) {
52 writer.writeStartVCard();
53 writer.writeProperty("prop", VCardDataType.TEXT, JCardValue.single("value"));
54 writer.writeEndVCard();
55 writer.writeStartVCard();
56 writer.writeProperty("prop", VCardDataType.TEXT, JCardValue.single("value"));
57 writer.writeEndVCard();
58 }
59
60 String actual = sw.toString();
61 //@formatter:off
62 String expected =
63 "[" +
64 "[\"vcard\"," +
65 "[" +
66 "[\"prop\",{},\"text\",\"value\"]" +
67 "]" +
68 "]," +
69 "[\"vcard\"," +
70 "[" +
71 "[\"prop\",{},\"text\",\"value\"]" +
72 "]" +
73 "]" +
74 "]";
75 //@formatter:on
76 assertEquals(expected, actual);
77 }
78
79 @Test
80 public void writeProperty() throws Throwable {
81 StringWriter sw = new StringWriter();
82 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
83 writer.writeStartVCard();
84 writer.writeProperty("prop", VCardDataType.TEXT, JCardValue.single("value\nvalue"));
85 }
86
87 String actual = sw.toString();
88 //@formatter:off
89 String expected =
90 "[\"vcard\"," +
91 "[" +
92 "[\"prop\",{},\"text\",\"value\\nvalue\"]" +
93 "]" +
94 "]";
95 //@formatter:on
96 assertEquals(expected, actual);
97 }
98
99 @Test
100 public void writeProperty_null_value() throws Throwable {
101 StringWriter sw = new StringWriter();
102 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
103 writer.writeStartVCard();
104 writer.writeProperty("prop", VCardDataType.TEXT, JCardValue.single(null));
105 }
106
107 String actual = sw.toString();
108 //@formatter:off
109 String expected =
110 "[\"vcard\"," +
111 "[" +
112 "[\"prop\",{},\"text\",null]" +
113 "]" +
114 "]";
115 //@formatter:on
116 assertEquals(expected, actual);
117 }
118
119 @Test
120 public void parameters() throws Throwable {
121 StringWriter sw = new StringWriter();
122 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
123 writer.writeStartVCard();
124 VCardParameters parameters = new VCardParameters();
125 parameters.put("a", "value1");
126 parameters.put("b", "value2");
127 parameters.put("b", "value3");
128 writer.writeProperty(null, "prop", parameters, VCardDataType.TEXT, JCardValue.single("value"));
129 }
130
131 String actual = sw.toString();
132 //@formatter:off
133 String expected =
134 "[\"vcard\"," +
135 "[" +
136 "[\"prop\",{\"a\":\"value1\",\"b\":[\"value2\",\"value3\"]},\"text\",\"value\"]" +
137 "]" +
138 "]";
139 //@formatter:on
140 assertEquals(expected, actual);
141 }
142
143 @Test
144 public void group() throws Throwable {
145 StringWriter sw = new StringWriter();
146 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
147 writer.writeStartVCard();
148 writer.writeProperty("one", "prop", new VCardParameters(), VCardDataType.TEXT, JCardValue.single("value"));
149 }
150
151 String actual = sw.toString();
152 //@formatter:off
153 String expected =
154 "[\"vcard\"," +
155 "[" +
156 "[\"prop\",{\"group\":\"one\"},\"text\",\"value\"]" +
157 "]" +
158 "]";
159 //@formatter:on
160 assertEquals(expected, actual);
161 }
162
163 @Test
164 public void complex_value() throws Throwable {
165 StringWriter sw = new StringWriter();
166 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
167 writer.writeStartVCard();
168 List<JsonValue> jsonValues = new ArrayList<>();
169 Map<String, JsonValue> m = new LinkedHashMap<>();
170 m.put("a", new JsonValue(Arrays.asList(new JsonValue("one"), new JsonValue("two"))));
171 Map<String, JsonValue> m2 = new LinkedHashMap<>();
172 m2.put("c", new JsonValue(Arrays.asList(new JsonValue("three"))));
173 m2.put("d", new JsonValue(new LinkedHashMap<>()));
174 m.put("b", new JsonValue(m2));
175 jsonValues.add(new JsonValue(m));
176 jsonValues.add(new JsonValue("four"));
177 writer.writeProperty("prop", VCardDataType.TEXT, new JCardValue(jsonValues));
178 }
179
180 String actual = sw.toString();
181 //@formatter:off
182 String expected =
183 "[\"vcard\"," +
184 "[" +
185 "[\"prop\",{},\"text\",{" +
186 "\"a\":[\"one\",\"two\"]," +
187 "\"b\":{" +
188 "\"c\":[\"three\"]," +
189 "\"d\":{}" +
190 "}" +
191 "},\"four\"]" +
192 "]" +
193 "]";
194 //@formatter:on
195 assertEquals(expected, actual);
196 }
197
198 @Test
199 public void data_type_unknown() throws Throwable {
200 StringWriter sw = new StringWriter();
201 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
202 writer.writeStartVCard();
203 writer.writeProperty("prop", null, JCardValue.single("value"));
204 }
205
206 String actual = sw.toString();
207 //@formatter:off
208 String expected =
209 "[\"vcard\"," +
210 "[" +
211 "[\"prop\",{},\"unknown\",\"value\"]" +
212 "]" +
213 "]";
214 //@formatter:on
215 assertEquals(expected, actual);
216 }
217
218 @Test
219 public void different_value_types() throws Throwable {
220 StringWriter sw = new StringWriter();
221 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
222 writer.writeStartVCard();
223 writer.writeProperty("prop", VCardDataType.TEXT, JCardValue.multi(false, true, 1.1, 1, null, "text"));
224 }
225
226 String actual = sw.toString();
227 //@formatter:off
228 String expected =
229 "[\"vcard\"," +
230 "[" +
231 "[\"prop\",{},\"text\",false,true,1.1,1,null,\"text\"]" +
232 "]" +
233 "]";
234 //@formatter:on
235 assertEquals(expected, actual);
236 }
237
238 @Test
239 public void empty() throws Throwable {
240 StringWriter sw = new StringWriter();
241 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
242 }
243
244 String actual = sw.toString();
245 String expected = "";
246 assertEquals(expected, actual);
247 }
248
249 @Test(expected = IllegalStateException.class)
250 public void write_property_before_starting_vcard() throws Throwable {
251 StringWriter sw = new StringWriter();
252 JCardRawWriter writer = new JCardRawWriter(sw, false);
253
254 writer.writeProperty("prop", VCardDataType.TEXT, JCardValue.single("value"));
255 }
256
257 @Test(expected = IllegalStateException.class)
258 public void write_end_vcard_before_starting_vcard() throws Throwable {
259 StringWriter sw = new StringWriter();
260 JCardRawWriter writer = new JCardRawWriter(sw, false);
261
262 writer.writeEndVCard();
263 }
264
265 @Test(expected = IllegalStateException.class)
266 public void write_property_after_ending_vcard() throws Throwable {
267 StringWriter sw = new StringWriter();
268 JCardRawWriter writer = new JCardRawWriter(sw, false);
269
270 writer.writeStartVCard();
271 writer.writeEndVCard();
272 writer.writeProperty("prop", VCardDataType.TEXT, JCardValue.single("value"));
273 }
274
275 @Test
276 public void write_empty_vcard() throws Exception {
277 StringWriter sw = new StringWriter();
278 try (JCardRawWriter writer = new JCardRawWriter(sw, false)) {
279 writer.writeStartVCard();
280 }
281
282 String actual = sw.toString();
283 //@formatter:off
284 String expected =
285 "[\"vcard\"," +
286 "[" +
287 "]" +
288 "]";
289 //@formatter:on
290 assertEquals(expected, actual);
291 }
292
293 @Test
294 public void writeStartVCard_multiple_calls() throws Exception {
295 StringWriter sw = new StringWriter();
296 try (JCardRawWriter writer = new JCardRawWriter(sw, true)) {
297 writer.writeStartVCard();
298 writer.writeStartVCard();
299 }
300
301 String actual = sw.toString();
302 //@formatter:off
303 String expected =
304 "[" +
305 "[\"vcard\"," +
306 "[" +
307 "]" +
308 "]," +
309 "[\"vcard\"," +
310 "[" +
311 "]" +
312 "]" +
313 "]";
314 //@formatter:on
315 assertEquals(expected, actual);
316 }
317
318 @Test
319 public void indent() throws Throwable {
320 StringWriter sw = new StringWriter();
321 try (JCardRawWriter writer = new JCardRawWriter(sw, true)) {
322 writer.setPrettyPrint(true);
323
324 //@formatter:off
325 writer.writeStartVCard();
326 writer.writeProperty("prop1", VCardDataType.TEXT, JCardValue.single("value1"));
327 writer.writeProperty("prop2", VCardDataType.TEXT, JCardValue.single("value2"));
328 writer.writeEndVCard();
329 writer.writeStartVCard();
330 writer.writeProperty("prop3", VCardDataType.TEXT, JCardValue.single("value3"));
331 writer.writeEndVCard();
332 //@formatter:on
333 }
334
335 String actual = sw.toString();
336 //@formatter:off
337 String expected =
338 "[" + NEWLINE +
339 " [" + NEWLINE +
340 " \"vcard\"," + NEWLINE +
341 " [" + NEWLINE +
342 " [ \"prop1\", { }, \"text\", \"value1\" ]," + NEWLINE +
343 " [ \"prop2\", { }, \"text\", \"value2\" ]" + NEWLINE +
344 " ]" + NEWLINE +
345 " ]," + NEWLINE +
346 " [" + NEWLINE +
347 " \"vcard\"," + NEWLINE +
348 " [" + NEWLINE +
349 " [ \"prop3\", { }, \"text\", \"value3\" ]" + NEWLINE +
350 " ]" + NEWLINE +
351 " ]" + NEWLINE +
352 "]";
353 //@formatter:on
354 assertEquals(expected, actual);
355 }
356 }