View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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   * Tests {@link XmlStringLookup}.
38   */
39  class XmlStringLookupTest {
40  
41      private static final Path CURRENT_PATH = Paths.get(StringUtils.EMPTY); // NOT "."
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          // does not blow up and gives some kind of string.
95          assertFalse(XmlStringLookup.INSTANCE.toString().isEmpty());
96      }
97  
98  }