Repository: incubator-tamaya-extensions Updated Branches: refs/heads/master 3774a6c3f -> b82c4dc5b
Added property source factory method to config data. Added json/yaml path based providers and tests. 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/b82c4dc5 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/b82c4dc5 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/b82c4dc5 Branch: refs/heads/master Commit: b82c4dc5b205c08672e6026080c393c731debec9 Parents: 3774a6c Author: Anatole Tresch <[email protected]> Authored: Wed Nov 21 00:54:35 2018 +0100 Committer: Anatole Tresch <[email protected]> Committed: Wed Nov 21 07:47:07 2018 +0100 ---------------------------------------------------------------------- .../apache/tamaya/format/ConfigurationData.java | 10 + .../PathBasedJsonPropertySourceProvider.java | 58 ++++++ .../json/CommonJSONTestCaseCollection.java | 208 +++++++++++++++++++ .../org/apache/tamaya/json/JSONFormatIT.java | 49 +++++ .../org/apache/tamaya/json/JSONFormatTest.java | 75 +++++++ .../tamaya/json/JSONPropertySourceTest.java | 55 +++++ ...PathBasedJsonPropertySourceProviderTest.java | 66 ++++++ .../yaml/CommonJSONTestCaseCollection.java | 208 ------------------- .../org/apache/tamaya/yaml/JSONFormatIT.java | 49 ----- .../org/apache/tamaya/yaml/JSONFormatTest.java | 76 ------- .../tamaya/yaml/JSONPropertySourceTest.java | 57 ----- .../PathBasedYamlPropertySourceProvider.java | 58 ++++++ ...PathBasedYamlPropertySourceProviderTest.java | 65 ++++++ 13 files changed, 644 insertions(+), 390 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationData.java ---------------------------------------------------------------------- diff --git a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationData.java b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationData.java index 1ef829c..db5131e 100644 --- a/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationData.java +++ b/modules/formats/base/src/main/java/org/apache/tamaya/format/ConfigurationData.java @@ -18,6 +18,8 @@ */ package org.apache.tamaya.format; +import org.apache.tamaya.spi.ChangeSupport; +import org.apache.tamaya.spi.PropertySource; import org.apache.tamaya.spi.PropertyValue; import java.util.*; @@ -96,6 +98,14 @@ public final class ConfigurationData { return data.isEmpty(); } + /** + * Creates a {@link PropertySource} from the given property data. + * @return a corresponding property source, never nhull. + */ + public PropertySource toPropertySource() { + return new MappedConfigurationDataPropertySource(this).setChangeSupport(ChangeSupport.IMMUTABLE); + } + @Override public String toString() { return "ConfigurationData{" + http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/json/src/main/java/org/apache/tamaya/json/PathBasedJsonPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/main/java/org/apache/tamaya/json/PathBasedJsonPropertySourceProvider.java b/modules/formats/json/src/main/java/org/apache/tamaya/json/PathBasedJsonPropertySourceProvider.java new file mode 100644 index 0000000..95854d8 --- /dev/null +++ b/modules/formats/json/src/main/java/org/apache/tamaya/json/PathBasedJsonPropertySourceProvider.java @@ -0,0 +1,58 @@ +/* + * 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.json; + +import org.apache.tamaya.resource.AbstractPathPropertySourceProvider; +import org.apache.tamaya.spi.PropertySource; + +import java.io.IOException; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Path based default provider for yaml formatted config files. + */ +public class PathBasedJsonPropertySourceProvider extends AbstractPathPropertySourceProvider{ + + private static final Logger LOG = Logger.getLogger(PathBasedJsonPropertySourceProvider.class.getName()); + private JSONFormat jsonFormat = new JSONFormat(); + + public PathBasedJsonPropertySourceProvider(String... paths) { + super(paths); + } + + @Override + protected Collection<PropertySource> getPropertySources(URL url) { + if(jsonFormat.accepts(url)){ + try { + return Collections.singletonList( + jsonFormat.readConfiguration(url.toString(), url.openStream()).toPropertySource()); + } catch (IOException e) { + LOG.log(Level.SEVERE, "Failed to read yaml file: " +url, e); + } + } + return Collections.emptyList(); + } + +} + + http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/json/src/test/java/org/apache/tamaya/json/CommonJSONTestCaseCollection.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/json/CommonJSONTestCaseCollection.java b/modules/formats/json/src/test/java/org/apache/tamaya/json/CommonJSONTestCaseCollection.java new file mode 100644 index 0000000..fe445f2 --- /dev/null +++ b/modules/formats/json/src/test/java/org/apache/tamaya/json/CommonJSONTestCaseCollection.java @@ -0,0 +1,208 @@ +/* + * 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.json; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.net.URL; + +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; + +/** + * Class with a collection of common test cases each JSON processing + * class must be able to pass. + */ +public abstract class CommonJSONTestCaseCollection { + + abstract PropertySource getPropertiesFrom(URL source) throws Exception; + + @Test + public void canReadNonLatinCharacters() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class + .getResource("/configs/valid/cyrillic.json"); + + assertThat(configURL, Matchers.notNullValue()); + + PropertySource propertySource = getPropertiesFrom(configURL); + + assertThat(propertySource.get("name"), Matchers.notNullValue()); + assertThat(propertySource.get("name").getValue(), equalTo("\u041e\u043b\u0438\u0432\u0435\u0440")); + assertThat(propertySource.get("\u0444\u0430\u043c\u0438\u043b\u0438\u044f"), Matchers.notNullValue()); + assertThat(propertySource.get("\u0444\u0430\u043c\u0438\u043b\u0438\u044f").getValue(), Matchers.equalTo("Fischer")); + } + + @Test + public void canReadUnicodeCharacters() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class + .getResource("/configs/valid/kanji.json"); + + assertThat(configURL, Matchers.notNullValue()); + + PropertySource propertySource = getPropertiesFrom(configURL); + + assertThat(propertySource.get("onamae"), Matchers.notNullValue()); + // éå± = Tamaya + assertThat(propertySource.get("onamae").getValue(), equalTo("\u970a\u5c4b")); + } + + @Test + public void canReadNestedStringOnlyJSONConfigFile2() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class + .getResource("/configs/valid/simple-nested-string-only-config-1.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + PropertySource properties = getPropertiesFrom(configURL); + + System.out.println("simple-nested-string-only-config-1.json -> " + properties.getProperties().values()); + + assertTrue(properties.getProperties().keySet().size()>=5); + + PropertyValue keyB = properties.get("b"); + PropertyValue keyDO = properties.get("d.o"); + PropertyValue keyDP = properties.get("d.p"); + + assertThat(keyB, notNullValue()); + assertThat(keyB.getValue(), equalTo("B")); + assertThat(keyDO, notNullValue()); + assertThat(keyDO.getValue(), equalTo("O")); + assertThat(keyDP, Matchers.notNullValue()); + assertThat(keyDP.getValue(), is("P")); + } + + @Test + public void canReadNestedStringOnlyJSONConfigFileWithObjectInTheMiddle() + throws Exception { + URL configURL = CommonJSONTestCaseCollection.class + .getResource("/configs/valid/simple-nested-string-only-config-2.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + PropertySource properties = getPropertiesFrom(configURL); + + assertTrue(properties.getProperties().keySet().size()>=4); + + PropertyValue keyA = properties.get("a"); + PropertyValue keyDO = properties.get("b.o"); + PropertyValue keyDP = properties.get("b.p"); + PropertyValue keyC = properties.get("c"); + + assertThat(keyA, notNullValue()); + assertThat(keyA.getValue(), is("A")); + assertThat(keyC, notNullValue()); + assertThat(keyC.getValue(), equalTo("C")); + assertThat(keyDO, notNullValue()); + assertThat(keyDO.getValue(), equalTo("O")); + assertThat(keyDP, notNullValue()); + assertThat(keyDP.getValue(), is("P")); + } + + @Test + public void canHandleIllegalJSONFileWhichContainsAnArray() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/with-array.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + getPropertiesFrom(configURL).getProperties(); + } + + @Test(expected = IOException.class) + public void canHandleIllegalJSONFileConsistingOfOneOpeningBracket() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/only-opening-bracket.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + getPropertiesFrom(configURL).getProperties(); + } + + @Test(expected = IOException.class) + public void canHandleIllegalJSONFileWhichIsEmpty() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/empty-file.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + getPropertiesFrom(configURL).getProperties(); + } + + @Test + public void priorityInConfigFileOverwriteExplicitlyGivenPriority() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/valid/with-explicit-priority.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + PropertySource properties = getPropertiesFrom(configURL); + + assertThat(PropertySourceComparator.getOrdinal(properties), is(16784)); + } + + @Test + public void canReadFlatStringOnlyJSONConfigFile() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/valid/simple-flat-string-only-config.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + PropertySource properties = getPropertiesFrom(configURL); + + assertEquals(3, properties.getProperties().size()); + + PropertyValue keyA = properties.get("a"); + PropertyValue keyB = properties.get("b"); + PropertyValue keyC = properties.get("c"); + + assertThat(keyA, notNullValue()); + assertThat(keyA.getValue(), equalTo("A")); + assertThat(keyB, notNullValue()); + assertThat(keyB.getValue(), is("B")); + assertThat(keyC, notNullValue()); + assertThat(keyC.getValue(), is("C")); + } + + @Test(expected = IOException.class) + public void emptyJSONFileResultsInConfigException() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/empty-file.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + PropertySource properties = getPropertiesFrom(configURL); + + properties.getProperties(); + } + + @Test + public void canHandleEmptyJSONObject() throws Exception { + URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/valid/empty-object-config.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + PropertySource properties = getPropertiesFrom(configURL); + + assertTrue(properties.getProperties().keySet().size()>=0); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONFormatIT.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONFormatIT.java b/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONFormatIT.java new file mode 100644 index 0000000..6c6cadd --- /dev/null +++ b/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONFormatIT.java @@ -0,0 +1,49 @@ +/* + * 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.json; + +import org.apache.tamaya.format.ConfigurationFormat; +import org.apache.tamaya.json.JSONFormat; +import org.apache.tamaya.spi.ServiceContextManager; +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; + +/** + * Integration tests for {@link JSONFormat}. + */ +public class JSONFormatIT { + @Test + public void jsonFormatCanBeFoundViaServiceLoader() throws Exception { + List<ConfigurationFormat> formats = ServiceContextManager.getServiceContext() + .getServices(ConfigurationFormat.class); + + ConfigurationFormat format = null; + for (ConfigurationFormat f : formats) { + if (f instanceof JSONFormat) { + format = f; + break; + } + } + assertThat(format, notNullValue()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONFormatTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONFormatTest.java b/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONFormatTest.java new file mode 100644 index 0000000..21b3aa8 --- /dev/null +++ b/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONFormatTest.java @@ -0,0 +1,75 @@ +/* + * 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.json; + + +import org.apache.tamaya.format.ConfigurationData; +import org.apache.tamaya.format.MappedConfigurationDataPropertySource; +import org.apache.tamaya.spi.PropertySource; +import org.junit.Test; + +import java.io.InputStream; +import java.net.URL; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class JSONFormatTest extends CommonJSONTestCaseCollection { + private final JSONFormat format = new JSONFormat(); + + @Test(expected = NullPointerException.class) + public void acceptsNeedsNonNullParameter() throws Exception { + format.accepts(null); + } + + @Test + public void aNonJSONFileBasedURLIsNotAccepted() throws Exception { + URL url = new URL("file:///etc/service/conf.conf"); + + assertThat(format.accepts(url), is(false)); + } + + @Test + public void aJSONFileBasedURLIsAccepted() throws Exception { + URL url = new URL("file:///etc/service/conf.json"); + + assertThat(format.accepts(url), is(true)); + } + + @Test + public void aHTTPBasedURLIsNotAccepted() throws Exception { + URL url = new URL("http://nowhere.somewhere/conf.json"); + assertThat(format.accepts(url), is(true)); + } + + @Test + public void aFTPBasedURLIsNotAccepted() throws Exception { + URL url = new URL("ftp://nowhere.somewhere/a/b/c/d/conf.json"); + + assertThat(format.accepts(url), is(true)); + } + + @Override + PropertySource getPropertiesFrom(URL source) throws Exception { + try (InputStream is = source.openStream()) { + ConfigurationData data = format.readConfiguration(source.toString(), is); + return new MappedConfigurationDataPropertySource(data); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceTest.java b/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceTest.java new file mode 100644 index 0000000..fd2dc87 --- /dev/null +++ b/modules/formats/json/src/test/java/org/apache/tamaya/json/JSONPropertySourceTest.java @@ -0,0 +1,55 @@ +/* + * 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.json; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; + +import java.net.URL; + +import org.apache.tamaya.spi.PropertySource; +import org.hamcrest.CoreMatchers; +import org.junit.Test; + +public class JSONPropertySourceTest extends CommonJSONTestCaseCollection { + + @Test + public void tamayaOrdinalKeywordIsNotPropagatedAsNormalProperty() throws Exception { + URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/with-explicit-priority.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + JSONPropertySource source = new JSONPropertySource(configURL, 4); + assertEquals(source.getOrdinal(), 16784); + } + + @Test + public void testAcceptJsonArrays() throws Exception { + URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/array.json"); + + assertThat(configURL, CoreMatchers.notNullValue()); + + new JSONPropertySource(configURL); + } + + @Override + PropertySource getPropertiesFrom(URL source) throws Exception { + return new JSONPropertySource(source); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/json/src/test/java/org/apache/tamaya/json/PathBasedJsonPropertySourceProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/json/PathBasedJsonPropertySourceProviderTest.java b/modules/formats/json/src/test/java/org/apache/tamaya/json/PathBasedJsonPropertySourceProviderTest.java new file mode 100644 index 0000000..ea45b39 --- /dev/null +++ b/modules/formats/json/src/test/java/org/apache/tamaya/json/PathBasedJsonPropertySourceProviderTest.java @@ -0,0 +1,66 @@ +/* + * 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.json; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +/** + * Tests for {@link PathBasedJsonPropertySourceProvider}. + */ +public class PathBasedJsonPropertySourceProviderTest { + + @Test + public void getPropertySources() { + PathBasedJsonPropertySourceProvider provider = new PathBasedJsonPropertySourceProvider( + "configs/valid/*.json" + ); + assertNotNull(provider.getPropertySources()); + assertEquals(7, provider.getPropertySources().size()); + } + + @Test + public void getPropertySources_one() { + PathBasedJsonPropertySourceProvider provider = new PathBasedJsonPropertySourceProvider( + "configs/valid/cyril*.json" + ); + assertNotNull(provider.getPropertySources()); + assertEquals(1, provider.getPropertySources().size()); + } + + @Test + public void getPropertySources_two() { + PathBasedJsonPropertySourceProvider provider = new PathBasedJsonPropertySourceProvider( + "configs/valid/simple-*.json" + ); + assertNotNull(provider.getPropertySources()); + assertEquals(3, provider.getPropertySources().size()); + } + + @Test + public void getPropertySources_none() { + PathBasedJsonPropertySourceProvider provider = new PathBasedJsonPropertySourceProvider( + "configs/valid/foo*.json", "configs/valid/*.JSON" + ); + assertNotNull(provider.getPropertySources()); + assertEquals(0, provider.getPropertySources().size()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/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 deleted file mode 100644 index b9d047a..0000000 --- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/CommonJSONTestCaseCollection.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * 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.yaml; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.net.URL; - -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; - -/** - * Class with a collection of common test cases each JSON processing - * class must be able to pass. - */ -public abstract class CommonJSONTestCaseCollection { - - abstract PropertySource getPropertiesFrom(URL source) throws Exception; - - @Test - public void canReadNonLatinCharacters() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class - .getResource("/configs/valid/cyrillic.json"); - - assertThat(configURL, Matchers.notNullValue()); - - PropertySource propertySource = getPropertiesFrom(configURL); - - assertThat(propertySource.get("name"), Matchers.notNullValue()); - assertThat(propertySource.get("name").getValue(), equalTo("\u041e\u043b\u0438\u0432\u0435\u0440")); - assertThat(propertySource.get("\u0444\u0430\u043c\u0438\u043b\u0438\u044f"), Matchers.notNullValue()); - assertThat(propertySource.get("\u0444\u0430\u043c\u0438\u043b\u0438\u044f").getValue(), Matchers.equalTo("Fischer")); - } - - @Test - public void canReadUnicodeCharacters() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class - .getResource("/configs/valid/kanji.json"); - - assertThat(configURL, Matchers.notNullValue()); - - PropertySource propertySource = getPropertiesFrom(configURL); - - assertThat(propertySource.get("onamae"), Matchers.notNullValue()); - // éå± = Tamaya - assertThat(propertySource.get("onamae").getValue(), equalTo("\u970a\u5c4b")); - } - - @Test - public void canReadNestedStringOnlyJSONConfigFile2() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class - .getResource("/configs/valid/simple-nested-string-only-config-1.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - PropertySource properties = getPropertiesFrom(configURL); - - System.out.println("simple-nested-string-only-config-1.json -> " + properties.getProperties().values()); - - assertTrue(properties.getProperties().keySet().size()>=5); - - PropertyValue keyB = properties.get("b"); - PropertyValue keyDO = properties.get("d.o"); - PropertyValue keyDP = properties.get("d.p"); - - assertThat(keyB, notNullValue()); - assertThat(keyB.getValue(), equalTo("B")); - assertThat(keyDO, notNullValue()); - assertThat(keyDO.getValue(), equalTo("O")); - assertThat(keyDP, Matchers.notNullValue()); - assertThat(keyDP.getValue(), is("P")); - } - - @Test - public void canReadNestedStringOnlyJSONConfigFileWithObjectInTheMiddle() - throws Exception { - URL configURL = CommonJSONTestCaseCollection.class - .getResource("/configs/valid/simple-nested-string-only-config-2.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - PropertySource properties = getPropertiesFrom(configURL); - - assertTrue(properties.getProperties().keySet().size()>=4); - - PropertyValue keyA = properties.get("a"); - PropertyValue keyDO = properties.get("b.o"); - PropertyValue keyDP = properties.get("b.p"); - PropertyValue keyC = properties.get("c"); - - assertThat(keyA, notNullValue()); - assertThat(keyA.getValue(), is("A")); - assertThat(keyC, notNullValue()); - assertThat(keyC.getValue(), equalTo("C")); - assertThat(keyDO, notNullValue()); - assertThat(keyDO.getValue(), equalTo("O")); - assertThat(keyDP, notNullValue()); - assertThat(keyDP.getValue(), is("P")); - } - - @Test - public void canHandleIllegalJSONFileWhichContainsAnArray() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/with-array.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - getPropertiesFrom(configURL).getProperties(); - } - - @Test(expected = IOException.class) - public void canHandleIllegalJSONFileConsistingOfOneOpeningBracket() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/only-opening-bracket.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - getPropertiesFrom(configURL).getProperties(); - } - - @Test(expected = IOException.class) - public void canHandleIllegalJSONFileWhichIsEmpty() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/empty-file.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - getPropertiesFrom(configURL).getProperties(); - } - - @Test - public void priorityInConfigFileOverwriteExplicitlyGivenPriority() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/valid/with-explicit-priority.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - PropertySource properties = getPropertiesFrom(configURL); - - assertThat(PropertySourceComparator.getOrdinal(properties), is(16784)); - } - - @Test - public void canReadFlatStringOnlyJSONConfigFile() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/valid/simple-flat-string-only-config.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - PropertySource properties = getPropertiesFrom(configURL); - - assertEquals(3, properties.getProperties().size()); - - PropertyValue keyA = properties.get("a"); - PropertyValue keyB = properties.get("b"); - PropertyValue keyC = properties.get("c"); - - assertThat(keyA, notNullValue()); - assertThat(keyA.getValue(), equalTo("A")); - assertThat(keyB, notNullValue()); - assertThat(keyB.getValue(), is("B")); - assertThat(keyC, notNullValue()); - assertThat(keyC.getValue(), is("C")); - } - - @Test(expected = IOException.class) - public void emptyJSONFileResultsInConfigException() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/invalid/empty-file.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - PropertySource properties = getPropertiesFrom(configURL); - - properties.getProperties(); - } - - @Test - public void canHandleEmptyJSONObject() throws Exception { - URL configURL = CommonJSONTestCaseCollection.class.getResource("/configs/valid/empty-object-config.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - PropertySource properties = getPropertiesFrom(configURL); - - assertTrue(properties.getProperties().keySet().size()>=0); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatIT.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatIT.java b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatIT.java deleted file mode 100644 index a13810e..0000000 --- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatIT.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.yaml; - -import org.apache.tamaya.format.ConfigurationFormat; -import org.apache.tamaya.json.JSONFormat; -import org.apache.tamaya.spi.ServiceContextManager; -import org.junit.Test; - -import java.util.List; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.notNullValue; - -/** - * Integration tests for {@link JSONFormat}. - */ -public class JSONFormatIT { - @Test - public void jsonFormatCanBeFoundViaServiceLoader() throws Exception { - List<ConfigurationFormat> formats = ServiceContextManager.getServiceContext() - .getServices(ConfigurationFormat.class); - - ConfigurationFormat format = null; - for (ConfigurationFormat f : formats) { - if (f instanceof JSONFormat) { - format = f; - break; - } - } - assertThat(format, notNullValue()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java b/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java deleted file mode 100644 index 216573e..0000000 --- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONFormatTest.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.yaml; - - -import org.apache.tamaya.format.ConfigurationData; -import org.apache.tamaya.format.MappedConfigurationDataPropertySource; -import org.apache.tamaya.json.JSONFormat; -import org.apache.tamaya.spi.PropertySource; -import org.junit.Test; - -import java.io.InputStream; -import java.net.URL; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; - -public class JSONFormatTest extends CommonJSONTestCaseCollection { - private final JSONFormat format = new JSONFormat(); - - @Test(expected = NullPointerException.class) - public void acceptsNeedsNonNullParameter() throws Exception { - format.accepts(null); - } - - @Test - public void aNonJSONFileBasedURLIsNotAccepted() throws Exception { - URL url = new URL("file:///etc/service/conf.conf"); - - assertThat(format.accepts(url), is(false)); - } - - @Test - public void aJSONFileBasedURLIsAccepted() throws Exception { - URL url = new URL("file:///etc/service/conf.json"); - - assertThat(format.accepts(url), is(true)); - } - - @Test - public void aHTTPBasedURLIsNotAccepted() throws Exception { - URL url = new URL("http://nowhere.somewhere/conf.json"); - assertThat(format.accepts(url), is(true)); - } - - @Test - public void aFTPBasedURLIsNotAccepted() throws Exception { - URL url = new URL("ftp://nowhere.somewhere/a/b/c/d/conf.json"); - - assertThat(format.accepts(url), is(true)); - } - - @Override - PropertySource getPropertiesFrom(URL source) throws Exception { - try (InputStream is = source.openStream()) { - ConfigurationData data = format.readConfiguration(source.toString(), is); - return new MappedConfigurationDataPropertySource(data); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/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 deleted file mode 100644 index ccd57b8..0000000 --- a/modules/formats/json/src/test/java/org/apache/tamaya/yaml/JSONPropertySourceTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.yaml; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.net.URL; - -import org.apache.tamaya.json.JSONPropertySource; -import org.apache.tamaya.spi.PropertySource; -import org.hamcrest.CoreMatchers; -import org.junit.Test; - -public class JSONPropertySourceTest extends CommonJSONTestCaseCollection { - - @Test - public void tamayaOrdinalKeywordIsNotPropagatedAsNormalProperty() throws Exception { - URL configURL = JSONPropertySourceTest.class.getResource("/configs/valid/with-explicit-priority.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - JSONPropertySource source = new JSONPropertySource(configURL, 4); - assertEquals(source.getOrdinal(), 16784); - } - - @Test - public void testAcceptJsonArrays() throws Exception { - URL configURL = JSONPropertySourceTest.class.getResource("/configs/invalid/array.json"); - - assertThat(configURL, CoreMatchers.notNullValue()); - - new JSONPropertySource(configURL); - } - - @Override - PropertySource getPropertiesFrom(URL source) throws Exception { - return new JSONPropertySource(source); - } -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/PathBasedYamlPropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/PathBasedYamlPropertySourceProvider.java b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/PathBasedYamlPropertySourceProvider.java new file mode 100644 index 0000000..795ac69 --- /dev/null +++ b/modules/formats/yaml/src/main/java/org/apache/tamaya/yaml/PathBasedYamlPropertySourceProvider.java @@ -0,0 +1,58 @@ +/* + * 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.yaml; + +import org.apache.tamaya.resource.AbstractPathPropertySourceProvider; +import org.apache.tamaya.spi.PropertySource; + +import java.io.IOException; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Path based default provider for yaml formatted config files. + */ +public class PathBasedYamlPropertySourceProvider extends AbstractPathPropertySourceProvider{ + + private static final Logger LOG = Logger.getLogger(PathBasedYamlPropertySourceProvider.class.getName()); + private YAMLFormat yamlFormat = new YAMLFormat(); + + public PathBasedYamlPropertySourceProvider(String... paths) { + super(paths); + } + + @Override + protected Collection<PropertySource> getPropertySources(URL url) { + if(yamlFormat.accepts(url)){ + try { + return Collections.singletonList( + yamlFormat.readConfiguration(url.toString(), url.openStream()).toPropertySource()); + } catch (IOException e) { + LOG.log(Level.SEVERE, "Failed to read yaml file: " +url, e); + } + } + return Collections.emptyList(); + } + +} + + http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/b82c4dc5/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/PathBasedYamlPropertySourceProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/PathBasedYamlPropertySourceProviderTest.java b/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/PathBasedYamlPropertySourceProviderTest.java new file mode 100644 index 0000000..061a82d --- /dev/null +++ b/modules/formats/yaml/src/test/java/org/apache/tamaya/yaml/PathBasedYamlPropertySourceProviderTest.java @@ -0,0 +1,65 @@ +/* + * 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.yaml; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for {@link PathBasedYamlPropertySourceProvider}. + */ +public class PathBasedYamlPropertySourceProviderTest { + + @Test + public void getPropertySources() { + PathBasedYamlPropertySourceProvider provider = new PathBasedYamlPropertySourceProvider( + "configs/valid/*.yaml" + ); + assertNotNull(provider.getPropertySources()); + assertEquals(3, provider.getPropertySources().size()); + } + + @Test + public void getPropertySources_one() { + PathBasedYamlPropertySourceProvider provider = new PathBasedYamlPropertySourceProvider( + "configs/valid/conta*.yaml" + ); + assertNotNull(provider.getPropertySources()); + assertEquals(1, provider.getPropertySources().size()); + } + + @Test + public void getPropertySources_two() { + PathBasedYamlPropertySourceProvider provider = new PathBasedYamlPropertySourceProvider( + "configs/valid/test*.yaml" + ); + assertNotNull(provider.getPropertySources()); + assertEquals(2, provider.getPropertySources().size()); + } + + @Test + public void getPropertySources_none() { + PathBasedYamlPropertySourceProvider provider = new PathBasedYamlPropertySourceProvider( + "configs/valid/foo*.yaml", "configs/valid/*.yml" + ); + assertNotNull(provider.getPropertySources()); + assertEquals(0, provider.getPropertySources().size()); + } +} \ No newline at end of file
