Debian Codegraph @

src/test/java/ezvcard/io/json/JCardSerializerTest.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.List;
9
10 import org.junit.Before;
11 import org.junit.Test;
12
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import com.fasterxml.jackson.databind.SerializationFeature;
15 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
16
17 import ezvcard.Ezvcard;
18 import ezvcard.VCard;
19
20 /*
21 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
44 The views and conclusions contained in the software and documentation are those
45 of the authors and should not be interpreted as representing official policies,
46 either expressed or implied, of the FreeBSD Project.
47 */
48
49 /**
50 * @author Buddy Gorven
51 * @author Michael Angstadt
52 */
53 public class JCardSerializerTest {
54 //TODO test setScribe()
55
56 private ObjectMapper mapper;
57
58 @Before
59 public void before() {
60 mapper = new ObjectMapper();
61 }
62
63 @Test
64 public void serialize_single() throws Exception {
65 VCard vcard = new VCard();
66 vcard.setFormattedName("John Doe");
67
68 JCardModule module = new JCardModule();
69 module.setAddProdId(false);
70 mapper.registerModule(module);
71
72 String actual = mapper.writeValueAsString(vcard);
73
74 //@formatter:off
75 String expected =
76 "[\"vcard\"," +
77 "[" +
78 "[\"version\",{},\"text\",\"4.0\"]," +
79 "[\"fn\",{},\"text\",\"John Doe\"]" +
80 "]" +
81 "]";
82 //@formatter:on
83 assertEquals(expected, actual);
84 }
85
86 @Test
87 public void serialize_single_prettyPrint() throws Exception {
88 VCard vcard = new VCard();
89 vcard.setFormattedName("John Doe");
90
91 JCardModule module = new JCardModule();
92 module.setAddProdId(false);
93 mapper.registerModule(module);
94 mapper.setDefaultPrettyPrinter(new JCardPrettyPrinter());
95 mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
96
97 String actual = mapper.writeValueAsString(vcard);
98
99 //@formatter:off
100 String expected =
101 "[" + NEWLINE +
102 " \"vcard\"," + NEWLINE +
103 " [" + NEWLINE +
104 " [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE +
105 " [ \"fn\", { }, \"text\", \"John Doe\" ]" + NEWLINE +
106 " ]" + NEWLINE +
107 "]";
108 //@formatter:on
109 assertEquals(expected, actual);
110 }
111
112 @Test
113 public void serialize_multiple() throws Exception {
114 List<VCard> vcards = new ArrayList<>();
115 VCard vcard = new VCard();
116 vcard.setFormattedName("John Doe");
117 vcards.add(vcard);
118
119 vcard = new VCard();
120 vcard.setFormattedName("Jane Doe");
121 vcards.add(vcard);
122
123 JCardModule module = new JCardModule();
124 module.setAddProdId(false);
125 mapper.registerModule(module);
126
127 String actual = mapper.writeValueAsString(vcards);
128
129 //@formatter:off
130 String expected =
131 "[" +
132 "[\"vcard\"," +
133 "[" +
134 "[\"version\",{},\"text\",\"4.0\"]," +
135 "[\"fn\",{},\"text\",\"John Doe\"]" +
136 "]" +
137 "]," +
138 "[\"vcard\"," +
139 "[" +
140 "[\"version\",{},\"text\",\"4.0\"]," +
141 "[\"fn\",{},\"text\",\"Jane Doe\"]" +
142 "]" +
143 "]" +
144 "]";
145 //@formatter:on
146 assertEquals(expected, actual);
147 }
148
149 @Test
150 public void serialize_multiple_prettyPrint() throws Exception {
151 List<VCard> vcards = new ArrayList<>();
152 VCard vcard = new VCard();
153 vcard.setFormattedName("John Doe");
154 vcards.add(vcard);
155
156 vcard = new VCard();
157 vcard.setFormattedName("Jane Doe");
158 vcards.add(vcard);
159
160 JCardModule module = new JCardModule();
161 module.setAddProdId(false);
162 mapper.registerModule(module);
163 mapper.setDefaultPrettyPrinter(new JCardPrettyPrinter());
164 mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
165
166 String actual = mapper.writeValueAsString(vcards);
167
168 //@formatter:off
169 String expected =
170 "[" + NEWLINE +
171 " [" + NEWLINE +
172 " \"vcard\"," + NEWLINE +
173 " [" + NEWLINE +
174 " [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE +
175 " [ \"fn\", { }, \"text\", \"John Doe\" ]" + NEWLINE +
176 " ]" + NEWLINE +
177 " ]," + NEWLINE +
178 " [" + NEWLINE +
179 " \"vcard\"," + NEWLINE +
180 " [" + NEWLINE +
181 " [ \"version\", { }, \"text\", \"4.0\" ]," + NEWLINE +
182 " [ \"fn\", { }, \"text\", \"Jane Doe\" ]" + NEWLINE +
183 " ]" + NEWLINE +
184 " ]" + NEWLINE +
185 "]";
186 //@formatter:on
187 assertEquals(expected, actual);
188 }
189
190 @Test
191 public void container() throws Exception {
192 VCard vcard = new VCard();
193 vcard.setFormattedName("John Doe");
194 vcard.setMailer("mailer");
195 Container container = new Container(vcard);
196
197 StringWriter result = new StringWriter();
198 mapper.writeValue(result, container);
199 String actual = result.toString();
200
201 //@formatter:off
202 String expected =
203 "{" +
204 "\"contact\":[\"vcard\"," +
205 "[" +
206 "[\"version\",{},\"text\",\"4.0\"]," +
207 "[\"prodid\",{},\"text\",\"ez-vcard " + Ezvcard.VERSION + "\"]," +
208 "[\"fn\",{},\"text\",\"John Doe\"]" +
209 "]" +
210 "]" +
211 "}";
212 //@formatter:on
213 assertEquals(expected, actual);
214 }
215
216 @Test
217 public void container_null() throws Exception {
218 Container container = new Container(null);
219
220 StringWriter result = new StringWriter();
221 mapper.writeValue(result, container);
222 String actual = result.toString();
223
224 //@formatter:off
225 String expected =
226 "{" +
227 "\"contact\":null"+
228 "}";
229 //@formatter:on
230 assertEquals(expected, actual);
231 }
232
233 @Test
234 public void container_annotation() throws Exception {
235 VCard vcard = new VCard();
236 vcard.setFormattedName("John Doe");
237 vcard.setMailer("mailer");
238 ContainerAnnotation container = new ContainerAnnotation(vcard);
239
240 StringWriter result = new StringWriter();
241 mapper.writeValue(result, container);
242 String actual = result.toString();
243
244 //@formatter:off
245 String expected =
246 "{" +
247 "\"contact\":[\"vcard\"," +
248 "[" +
249 "[\"version\",{},\"text\",\"4.0\"]," +
250 "[\"fn\",{},\"text\",\"John Doe\"]," +
251 "[\"mailer\",{},\"text\",\"mailer\"]" +
252 "]" +
253 "]" +
254 "}";
255 //@formatter:on
256 assertEquals(expected, actual);
257 }
258
259 private static class Container {
260 private final VCard contact;
261
262 public Container(VCard contact) {
263 this.contact = contact;
264 }
265
266 @JsonSerialize(using = JCardSerializer.class)
267 public VCard getContact() {
268 return contact;
269 }
270 }
271
272 private static class ContainerAnnotation {
273 private final VCard contact;
274
275 public ContainerAnnotation(VCard contact) {
276 this.contact = contact;
277 }
278
279 @JCardFormat(addProdId = false, versionStrict = false)
280 @JsonSerialize(using = JCardSerializer.class)
281 public VCard getContact() {
282 return contact;
283 }
284 }
285 }