Debian Codegraph @

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

raw symbols SCIP
1 package ezvcard.io.scribe;
2
3 import static ezvcard.VCardVersion.V2_1;
4 import static ezvcard.VCardVersion.V3_0;
5 import static ezvcard.VCardVersion.V4_0;
6
7 import java.util.Arrays;
8
9 import org.junit.Test;
10
11 import ezvcard.io.json.JCardValue;
12 import ezvcard.property.StructuredName;
13
14 /*
15 Copyright (c) 2012-2023, Michael Angstadt
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20
21 1. Redistributions of source code must retain the above copyright notice, this
22 list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright notice,
24 this list of conditions and the following disclaimer in the documentation
25 and/or other materials provided with the distribution.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
28 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
31 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 The views and conclusions contained in the software and documentation are those
39 of the authors and should not be interpreted as representing official policies,
40 either expressed or implied, of the FreeBSD Project.
41 */
42
43 /**
44 * @author Michael Angstadt
45 */
46 public class StructuredNameScribeTest {
47 private final StructuredNameScribe scribe = new StructuredNameScribe();
48 private final Sensei<StructuredName> sensei = new Sensei<>(scribe);
49
50 private final StructuredName withAllValues = new StructuredName();
51 {
52 withAllValues.setGiven("Jonathan");
53 withAllValues.setFamily("Doe");
54 withAllValues.getAdditionalNames().add("Joh;nny,");
55 withAllValues.getAdditionalNames().add("John");
56 withAllValues.getPrefixes().add("Mr.");
57 withAllValues.getSuffixes().add("III");
58 }
59 private final StructuredName withEmptyValues = new StructuredName();
60 {
61 withEmptyValues.setGiven("Jonathan");
62 withEmptyValues.setFamily(null);
63 withEmptyValues.getAdditionalNames().add("Joh;nny,");
64 withEmptyValues.getAdditionalNames().add("John");
65 }
66 private final StructuredName empty = new StructuredName();
67
68 @Test
69 public void writeText() {
70 sensei.assertWriteText(withAllValues).versions(V2_1).run("Doe;Jonathan;Joh\\;nny,,John;Mr.;III");
71 sensei.assertWriteText(withAllValues).versions(V3_0, V4_0).run("Doe;Jonathan;Joh\\;nny\\,,John;Mr.;III");
72
73 sensei.assertWriteText(withEmptyValues).versions(V2_1).run(";Jonathan;Joh\\;nny,,John");
74 sensei.assertWriteText(withEmptyValues).versions(V3_0, V4_0).run(";Jonathan;Joh\\;nny\\,,John");
75 sensei.assertWriteText(withEmptyValues).includeTrailingSemicolons(true).versions(V3_0, V4_0).run(";Jonathan;Joh\\;nny\\,,John;;");
76
77 sensei.assertWriteText(empty).run("");
78 sensei.assertWriteText(empty).includeTrailingSemicolons(true).run(";;;;");
79 }
80
81 @Test
82 public void writeXml() {
83 //@formatter:off
84 sensei.assertWriteXml(withAllValues).run(
85 "<surname>Doe</surname>" +
86 "<given>Jonathan</given>" +
87 "<additional>Joh;nny,</additional>" +
88 "<additional>John</additional>" +
89 "<prefix>Mr.</prefix>" +
90 "<suffix>III</suffix>"
91 );
92
93 sensei.assertWriteXml(withEmptyValues).run(
94 "<surname/>" +
95 "<given>Jonathan</given>" +
96 "<additional>Joh;nny,</additional>" +
97 "<additional>John</additional>" +
98 "<prefix/>" +
99 "<suffix/>"
100 );
101
102 sensei.assertWriteXml(empty).run(
103 "<surname/>" +
104 "<given/>" +
105 "<additional/>" +
106 "<prefix/>" +
107 "<suffix/>"
108 );
109 //@formatter:on
110 }
111
112 @Test
113 public void writeJson() {
114 sensei.assertWriteJson(withAllValues).run(JCardValue.structured("Doe", "Jonathan", Arrays.asList("Joh;nny,", "John"), "Mr.", "III"));
115 sensei.assertWriteJson(withEmptyValues).run(JCardValue.structured("", "Jonathan", Arrays.asList("Joh;nny,", "John"), "", ""));
116 sensei.assertWriteJson(empty).run(JCardValue.structured("", "", "", "", ""));
117 }
118
119 @Test
120 public void parseText() {
121 StructuredName withAllValuesV2_1 = new StructuredName(withAllValues);
122 withAllValuesV2_1.getAdditionalNames().clear();
123 withAllValuesV2_1.getAdditionalNames().add("Joh;nny,,John");
124 sensei.assertParseText("Doe;Jonathan;Joh\\;nny\\,,John;Mr.;III").versions(V2_1).run(withAllValuesV2_1);
125 sensei.assertParseText("Doe;Jonathan;Joh\\;nny\\,,John;Mr.;III").versions(V3_0, V4_0).run(withAllValues);
126
127 StructuredName withEmptyValuesV2_1 = new StructuredName(withEmptyValues);
128 withEmptyValuesV2_1.getAdditionalNames().clear();
129 withEmptyValuesV2_1.getAdditionalNames().add("Joh;nny,,John");
130 sensei.assertParseText(";Jonathan;Joh\\;nny\\,,John;;").versions(V2_1).run(withEmptyValuesV2_1);
131 sensei.assertParseText(";Jonathan;Joh\\;nny\\,,John;;").versions(V3_0, V4_0).run(withEmptyValues);
132
133 sensei.assertParseText(";;;;").run(empty);
134 sensei.assertParseText("").run(empty);
135 }
136
137 @Test
138 public void parseXml() {
139 //@formatter:off
140 sensei.assertParseXml(
141 "<surname>Doe</surname>" +
142 "<given>Jonathan</given>" +
143 "<additional>Joh;nny,</additional>" +
144 "<additional>John</additional>" +
145 "<prefix>Mr.</prefix>" +
146 "<suffix>III</suffix>"
147 ).run(withAllValues);
148
149 sensei.assertParseXml(
150 "<surname/>" +
151 "<given>Jonathan</given>" +
152 "<additional>Joh;nny,</additional>" +
153 "<additional>John</additional>" +
154 "<prefix/>" +
155 "<suffix/>"
156 ).run(withEmptyValues);
157
158 sensei.assertParseXml(
159 "<surname/>" +
160 "<given/>" +
161 "<additional/>" +
162 "<prefix/>" +
163 "<suffix/>"
164 ).run(empty);
165 //@formatter:on
166 }
167
168 @Test
169 public void parseHtml() {
170 //@formatter:off
171 sensei.assertParseHtml(
172 "<div>" +
173 "<span class=\"family-name\">Doe</span>" +
174 "<span class=\"given-name\">Jonathan</span>" +
175 "<span class=\"additional-name\">Joh;nny,</span>" +
176 "<span class=\"additional-name\">John</span>" +
177 "<span class=\"honorific-prefix\">Mr.</span>" +
178 "<span class=\"honorific-suffix\">III</span>" +
179 "</div>"
180 ).run(withAllValues);
181
182 sensei.assertParseHtml(
183 "<div>" +
184 "<span class=\"given-name\">Jonathan</span>" +
185 "<span class=\"additional-name\">Joh;nny,</span>" +
186 "<span class=\"additional-name\">John</span>" +
187 "</div>"
188 ).run(withEmptyValues);
189
190 sensei.assertParseHtml(
191 "<div>" +
192 "<span class=\"given-name\"></span>" +
193 "</div>"
194 ).run(empty);
195 //@formatter:on
196 }
197
198 @Test
199 public void parseJson() {
200 JCardValue value = JCardValue.structured("Doe", "Jonathan", Arrays.asList("Joh;nny,", "John"), "Mr.", "III");
201 sensei.assertParseJson(value).run(withAllValues);
202
203 value = JCardValue.structured(null, "Jonathan", Arrays.asList("Joh;nny,", "John"), "", null);
204 sensei.assertParseJson(value).run(withEmptyValues);
205
206 value = JCardValue.structured(null, "Jonathan", Arrays.asList("Joh;nny,", "John"));
207 sensei.assertParseJson(value).run(withEmptyValues);
208
209 value = JCardValue.structured(null, null, "", null, null);
210 sensei.assertParseJson(value).run(empty);
211
212 sensei.assertParseJson("").run(empty);
213 }
214 }