Debian Codegraph @

src/test/java/ezvcard/io/scribe/AgentScribeTest.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 ezvcard.VCardVersion.values;
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertNull;
9 import static org.junit.Assert.fail;
10
11 import org.junit.Test;
12
13 import ezvcard.VCard;
14 import ezvcard.VCardDataType;
15 import ezvcard.VCardVersion;
16 import ezvcard.io.EmbeddedVCardException;
17 import ezvcard.io.text.WriteContext;
18 import ezvcard.property.Agent;
19
20 /*
21 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
44 The views and conclusions contained in the software and documentation are those
45 of the authors and should not be interpreted as representing official policies,
46 either expressed or implied, of the FreeBSD Project.
47 */
48
49 /**
50 * @author Michael Angstadt
51 */
52 public class AgentScribeTest {
53 private final AgentScribe scribe = new AgentScribe();
54 private final Sensei<Agent> sensei = new Sensei<>(scribe);
55
56 private final VCard vcard = new VCard();
57 private final String url = "http://mi5.co.uk/007";
58 private final Agent withUrl = new Agent(url);
59 private final Agent withVCard = new Agent(vcard);
60 private final Agent empty = new Agent();
61
62 @Test
63 public void dataType() {
64 sensei.assertDataType(withUrl).versions(V2_1).run(VCardDataType.URL);
65 sensei.assertDataType(withUrl).versions(V3_0, V4_0).run(VCardDataType.URI);
66
67 sensei.assertDataType(withVCard).run(null);
68 sensei.assertDataType(empty).run(null);
69 }
70
71 @Test
72 public void writeText() {
73 sensei.assertWriteText(withUrl).run(url);
74
75 for (VCardVersion version : values()) {
76 try {
77 scribe.writeText(withVCard, new WriteContext(version, null, false));
78 fail();
79 } catch (EmbeddedVCardException e) {
80 assertEquals(vcard, e.getVCard());
81 }
82 }
83
84 sensei.assertWriteText(empty).skipMe();
85 }
86
87 @Test
88 public void parseText_url() {
89 sensei.assertParseText(url).dataType(VCardDataType.URL).run(withUrl);
90 sensei.assertParseText(url).dataType(VCardDataType.URI).run(withUrl);
91 }
92
93 @Test
94 public void parseText_vcard_2_1() {
95 try {
96 sensei.assertParseText("").versions(V2_1).run();
97 fail();
98 } catch (EmbeddedVCardException e) {
99 Agent property = (Agent) e.getProperty();
100 assertNull(property.getUrl());
101 assertNull(property.getVCard());
102
103 e.injectVCard(vcard);
104 assertNull(property.getUrl());
105 assertEquals(vcard, property.getVCard());
106 }
107 }
108
109 @Test
110 public void parseText_vcard_3_0() {
111 try {
112 sensei.assertParseText("BEGIN:VCARD\\nEND:VCARD").versions(V3_0).run();
113 fail();
114 } catch (EmbeddedVCardException e) {
115 Agent property = (Agent) e.getProperty();
116 assertNull(property.getUrl());
117 assertNull(property.getVCard());
118
119 e.injectVCard(vcard);
120 assertNull(property.getUrl());
121 assertEquals(vcard, property.getVCard());
122 }
123 }
124
125 @Test
126 public void parseHtml() {
127 sensei.assertParseHtml("<a href=\"" + url + "\"></a>").run(withUrl);
128 sensei.assertParseHtml("<div>" + url + "</div>").run(withUrl);
129
130 try {
131 //@formatter:off
132 sensei.assertParseHtml(
133 "<div class=\"agent vcard\">" +
134 "<span class=\"fn\">Jane Doe</span>" +
135 "<div class=\"agent vcard\">" +
136 "<span class=\"fn\">Joseph Doe</span>" +
137 "</div>" +
138 "</div>").run();
139 //@formatter:on
140
141 fail();
142 } catch (EmbeddedVCardException e) {
143 Agent property = (Agent) e.getProperty();
144 assertNull(property.getUrl());
145 assertNull(property.getVCard());
146
147 e.injectVCard(vcard);
148 assertNull(property.getUrl());
149 assertEquals(vcard, property.getVCard());
150 }
151 }
152 }