1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.text.lookup;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.HashMap;
30
31 import javax.xml.XMLConstants;
32
33 import org.apache.commons.lang3.StringUtils;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39 class XmlStringLookupTest {
40
41 private static final Path CURRENT_PATH = Paths.get(StringUtils.EMPTY);
42 private static final Path ABSENT_PATH = Paths.get("does not exist at all");
43 private static final String DOC_RELATIVE = "src/test/resources/org/apache/commons/text/document.xml";
44 private static final String DOC_ROOT = "/document.xml";
45
46 static void assertLookup(final StringLookup xmlStringLookup) {
47 assertNotNull(xmlStringLookup);
48 assertInstanceOf(XmlStringLookup.class, xmlStringLookup);
49 assertEquals("Hello World!", xmlStringLookup.apply(DOC_RELATIVE + ":/root/path/to/node"));
50 assertNull(xmlStringLookup.apply(null));
51 }
52
53 @Test
54 void testBadXPath() {
55 assertThrows(IllegalArgumentException.class, () -> XmlStringLookup.INSTANCE.apply("docName"));
56 }
57
58 @Test
59 void testMissingXPath() {
60 assertThrows(IllegalArgumentException.class, () -> XmlStringLookup.INSTANCE.apply(DOC_RELATIVE + ":" + "!JUNK!"));
61 }
62
63 @Test
64 void testNoFeatures() {
65 final String xpath = "/root/path/to/node";
66 assertEquals("Hello World!", new XmlStringLookup(new HashMap<>()).apply(DOC_RELATIVE + ":" + xpath));
67 assertEquals("Hello World!", new XmlStringLookup(new HashMap<>(), CURRENT_PATH).apply(DOC_RELATIVE + ":" + xpath));
68 assertEquals("Hello World!", new XmlStringLookup(new HashMap<>(), CURRENT_PATH, ABSENT_PATH).apply(DOC_RELATIVE + ":" + xpath));
69 assertEquals("Hello World!", new XmlStringLookup(new HashMap<>(), ABSENT_PATH, CURRENT_PATH).apply(DOC_RELATIVE + ":" + xpath));
70 assertThrows(IllegalArgumentException.class, () -> new XmlStringLookup(new HashMap<>(), ABSENT_PATH).apply(DOC_ROOT + ":" + xpath));
71 assertThrows(IllegalArgumentException.class, () -> new XmlStringLookup(new HashMap<>(), CURRENT_PATH).apply(DOC_ROOT + ":" + xpath));
72 assertThrows(IllegalArgumentException.class, () -> new XmlStringLookup(new HashMap<>(), ABSENT_PATH, CURRENT_PATH).apply(DOC_ROOT + ":" + xpath));
73 }
74
75 @Test
76 void testNoFeaturesDefault() {
77 final HashMap<String, Boolean> features = new HashMap<>(1);
78 features.put(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
79 assertLookup(new XmlStringLookup(features));
80 }
81
82 @Test
83 void testNull() {
84 assertNull(XmlStringLookup.INSTANCE.apply(null));
85 }
86
87 @Test
88 void testOne() {
89 assertLookup(XmlStringLookup.INSTANCE);
90 }
91
92 @Test
93 void testToString() {
94
95 assertFalse(XmlStringLookup.INSTANCE.toString().isEmpty());
96 }
97
98 }