From d5bf76870f507944d5e0e68ce77d8ac974968c03 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 16 Feb 2020 20:42:27 -0500 Subject: [PATCH] [COLLECTIONS-748] Let org.apache.commons.collections4.properties.[Sorted]PropertiesFactory accept XML input. --- src/changes/changes.xml | 3 ++ .../properties/AbstractPropertiesFactory.java | 52 ++++++++++++++++-- .../AbstractPropertiesFactoryTest.java | 54 +++++++++++++++---- .../properties/PropertiesFactoryTest.java | 4 +- .../SortedPropertiesFactoryTest.java | 4 +- src/test/resources/properties/test.xml | 32 +++++++++++ 6 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 src/test/resources/properties/test.xml diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5ad551741..578ed5a04 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -132,6 +132,9 @@ MultiKey.getKeys class cast exception. + + Let org.apache.commons.collections4.properties.[Sorted]PropertiesFactory accept XML input. + diff --git a/src/main/java/org/apache/commons/collections4/properties/AbstractPropertiesFactory.java b/src/main/java/org/apache/commons/collections4/properties/AbstractPropertiesFactory.java index 7c89f604e..249905b69 100644 --- a/src/main/java/org/apache/commons/collections4/properties/AbstractPropertiesFactory.java +++ b/src/main/java/org/apache/commons/collections4/properties/AbstractPropertiesFactory.java @@ -28,6 +28,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Objects; import java.util.Properties; /** @@ -39,6 +40,24 @@ import java.util.Properties; */ public abstract class AbstractPropertiesFactory { + /** + * Enumerat5es property formats. + * + * @since 4.5 + */ + public enum PropertyFormat { + + /** Properties file format. */ + PROPERTIES, + + /** XML file format. */ + XML; + + static PropertyFormat toPropertyFormat(String fileName) { + return Objects.requireNonNull(fileName, "fileName").endsWith(".xml") ? XML : PROPERTIES; + } + } + /** * Constructs an instance. */ @@ -64,7 +83,7 @@ public abstract class AbstractPropertiesFactory { */ public T load(final ClassLoader classLoader, final String name) throws IOException { try (final InputStream inputStream = classLoader.getResourceAsStream(name)) { - return load(inputStream); + return load(inputStream, PropertyFormat.toPropertyFormat(name)); } } @@ -82,7 +101,7 @@ public abstract class AbstractPropertiesFactory { */ public T load(final File file) throws FileNotFoundException, IOException { try (final FileInputStream inputStream = new FileInputStream(file)) { - return load(inputStream); + return load(inputStream, PropertyFormat.toPropertyFormat(file.getName())); } } @@ -103,6 +122,29 @@ public abstract class AbstractPropertiesFactory { return properties; } + /** + * Creates and loads properties from the given input stream. + * + * @param inputStream the location of the properties file. + * @param propertyFormat The format of the given file. + * @return a new properties object. + * @throws IOException Thrown if an error occurred reading the input stream. + * @throws IllegalArgumentException Thrown if the input contains a malformed Unicode escape sequence. + * @since 4.5 + */ + public T load(final InputStream inputStream, final PropertyFormat propertyFormat) throws IOException { + if (inputStream == null) { + return null; + } + final T properties = createProperties(); + if (propertyFormat == PropertyFormat.XML) { + properties.loadFromXML(inputStream); + } else { + properties.load(inputStream); + } + return properties; + } + /** * Creates and loads properties from the given path. * @@ -113,7 +155,7 @@ public abstract class AbstractPropertiesFactory { */ public T load(final Path path) throws IOException { try (final InputStream inputStream = Files.newInputStream(path)) { - return load(inputStream); + return load(inputStream, PropertyFormat.toPropertyFormat(Objects.toString(path.getFileName(), null))); } } @@ -141,7 +183,7 @@ public abstract class AbstractPropertiesFactory { */ public T load(final String name) throws IOException { try (final FileInputStream inputStream = new FileInputStream(name)) { - return load(inputStream); + return load(inputStream, PropertyFormat.toPropertyFormat(name)); } } @@ -167,7 +209,7 @@ public abstract class AbstractPropertiesFactory { */ public T load(final URL url) throws IOException { try (final InputStream inputStream = url.openStream()) { - return load(inputStream); + return load(inputStream, PropertyFormat.toPropertyFormat(url.getFile())); } } diff --git a/src/test/java/org/apache/commons/collections4/properties/AbstractPropertiesFactoryTest.java b/src/test/java/org/apache/commons/collections4/properties/AbstractPropertiesFactoryTest.java index 3e68a65b9..2ea6a0700 100644 --- a/src/test/java/org/apache/commons/collections4/properties/AbstractPropertiesFactoryTest.java +++ b/src/test/java/org/apache/commons/collections4/properties/AbstractPropertiesFactoryTest.java @@ -24,22 +24,48 @@ import java.nio.file.Paths; import java.util.Properties; import org.junit.Assert; +import org.junit.Assume; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +@RunWith(Parameterized.class) public abstract class AbstractPropertiesFactoryTest { - private static final String PATH_STRING = "src/test/resources/properties/test.properties"; + @Parameters(name = "{0}") + public static Object[][] getParameters() { + return new Object[][] { { ".properties" }, { ".xml" } }; + + } private final AbstractPropertiesFactory factory; + private final String pathString; + private final String fileExtention; - protected AbstractPropertiesFactoryTest(final AbstractPropertiesFactory factory) { + protected AbstractPropertiesFactoryTest(final AbstractPropertiesFactory factory, final String fileExtension) { super(); this.factory = factory; + this.fileExtention = fileExtension; + this.pathString = "src/test/resources/properties/test" + fileExtention; } private void assertContents(final T properties) { Assert.assertEquals("value1", properties.getProperty("key1")); Assert.assertEquals("value2", properties.getProperty("key2")); + Assert.assertEquals("value3", properties.getProperty("key3")); + Assert.assertEquals("value4", properties.getProperty("key4")); + Assert.assertEquals("value5", properties.getProperty("key5")); + Assert.assertEquals("value6", properties.getProperty("key6")); + Assert.assertEquals("value7", properties.getProperty("key7")); + Assert.assertEquals("value8", properties.getProperty("key8")); + Assert.assertEquals("value9", properties.getProperty("key9")); + Assert.assertEquals("value10", properties.getProperty("key10")); + Assert.assertEquals("value11", properties.getProperty("key11")); + } + + private boolean isXmlTest() { + return ".xml".equals(fileExtention); } @Test @@ -49,50 +75,56 @@ public abstract class AbstractPropertiesFactoryTest { @Test public void testLoadClassLoaderMissingResource() throws Exception { - Assert.assertNull(factory.load(ClassLoader.getSystemClassLoader(), "missing/test.properties")); + Assert.assertNull(factory.load(ClassLoader.getSystemClassLoader(), "missing/test" + fileExtention)); } @Test public void testLoadClassLoaderResource() throws Exception { - assertContents(factory.load(ClassLoader.getSystemClassLoader(), "properties/test.properties")); + assertContents(factory.load(ClassLoader.getSystemClassLoader(), "properties/test" + fileExtention)); } @Test public void testLoadFile() throws Exception { - assertContents(factory.load(Paths.get(PATH_STRING).toFile())); + assertContents(factory.load(Paths.get(pathString).toFile())); } @Test public void testLoadFileName() throws Exception { - assertContents(factory.load(PATH_STRING)); + assertContents(factory.load(pathString)); } @Test public void testLoadInputStream() throws Exception { - try (final FileInputStream inputStream = new FileInputStream(PATH_STRING)) { + // Can't tell what we are reading + Assume.assumeFalse(isXmlTest()); + // + try (final FileInputStream inputStream = new FileInputStream(pathString)) { assertContents(factory.load(inputStream)); } } @Test public void testLoadPath() throws Exception { - assertContents(factory.load(Paths.get(PATH_STRING))); + assertContents(factory.load(Paths.get(pathString))); } @Test public void testLoadReader() throws Exception { - try (final BufferedReader inputStream = Files.newBufferedReader(Paths.get(PATH_STRING))) { + // Can't tell what we are reading + Assume.assumeFalse(isXmlTest()); + // + try (final BufferedReader inputStream = Files.newBufferedReader(Paths.get(pathString))) { assertContents(factory.load(inputStream)); } } @Test public void testLoadUri() throws Exception { - assertContents(factory.load(Paths.get(PATH_STRING).toUri())); + assertContents(factory.load(Paths.get(pathString).toUri())); } @Test public void testLoadUrl() throws Exception { - assertContents(factory.load(Paths.get(PATH_STRING).toUri().toURL())); + assertContents(factory.load(Paths.get(pathString).toUri().toURL())); } } diff --git a/src/test/java/org/apache/commons/collections4/properties/PropertiesFactoryTest.java b/src/test/java/org/apache/commons/collections4/properties/PropertiesFactoryTest.java index 83133d06d..ddfe40aac 100644 --- a/src/test/java/org/apache/commons/collections4/properties/PropertiesFactoryTest.java +++ b/src/test/java/org/apache/commons/collections4/properties/PropertiesFactoryTest.java @@ -24,8 +24,8 @@ import org.junit.Test; public class PropertiesFactoryTest extends AbstractPropertiesFactoryTest { - public PropertiesFactoryTest() { - super(PropertiesFactory.INSTANCE); + public PropertiesFactoryTest(final String fileExtension) { + super(PropertiesFactory.INSTANCE, fileExtension); } @Override diff --git a/src/test/java/org/apache/commons/collections4/properties/SortedPropertiesFactoryTest.java b/src/test/java/org/apache/commons/collections4/properties/SortedPropertiesFactoryTest.java index 19e1429ff..8f81612ee 100644 --- a/src/test/java/org/apache/commons/collections4/properties/SortedPropertiesFactoryTest.java +++ b/src/test/java/org/apache/commons/collections4/properties/SortedPropertiesFactoryTest.java @@ -22,8 +22,8 @@ import org.junit.Test; public class SortedPropertiesFactoryTest extends AbstractPropertiesFactoryTest { - public SortedPropertiesFactoryTest() { - super(SortedPropertiesFactory.INSTANCE); + public SortedPropertiesFactoryTest(final String fileExtension) { + super(SortedPropertiesFactory.INSTANCE, fileExtension); } @Override diff --git a/src/test/resources/properties/test.xml b/src/test/resources/properties/test.xml new file mode 100644 index 000000000..8e7699ee6 --- /dev/null +++ b/src/test/resources/properties/test.xml @@ -0,0 +1,32 @@ + + + + + Hello comment! + value1 + value2 + value3 + value4 + value5 + value6 + value7 + value8 + value9 + value10 + value11 +