Debian Codegraph @

src/test/java/ezvcard/io/scribe/SimplePropertyScribeTest.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 import static org.junit.Assert.assertEquals;
7
8 import org.junit.Test;
9
10 import ezvcard.VCardDataType;
11 import ezvcard.io.scribe.Sensei.Check;
12 import ezvcard.property.VCardProperty;
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 SimplePropertyScribeTest {
47 private final SimplePropertyScribeImpl scribe = new SimplePropertyScribeImpl();
48 private final Sensei<TestProperty> sensei = new Sensei<>(scribe);
49
50 private final String value = "One, two\nthree; four\\.";
51 private final String valueEscaped = "One\\, two\nthree\\; four\\\\.";
52 private final String valueHtml = "One, two three; four\\.";
53
54 private final TestProperty withValue = new TestProperty(value);
55 private final TestProperty empty = new TestProperty(null);
56
57 @Test
58 public void writeText() {
59 sensei.assertWriteText(withValue).versions(V2_1).run(value);
60 sensei.assertWriteText(withValue).versions(V3_0, V4_0).run(valueEscaped);
61 sensei.assertWriteText(empty).run("");
62 }
63
64 @Test
65 public void writeXml() {
66 sensei.assertWriteXml(withValue).run("<text>" + value + "</text>");
67 sensei.assertWriteXml(empty).run("<text/>");
68 }
69
70 @Test
71 public void writeJson() {
72 sensei.assertWriteJson(withValue).run(value);
73 sensei.assertWriteJson(empty).run("");
74 }
75
76 @Test
77 public void parseText() {
78 sensei.assertParseText(valueEscaped).run(withValue);
79 sensei.assertParseText("").run(hasText(""));
80 }
81
82 @Test
83 public void parseXml() {
84 sensei.assertParseXml("<text>" + value + "</text>").run(withValue);
85 sensei.assertParseXml("").cannotParse(0);
86 }
87
88 @Test
89 public void parseHtml() {
90 sensei.assertParseHtml("<div>" + value + "</div>").run(hasText(valueHtml));
91 sensei.assertParseHtml("<div></div>").run(hasText(""));
92 }
93
94 @Test
95 public void parseJson() {
96 sensei.assertParseJson(value).run(withValue);
97 sensei.assertParseJson("").run(hasText(""));
98 }
99
100 private static class SimplePropertyScribeImpl extends SimplePropertyScribe<TestProperty> {
101 public SimplePropertyScribeImpl() {
102 super(TestProperty.class, "TEST", VCardDataType.TEXT);
103 }
104
105 @Override
106 protected String _writeValue(TestProperty property) {
107 return property.value;
108 }
109
110 @Override
111 protected TestProperty _parseValue(String value) {
112 return new TestProperty(value);
113 }
114 }
115
116 private static class TestProperty extends VCardProperty {
117 public String value;
118
119 public TestProperty(String value) {
120 this.value = value;
121 }
122 }
123
124 private Check<TestProperty> hasText(final String text) {
125 return actual -> assertEquals(text, actual.value);
126 }
127 }