This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push:
new b8347466 Add tests for new factory method
b8347466 is described below
commit b83474667d6f8ede75d6a0c4289e01bc2a54bae9
Author: Gary Gregory <[email protected]>
AuthorDate: Sat May 6 15:36:13 2023 -0400
Add tests for new factory method
Better types
---
src/changes/changes.xml | 2 +-
.../commons/text/lookup/StringLookupFactory.java | 3 +-
.../commons/text/lookup/XmlStringLookup.java | 20 +++++++-----
.../text/lookup/StringLookupFactoryTest.java | 11 +++++++
.../commons/text/lookup/XmlStringLookupTest.java | 37 +++++++++++++++++++---
5 files changed, 57 insertions(+), 16 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7b25d72d..f042379e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -59,7 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Add and use a package-private singleton for JaccardSimilarity.</action>
<!-- ADD -->
<action issue="TEXT-224" type="add" dev="ggregory" due-to="PJ Fanning,
Gary Gregory">Set SecureProcessing feature in XmlStringLookup by
default.</action>
- <action issue="TEXT-224" type="add" dev="ggregory" due-to="Gary
Gregory">Add StringLookupFactory.xmlStringLookup(Pair<String,
Boolean>...).</action>
+ <action issue="TEXT-224" type="add" dev="ggregory" due-to="Gary
Gregory">Add StringLookupFactory.xmlStringLookup(Map<String,
Boolean>...).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory"
due-to="Dependabot">Bump actions/cache from 3.0.8 to 3.0.10 #361, #365.</action>
<action type="update" dev="ggregory"
due-to="Dependabot">Bump actions/setup-java from 3 to 3.5.1.</action>
diff --git
a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
index 8e826e04..be952db1 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -29,7 +29,6 @@ import java.util.function.Function;
import javax.xml.xpath.XPathFactory;
-import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.text.StringSubstitutor;
/**
@@ -1351,7 +1350,7 @@ public final class StringLookupFactory {
* @see XPathFactory#setFeature(String, boolean)
* @since 1.11.0
*/
- public StringLookup xmlStringLookup(@SuppressWarnings("unchecked")
Pair<String, Boolean>... xPathFactoryFeatures) {
+ public StringLookup xmlStringLookup(final Map<String, Boolean>
xPathFactoryFeatures) {
return new XmlStringLookup(xPathFactoryFeatures);
}
}
diff --git a/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
b/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
index 2c6c73dc..f3bdd0aa 100644
--- a/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
+++ b/src/main/java/org/apache/commons/text/lookup/XmlStringLookup.java
@@ -20,13 +20,15 @@ package org.apache.commons.text.lookup;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.Objects;
import javax.xml.XMLConstants;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.tuple.Pair;
import org.xml.sax.InputSource;
/**
@@ -45,9 +47,12 @@ final class XmlStringLookup extends AbstractStringLookup {
/**
* Defines default XPath factory features.
*/
- @SuppressWarnings("unchecked")
- private static final Pair<String, Boolean>[] DEFAULT_FEATURES = new Pair[]
{
- Pair.of(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE) };
+ private static final Map<String, Boolean> DEFAULT_FEATURES;
+
+ static {
+ DEFAULT_FEATURES = new HashMap<>(1);
+ DEFAULT_FEATURES.put(XMLConstants.FEATURE_SECURE_PROCESSING,
Boolean.TRUE);
+ }
/**
* Defines the singleton for this class.
@@ -57,7 +62,7 @@ final class XmlStringLookup extends AbstractStringLookup {
/**
* Defines XPath factory features.
*/
- private final Pair<String, Boolean>[] xPathFactoryFeatures;
+ private final Map<String, Boolean> xPathFactoryFeatures;
/**
* No need to build instances for now.
@@ -65,8 +70,7 @@ final class XmlStringLookup extends AbstractStringLookup {
* @param xPathFactoryFeatures XPathFactory features to set.
* @see XPathFactory#setFeature(String, boolean)
*/
- @SafeVarargs
- XmlStringLookup(final Pair<String, Boolean>... xPathFactoryFeatures) {
+ XmlStringLookup(final Map<String, Boolean> xPathFactoryFeatures) {
this.xPathFactoryFeatures =
Objects.requireNonNull(xPathFactoryFeatures, "xPathFfactoryFeatures");
}
@@ -94,7 +98,7 @@ final class XmlStringLookup extends AbstractStringLookup {
final String xpath = StringUtils.substringAfter(key, SPLIT_CH);
try (InputStream inputStream =
Files.newInputStream(Paths.get(documentPath))) {
final XPathFactory factory = XPathFactory.newInstance();
- for (final Pair<String, Boolean> p : xPathFactoryFeatures) {
+ for (final Entry<String, Boolean> p :
xPathFactoryFeatures.entrySet()) {
factory.setFeature(p.getKey(), p.getValue());
}
return factory.newXPath().evaluate(xpath, new
InputSource(inputStream));
diff --git
a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
index 682cdbd9..0529b65f 100644
--- a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java
@@ -22,6 +22,8 @@ import java.util.Map;
import java.util.Properties;
import java.util.Set;
+import javax.xml.XMLConstants;
+
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -228,4 +230,13 @@ public class StringLookupFactoryTest {
Assertions.assertSame(UrlStringLookup.INSTANCE,
stringLookupFactory.urlStringLookup());
Assertions.assertSame(XmlStringLookup.INSTANCE,
stringLookupFactory.xmlStringLookup());
}
+
+ @Test
+ public void testXmlStringLookup() {
+ final StringLookupFactory stringLookupFactory =
StringLookupFactory.INSTANCE;
+ final HashMap<String, Boolean> features = new HashMap<>(1);
+ features.put(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
+
XmlStringLookupTest.assertLookup(stringLookupFactory.xmlStringLookup(features));
+
XmlStringLookupTest.assertLookup(stringLookupFactory.xmlStringLookup(new
HashMap<>()));
+ }
}
diff --git
a/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java
b/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java
index 6a437315..12211597 100644
--- a/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java
+++ b/src/test/java/org/apache/commons/text/lookup/XmlStringLookupTest.java
@@ -17,9 +17,17 @@
package org.apache.commons.text.lookup;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.HashMap;
+
+import javax.xml.XMLConstants;
-import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
@@ -29,6 +37,13 @@ public class XmlStringLookupTest {
private static final String DOC_PATH =
"src/test/resources/org/apache/commons/text/document.xml";
+ static void assertLookup(final StringLookup xmlStringLookup) {
+ assertNotNull(xmlStringLookup);
+ assertTrue(xmlStringLookup instanceof XmlStringLookup);
+ assertEquals("Hello World!", xmlStringLookup.lookup(DOC_PATH +
":/root/path/to/node"));
+ assertNull(xmlStringLookup.lookup(null));
+ }
+
@Test
public void testBadXPath() {
assertThrows(IllegalArgumentException.class, () ->
XmlStringLookup.INSTANCE.lookup("docName"));
@@ -39,21 +54,33 @@ public class XmlStringLookupTest {
assertThrows(IllegalArgumentException.class, () ->
XmlStringLookup.INSTANCE.lookup(DOC_PATH + ":" + "!JUNK!"));
}
+ @Test
+ public void testNoFeatures() {
+ final String xpath = "/root/path/to/node";
+ assertEquals("Hello World!", new XmlStringLookup(new
HashMap<>()).lookup(DOC_PATH + ":" + xpath));
+ }
+
+ @Test
+ public void testNoFeaturesDefault() {
+ final HashMap<String, Boolean> features = new HashMap<>(1);
+ features.put(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
+ assertLookup(new XmlStringLookup(features));
+ }
+
@Test
public void testNull() {
- Assertions.assertNull(XmlStringLookup.INSTANCE.lookup(null));
+ assertNull(XmlStringLookup.INSTANCE.lookup(null));
}
@Test
public void testOne() {
- final String xpath = "/root/path/to/node";
- Assertions.assertEquals("Hello World!",
XmlStringLookup.INSTANCE.lookup(DOC_PATH + ":" + xpath));
+ assertLookup(XmlStringLookup.INSTANCE);
}
@Test
public void testToString() {
// does not blow up and gives some kind of string.
- Assertions.assertFalse(XmlStringLookup.INSTANCE.toString().isEmpty());
+ assertFalse(XmlStringLookup.INSTANCE.toString().isEmpty());
}
}