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/05/21 10:17:05 UTC
incubator-tamaya git commit: Added tests.
Repository: incubator-tamaya
Updated Branches:
refs/heads/master d941df7fc -> ef82b70ad
Added 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/ef82b70a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/ef82b70a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/ef82b70a
Branch: refs/heads/master
Commit: ef82b70ad16cc10df97d4964d318dad7818537a3
Parents: d941df7
Author: anatole <an...@apache.org>
Authored: Thu May 21 10:16:57 2015 +0200
Committer: anatole <an...@apache.org>
Committed: Thu May 21 10:16:57 2015 +0200
----------------------------------------------------------------------
.../format/FlattenedDefaultPropertySource.java | 42 +++++----
.../FlattenedDefaultPropertySourceTest.java | 93 ++++++++++++++++++++
2 files changed, 120 insertions(+), 15 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ef82b70a/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java b/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java
index 0e91ec3..619f559 100644
--- a/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java
+++ b/modules/formats/src/main/java/org/apache/tamaya/format/FlattenedDefaultPropertySource.java
@@ -21,6 +21,7 @@ package org.apache.tamaya.format;
import org.apache.tamaya.spi.PropertySource;
import java.util.Collections;
+import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -31,7 +32,7 @@ import java.util.logging.Logger;
*/
public class FlattenedDefaultPropertySource implements PropertySource {
private static final Logger LOG = Logger.getLogger(FlattenedDefaultPropertySource.class.getName());
- private Map<String, String> defaultSection;
+ private Map<String, String> properties;
private ConfigurationData data;
private int defaultOrdinal = 0;
@@ -41,11 +42,7 @@ public class FlattenedDefaultPropertySource implements PropertySource {
* ${@link org.apache.tamaya.format.ConfigurationFormat}, and if not present falls back to the default section.
*/
public FlattenedDefaultPropertySource(ConfigurationData data) {
- this.defaultSection = data.getSection(ConfigurationData.FLATTENED_SECTION_NAME);
- if (this.defaultSection == null) {
- this.defaultSection = data.getDefaultSection();
- }
- this.defaultSection = Collections.unmodifiableMap(this.defaultSection);
+ this.properties = populateData(data);
this.data = data;
}
@@ -54,18 +51,33 @@ public class FlattenedDefaultPropertySource implements PropertySource {
* ${@link org.apache.tamaya.format.ConfigurationFormat}, and if not present falls back to the default section.
*/
public FlattenedDefaultPropertySource(int defaultOrdinal, ConfigurationData data) {
- this.defaultSection = data.getSection(ConfigurationData.FLATTENED_SECTION_NAME);
- if (this.defaultSection == null) {
- this.defaultSection = data.getDefaultSection();
- }
- this.defaultSection = Collections.unmodifiableMap(this.defaultSection);
+ this.properties = populateData(data);
this.data = data;
this.defaultOrdinal = defaultOrdinal;
}
+ private Map<String, String> populateData(ConfigurationData data) {
+ Map<String, String> result = data.getSection(ConfigurationData.FLATTENED_SECTION_NAME);
+ if (result == null) {
+ result = data.getDefaultSection();
+ }
+ if (result == null) {
+ result = new HashMap<>();
+ }
+ if(result.isEmpty()){
+ for(String section:data.getSections()){
+ Map<String,String> sectionMap = data.getSection(section);
+ for(Map.Entry<String,String> en: sectionMap.entrySet()){
+ result.put(section + '.' + en.getKey(), en.getValue());
+ }
+ }
+ }
+ return Collections.unmodifiableMap(result);
+ }
+
@Override
public String getName() {
- String name = this.defaultSection.get("[meta].name");
+ String name = this.properties.get("[meta].name");
if (name == null) {
name = this.data.getResource();
}
@@ -77,7 +89,7 @@ public class FlattenedDefaultPropertySource implements PropertySource {
@Override
public int getOrdinal() {
- String ordinalValue = this.defaultSection.get(PropertySource.TAMAYA_ORDINAL);
+ String ordinalValue = this.properties.get(PropertySource.TAMAYA_ORDINAL);
if (ordinalValue != null) {
try {
return Integer.parseInt(ordinalValue.trim());
@@ -90,11 +102,11 @@ public class FlattenedDefaultPropertySource implements PropertySource {
@Override
public String get(String key) {
- return defaultSection.get(key);
+ return properties.get(key);
}
@Override
public Map<String, String> getProperties() {
- return defaultSection;
+ return properties;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/ef82b70a/modules/formats/src/test/java/org/apache/tamaya/format/FlattenedDefaultPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/modules/formats/src/test/java/org/apache/tamaya/format/FlattenedDefaultPropertySourceTest.java b/modules/formats/src/test/java/org/apache/tamaya/format/FlattenedDefaultPropertySourceTest.java
new file mode 100644
index 0000000..c39de4e
--- /dev/null
+++ b/modules/formats/src/test/java/org/apache/tamaya/format/FlattenedDefaultPropertySourceTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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 org.apache.tamaya.format.formats.PropertiesFormat;
+import org.apache.tamaya.spi.PropertySource;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Tests for {@link org.apache.tamaya.format.FlattenedDefaultPropertySource}.
+ */
+public class FlattenedDefaultPropertySourceTest {
+
+ @Test
+ public void testGetName() throws Exception {
+ FlattenedDefaultPropertySource ps = new FlattenedDefaultPropertySource(createConfigurationData("test1"));
+ assertEquals("test1", ps.getName());
+ }
+
+ private ConfigurationData createConfigurationData(String sourceName) {
+ return ConfigurationDataBuilder.of(sourceName, new PropertiesFormat())
+ .addProperty("a", "aValue").addProperty("section1", "sectionKey1", "sectionValue11")
+ .addSections("section1", "section12")
+ .addProperty("section2", "sectionKey1", "sectionValue21").build();
+ }
+
+ private ConfigurationData createConfigurationData(String sourceName, int ordinal) {
+ return ConfigurationDataBuilder.of(sourceName, new PropertiesFormat())
+ .addProperty("a", "aValue").addProperty("section1", "sectionKey1", "sectionValue11")
+ .addSections("section1", "section12").addProperty(PropertySource.TAMAYA_ORDINAL, String.valueOf(ordinal))
+ .addProperty("section2", "sectionKey1", "sectionValue21").build();
+ }
+
+ private ConfigurationData createConfigurationDataNoDefault(String sourceName) {
+ return ConfigurationDataBuilder.of(sourceName, new PropertiesFormat())
+ .addProperty("section1", "sectionKey1", "sectionValue11")
+ .addSections("section1", "section12")
+ .addProperty("section2", "sectionKey1", "sectionValue21").build();
+ }
+
+ @Test
+ public void testGetOrdinal() throws Exception {
+ FlattenedDefaultPropertySource ps = new FlattenedDefaultPropertySource(createConfigurationData("test1", 11));
+ assertEquals(11, ps.getOrdinal());
+ }
+
+ @Test
+ public void testGet() throws Exception {
+ FlattenedDefaultPropertySource ps = new FlattenedDefaultPropertySource(createConfigurationData("test2"));
+ assertEquals("aValue", ps.get("a"));
+ assertNull(ps.get("section1.sectionKey1"));
+ assertNull(ps.get("section2.sectionKey1"));
+ ps = new FlattenedDefaultPropertySource(createConfigurationDataNoDefault("test2"));
+ assertEquals("sectionValue11", ps.get("section1.sectionKey1"));
+ assertEquals("sectionValue21", ps.get("section2.sectionKey1"));
+ assertNull(ps.get("a"));
+ assertNull(ps.get("section1"));
+ }
+
+ @Test
+ public void testGetProperties() throws Exception {
+ FlattenedDefaultPropertySource ps = new FlattenedDefaultPropertySource(createConfigurationData("test3"));
+ assertNotNull(ps.getProperties());
+ assertEquals("aValue", ps.getProperties().get("a"));
+ assertNull(ps.getProperties().get("section1.sectionKey1"));
+ assertNull(ps.getProperties().get("section2.sectionKey1"));
+ ps = new FlattenedDefaultPropertySource(createConfigurationDataNoDefault("test3"));
+ assertNotNull(ps.getProperties());
+ assertEquals("sectionValue11", ps.getProperties().get("section1.sectionKey1"));
+ assertEquals("sectionValue21", ps.getProperties().get("section2.sectionKey1"));
+ assertNull(ps.get("section1"));
+ }
+}
\ No newline at end of file