Debian Codegraph @

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