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 2017/02/26 23:05:16 UTC
[03/11] incubator-tamaya-extensions git commit: TAMAYA-240:
ConfigFormat. readXX should throw IOException instead of ConfigException
TAMAYA-240: ConfigFormat. readXX should throw IOException instead of ConfigException
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/commit/b06105f8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/b06105f8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/b06105f8
Branch: refs/heads/master
Commit: b06105f8eac8a00d4da30daf0cef84c7f79d358d
Parents: 364604b
Author: anatole <an...@apache.org>
Authored: Thu Feb 23 01:15:24 2017 +0100
Committer: anatole <an...@apache.org>
Committed: Mon Feb 27 00:05:00 2017 +0100
----------------------------------------------------------------------
.../BaseFormatPropertySourceProvider.java | 39 ++------
.../tamaya/format/ConfigurationFormat.java | 9 +-
.../tamaya/format/ConfigurationFormats.java | 99 +++++++++++++++++++-
.../format/formats/IniConfigurationFormat.java | 12 ++-
.../tamaya/format/formats/PropertiesFormat.java | 12 +--
.../format/formats/PropertiesXmlFormat.java | 22 ++---
.../java/org/apache/tamaya/json/JSONFormat.java | 13 ++-
.../apache/tamaya/json/JSONPropertySource.java | 15 +--
.../yaml/CommonJSONTestCaseCollection.java | 12 ++-
.../tamaya/yaml/JSONPropertySourceTest.java | 3 +-
.../java/org/apache/tamaya/yaml/YAMLFormat.java | 35 +++----
.../apache/tamaya/yaml/YAMLPropertySource.java | 5 +-
12 files changed, 164 insertions(+), 112 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java
----------------------------------------------------------------------
diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java
index 49f05df..e0e39d3 100644
--- a/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java
+++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/BaseFormatPropertySourceProvider.java
@@ -140,39 +140,20 @@ public abstract class BaseFormatPropertySourceProvider implements PropertySource
@Override
public Collection<PropertySource> getPropertySources() {
List<PropertySource> propertySources = new ArrayList<>();
- byte[] buff = new byte[512];
for (URL res : this.paths) {
- byte[] dataBytes = null;
- int read = 0;
- try(InputStream is = res.openStream();
- ByteArrayOutputStream bos = new ByteArrayOutputStream();) {
- read = is.read(buff);
- while (read > 0) {
- bos.write(buff, 0, read);
- read = is.read(buff);
- }
- bos.flush();
- dataBytes = bos.toByteArray();
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Failed to read resource based config: " + res, e);
- }
- if(dataBytes!=null) {
- try (InputStream is = new ByteArrayInputStream(dataBytes);) {
- for (ConfigurationFormat format : configFormats) {
- try {
- if (format.accepts(res)) {
- ConfigurationData data = format.readConfiguration(res.toString(), is);
- propertySources.addAll(getPropertySources(data));
- }
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e);
- } finally {
- is.reset();
+ try{
+ for (ConfigurationFormat format : configFormats) {
+ try (InputStream inputStream = res.openStream()){
+ if (format.accepts(res)) {
+ ConfigurationData data = format.readConfiguration(res.toString(), inputStream);
+ propertySources.addAll(getPropertySources(data));
}
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e);
}
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e);
}
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Failed to put resource based config: " + res, e);
}
}
return propertySources;
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
----------------------------------------------------------------------
diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
index ee11207..e8e48a2 100644
--- a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
+++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormat.java
@@ -18,6 +18,7 @@
*/
package org.apache.tamaya.format;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@@ -53,6 +54,7 @@ public interface ConfigurationFormat {
*/
boolean accepts(URL url);
+
/**
* Reads a configuration from an URL, hereby parsing the given {@link java.io.InputStream}. Dependening on
* the capabilities of the format the returned {@link org.apache.tamaya.format.ConfigurationData} may contain
@@ -89,12 +91,11 @@ public interface ConfigurationFormat {
* ladder case multiple PropertySources can be returned, each one with its own ordinal and the corresponding
* entries.
* @see org.apache.tamaya.spi.PropertySource
- * @param resource a descriptive name for the resource, since an InputStream does not have any)
- * @param inputStream the inputStream to read the configuration data from (could be a file, a server location, a classpath
- * resource or something else.
+ * @param inputStream the inputStream to read from, not null.
+ * @param resource the resource id, not null.
* @return the corresponding {@link ConfigurationData} containing sections/properties read, never {@code null}.
* @throws org.apache.tamaya.ConfigException if parsing of the input fails.
*/
- ConfigurationData readConfiguration(String resource, InputStream inputStream);
+ ConfigurationData readConfiguration(String resource, InputStream inputStream) throws IOException;
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java
----------------------------------------------------------------------
diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java
index 5b2d819..f08e967 100644
--- a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java
+++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationFormats.java
@@ -21,10 +21,18 @@ package org.apache.tamaya.format;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.spi.PropertySource;
import org.apache.tamaya.spi.ServiceContextManager;
/**
@@ -191,13 +199,12 @@ public final class ConfigurationFormats {
* @param inputStream the inputStream from where to read, not null.
* @param formats the formats to try.
* @return the ConfigurationData read, or null.
- * @throws IOException if the resource cannot be read.
+ * @throws ConfigException if the resource cannot be read.
*/
public static ConfigurationData readConfigurationData(String resource, InputStream inputStream,
Collection<ConfigurationFormat> formats) throws IOException {
- Objects.requireNonNull(inputStream);
- Objects.requireNonNull(resource);
- try(InputStreamFactory isFactory = new InputStreamFactory(inputStream)) {
+ Objects.requireNonNull(resource, "Config resource required for traceability.");
+ try(InputStreamFactory isFactory = new InputStreamFactory(Objects.requireNonNull(inputStream))) {
for (final ConfigurationFormat format : formats) {
try (InputStream is = isFactory.createInputStream()) {
final ConfigurationData data = format.readConfiguration(resource, is);
@@ -213,5 +220,87 @@ public final class ConfigurationFormats {
return null;
}
+ /**
+ * Tries to read configuration data from a given URL, hereby explicitly trying all given formats
+ * in order and transforms it into a {@link PropertySource} using a default mapping.
+ *
+ * @param url the URL to read, not null.
+ * @param formats the formats to try. If not formats are passed explicitly, all known formats
+ * are tried.
+ * @return a corresponding property source, or null.
+ * @throws ConfigException if the resource cannot be read.
+ * @throws IOException if the URL's stream can not be opened.
+ */
+ public static PropertySource createPropertySource(URL url, ConfigurationFormat... formats)throws IOException{
+ return createPropertySource(url.toString(), url.openStream(), formats);
+ }
+
+ /**
+ * Tries to read configuration data from a given URL, hereby explicitly trying all given formats
+ * in order and transforms it into a {@link PropertySource} using a default mapping.
+ *
+ * @param url the URL to read, not null.
+ * @param formats the formats to try. If not formats are passed explicitly, all known formats
+ * are tried.
+ * @return a corresponding property source, or null.
+ * @throws ConfigException if the resource cannot be read.
+ * @throws IOException if the URL's stream can not be opened.
+ */
+ public static PropertySource createPropertySource(URL url, Collection<ConfigurationFormat> formats)throws IOException{
+ return createPropertySource(url.toString(), url.openStream(), formats);
+ }
+
+ /**
+ * Tries to read configuration data from a given URL, hereby explicitly trying all given formats
+ * in order and transforms it into a {@link PropertySource} using a default mapping.
+ *
+ * @param resource a descriptive name for the resource, since an InputStream does not have any
+ * @param inputStream the inputStream from where to read, not null.
+ * @param formats the formats to try. If not formats are passed explicitly, all known formats
+ * are tried.
+ * @return a corresponding property source, or null.
+ * @throws ConfigException if the resource cannot be read.
+ */
+ public static PropertySource createPropertySource(String resource, InputStream inputStream,
+ ConfigurationFormat... formats){
+ return createPropertySource(resource, inputStream, Arrays.asList(formats));
+ }
+
+
+ /**
+ * Tries to read configuration data from a given URL, hereby explicitly trying all given formats
+ * in order and transforms it into a {@link PropertySource} using a default mapping.
+ *
+ * @param resource a descriptive name for the resource, since an InputStream does not have any
+ * @param inputStream the inputStream from where to read, not null.
+ * @param formats the formats to try. If not formats are passed explicitly, all known formats
+ * are tried.
+ * @return a corresponding property source, or null.
+ * @throws ConfigException if the resource cannot be read.
+ */
+ public static PropertySource createPropertySource(String resource, InputStream inputStream,
+ Collection<ConfigurationFormat> formats) {
+ Objects.requireNonNull(resource, "Config resource required for traceability.");
+ try(InputStreamFactory isFactory = new InputStreamFactory(Objects.requireNonNull(inputStream))) {
+ if(formats.isEmpty()){
+ formats = getFormats();
+ }
+ for (final ConfigurationFormat format : formats) {
+ try (InputStream is = isFactory.createInputStream()) {
+ final ConfigurationData data = format.readConfiguration(resource, is);
+ if (data != null) {
+ return new MappedConfigurationDataPropertySource(data);
+ }
+ } catch (final Exception e) {
+ LOG.log(Level.INFO,
+ "Format " + format.getClass().getName() + " failed to read resource " + resource, e);
+ }
+ }
+ }catch(IOException ioe){
+ throw new ConfigException("Failed to read from input stream for "+resource, ioe);
+ }
+ throw new ConfigException("No matching format found for "+resource+", tried: "+ formats);
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java
----------------------------------------------------------------------
diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java
index ea52367..af2cd94 100644
--- a/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java
+++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/IniConfigurationFormat.java
@@ -24,10 +24,10 @@ import org.apache.tamaya.format.ConfigurationDataBuilder;
import org.apache.tamaya.format.ConfigurationFormat;
import java.io.BufferedReader;
+import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
-import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -52,7 +52,8 @@ public class IniConfigurationFormat implements ConfigurationFormat {
}
@Override
- public ConfigurationData readConfiguration(String resource, InputStream inputStream) {
+ public ConfigurationData readConfiguration(String resource, InputStream inputStream)
+ throws IOException{
ConfigurationDataBuilder builder = ConfigurationDataBuilder.of(resource, this);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
String line = reader.readLine();
@@ -88,8 +89,11 @@ public class IniConfigurationFormat implements ConfigurationFormat {
}
return builder.build();
} catch (Exception e) {
- LOG.log(Level.SEVERE, "Could not read configuration: " + resource, e);
+ if(e instanceof IOException){
+ throw (IOException)e;
+ }else{
+ throw new IOException("Could not read configuration: " + resource, e);
+ }
}
- return null;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java
----------------------------------------------------------------------
diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java
index c45185b..42388c3 100644
--- a/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java
+++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesFormat.java
@@ -22,12 +22,11 @@ import org.apache.tamaya.format.ConfigurationData;
import org.apache.tamaya.format.ConfigurationDataBuilder;
import org.apache.tamaya.format.ConfigurationFormat;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
-import java.util.Objects;
import java.util.Properties;
-import java.util.logging.Level;
import java.util.logging.Logger;
/**
@@ -56,16 +55,9 @@ public class PropertiesFormat implements ConfigurationFormat {
@SuppressWarnings("unchecked")
@Override
- public ConfigurationData readConfiguration(String resource, InputStream inputStream) {
- Objects.requireNonNull(inputStream);
- Objects.requireNonNull(resource);
- try {
+ public ConfigurationData readConfiguration(String resource, InputStream inputStream)throws IOException {
final Properties p = new Properties();
p.load(inputStream);
return ConfigurationDataBuilder.of(resource, this).addDefaultProperties(Map.class.cast(p)).build();
- } catch (Exception e) {
- LOG.log(Level.FINEST, "Failed to read config from resource: " + resource, e);
- }
- return null;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java
----------------------------------------------------------------------
diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java
index df150ea..61b45f1 100644
--- a/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java
+++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/formats/PropertiesXmlFormat.java
@@ -22,10 +22,11 @@ import org.apache.tamaya.format.ConfigurationData;
import org.apache.tamaya.format.ConfigurationDataBuilder;
import org.apache.tamaya.format.ConfigurationFormat;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
-import java.util.*;
-import java.util.logging.Level;
+import java.util.Map;
+import java.util.Properties;
import java.util.logging.Logger;
/**
@@ -53,17 +54,10 @@ public class PropertiesXmlFormat implements ConfigurationFormat {
@SuppressWarnings("unchecked")
@Override
- public ConfigurationData readConfiguration(String resource, InputStream inputStream) {
- Objects.requireNonNull(inputStream);
- Objects.requireNonNull(resource);
-
- try {
- final Properties p = new Properties();
- p.loadFromXML(inputStream);
- return ConfigurationDataBuilder.of(resource, this).addDefaultProperties(Map.class.cast(p)).build();
- } catch (Exception e) {
- LOG.log(Level.FINEST, "Failed to read config from resource: " + resource, e);
- }
- return null;
+ public ConfigurationData readConfiguration(String resource, InputStream inputStream)
+ throws IOException{
+ final Properties p = new Properties();
+ p.loadFromXML(inputStream);
+ return ConfigurationDataBuilder.of(resource, this).addDefaultProperties(Map.class.cast(p)).build();
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java
----------------------------------------------------------------------
diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java
index 66f684e..5902cbd 100644
--- a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java
+++ b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONFormat.java
@@ -18,11 +18,11 @@
*/
package org.apache.tamaya.json;
-import org.apache.tamaya.ConfigException;
import org.apache.tamaya.format.ConfigurationData;
import org.apache.tamaya.format.ConfigurationDataBuilder;
import org.apache.tamaya.format.ConfigurationFormat;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
@@ -31,7 +31,6 @@ import java.util.Map;
import java.util.Objects;
import javax.json.Json;
-import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
@@ -68,9 +67,9 @@ public class JSONFormat implements ConfigurationFormat {
}
@Override
- public ConfigurationData readConfiguration(String resource, InputStream inputStream) {
-
- try {
+ public ConfigurationData readConfiguration(String resource, InputStream inputStream)
+ throws IOException{
+ try{
final JsonReader reader = this.readerFactory.createReader(inputStream, Charset.forName("UTF-8"));
JsonObject root = reader.readObject();
HashMap<String, String> values = new HashMap<>();
@@ -78,8 +77,8 @@ public class JSONFormat implements ConfigurationFormat {
visitor.run();
return ConfigurationDataBuilder.of(resource, this).addDefaultProperties(values)
.build();
- } catch (JsonException e) {
- throw new ConfigException("Failed to read data from " + resource, e);
+ } catch(Exception e) {
+ throw new IOException("Failed to read data from " + resource, e);
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java
index 18d64ce..1ac5ee7 100644
--- a/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java
+++ b/modules/formats/json/src/main/java/org/apache/tamaya/json/JSONPropertySource.java
@@ -22,6 +22,7 @@ import org.apache.tamaya.ConfigException;
import org.apache.tamaya.spi.PropertySource;
import org.apache.tamaya.spi.PropertyValue;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
@@ -66,7 +67,7 @@ public class JSONPropertySource implements PropertySource {
* Constructor, hereby using 0 as the default ordinal.
* @param resource the resource modelled as URL, not null.
*/
- public JSONPropertySource(URL resource) {
+ public JSONPropertySource(URL resource)throws IOException {
this(resource, 0);
}
@@ -75,7 +76,7 @@ public class JSONPropertySource implements PropertySource {
* @param resource the resource modelled as URL, not null.
* @param defaultOrdinal the defaultOrdinal to be used.
*/
- public JSONPropertySource(URL resource, int defaultOrdinal) {
+ public JSONPropertySource(URL resource, int defaultOrdinal)throws IOException {
urlResource = Objects.requireNonNull(resource);
this.ordinal = defaultOrdinal; // may be overriden by read...
this.values = readConfig(urlResource);
@@ -88,7 +89,6 @@ public class JSONPropertySource implements PropertySource {
}
- @Override
public int getOrdinal() {
PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL);
if(configuredOrdinal!=null){
@@ -123,7 +123,7 @@ public class JSONPropertySource implements PropertySource {
* @return the configuration read from the given resource URL.
* @throws ConfigException if resource URL cannot be read.
*/
- protected Map<String, String> readConfig(URL urlResource) {
+ protected Map<String, String> readConfig(URL urlResource) throws IOException{
try (InputStream is = urlResource.openStream()) {
JsonStructure root = this.readerFactory.createReader(is, Charset.forName("UTF-8")).read();
@@ -136,9 +136,10 @@ public class JSONPropertySource implements PropertySource {
JSONVisitor visitor = new JSONVisitor((JsonObject)root, values);
visitor.run();
return values;
- }
- catch (Throwable t) {
- throw new ConfigException(format("Failed to read properties from %s", urlResource.toExternalForm()), t);
+ }catch(IOException ioe){
+ throw ioe;
+ }catch (Exception t) {
+ throw new IOException(format("Failed to read properties from %s", urlResource.toExternalForm()), t);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java
----------------------------------------------------------------------
diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java
index 297c7cf..1d93d20 100644
--- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java
+++ b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java
@@ -21,10 +21,12 @@ package org.apache.tamaya.yaml;
import org.apache.tamaya.ConfigException;
import org.apache.tamaya.spi.PropertySource;
import org.apache.tamaya.spi.PropertyValue;
+import org.apache.tamaya.spisupport.PropertySourceComparator;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.Test;
+import java.io.IOException;
import java.net.URL;
import static org.hamcrest.CoreMatchers.equalTo;
@@ -107,7 +109,7 @@ public abstract class CommonJSONTestCaseCollection {
assertThat(keyDP.getValue(), is("P"));
}
- @Test(expected = ConfigException.class)
+ @Test(expected = IOException.class)
public void canHandleIllegalJSONFileWhichContainsAnArray() throws Exception {
URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/with-array.json");
@@ -116,7 +118,7 @@ public abstract class CommonJSONTestCaseCollection {
getPropertiesFrom(configURL).getProperties();
}
- @Test(expected = ConfigException.class)
+ @Test(expected = IOException.class)
public void canHandleIllegalJSONFileConsistingOfOneOpeningBracket() throws Exception {
URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/only-opening-bracket.json");
@@ -125,7 +127,7 @@ public abstract class CommonJSONTestCaseCollection {
getPropertiesFrom(configURL).getProperties();
}
- @Test(expected = ConfigException.class)
+ @Test(expected = IOException.class)
public void canHandleIllegalJSONFileWhichIsEmpty() throws Exception {
URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/empty-file.json");
@@ -142,7 +144,7 @@ public abstract class CommonJSONTestCaseCollection {
PropertySource properties = getPropertiesFrom(configURL);
- assertThat(properties.getOrdinal(), is(16784));
+ assertThat(PropertySourceComparator.getOrdinal(properties), is(16784));
}
@Test
@@ -167,7 +169,7 @@ public abstract class CommonJSONTestCaseCollection {
assertThat(keyC.getValue(), is("C"));
}
- @Test(expected = ConfigException.class)
+ @Test(expected = IOException.class)
public void emptyJSONFileResultsInConfigException() throws Exception {
URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/empty-file.json");
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java
----------------------------------------------------------------------
diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java
index 83944f5..015ad0a 100644
--- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java
+++ b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java
@@ -24,6 +24,7 @@ import org.apache.tamaya.spi.PropertySource;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
+import java.io.IOException;
import java.net.URL;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -41,7 +42,7 @@ public class JSONPropertySourceTest extends CommonJSONTestCaseCollection {
assertEquals(source.get(PropertySource.TAMAYA_ORDINAL).getValue(), "16784");
}
- @Test(expected=ConfigException.class)
+ @Test(expected=IOException.class)
public void testDoNotAcceptJsonArrays() throws Exception {
URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/array.json");
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java
----------------------------------------------------------------------
diff --git a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java
index bdb83aa..adf7b34 100644
--- a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java
+++ b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLFormat.java
@@ -48,12 +48,6 @@ public class YAMLFormat implements ConfigurationFormat {
*/
private static final Logger LOG = Logger.getLogger(YAMLFormat.class.getName());
- /**
- * Constructor, itniaitlizing zhe JSON reader factory.
- */
- public YAMLFormat(){
- }
-
@Override
public String getName() {
return "yaml";
@@ -66,13 +60,9 @@ public class YAMLFormat implements ConfigurationFormat {
@Override
public ConfigurationData readConfiguration(String resource, InputStream inputStream) {
- try( InputStream in = inputStream;) {
- Map<String, String> values = readConfig(resource, inputStream);
- return ConfigurationDataBuilder.of(resource, this).addProperties(values)
- .build();
- } catch (Exception e) {
- throw new ConfigException("Failed to read data from " + resource, e);
- }
+ Map<String, String> values = readConfig(resource, inputStream);
+ return ConfigurationDataBuilder.of(resource, this).addProperties(values)
+ .build();
}
/**
@@ -83,19 +73,16 @@ public class YAMLFormat implements ConfigurationFormat {
* @throws ConfigException if resource URI cannot be read.
*/
protected Map<String, String> readConfig(String resource, InputStream inputStream) {
- try{
- Yaml yaml = new Yaml();
- HashMap<String, String> values = new HashMap<>();
- Object config = yaml.load(inputStream);
- mapYamlIntoProperties(config, values);
- if(LOG.isLoggable(Level.FINEST)){
- LOG.finest("Read data from " + resource + " : " + values);
- }
- return values;
- }catch (Throwable t) {
- throw new ConfigException(format("Failed to read properties from %s", resource), t);
+ Yaml yaml = new Yaml();
+ HashMap<String, String> values = new HashMap<>();
+ Object config = yaml.load(inputStream);
+ mapYamlIntoProperties(config, values);
+ if(LOG.isLoggable(Level.FINEST)){
+ LOG.finest("Read data from " + resource + " : " + values);
}
+ return values;
}
+
/**
* Reads the configuration.
* @param urlResource soure of the configuration.
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b06105f8/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java
----------------------------------------------------------------------
diff --git a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java
index 9edc15b..ae54624 100644
--- a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java
+++ b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/YAMLPropertySource.java
@@ -22,7 +22,9 @@ import org.apache.tamaya.spi.PropertySource;
import org.apache.tamaya.spi.PropertyValue;
import java.net.URL;
-import java.util.*;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -63,7 +65,6 @@ public class YAMLPropertySource implements PropertySource {
}
}
- @Override
public int getOrdinal() {
PropertyValue configuredOrdinal = get(TAMAYA_ORDINAL);
if(configuredOrdinal!=null){