| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 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 |
|
| 134 |
} |
| 135 |
} |