Am 07.01.2015 um 21:56 schrieb [email protected]:
Repository: incubator-tamaya
Updated Branches:
refs/heads/master 5f109da47 -> 7985c039c
TAMAYA-39 Added support for a configuration specific priority.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/7985c039
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/7985c039
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/7985c039
Branch: refs/heads/master
Commit: 7985c039ca7dec7e4b5a75057b4ea3d6e40e9262
Parents: 5f109da
Author: Oliver B. Fischer <[email protected]>
Authored: Wed Jan 7 21:48:47 2015 +0100
Committer: Oliver B. Fischer <[email protected]>
Committed: Wed Jan 7 21:48:47 2015 +0100
----------------------------------------------------------------------
.../tamaya/modules/json/JSONPropertySource.java | 32 +++++++++++---
.../modules/json/JSONPropertySourceTest.java | 44 ++++++++++++++++++++
.../configs/valid/with-explicit-priority.json | 4 ++
3 files changed, 75 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7985c039/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java
----------------------------------------------------------------------
diff --git
a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java
b/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java
index 54b8e4c..bff8004 100644
---
a/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java
+++
b/modules/json/src/main/java/org/apache/tamaya/modules/json/JSONPropertySource.java
@@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.core.propertysource.DefaultOrdinal;
import org.apache.tamaya.spi.PropertySource;
import java.io.File;
@@ -36,10 +37,21 @@ import static java.lang.String.format;
public class JSONPropertySource
implements PropertySource {
- private int priority = 0;
+
+ /**
+ * Constant for the key which could be used to specify the priority
+ * of a property source in the JSON object directly.
+ */
+ public final static CharSequence TAMAYA_ORDINAL = "tamaya.ordinal";
+
+ private int priority = DefaultOrdinal.FILE_PROPERTIES;
private InputResource source;
private HashMap<String, String> values;
+ public JSONPropertySource(File file) {
+ this(file, 0);
+ }
+
public JSONPropertySource(File file, int priority) {
this.priority = priority;
source = new FileBasedResource(file);
@@ -47,13 +59,18 @@ public class JSONPropertySource
@Override
public int getOrdinal() {
+ synchronized (this) {
+ if (values == null) {
+ readSource();
+ }
+ }
+
return priority;
}
@Override
public String getName() {
- // @todo Implement me
- throw new RuntimeException("Not implemented yet.");
+ return "json-properties";
}
@Override
@@ -89,6 +106,12 @@ public class JSONPropertySource
visitor.run();
this.values = values;
+
+ if (this.values.containsKey(TAMAYA_ORDINAL)) {
+ int newPriority =
Integer.valueOf(this.values.get(TAMAYA_ORDINAL)).intValue();
+ priority = newPriority;
+ this.values.remove(TAMAYA_ORDINAL);
+ }
}
catch (Throwable t) {
throw new ConfigException(format("Failed to read properties from
%s", source.getDescription()), t);
@@ -98,7 +121,6 @@ public class JSONPropertySource
@Override
public boolean isScannable() {
- // @todo Implement me
- throw new RuntimeException("Not implemented yet.");
+ return true;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7985c039/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceTest.java
----------------------------------------------------------------------
diff --git
a/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceTest.java
b/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceTest.java
index ecf7784..87a9ff2 100644
---
a/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceTest.java
+++
b/modules/json/src/test/java/org/apache/tamaya/modules/json/JSONPropertySourceTest.java
@@ -19,10 +19,13 @@
package org.apache.tamaya.modules.json;
import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.core.propertysource.DefaultOrdinal;
+import org.apache.tamaya.spi.PropertySource;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import java.io.File;
+import java.net.URISyntaxException;
import java.net.URL;
import java.util.Optional;
@@ -30,6 +33,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
+import static org.junit.Assert.fail;
public class JSONPropertySourceTest {
@@ -134,4 +138,44 @@ public class JSONPropertySourceTest {
assertThat(keyDP.isPresent(), is(true));
assertThat(keyDP.get(), is("P"));
}
+
+ @Test
+ public void tamayaOrdinalKeywordIsNotPropagatedAsNormalProperty() throws
Exception {
+ URL configURL =
JSONPropertySourceTest.class.getResource("/configs/valid/with-explicit-priority.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ File configFile = new File(configURL.toURI());
+
+ JSONPropertySource source = new JSONPropertySource(configFile, 10);
+
+ assertThat(source.get(PropertySource.TAMAYA_ORDINAL).isPresent(),
is(false));
+ }
+
+ @Test
+ public void priorityInConfigFileOverwriteExplicitlyGivenPriority() throws
URISyntaxException {
+ URL configURL =
JSONPropertySourceTest.class.getResource("/configs/valid/with-explicit-priority.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ File configFile = new File(configURL.toURI());
+
+ JSONPropertySource source = new JSONPropertySource(configFile, 10);
+
+ assertThat(source.getOrdinal(), is(16784));
+ }
+
+ @Test
+ public void priorityInConfigFileIsReturnedPriority() throws
URISyntaxException {
+ URL configURL =
JSONPropertySourceTest.class.getResource("/configs/valid/with-explicit-priority.json");
+
+ assertThat(configURL, CoreMatchers.notNullValue());
+
+ File configFile = new File(configURL.toURI());
+
+ JSONPropertySource source = new JSONPropertySource(configFile);
+
+ assertThat(source.getOrdinal(), is(16784));
+
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/7985c039/modules/json/src/test/resources/configs/valid/with-explicit-priority.json
----------------------------------------------------------------------
diff --git
a/modules/json/src/test/resources/configs/valid/with-explicit-priority.json
b/modules/json/src/test/resources/configs/valid/with-explicit-priority.json
new file mode 100644
index 0000000..9e34151
--- /dev/null
+++ b/modules/json/src/test/resources/configs/valid/with-explicit-priority.json
@@ -0,0 +1,4 @@
+{
+ "tamaya.ordinal" : 16784,
+ "a" : "A"
+}
\ No newline at end of file