Debian Codegraph @

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

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