Debian Codegraph @

src/test/java/ezvcard/io/StreamWriterTest.java

raw symbols SCIP
1 package ezvcard.io;
2
3 import static ezvcard.VCardVersion.V2_1;
4 import static ezvcard.VCardVersion.V3_0;
5 import static ezvcard.VCardVersion.V4_0;
6 import static ezvcard.util.TestUtils.each;
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNotEquals;
10 import static org.junit.Assert.assertNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Iterator;
18 import java.util.List;
19
20 import org.junit.Before;
21 import org.junit.Test;
22
23 import ezvcard.VCard;
24 import ezvcard.VCardVersion;
25 import ezvcard.parameter.AddressType;
26 import ezvcard.property.Address;
27 import ezvcard.property.FormattedName;
28 import ezvcard.property.Gender;
29 import ezvcard.property.Label;
30 import ezvcard.property.Mailer;
31 import ezvcard.property.ProductId;
32 import ezvcard.property.RawProperty;
33 import ezvcard.property.VCardProperty;
34 import ezvcard.util.ListMultimap;
35
36 /*
37 Copyright (c) 2012-2023, Michael Angstadt
38 All rights reserved.
39
40 Redistribution and use in source and binary forms, with or without
41 modification, are permitted provided that the following conditions are met:
42
43 1. Redistributions of source code must retain the above copyright notice, this
44 list of conditions and the following disclaimer.
45 2. Redistributions in binary form must reproduce the above copyright notice,
46 this list of conditions and the following disclaimer in the documentation
47 and/or other materials provided with the distribution.
48
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
50 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
53 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59
60 The views and conclusions contained in the software and documentation are those
61 of the authors and should not be interpreted as representing official policies,
62 either expressed or implied, of the FreeBSD Project.
63 */
64
65 /**
66 * @author Michael Angstadt
67 */
68 public class StreamWriterTest {
69 private StreamWriterStub writer;
70 private VCard vcard;
71
72 @Before
73 public void before() {
74 writer = new StreamWriterStub();
75 vcard = new VCard();
76 }
77
78 @Test
79 public void unregistered_property() throws Exception {
80 vcard.addProperty(new TestProperty());
81
82 try {
83 writer.write(vcard);
84 fail("Expected IllegalArgumentException");
85 } catch (IllegalArgumentException e) {
86 //expected
87 }
88
89 assertNull(writer.properties); //vCard was not written
90 }
91
92 @Test
93 public void productId() throws IOException {
94 assertNull(vcard.getProductId());
95
96 //default value
97 {
98 vcard.setProductId((String) null);
99 {
100 writer.write(vcard, V2_1);
101 assertEquals(1, writer.count());
102 assertEquals(1, writer.count(RawProperty.class));
103
104 for (VCardVersion version : each(V3_0, V4_0)) {
105 writer.write(vcard, version);
106 assertEquals(1, writer.count());
107 assertEquals(1, writer.count(ProductId.class));
108 }
109 }
110
111 vcard.setProductId("value"); //should be ignored
112 {
113 writer.write(vcard, V2_1);
114 assertEquals(1, writer.count());
115 assertEquals(1, writer.count(RawProperty.class));
116
117 for (VCardVersion version : each(V3_0, V4_0)) {
118 writer.write(vcard, version);
119 assertEquals(1, writer.count());
120 ProductId prodid = writer.first(ProductId.class);
121 assertNotEquals("value", prodid.getValue());
122 }
123 }
124 }
125
126 writer.setAddProdId(false);
127 {
128 vcard.setProductId((String) null);
129 {
130 for (VCardVersion version : VCardVersion.values()) {
131 writer.write(vcard, version);
132 assertEquals(0, writer.count());
133 }
134 }
135
136 vcard.setProductId("value");
137 {
138 writer.write(vcard, V2_1);
139 assertEquals(0, writer.count());
140
141 for (VCardVersion version : each(V3_0, V4_0)) {
142 writer.write(vcard, version);
143 ProductId prodId = writer.first(ProductId.class);
144 assertEquals("value", prodId.getValue());
145 }
146 }
147 }
148
149 writer.setAddProdId(true);
150 {
151 vcard.setProductId((String) null);
152 {
153 writer.write(vcard, V2_1);
154 assertEquals(1, writer.count());
155 assertEquals(1, writer.count(RawProperty.class));
156
157 for (VCardVersion version : each(V3_0, V4_0)) {
158 writer.write(vcard, version);
159 assertEquals(1, writer.count());
160 assertEquals(1, writer.count(ProductId.class));
161 }
162 }
163
164 vcard.setProductId("value"); //should be ignored
165 {
166 writer.write(vcard, V2_1);
167 assertEquals(1, writer.count());
168 assertEquals(1, writer.count(RawProperty.class));
169
170 for (VCardVersion version : each(V3_0, V4_0)) {
171 writer.write(vcard, version);
172 assertEquals(1, writer.count());
173 ProductId prodid = writer.first(ProductId.class);
174 assertNotEquals("value", prodid.getValue());
175 }
176 }
177 }
178 }
179
180 /**
181 * Asserts that the PRODID property is always put at the front of the vCard.
182 */
183 @Test
184 public void productId_order() throws IOException {
185 vcard.setFormattedName("Name");
186
187 {
188 writer.write(vcard, V2_1);
189
190 Iterator<VCardProperty> it = writer.propertiesList.iterator();
191
192 VCardProperty property = it.next();
193 assertTrue(property instanceof RawProperty);
194 property = it.next();
195 assertTrue(property instanceof FormattedName);
196
197 assertFalse(it.hasNext());
198 }
199
200 for (VCardVersion version : each(V3_0, V4_0)) {
201 writer.write(vcard, version);
202
203 Iterator<VCardProperty> it = writer.propertiesList.iterator();
204
205 VCardProperty property = it.next();
206 assertTrue(property instanceof ProductId);
207 property = it.next();
208 assertTrue(property instanceof FormattedName);
209
210 assertFalse(it.hasNext());
211 }
212
213 vcard.setProductId("value");
214 writer.setAddProdId(false);
215 for (VCardVersion version : each(V3_0, V4_0)) {
216 writer.write(vcard, version);
217
218 Iterator<VCardProperty> it = writer.propertiesList.iterator();
219
220 VCardProperty property = it.next();
221 assertTrue(property instanceof ProductId);
222 property = it.next();
223 assertTrue(property instanceof FormattedName);
224
225 assertFalse(it.hasNext());
226 }
227 }
228
229 @Test
230 public void versionStrict() throws Exception {
231 writer.setAddProdId(false);
232 vcard.setGender(Gender.male());
233 vcard.setMailer("value");
234
235 //default value
236 {
237 for (VCardVersion version : each(V2_1, V3_0)) {
238 writer.write(vcard, version);
239 assertEquals(1, writer.count());
240 assertEquals(1, writer.count(Mailer.class));
241 }
242
243 writer.write(vcard, V4_0);
244 assertEquals(1, writer.count());
245 assertEquals(1, writer.count(Gender.class));
246 }
247
248 writer.setVersionStrict(false);
249 {
250 for (VCardVersion version : VCardVersion.values()) {
251 writer.write(vcard, version);
252 assertEquals(2, writer.count());
253 assertEquals(1, writer.count(Mailer.class));
254 assertEquals(1, writer.count(Gender.class));
255 }
256 }
257
258 writer.setVersionStrict(true);
259 {
260 for (VCardVersion version : each(V2_1, V3_0)) {
261 writer.write(vcard, version);
262 assertEquals(1, writer.count());
263 assertEquals(1, writer.count(Mailer.class));
264 }
265
266 writer.write(vcard, V4_0);
267 assertEquals(1, writer.count());
268 assertEquals(1, writer.count(Gender.class));
269 }
270 }
271
272 @Test
273 public void labels() throws Throwable {
274 writer.setAddProdId(false);
275
276 //address with label and type
277 Address adr = new Address();
278 adr.setLabel("value1");
279 adr.getTypes().add(AddressType.HOME);
280 vcard.addAddress(adr);
281
282 //address with label
283 adr = new Address();
284 adr.setLabel("value2");
285 vcard.addAddress(adr);
286
287 //address
288 adr = new Address();
289 vcard.addAddress(adr);
290
291 for (VCardVersion version : each(V2_1, V3_0)) {
292 writer.write(vcard, version);
293 assertEquals(5, writer.count());
294 assertEquals(3, writer.count(Address.class));
295 assertEquals(2, writer.count(Label.class));
296
297 Label label = writer.get(Label.class).get(0);
298 assertEquals("value1", label.getValue());
299 assertEquals(Arrays.asList(AddressType.HOME), label.getTypes());
300
301 label = writer.get(Label.class).get(1);
302 assertEquals("value2", label.getValue());
303 assertEquals(Arrays.asList(), label.getTypes());
304 }
305
306 writer.write(vcard, V4_0);
307 assertEquals(3, writer.count());
308 assertEquals(3, writer.count(Address.class));
309 }
310
311 private class StreamWriterStub extends StreamWriter {
312 private VCardVersion targetVersion;
313 private List<VCardProperty> propertiesList;
314 private ListMultimap<Class<? extends VCardProperty>, VCardProperty> properties = null;
315
316 public StreamWriterStub() {
317 this(V4_0);
318 }
319
320 public StreamWriterStub(VCardVersion targetVersion) {
321 this.targetVersion = targetVersion;
322 }
323
324 @Override
325 protected VCardVersion getTargetVersion() {
326 return targetVersion;
327 }
328
329 public void write(VCard vcard, VCardVersion targetVersion) throws IOException {
330 this.targetVersion = targetVersion;
331 super.write(vcard);
332 }
333
334 @Override
335 protected void _write(VCard vcard, List<VCardProperty> properties) throws IOException {
336 propertiesList = properties;
337 this.properties = new ListMultimap<>();
338 for (VCardProperty property : properties) {
339 this.properties.put(property.getClass(), property);
340 }
341 }
342
343 public int count() {
344 return properties.size();
345 }
346
347 public int count(Class<? extends VCardProperty> clazz) {
348 return properties.get(clazz).size();
349 }
350
351 public <T extends VCardProperty> List<T> get(Class<T> clazz) {
352 List<VCardProperty> props = properties.get(clazz);
353 List<T> casted = new ArrayList<>(props.size());
354 for (VCardProperty property : props) {
355 casted.add(clazz.cast(property));
356 }
357 return casted;
358 }
359
360 public <T extends VCardProperty> T first(Class<T> clazz) {
361 List<VCardProperty> props = properties.get(clazz);
362 return props.isEmpty() ? null : clazz.cast(props.get(0));
363 }
364
365 public void close() throws IOException {
366 //empty
367 }
368 }
369
370 private class TestProperty extends VCardProperty {
371 //empty
372 }
373 }