| 1 |
package ezvcard.io.xml; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertEquals; |
| 4 |
import static org.junit.Assert.assertFalse; |
| 5 |
import static org.junit.Assert.assertNull; |
| 6 |
import static org.junit.Assert.assertTrue; |
| 7 |
|
| 8 |
import java.util.Arrays; |
| 9 |
import java.util.Iterator; |
| 10 |
import java.util.List; |
| 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.VCardDataType; |
| 18 |
import ezvcard.VCardVersion; |
| 19 |
import ezvcard.io.xml.XCardElement.XCardValue; |
| 20 |
import ezvcard.util.XmlUtils; |
| 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 |
|
| 53 |
|
| 54 |
public class XCardElementTest { |
| 55 |
@Test |
| 56 |
public void first() { |
| 57 |
XCardElement xcardElement = build("<prop><one>1</one><two>2</two></prop>"); |
| 58 |
assertEquals("2", xcardElement.first("two")); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void first_data_type() { |
| 63 |
XCardElement xcardElement = build("<prop><one>1</one><text>2</text></prop>"); |
| 64 |
assertEquals("2", xcardElement.first(VCardDataType.TEXT)); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void first_unknown() { |
| 69 |
XCardElement xcardElement = build("<prop><one>1</one><unknown>2</unknown></prop>"); |
| 70 |
assertEquals("2", xcardElement.first((VCardDataType) null)); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void first_xcard_namespace_with_prefix() { |
| 75 |
XCardElement xcardElement = build("<v:prop><v:one>1</v:one><v:two>2</v:two></v:prop>", "v"); |
| 76 |
assertEquals("2", xcardElement.first("two")); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void first_empty() { |
| 81 |
XCardElement xcardElement = build("<prop><one>1</one><two></two></prop>"); |
| 82 |
assertEquals("", xcardElement.first("two")); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void first_none() { |
| 87 |
XCardElement xcardElement = build("<prop><one>1</one><two>2</two></prop>"); |
| 88 |
assertNull(xcardElement.first("three")); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void first_multiple_names() { |
| 93 |
XCardElement xcardElement = build("<prop><one>1</one><two>2</two><three>3</three></prop>"); |
| 94 |
assertEquals("2", xcardElement.first("two", "three")); |
| 95 |
assertEquals("2", xcardElement.first("three", "two")); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void first_only_xcard_namespace() { |
| 100 |
XCardElement xcardElement = build("<prop><n:four xmlns:n=\"http://example.com\"></n:four></prop>"); |
| 101 |
assertNull(xcardElement.first("four")); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void all() { |
| 106 |
XCardElement xcardElement = build("<prop><one>1</one><two>2</two><two /><three>3</three><two>2-2</two></prop>"); |
| 107 |
assertEquals(Arrays.asList("2", "2-2"), xcardElement.all("two")); |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void all_data_type() { |
| 112 |
XCardElement xcardElement = build("<prop><one>1</one><text>2</text><two /><three>3</three><text>2-2</text></prop>"); |
| 113 |
assertEquals(Arrays.asList("2", "2-2"), xcardElement.all(VCardDataType.TEXT)); |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
public void all_unknown() { |
| 118 |
XCardElement xcardElement = build("<prop><one>1</one><unknown>2</unknown><two /><three>3</three><unknown>2-2</unknown></prop>"); |
| 119 |
assertEquals(Arrays.asList("2", "2-2"), xcardElement.all((VCardDataType) null)); |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void all_none() { |
| 124 |
XCardElement xcardElement = build("<prop><one>1</one><two>2</two></prop>"); |
| 125 |
assertTrue(xcardElement.all("three").isEmpty()); |
| 126 |
} |
| 127 |
|
| 128 |
@Test |
| 129 |
public void firstValue() { |
| 130 |
XCardElement xcardElement = build("<prop><text>one</text></prop>"); |
| 131 |
XCardValue child = xcardElement.firstValue(); |
| 132 |
assertEquals(VCardDataType.TEXT, child.getDataType()); |
| 133 |
assertEquals("one", child.getValue()); |
| 134 |
} |
| 135 |
|
| 136 |
@Test |
| 137 |
public void firstValue_unknown() { |
| 138 |
XCardElement xcardElement = build("<prop><unknown>one</unknown></prop>"); |
| 139 |
XCardValue child = xcardElement.firstValue(); |
| 140 |
assertNull(child.getDataType()); |
| 141 |
assertEquals("one", child.getValue()); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void firstValue_namespace() { |
| 146 |
XCardElement xcardElement = build("<prop><n:foo xmlns:n=\"http://example.com\">one</n:foo><text>two</text></prop>"); |
| 147 |
XCardValue child = xcardElement.firstValue(); |
| 148 |
assertEquals(VCardDataType.TEXT, child.getDataType()); |
| 149 |
assertEquals("two", child.getValue()); |
| 150 |
} |
| 151 |
|
| 152 |
@Test |
| 153 |
public void firstValue_no_xcard_children() { |
| 154 |
XCardElement xcardElement = build("<prop><n:foo xmlns:n=\"http://example.com\">one</n:foo><n:bar xmlns:n=\"http://example.com\">two</n:bar></prop>"); |
| 155 |
XCardValue child = xcardElement.firstValue(); |
| 156 |
assertNull(child.getDataType()); |
| 157 |
assertEquals("onetwo", child.getValue()); |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void append() { |
| 162 |
XCardElement xcardElement = build("<prop><one>1</one></prop>"); |
| 163 |
Element appendedElement = xcardElement.append("two", "2"); |
| 164 |
assertEquals("two", appendedElement.getLocalName()); |
| 165 |
assertEquals(VCardVersion.V4_0.getXmlNamespace(), appendedElement.getNamespaceURI()); |
| 166 |
assertEquals("2", appendedElement.getTextContent()); |
| 167 |
|
| 168 |
Iterator<Element> it = XmlUtils.toElementList(xcardElement.element().getChildNodes()).iterator(); |
| 169 |
|
| 170 |
Element element = it.next(); |
| 171 |
assertEquals("one", element.getLocalName()); |
| 172 |
assertEquals(VCardVersion.V4_0.getXmlNamespace(), element.getNamespaceURI()); |
| 173 |
assertEquals("1", element.getTextContent()); |
| 174 |
|
| 175 |
element = it.next(); |
| 176 |
assertEquals(appendedElement, element); |
| 177 |
|
| 178 |
assertFalse(it.hasNext()); |
| 179 |
} |
| 180 |
|
| 181 |
@Test |
| 182 |
public void append_multiple() { |
| 183 |
XCardElement xcardElement = build("<prop />"); |
| 184 |
List<Element> elements = xcardElement.append("number", Arrays.asList("1", "2", "3")); |
| 185 |
Iterator<Element> it = elements.iterator(); |
| 186 |
|
| 187 |
Element element = it.next(); |
| 188 |
assertEquals("number", element.getLocalName()); |
| 189 |
assertEquals(VCardVersion.V4_0.getXmlNamespace(), element.getNamespaceURI()); |
| 190 |
assertEquals("1", element.getTextContent()); |
| 191 |
|
| 192 |
element = it.next(); |
| 193 |
assertEquals("number", element.getLocalName()); |
| 194 |
assertEquals(VCardVersion.V4_0.getXmlNamespace(), element.getNamespaceURI()); |
| 195 |
assertEquals("2", element.getTextContent()); |
| 196 |
|
| 197 |
element = it.next(); |
| 198 |
assertEquals("number", element.getLocalName()); |
| 199 |
assertEquals(VCardVersion.V4_0.getXmlNamespace(), element.getNamespaceURI()); |
| 200 |
assertEquals("3", element.getTextContent()); |
| 201 |
|
| 202 |
assertFalse(it.hasNext()); |
| 203 |
|
| 204 |
assertEquals(XmlUtils.toElementList(xcardElement.element().getChildNodes()), elements); |
| 205 |
} |
| 206 |
|
| 207 |
private static XCardElement build(String innerXml) { |
| 208 |
return build(innerXml, null); |
| 209 |
} |
| 210 |
|
| 211 |
private static XCardElement build(String innerXml, String prefix) { |
| 212 |
|
| 213 |
String xml = |
| 214 |
"<%sroot xmlns%s=\"" + VCardVersion.V4_0.getXmlNamespace() + "\">" + |
| 215 |
innerXml + |
| 216 |
"</%sroot>"; |
| 217 |
|
| 218 |
if (prefix == null) { |
| 219 |
xml = String.format(xml, "", "", "", "", ""); |
| 220 |
} else { |
| 221 |
xml = String.format(xml, prefix + ":", ":" + prefix, prefix + ":"); |
| 222 |
} |
| 223 |
|
| 224 |
Document document; |
| 225 |
try { |
| 226 |
document = XmlUtils.toDocument(xml); |
| 227 |
} catch (SAXException e) { |
| 228 |
throw new RuntimeException(e); |
| 229 |
} |
| 230 |
Element element = XmlUtils.getFirstChildElement(document.getDocumentElement()); |
| 231 |
return new XCardElement(element); |
| 232 |
} |
| 233 |
} |