tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ple...@apache.org
Subject incubator-tamaya git commit: TAMAYA-166 Tamaya core now also supports XML based property files.
Date Tue, 28 Jun 2016 20:10:04 GMT
Repository: incubator-tamaya
Updated Branches:
  refs/heads/master ebb31aa32 -> 441f26116


TAMAYA-166 Tamaya core now also supports XML based property files.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/441f2611
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/441f2611
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/441f2611

Branch: refs/heads/master
Commit: 441f2611673cda0882ef6091abde1f6504c0f2cb
Parents: ebb31aa
Author: Oliver B. Fischer <plexus@apache.org>
Authored: Tue Jun 28 22:09:25 2016 +0200
Committer: Oliver B. Fischer <plexus@apache.org>
Committed: Tue Jun 28 22:09:25 2016 +0200

----------------------------------------------------------------------
 code/core/pom.xml                               |  4 +
 .../propertysource/SimplePropertySource.java    | 27 ++++--
 .../provider/JavaConfigurationProvider.java     | 69 +++++++++++----
 .../SimplePropertySourceTest.java               | 88 ++++++++++++++++++++
 .../provider/JavaConfigurationProviderTest.java | 28 ++++---
 .../resources/META-INF/javaconfiguration.xml    | 25 ++++++
 .../src/test/resources/invalid-properties.xml   | 25 ++++++
 .../src/test/resources/non-xml-properties.xml   | 18 ++++
 .../src/test/resources/valid-properties.xml     | 25 ++++++
 src/site/asciidoc/Core.adoc                     |  5 +-
 10 files changed, 280 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/pom.xml
----------------------------------------------------------------------
diff --git a/code/core/pom.xml b/code/core/pom.xml
index 673b22f..109681f 100644
--- a/code/core/pom.xml
+++ b/code/core/pom.xml
@@ -62,6 +62,10 @@ under the License.
             <scope>provided</scope>
             <optional>true</optional>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
