You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tamaya.apache.org by an...@apache.org on 2015/02/14 17:11:20 UTC
[2/2] incubator-tamaya git commit: Extended ConfigurationFormats.
Added simple tests.
Extended ConfigurationFormats. Added simple tests.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/1c234676
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/1c234676
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/1c234676
Branch: refs/heads/master
Commit: 1c23467613dd77ecc7b473bc32fa7304b4a5e1a4
Parents: ff7e48a
Author: anatole <an...@apache.org>
Authored: Sat Feb 14 17:10:56 2015 +0100
Committer: anatole <an...@apache.org>
Committed: Sat Feb 14 17:10:56 2015 +0100
----------------------------------------------------------------------
modules/formats/pom.xml | 11 +++
...hBasedMultiFormatPropertySourceProvider.java | 5 +-
.../tamaya/format/ConfigurationFormats.java | 16 ++++-
...org.apache.tamaya.format.ConfigurationFormat | 21 ++++++
.../tamaya/format/ConfigurationFormatsTest.java | 70 ++++++++++++++++++++
modules/formats/src/test/resources/Test.ini | 26 ++++++++
.../formats/src/test/resources/Test.properties | 21 ++++++
7 files changed, 165 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1c234676/modules/formats/pom.xml
----------------------------------------------------------------------
diff --git a/modules/formats/pom.xml b/modules/formats/pom.xml
index 65b40f4..39fca56 100644
--- a/modules/formats/pom.xml
+++ b/modules/formats/pom.xml
@@ -41,5 +41,16 @@ under the License.
<artifactId>tamaya-resources</artifactId>
<version>${project.version}</version>
</dependency>
+ <!-- Test scope only, do not create a code dependency! -->
+ <dependency>
+ <groupId>org.apache.tamaya</groupId>
+ <artifactId>tamaya-core</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
</dependencies>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1c234676/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java b/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java
index cb60d6e..32b25fb 100644
--- a/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java
+++ b/modules/formats/src/main/java/org/apache/tamaya/format/BasePathBasedMultiFormatPropertySourceProvider.java
@@ -23,6 +23,7 @@ import org.apache.tamaya.spi.PropertySource;
import org.apache.tamaya.spi.PropertySourceProvider;
import org.apache.tamaya.spi.ServiceContext;
+import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
@@ -108,9 +109,9 @@ public abstract class BasePathBasedMultiFormatPropertySourceProvider implements
for (URL res : resourceResolver.getResources(
this.classLoader.orElse(Thread.currentThread().getContextClassLoader()),
path)) {
- try {
+ try(InputStream is = res.openStream()) {
for (ConfigurationFormat format : configFormats) {
- ConfigurationData entries = format.readConfiguration(res);
+ ConfigurationData entries = format.readConfiguration(res.toString(), is);
propertySources.addAll(getPropertySources(entries));
}
} catch (Exception e) {
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1c234676/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java
----------------------------------------------------------------------
diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java
index 2ec0e0e..4c6f67f 100644
--- a/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java
+++ b/modules/formats/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java
@@ -57,6 +57,16 @@ public class ConfigurationFormats {
}
/**
+ * Get all currently available formats, ordered by priority.
+ *
+ * @return the currently available formats, never null.
+ */
+ public static List<ConfigurationFormat> getFormats(URL url) {
+ List<ConfigurationFormat> formats = getFormats();
+ return formats.stream().filter(f -> f.accepts(url)).collect(Collectors.toList());
+ }
+
+ /**
* Tries to read configuration data from a given URL, hereby traversing all known formats in order of precedence.
* Hereby the formats are first filtered to check if the URL is acceptable, before the input is being parsed.
*
@@ -65,8 +75,7 @@ public class ConfigurationFormats {
* @throws IOException if the resource cannot be read.
*/
public static ConfigurationData readConfigurationData(final URL url) throws IOException{
- List<ConfigurationFormat> formats = getFormats();
- formats = formats.stream().filter(f -> f.accepts(url)).collect(Collectors.toList());
+ List<ConfigurationFormat> formats = getFormats(url);
return readConfigurationData(url, formats.toArray(new ConfigurationFormat[formats.size()]));
}
@@ -99,8 +108,9 @@ public class ConfigurationFormats {
byte[] bytes = new byte[256];
try{
int read = inputStream.read(bytes);
- while(read != 0){
+ while(read > 0){
bos.write(bytes, 0, read);
+ read = inputStream.read(bytes);
}
} finally{
try {
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1c234676/modules/formats/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
----------------------------------------------------------------------
diff --git a/modules/formats/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat b/modules/formats/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
new file mode 100644
index 0000000..96e898f
--- /dev/null
+++ b/modules/formats/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
@@ -0,0 +1,21 @@
+#
+# 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 current 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.
+#
+org.apache.tamaya.format.formats.IniConfigurationFormat
+org.apache.tamaya.format.formats.PropertiesFormat
+org.apache.tamaya.format.formats.PropertiesXmlFormat
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1c234676/modules/formats/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java
----------------------------------------------------------------------
diff --git a/modules/formats/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java b/modules/formats/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java
new file mode 100644
index 0000000..273439b
--- /dev/null
+++ b/modules/formats/src/test/java/org/apache/tamaya/format/ConfigurationFormatsTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.format;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Tests for {@link org.apache.tamaya.format.ConfigurationFormats}.
+ */
+public class ConfigurationFormatsTest {
+
+ @org.junit.Test
+ public void testGetFormats() throws Exception {
+ List<ConfigurationFormat> formats = ConfigurationFormats.getFormats();
+ assertNotNull(formats);
+ assertEquals(formats.size(), 3);
+ }
+
+ @org.junit.Test
+ public void testReadConfigurationData() throws Exception {
+ List<ConfigurationFormat> formats = ConfigurationFormats.getFormats(getClass().getResource("/Test.ini"));
+ assertNotNull(formats);
+ assertEquals(formats.size(), 1);
+ formats = ConfigurationFormats.getFormats(getClass().getResource("/Test.properties"));
+ assertNotNull(formats);
+ assertEquals(formats.size(), 1);
+// formats = ConfigurationFormats.getFormats(getClass().getResource("/Test.xml"));
+// assertNotNull(formats);
+// assertEquals(formats.size(), 1);
+
+ }
+
+ @org.junit.Test
+ public void testReadConfigurationData_URL() throws Exception {
+ ConfigurationData data = ConfigurationFormats.readConfigurationData(getClass().getResource("/Test.ini"));
+ assertNotNull(data);
+ data = ConfigurationFormats.readConfigurationData(getClass().getResource("/Test.properties"));
+ assertNotNull(data);
+// data = ConfigurationFormats.readConfigurationData(getClass().getResource("/Test.xml"));
+// assertNotNull(data);
+ }
+
+ @org.junit.Test
+ public void testReadConfigurationData2() throws Exception {
+ List<ConfigurationFormat> formats = ConfigurationFormats.getFormats();
+ ConfigurationData data = ConfigurationFormats.readConfigurationData(getClass().getResource("/Test.ini"),
+ formats.toArray(new ConfigurationFormat[formats.size()]));
+ assertNotNull(data);
+ System.out.println(data);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1c234676/modules/formats/src/test/resources/Test.ini
----------------------------------------------------------------------
diff --git a/modules/formats/src/test/resources/Test.ini b/modules/formats/src/test/resources/Test.ini
new file mode 100644
index 0000000..906a1e6
--- /dev/null
+++ b/modules/formats/src/test/resources/Test.ini
@@ -0,0 +1,26 @@
+#
+# 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 current 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.
+#
+
+aGeneralEntry=blabla
+
+[MySection1]
+sectionEntry1=value1
+
+[MySection2]
+sectionEntry2=value2
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/1c234676/modules/formats/src/test/resources/Test.properties
----------------------------------------------------------------------
diff --git a/modules/formats/src/test/resources/Test.properties b/modules/formats/src/test/resources/Test.properties
new file mode 100644
index 0000000..ced544b
--- /dev/null
+++ b/modules/formats/src/test/resources/Test.properties
@@ -0,0 +1,21 @@
+#
+# 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 current 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.
+#
+aGeneralEntry=blabla
+MySection1.sectionEntry1=value1
+MySection2.sectionEntry2=value2
\ No newline at end of file