Debian Codegraph @

src/test/java/ezvcard/property/XmlTest.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import static ezvcard.property.PropertySensei.assertCopy;
4 import static ezvcard.property.PropertySensei.assertEqualsMethod;
5 import static ezvcard.property.PropertySensei.assertNothingIsEqual;
6 import static ezvcard.property.PropertySensei.assertValidate;
7 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNull;
10 import static org.junit.Assert.assertSame;
11
12 import org.junit.Test;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.xml.sax.SAXException;
16
17 import ezvcard.VCardVersion;
18 import ezvcard.util.XmlUtils;
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 XmlTest {
53 @Test
54 public void constructors() throws Exception {
55 Xml property = new Xml((Document) null);
56 assertNull(property.getValue());
57
58 property = new Xml((Element) null);
59 assertNull(property.getValue());
60
61 Document value = XmlUtils.toDocument("<root><child/></root>");
62 property = new Xml((Element) value.getDocumentElement().getFirstChild());
63 assertXMLEqual(XmlUtils.toDocument("<child/>"), property.getValue());
64
65 property = new Xml("<root/>");
66 assertXMLEqual(XmlUtils.toDocument("<root/>"), property.getValue());
67 }
68
69 @Test(expected = SAXException.class)
70 public void constructors_invalid_xml() throws Throwable {
71 new Xml("not valid XML");
72 }
73
74 @Test
75 public void set_value() throws Exception {
76 Document value = XmlUtils.toDocument("<root/>");
77 Xml property = new Xml(value);
78
79 Document value2 = XmlUtils.toDocument("<root2/>");
80 property.setValue(value2);
81 assertSame(value2, property.getValue());
82
83 property.setValue(null);
84 assertNull(property.getValue());
85 }
86
87 @Test
88 public void validate() throws Throwable {
89 Xml empty = new Xml((Document) null);
90 assertValidate(empty).versions(VCardVersion.V2_1).run(2, 8);
91 assertValidate(empty).versions(VCardVersion.V3_0).run(2, 8);
92 assertValidate(empty).versions(VCardVersion.V4_0).run(8);
93
94 Xml withValue = new Xml("<root/>");
95 assertValidate(withValue).versions(VCardVersion.V2_1).run(2);
96 assertValidate(withValue).versions(VCardVersion.V3_0).run(2);
97 assertValidate(withValue).versions(VCardVersion.V4_0).run();
98 }
99
100 @Test
101 public void toStringValues() throws Exception {
102 Xml property = new Xml(XmlUtils.toDocument("<root/>"));
103 assertFalse(property.toStringValues().isEmpty());
104
105 property = new Xml((Document) null);
106 assertFalse(property.toStringValues().isEmpty());
107 }
108
109 @Test
110 public void copy() throws Exception {
111 Xml original = new Xml((Document) null);
112 assertCopy(original);
113
114 original = new Xml("<root/>");
115 assertCopy(original).notSame("getValue");
116
117 original = new Xml(XmlUtils.createDocument());
118 assertCopy(original);
119 }
120
121 @Test
122 public void equals() throws Exception {
123 //@formatter:off
124 assertNothingIsEqual(
125 new Xml((Document)null),
126 new Xml("<root/>"),
127 new Xml("<root2/>")
128 );
129
130 assertEqualsMethod(Xml.class, "<root/>")
131 .constructor(new Class<?>[]{String.class}, (String)null).test()
132 .constructor("<root/>").test();
133 //@formatter:on
134 }
135 }