Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5
6 import org.junit.Test;
7
8 import ezvcard.VCardDataType;
9 import ezvcard.io.scribe.Sensei.Check;
10 import ezvcard.property.Related;
11
12 /*
13 Copyright (c) 2012-2023, Michael Angstadt
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18
19 1. Redistributions of source code must retain the above copyright notice, this
20 list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright notice,
22 this list of conditions and the following disclaimer in the documentation
23 and/or other materials provided with the distribution.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
29 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 The views and conclusions contained in the software and documentation are those
37 of the authors and should not be interpreted as representing official policies,
38 either expressed or implied, of the FreeBSD Project.
39 */
40
41 /**
42 * @author Michael Angstadt
43 */
44 public class RelatedScribeTest {
45 private final RelatedScribe scribe = new RelatedScribe();
46 private final Sensei<Related> sensei = new Sensei<>(scribe);
47
48 private final String text = "New York, NY";
49 private final String textEscaped = "New York\\, NY";
50 private final String uri = "geo:40.71448,-74.00598";
51
52 private final Related withText = new Related();
53 {
54 withText.setText(text);
55 }
56 private final Related withUri = new Related();
57 {
58 withUri.setUri(uri);
59 }
60 private final Related empty = new Related();
61
62 @Test
63 public void dataType() {
64 sensei.assertDataType(withText).run(VCardDataType.TEXT);
65 sensei.assertDataType(withUri).run(VCardDataType.URI);
66 sensei.assertDataType(empty).run(VCardDataType.URI);
67 }
68
69 @Test
70 public void writeText() {
71 sensei.assertWriteText(withText).run(textEscaped);
72 sensei.assertWriteText(withUri).run(uri);
73 sensei.assertWriteText(empty).run("");
74 }
75
76 @Test
77 public void writeXml() {
78 sensei.assertWriteXml(withText).run("<text>" + text + "</text>");
79 sensei.assertWriteXml(withUri).run("<uri>" + uri + "</uri>");
80 sensei.assertWriteXml(empty).run("<uri/>");
81 }
82
83 @Test
84 public void writeJson() {
85 sensei.assertWriteJson(withText).run(text);
86 sensei.assertWriteJson(withUri).run(uri);
87 sensei.assertWriteJson(empty).run("");
88 }
89
90 @Test
91 public void parseText() {
92 sensei.assertParseText(textEscaped).dataType(VCardDataType.TEXT).run(hasText(text));
93 sensei.assertParseText(uri).dataType(VCardDataType.URI).run(hasUri(uri));
94 sensei.assertParseText("").run(hasUri(""));
95 }
96
97 @Test
98 public void parseXml() {
99 sensei.assertParseXml("<text>" + text + "</text>").run(hasText(text));
100 sensei.assertParseXml("<uri>" + uri + "</uri>").run(hasUri(uri));
101 sensei.assertParseXml("<text>" + text + "</text><uri>" + uri + "</uri>").run(hasUri(uri));
102 sensei.assertParseXml("").cannotParse(0);
103 }
104
105 @Test
106 public void parseJson() {
107 sensei.assertParseJson(text).dataType(VCardDataType.TEXT).run(hasText(text));
108 sensei.assertParseJson(uri).dataType(VCardDataType.URI).run(hasUri(uri));
109
110 //use uri data type if set to something other than text or uri
111 sensei.assertParseJson(uri).dataType(VCardDataType.LANGUAGE_TAG).run(hasUri(uri));
112 }
113
114 private Check<Related> hasText(final String text) {
115 return actual -> {
116 assertEquals(text, actual.getText());
117 assertNull(actual.getUri());
118 };
119 }
120
121 private Check<Related> hasUri(final String uri) {
122 return actual -> {
123 assertNull(actual.getText());
124 assertEquals(uri, actual.getUri());
125 };
126 }
127 }