----------------------------------------------------------------------
diff --git a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
b/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
index 8268a72..e875d8c 100644
--- a/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
+++ b/code/core/src/main/java/org/apache/tamaya/core/propertysource/SimplePropertySource.java
@@ -31,15 +31,18 @@ import java.util.Properties;
 import java.util.logging.Logger;
 
 /**
- * Simple implementation of a {@link org.apache.tamaya.spi.PropertySource} for properties-files.
+ * Simple implementation of a {@link org.apache.tamaya.spi.PropertySource} for
+ * simple property files and XML property files.
  */
 public class SimplePropertySource extends BasePropertySource {
 
     private static final Logger LOG = Logger.getLogger(SimplePropertySource.class.getName());
+
     /**
      * The property source name.
      */
     private String name;
+
     /**
      * The current properties.
      */
@@ -113,24 +116,36 @@ public class SimplePropertySource extends BasePropertySource {
      * @throws IllegalStateException in case of an error while reading properties-file
      */
     private Map<String, String> load(URL propertiesFile) {
+        boolean isXML = isXMLPropertieFiles(propertiesFile);
+
         Map<String, String> properties = new HashMap<>();
         try (InputStream stream = propertiesFile.openStream()) {
             Properties props = new Properties();
             if (stream != null) {
-                props.load(stream);
+                if (isXML) {
+                    props.loadFromXML(stream);
+                } else {
+                    props.load(stream);
+                }
             }
+
             for (String key : props.stringPropertyNames()) {
                 properties.put(key, props.getProperty(key));
-                if(getName()==null){
-                    LOG.warning("No Property Source name found for " + this +", ommitting
source meta-entries.");
-                }else {
+                if (getName() == null){
+                    LOG.warning("No property source name found for " + this +", ommitting
source meta-entries.");
+                } else {
                     properties.put("_" + key + ".source", getName());
                 }
             }
         } catch (IOException e) {
-            throw new ConfigException("Error loading properties " + propertiesFile, e);
+            throw new ConfigException("Error loading properties from " + propertiesFile,
e);
         }
+
         return properties;
     }
 
+    private boolean isXMLPropertieFiles(URL url) {
+        return url.getFile().endsWith(".xml");
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
----------------------------------------------------------------------
diff --git a/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
b/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
index 331f88a..79017a5 100644
--- a/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
+++ b/code/core/src/main/java/org/apache/tamaya/core/provider/JavaConfigurationProvider.java
@@ -31,14 +31,26 @@ import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
 
+import static java.lang.String.format;
+import static java.lang.Thread.currentThread;
+
 /**
- * Provider which reads all {@code javaconfiguration.properties} files from classpath. By
setting
- * {@code tamaya.defaultprops.disable} or {@code tamaya.defaults.disable} as system or environment
property this feature
- * can be disabled.
+ * Provider which reads all {@value DEFAULT_SIMPLE_PROPERTIES_FILE_NAME} and
+ * {@value DEFAULT_XML_PROPERTIES_FILE_NAME} files found in the
+ * classpath. By setting
+ * {@code tamaya.defaultprops.disable} or {@code tamaya.defaults.disable}
+ * as system or environment property this feature can be disabled.
  */
 public class JavaConfigurationProvider implements PropertySourceProvider {
-    /** Default location in the classpath, where Tamaya looks for configuration by default.
*/
-    public static final String DEFAULT_PROPERTIES_FILE_NAME="META-INF/javaconfiguration.properties";
+    /**
+     * Default location in the classpath, where Tamaya looks for simple line based configuration
by default.
+     */
+    public static final String DEFAULT_SIMPLE_PROPERTIES_FILE_NAME="META-INF/javaconfiguration.properties";
+
+    /**
+     * Default location in the classpath, where Tamaya looks for XML based configuration
by default.
+     */
+    public static final String DEFAULT_XML_PROPERTIES_FILE_NAME = "META-INF/javaconfiguration.xml";
 
     private final boolean disabled = evaluateDisabled();
 
@@ -61,24 +73,49 @@ public class JavaConfigurationProvider implements PropertySourceProvider
{
 
     @Override
     public Collection<PropertySource> getPropertySources() {
-        if(disabled){
+        if (isDisabled()) {
             return Collections.emptySet();
         }
+
         List<PropertySource> propertySources = new ArrayList<>();
+
+        propertySources.addAll(loadPropertySourcesByName(DEFAULT_SIMPLE_PROPERTIES_FILE_NAME));
+        propertySources.addAll(loadPropertySourcesByName(DEFAULT_XML_PROPERTIES_FILE_NAME));
+
+        return Collections.unmodifiableList(propertySources);
+    }
+
+    private Collection<? extends PropertySource> loadPropertySourcesByName(String filename)
{
+        URL currentUrl = null;
+
+        List<PropertySource> propertySources = new ArrayList<>();
+        Enumeration<URL> propertyLocations = Collections.emptyEnumeration();
+
         try {
-            ClassLoader cl = Thread.currentThread().getContextClassLoader();
-            if(cl!=null) {
-                Enumeration<URL> urls = Thread.currentThread().getContextClassLoader()
-                        .getResources(DEFAULT_PROPERTIES_FILE_NAME);
-                while (urls.hasMoreElements()) {
-                    propertySources.add(new SimplePropertySource(urls.nextElement()));
-                }
-            }
+            ClassLoader cl = currentThread().getContextClassLoader();
 
+            if (cl != null) {
+                propertyLocations = currentThread().getContextClassLoader()
+                                                   .getResources(filename);
+            }
         } catch (IOException e) {
-            throw new ConfigException("Error while loading javaconfiguration.properties",
e);
+            String msg = format("Error while searching for %s", filename);
+
+            throw new ConfigException(msg, e);
         }
-        return Collections.unmodifiableList(propertySources);
+
+        while (propertyLocations.hasMoreElements()) {
+            currentUrl = propertyLocations.nextElement();
+            SimplePropertySource sps = new SimplePropertySource(currentUrl);
+
+            propertySources.add(sps);
+        }
+
+        return propertySources;
+    }
+
+    protected boolean isDisabled() {
+        return disabled;
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/src/test/java/org/apache/tamaya/core/propertysource/SimplePropertySourceTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/propertysource/SimplePropertySourceTest.java
b/code/core/src/test/java/org/apache/tamaya/core/propertysource/SimplePropertySourceTest.java
new file mode 100644
index 0000000..064611c
--- /dev/null
+++ b/code/core/src/test/java/org/apache/tamaya/core/propertysource/SimplePropertySourceTest.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tamaya.core.propertysource;
+
+import org.apache.tamaya.ConfigException;
+import org.junit.Test;
+
+import java.net.URL;
+
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.endsWith;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.aMapWithSize;
+import static org.hamcrest.Matchers.hasEntry;
+
+public class SimplePropertySourceTest {
+    @Test
+    public void successfulCreationWithPropertiesFromXMLPropertiesFile() {
+        URL resource = getClass().getResource("/valid-properties.xml");
+
+        SimplePropertySource source = new SimplePropertySource(resource);
+
+        assertThat(source, notNullValue());
+        assertThat(source.getProperties(), aMapWithSize(2));
+        assertThat(source.getProperties(), hasEntry("a", "b"));
+        assertThat(source.getProperties(), hasEntry("b", "1"));
+
+    }
+
+    @Test
+    public void failsToCreateFromNonXMLPropertiesXMLFile() {
+        URL resource = getClass().getResource("/non-xml-properties.xml");
+        ConfigException catchedException = null;
+
+        try {
+            new SimplePropertySource(resource);
+        } catch (ConfigException ce) {
+            catchedException = ce;
+        }
+
+        assertThat(catchedException.getMessage(), allOf(startsWith("Error loading properties
from"),
+                                                        endsWith("non-xml-properties.xml")));
+    }
+
+    @Test
+    public void failsToCreateFromInvalidPropertiesXMLFile() {
+        URL resource = getClass().getResource("/invalid-properties.xml");
+        ConfigException catchedException = null;
+
+        try {
+            new SimplePropertySource(resource);
+        } catch (ConfigException ce) {
+            catchedException = ce;
+        }
+
+        assertThat(catchedException.getMessage(), allOf(startsWith("Error loading properties
from"),
+                                                        endsWith("invalid-properties.xml")));
+    }
+
+
+    @Test
+    public void successfulCreationWithPropertiesFromSimplePropertiesFile() {
+        URL resource = getClass().getResource("/testfile.properties");
+
+        SimplePropertySource source = new SimplePropertySource(resource);
+
+        assertThat(source, notNullValue());
+        assertThat(source.getProperties(), aMapWithSize(5));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java
----------------------------------------------------------------------
diff --git a/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java
b/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java
index 0e0b91a..3ca3478 100644
--- a/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java
+++ b/code/core/src/test/java/org/apache/tamaya/core/provider/JavaConfigurationProviderTest.java
@@ -18,43 +18,49 @@
  */
 package org.apache.tamaya.core.provider;
 
-import org.apache.tamaya.ConfigurationProvider;
-import org.apache.tamaya.core.provider.JavaConfigurationProvider;
 import org.apache.tamaya.spi.PropertySource;
-import org.junit.Assert;
 import org.junit.Test;
 
 import java.util.Collection;
 
+import static org.apache.tamaya.ConfigurationProvider.getConfiguration;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
 
 public class JavaConfigurationProviderTest {
 
-    @Test
-    public void testJavaConfigurationProvider() {
+    private static final String A_UMLAUT = "\u00E4";
+    private static final String O_UMLAUT = "\u00F6";
 
+    @Test
+    public void loadsSimpleAndXMLPropertyFilesProper() {
         Collection<PropertySource> propertySources = new JavaConfigurationProvider().getPropertySources();
 
         assertThat(propertySources, notNullValue());
-        assertThat(propertySources, hasSize(1));
+        assertThat(propertySources, hasSize(2));
 
         PropertySource propertySource = propertySources.iterator().next();
 
-        assertThat(propertySource.getProperties().keySet(), hasSize(5));  // 5 entries +
5 metaentries
+        assertThat(propertySource.getProperties().keySet(), hasSize(5));
 
         for (int i = 1; i < 6; i++) {
             String key = "confkey" + i;
             String value = "javaconf-value" + i;
 
-            Assert.assertEquals(value, propertySource.get(key).get(key));
+            assertThat(value, equalTo(propertySource.get(key).get(key)));
 
             // check if we had our key in configuration.current
-            Assert.assertTrue(ConfigurationProvider.getConfiguration().getProperties().containsKey(key));
-            Assert.assertEquals(value, ConfigurationProvider.getConfiguration().get(key));
+            assertThat(getConfiguration().getProperties().containsKey(key), is(true));
+            assertThat(value, equalTo(getConfiguration().get(key)));
         }
 
-    }
+        assertThat(getConfiguration().getProperties().containsKey("aaeehh"), is(true));
+        assertThat(getConfiguration().getProperties().get("aaeehh"), equalTo(A_UMLAUT));
 
+        assertThat(getConfiguration().getProperties().containsKey(O_UMLAUT), is(true));
+        assertThat(getConfiguration().getProperties().get(O_UMLAUT), equalTo("o"));
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/src/test/resources/META-INF/javaconfiguration.xml
----------------------------------------------------------------------
diff --git a/code/core/src/test/resources/META-INF/javaconfiguration.xml b/code/core/src/test/resources/META-INF/javaconfiguration.xml
new file mode 100644
index 0000000..f6cdc97
--- /dev/null
+++ b/code/core/src/test/resources/META-INF/javaconfiguration.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+<properties>
+    <entry key="aaeehh">ä</entry>
+    <entry key="ö">o</entry>
+</properties>

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/src/test/resources/invalid-properties.xml
----------------------------------------------------------------------
diff --git a/code/core/src/test/resources/invalid-properties.xml b/code/core/src/test/resources/invalid-properties.xml
new file mode 100644
index 0000000..d8b10b7
--- /dev/null
+++ b/code/core/src/test/resources/invalid-properties.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+<properties>
+    <entry key="a">
+    <entry key="b">1</entry>
+</properties>

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/src/test/resources/non-xml-properties.xml
----------------------------------------------------------------------
diff --git a/code/core/src/test/resources/non-xml-properties.xml b/code/core/src/test/resources/non-xml-properties.xml
new file mode 100644
index 0000000..8de819a
--- /dev/null
+++ b/code/core/src/test/resources/non-xml-properties.xml
@@ -0,0 +1,18 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/code/core/src/test/resources/valid-properties.xml
----------------------------------------------------------------------
diff --git a/code/core/src/test/resources/valid-properties.xml b/code/core/src/test/resources/valid-properties.xml
new file mode 100644
index 0000000..7eb51d9
--- /dev/null
+++ b/code/core/src/test/resources/valid-properties.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
+<properties>
+    <entry key="a">b</entry>
+    <entry key="b">1</entry>
+</properties>

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/441f2611/src/site/asciidoc/Core.adoc
----------------------------------------------------------------------
diff --git a/src/site/asciidoc/Core.adoc b/src/site/asciidoc/Core.adoc
index 8e4edfe..e1d223c 100644
--- a/src/site/asciidoc/Core.adoc
+++ b/src/site/asciidoc/Core.adoc
@@ -138,6 +138,7 @@ applications already easily. Basically configuration is built  up by default
as
 
 . Read environment properties and add them prefixed with +env.+
 . Read all files found at +META-INF/javaconfiguration.properties+
+  and +META-INF/javaconfiguration.xml+
 
 
 === Overview of Registered Default Property Sources and Providers
@@ -150,6 +151,7 @@ registered. They are all in the package +org.apache.tamaya.core.propertysource+
 |=======
 |_Type_                                   |_Class Name_                   |_Ordinal Used_
 |META-INF/javaconfiguration.properties    |JavaConfigurationProvider      |0
+|META-INF/javaconfiguration.xml           |JavaConfigurationProvider      |0
 |Environment Properties                   |EnvironmentPropertySource      |300
 |System Properties                        |SystemPropertySource           |400
 |=======
@@ -211,7 +213,8 @@ the inherited +protected void initializeOrdinal(final int defaultOrdinal)+,
or d
 === Default PropertySourceProvider in Core
 
 With +org.apache.tamaya.core.provider.JavaConfigurationProvider+ there is also a default
+PropertySourceProvider+
-present that loads all .properties files found at +META-INF/javaconfiguration.properties+.
+present that loads all .properties files found at +META-INF/javaconfiguration.properties+
+and +META-INF/javaconfiguration.xml+.
 
 
 [[Extensions]]


Mime
View raw message