Debian Codegraph @

src/test/java/ezvcard/io/scribe/Sensei.java

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import static ezvcard.util.TestUtils.assertParseWarnings;
4 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertTrue;
7 import static org.junit.Assert.fail;
8
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.xml.namespace.QName;
13
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16 import org.xml.sax.SAXException;
17
18 import ezvcard.VCard;
19 import ezvcard.VCardDataType;
20 import ezvcard.VCardVersion;
21 import ezvcard.io.CannotParseException;
22 import ezvcard.io.ParseContext;
23 import ezvcard.io.SkipMeException;
24 import ezvcard.io.html.HCardElement;
25 import ezvcard.io.json.JCardValue;
26 import ezvcard.io.text.WriteContext;
27 import ezvcard.parameter.VCardParameters;
28 import ezvcard.property.VCardProperty;
29 import ezvcard.util.HtmlUtils;
30 import ezvcard.util.XmlUtils;
31
32 /*
33 Copyright (c) 2012-2023, Michael Angstadt
34 All rights reserved.
35
36 Redistribution and use in source and binary forms, with or without
37 modification, are permitted provided that the following conditions are met:
38
39 1. Redistributions of source code must retain the above copyright notice, this
40 list of conditions and the following disclaimer.
41 2. Redistributions in binary form must reproduce the above copyright notice,
42 this list of conditions and the following disclaimer in the documentation
43 and/or other materials provided with the distribution.
44
45 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
46 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
49 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
50 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
54 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 /**
58 * Utility class used for unit testing property scribes.
59 * @param <T> the property class
60 * @author Michael Angstadt
61 */
62 public class Sensei<T extends VCardProperty> {
63 private final VCardPropertyScribe<T> scribe;
64
65 /**
66 * Shorthand for creating a new instance of this class.
67 * @param scribe the property scribe
68 * @return the new instance
69 */
70 public static <T extends VCardProperty> Sensei<T> create(VCardPropertyScribe<T> scribe) {
71 return new Sensei<>(scribe);
72 }
73
74 /**
75 * Creates a new sensei.
76 * @param scribe the property scribe
77 */
78 public Sensei(VCardPropertyScribe<T> scribe) {
79 this.scribe = scribe;
80 }
81
82 /**
83 * Asserts the {@link VCardPropertyScribe#parseText} method.
84 * @param value the value to parse
85 * @return the tester object
86 */
87 public ParseTextTest assertParseText(String value) {
88 return new ParseTextTest(value);
89 }
90
91 /**
92 * Asserts the {@link VCardPropertyScribe#parseXml} method.
93 * @param innerXml the inner XML of the xCal element to parse
94 * @return the tester object
95 */
96 public ParseXmlTest assertParseXml(String innerXml) {
97 return new ParseXmlTest(innerXml);
98 }
99
100 /**
101 * Asserts the {@link VCardPropertyScribe#parseHtml} method.
102 * @param html the HTML element to parse
103 * @return the tester object
104 */
105 public ParseHtmlTest assertParseHtml(String html) {
106 return new ParseHtmlTest(html);
107 }
108
109 /**
110 * Asserts the {@link VCardPropertyScribe#parseJson} method.
111 * @param value the jCal value to parse
112 * @return the tester object
113 */
114 public ParseJsonTest assertParseJson(String value) {
115 return assertParseJson(JCardValue.single(value));
116 }
117
118 /**
119 * Asserts the {@link VCardPropertyScribe#parseJson} method.
120 * @param value the jCal value to parse
121 * @return the tester object
122 */
123 public ParseJsonTest assertParseJson(JCardValue value) {
124 return new ParseJsonTest(value);
125 }
126
127 /**
128 * Asserts the {@link VCardPropertyScribe#dataType} method.
129 * @param property the property to marshal
130 * @return the tester object
131 */
132 public DataTypeTest assertDataType(T property) {
133 return new DataTypeTest(property);
134 }
135
136 /**
137 * Asserts the {@link VCardPropertyScribe#prepareParameters} method.
138 * @param property the property to marshal
139 * @return the tester object
140 */
141 public PrepareParamsTest assertPrepareParams(T property) {
142 return new PrepareParamsTest(property);
143 }
144
145 /**
146 * Asserts the {@link VCardPropertyScribe#writeText} method.
147 * @param property the property to marshal
148 * @return the tester object
149 */
150 public WriteTextTest assertWriteText(T property) {
151 return new WriteTextTest(property);
152 }
153
154 /**
155 * Asserts the {@link VCardPropertyScribe#writeXml} method.
156 * @param property the property to marshal
157 * @return the tester object
158 */
159 public WriteXmlTest assertWriteXml(T property) {
160 return new WriteXmlTest(property);
161 }
162
163 /**
164 * Asserts the {@link VCardPropertyScribe#writeJson} method.
165 * @param property the property to marshal
166 * @return the tester object
167 */
168 public WriteJsonTest assertWriteJson(T property) {
169 return new WriteJsonTest(property);
170 }
171
172 /**
173 * Tester class used for testing the {@link VCardPropertyScribe#dataType}
174 * method.
175 */
176 public class DataTypeTest {
177 protected final T property;
178 private VCardVersion version[] = VCardVersion.values();
179
180 public DataTypeTest(T property) {
181 this.property = property;
182 }
183
184 /**
185 * Sets the versions to run this test under (defaults to all versions).
186 * @param version the versions
187 * @return this
188 */
189 public DataTypeTest versions(VCardVersion... version) {
190 this.version = version;
191 return this;
192 }
193
194 /**
195 * Runs the test.
196 * @param expected the expected data type
197 */
198 public void run(VCardDataType expected) {
199 for (VCardVersion version : this.version) {
200 VCardDataType actual = scribe.dataType(property, version);
201 assertEquals("Version " + version, expected, actual);
202 }
203 }
204 }
205
206 /**
207 * Tester class used for testing the
208 * {@link VCardPropertyScribe#prepareParameters} method.
209 */
210 public class PrepareParamsTest {
211 protected final T property;
212 private VCardVersion versions[] = VCardVersion.values();
213 private VCard vcard = new VCard();
214 private VCardParameters expected = new VCardParameters();
215
216 public PrepareParamsTest(T property) {
217 this.property = property;
218 }
219
220 /**
221 * Sets the versions to run this test under (defaults to all versions).
222 * @param versions the versions
223 * @return this
224 */
225 public PrepareParamsTest versions(VCardVersion... versions) {
226 this.versions = versions;
227 return this;
228 }
229
230 /**
231 * Sets the {@link VCard} object to use (defaults to an empty vCard).
232 * @param vcard the vCard
233 * @return this
234 */
235 public PrepareParamsTest vcard(VCard vcard) {
236 this.vcard = vcard;
237 return this;
238 }
239
240 /**
241 * Adds an expected parameter.
242 * @param name the parameter name
243 * @param values the parameter value
244 * @return this
245 */
246 public PrepareParamsTest expected(String name, String... values) {
247 for (String value : values) {
248 expected.put(name, value);
249 }
250 return this;
251 }
252
253 /**
254 * Runs the test.
255 */
256 public void run() {
257 for (VCardVersion version : versions) {
258 VCardParameters actual = scribe.prepareParameters(property, version, vcard);
259 assertEquals("Actual: " + actual, expected.size(), actual.size());
260
261 for (Map.Entry<String, List<String>> entry : expected) {
262 String expectedKey = entry.getKey();
263 List<String> expectedValues = entry.getValue();
264
265 List<String> actualValues = actual.get(expectedKey);
266 assertEquals("Actual: " + actual, expectedValues.size(), actualValues.size());
267 for (String expectedValue : expectedValues) {
268 assertTrue("Actual: " + actual, actualValues.contains(expectedValue));
269 }
270 }
271 }
272 }
273 }
274
275 /**
276 * Tester class used for testing the {@link VCardPropertyScribe#writeText}
277 * method.
278 */
279 public class WriteTextTest {
280 protected final T property;
281 private VCardVersion versions[] = VCardVersion.values();
282 private boolean includeTrailingSemicolons = false;
283
284 public WriteTextTest(T property) {
285 this.property = property;
286 }
287
288 /**
289 * Sets the versions to run this test under (defaults to all versions).
290 * @param versions the versions
291 * @return this
292 */
293 public WriteTextTest versions(VCardVersion... versions) {
294 this.versions = versions;
295 return this;
296 }
297
298 /**
299 * Sets whether to include trailing semicolons for structured property
300 * values whose list of values end with null or empty values
301 * @param include true to include them, false not to (defaults to false)
302 * @return this
303 */
304 public WriteTextTest includeTrailingSemicolons(boolean include) {
305 includeTrailingSemicolons = include;
306 return this;
307 }
308
309 /**
310 * Runs the test, expecting a {@link SkipMeException} to be thrown.
311 */
312 public void skipMe() {
313 run(SkipMeException.class);
314 }
315
316 /**
317 * Runs the test.
318 * @param expected the expected property value
319 */
320 public void run(String expected) {
321 for (VCardVersion version : versions) {
322 String actual = scribe.writeText(property, new WriteContext(version, null, includeTrailingSemicolons));
323 assertEquals("Version " + version, expected, actual);
324 }
325 }
326
327 public void run(Class<? extends RuntimeException> expected) {
328 for (VCardVersion version : versions) {
329 try {
330 scribe.writeText(property, new WriteContext(version, null, false));
331 fail("Expected " + expected.getSimpleName());
332 } catch (RuntimeException t) {
333 assertEquals("Expected " + expected.getSimpleName() + ", but was " + t.getClass().getSimpleName(), expected, t.getClass());
334 }
335 }
336 }
337 }
338
339 /**
340 * Tester class used for testing the {@link VCardPropertyScribe#writeXml}
341 * method.
342 */
343 public class WriteXmlTest {
344 protected final T property;
345
346 public WriteXmlTest(T property) {
347 this.property = property;
348 }
349
350 /**
351 * Runs the test.
352 * @param expectedInnerXml the expected inner XML of the xCard property
353 * element
354 */
355 public void run(String expectedInnerXml) {
356 Document actual = createXCardElement();
357 scribe.writeXml(property, actual.getDocumentElement());
358
359 Document expected = createXCardElement(expectedInnerXml);
360
361 assertXMLEqual(XmlUtils.toString(actual), expected, actual);
362 }
363 }
364
365 /**
366 * Tester class used for testing the {@link VCardPropertyScribe#writeJson}
367 * method.
368 */
369 public class WriteJsonTest {
370 protected final T property;
371
372 public WriteJsonTest(T property) {
373 this.property = property;
374 }
375
376 /**
377 * Runs the test.
378 * @return the marshalled value
379 */
380 public JCardValue run() {
381 return scribe.writeJson(property);
382 }
383
384 /**
385 * Runs the test.
386 * @param expected the expected jCard value
387 */
388 public void run(JCardValue expected) {
389 JCardValue value = run();
390 assertEquals(expected.getValues(), value.getValues());
391 }
392
393 /**
394 * Runs the test.
395 * @param expected the expected jCard value
396 */
397 public void run(String expected) {
398 JCardValue value = run();
399 assertEquals(1, value.getValues().size());
400 assertEquals(expected, value.getValues().get(0).getValue());
401 }
402 }
403
404 /**
405 * Parent class for the parser testers.
406 */
407 private abstract class ParseTest<U extends ParseTest<U>> {
408 protected VCardParameters parameters = new VCardParameters();
409 protected Integer[] parseWarningCodes = new Integer[0];
410
411 @SuppressWarnings("unchecked")
412 private final U this_ = (U) this;
413
414 /**
415 * Adds an expected parameter.
416 * @param name the parameter name
417 * @param value the parameter value
418 * @return this
419 */
420 public U param(String name, String value) {
421 parameters.put(name, value);
422 return this_;
423 }
424
425 /**
426 * Sets the expected parameters.
427 * @param parameters the parameters
428 * @return this
429 */
430 public U params(VCardParameters parameters) {
431 this.parameters = parameters;
432 return this_;
433 }
434
435 /**
436 * Sets the expected parse warning codes (by default, the scribe is not
437 * expected to return any parse warnings).
438 * @param codes the expected parse warning codes (order does not matter)
439 * @return this
440 */
441 public U warnings(Integer... codes) {
442 parseWarningCodes = codes;
443 return this_;
444 }
445
446 /**
447 * Runs the test, without testing the returned property object.
448 */
449 public void run() {
450 run(null, -1);
451 }
452
453 /**
454 * Runs the test.
455 * @param check object for validating the parsed property object
456 */
457 public void run(Check<T> check) {
458 run(check, -1);
459 }
460
461 /**
462 * Runs the test.
463 * @param expected the expected property. Expected and actual property
464 * objects will be compared with the {@code equals()} method.
465 */
466 public void run(final T expected) {
467 run(actual -> assertEquals(expected, actual), -1);
468 }
469
470 /**
471 * Runs the test, expecting a {@link CannotParseException} to be thrown.
472 * @param expectedCode the expected warning code of the exception, or
473 * null if the exception shouldn't have a warning code
474 */
475 public void cannotParse(Integer expectedCode) {
476 run(null, expectedCode);
477 }
478
479 /**
480 * Runs the test.
481 * @param check object for asserting the parsed property object or null
482 * not to assert the property object
483 * @param cannotParseExceptionCode if the parse operation is expected to throw a
484 * {@link CannotParseException}, this parameter is the expected warning
485 * code of the exception. This should be null if the exception is not
486 * expected to have a warning code. This should be -1 if a
487 * {@link CannotParseException} is not expected to be thrown
488 */
489 protected abstract void run(Check<T> check, Integer cannotParseExceptionCode);
490 }
491
492 /**
493 * Tester class used for testing the {@link VCardPropertyScribe#parseText}
494 * method.
495 */
496 public class ParseTextTest extends ParseTest<ParseTextTest> {
497 private final String value;
498 private VCardDataType dataType;
499 private VCardVersion versions[] = VCardVersion.values();
500
501 /**
502 * @param value the text to parse
503 */
504 public ParseTextTest(String value) {
505 this.value = value;
506 }
507
508 /**
509 * Sets the data type (defaults to the property's default data type)
510 * @param dataType the data type
511 * @return this
512 */
513 public ParseTextTest dataType(VCardDataType dataType) {
514 this.dataType = dataType;
515 return this;
516 }
517
518 /**
519 * Sets the versions to run this test under (defaults to all versions).
520 * @param versions the versions
521 * @return this
522 */
523 public ParseTextTest versions(VCardVersion... versions) {
524 this.versions = versions;
525 return this;
526 }
527
528 @Override
529 protected void run(Check<T> check, Integer cannotParseExceptionCode) {
530 for (VCardVersion version : versions) {
531 VCardDataType dataType = this.dataType;
532 if (dataType == null) {
533 dataType = scribe.defaultDataType(version);
534 }
535
536 ParseContext context = new ParseContext();
537 context.setVersion(version);
538 try {
539 T property = scribe.parseText(value, dataType, parameters, context);
540
541 if (cannotParseExceptionCode == null || cannotParseExceptionCode >= 0) {
542 fail("Expected a CannotParseException with code <" + cannotParseExceptionCode + "> to be thrown.");
543 }
544
545 if (check != null) {
546 check.check(property);
547 }
548
549 assertParseWarnings(context.getWarnings(), parseWarningCodes);
550 } catch (CannotParseException e) {
551 if (cannotParseExceptionCode == -1) {
552 /*
553 * Throw the exception to fail the test, since it was
554 * not expected to be thrown.
555 */
556 throw e;
557 }
558
559 assertEquals("CannotParseException's parse warning code was wrong.", cannotParseExceptionCode, e.getCode());
560 }
561 }
562 }
563 }
564
565 /**
566 * Tester class used for testing the {@link VCardPropertyScribe#parseXml}
567 * method.
568 */
569 public class ParseXmlTest extends ParseTest<ParseXmlTest> {
570 private final String innerXml;
571
572 /**
573 * @param innerXml the inner XML of the xCard property element
574 */
575 public ParseXmlTest(String innerXml) {
576 this.innerXml = innerXml;
577 }
578
579 @Override
580 protected void run(Check<T> check, Integer cannotParseExceptionCode) {
581 try {
582 ParseContext context = new ParseContext();
583 context.setVersion(VCardVersion.V4_0);
584 Document document = createXCardElement(innerXml);
585 Element element = document.getDocumentElement();
586 T property = scribe.parseXml(element, parameters, context);
587
588 if (cannotParseExceptionCode == null || cannotParseExceptionCode >= 0) {
589 fail("Expected a CannotParseException with code <" + cannotParseExceptionCode + "> to be thrown.");
590 }
591
592 if (check != null) {
593 check.check(property);
594 }
595
596 assertParseWarnings(context.getWarnings(), parseWarningCodes);
597 } catch (CannotParseException e) {
598 if (cannotParseExceptionCode == -1) {
599 /*
600 * Throw the exception to fail the test, since it was not
601 * expected to be thrown.
602 */
603 throw e;
604 }
605
606 assertEquals("CannotParseException's parse warning code was wrong.", cannotParseExceptionCode, e.getCode());
607 }
608 }
609 }
610
611 /**
612 * Tester class used for testing the {@link VCardPropertyScribe#parseHtml}
613 * method.
614 */
615 public class ParseHtmlTest extends ParseTest<ParseHtmlTest> {
616 private final String html;
617
618 /**
619 * @param html the HTML of the hCard property element
620 */
621 public ParseHtmlTest(String html) {
622 this.html = html;
623 }
624
625 @Override
626 public ParseHtmlTest param(String name, String value) {
627 throw new UnsupportedOperationException();
628 }
629
630 @Override
631 public ParseHtmlTest params(VCardParameters parameters) {
632 throw new UnsupportedOperationException();
633 }
634
635 @Override
636 protected void run(Check<T> check, Integer cannotParseExceptionCode) {
637 try {
638 ParseContext context = new ParseContext();
639 context.setVersion(VCardVersion.V3_0);
640 org.jsoup.nodes.Element element = HtmlUtils.toElement(html);
641 T property = scribe.parseHtml(new HCardElement(element), context);
642
643 if (cannotParseExceptionCode == null || cannotParseExceptionCode >= 0) {
644 fail("Expected a CannotParseException with code <" + cannotParseExceptionCode + "> to be thrown.");
645 }
646
647 if (check != null) {
648 check.check(property);
649 }
650
651 assertParseWarnings(context.getWarnings(), parseWarningCodes);
652 } catch (CannotParseException e) {
653 if (cannotParseExceptionCode == -1) {
654 /*
655 * Throw the exception to fail the test, since it was not
656 * expected to be thrown.
657 */
658 throw e;
659 }
660
661 assertEquals("CannotParseException's parse warning code was wrong.", cannotParseExceptionCode, e.getCode());
662 }
663 }
664 }
665
666 /**
667 * Tester class used for testing the {@link VCardPropertyScribe#parseJson}
668 * method.
669 */
670 public class ParseJsonTest extends ParseTest<ParseJsonTest> {
671 private final JCardValue value;
672 private VCardDataType dataType = scribe.defaultDataType(VCardVersion.V4_0);
673
674 /**
675 * @param value the value
676 */
677 public ParseJsonTest(JCardValue value) {
678 this.value = value;
679 }
680
681 /**
682 * Sets the data type (defaults to the property's default data type)
683 * @param dataType the data type
684 * @return this
685 */
686 public ParseJsonTest dataType(VCardDataType dataType) {
687 this.dataType = dataType;
688 return this;
689 }
690
691 @Override
692 protected void run(Check<T> check, Integer cannotParseExceptionCode) {
693 try {
694 ParseContext context = new ParseContext();
695 context.setVersion(VCardVersion.V4_0);
696 T property = scribe.parseJson(value, dataType, parameters, context);
697
698 if (cannotParseExceptionCode == null || cannotParseExceptionCode >= 0) {
699 fail("Expected a CannotParseException with code <" + cannotParseExceptionCode + "> to be thrown.");
700 }
701
702 if (check != null) {
703 check.check(property);
704 }
705
706 assertParseWarnings(context.getWarnings(), parseWarningCodes);
707 } catch (CannotParseException e) {
708 if (cannotParseExceptionCode == -1) {
709 /*
710 * Throw the exception to fail the test, since it was not
711 * expected to be thrown.
712 */
713 throw e;
714 }
715
716 assertEquals("CannotParseException's parse warning code was wrong.", cannotParseExceptionCode, e.getCode());
717 }
718 }
719 }
720
721 /**
722 * Used for validating the contents of a parsed property object.
723 * @param <T> the property class
724 */
725 public interface Check<T extends VCardProperty> {
726 /**
727 * Validates the contents of the parsed property object.
728 * @param property the parsed property object
729 */
730 void check(T property);
731 }
732
733 private Document createXCardElement() {
734 return createXCardElement("");
735 }
736
737 private Document createXCardElement(String innerXml) {
738 QName qname = scribe.getQName();
739 String localName = qname.getLocalPart();
740 String ns = qname.getNamespaceURI();
741
742 try {
743 return XmlUtils.toDocument("<" + localName + " xmlns=\"" + ns + "\">" + innerXml + "</" + localName + ">");
744 } catch (SAXException e) {
745 throw new RuntimeException(e);
746 }
747 }
748 }