TAMAYA-260 Added buildable items.
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/98d31b71 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/98d31b71 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/98d31b71 Branch: refs/heads/master Commit: 98d31b717b1f97f1a76c375c40412d3532fa9540 Parents: 4f5d90e Author: Anatole Tresch <[email protected]> Authored: Sat Oct 21 22:38:05 2017 +0200 Committer: Anatole Tresch <[email protected]> Committed: Sat Oct 21 22:39:55 2017 +0200 ---------------------------------------------------------------------- .../spisupport/BuildablePropertySource.java | 226 +++++++++++++++++++ .../BuildablePropertySourceProvider.java | 114 ++++++++++ .../BuildablePropertySourceProviderTest.java | 76 +++++++ .../spisupport/BuildablePropertySourceTest.java | 88 ++++++++ 4 files changed, 504 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/98d31b71/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BuildablePropertySource.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BuildablePropertySource.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BuildablePropertySource.java new file mode 100644 index 0000000..e135644 --- /dev/null +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BuildablePropertySource.java @@ -0,0 +1,226 @@ +/* + * 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.spisupport; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertyValue; + +import java.util.*; + +/** + * A Buildable property source. + */ +public class BuildablePropertySource implements PropertySource{ + + private int ordinal; + private String name = "PropertySource-"+UUID.randomUUID().toString(); + private Map<String,PropertyValue> properties = new HashMap<>(); + + @Override + public int getOrdinal() { + return ordinal; + } + + @Override + public String getName() { + return name; + } + + @Override + public PropertyValue get(String key) { + return properties.get(key); + } + + @Override + public Map<String, PropertyValue> getProperties() { + return Collections.unmodifiableMap(properties); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + BuildablePropertySource that = (BuildablePropertySource) o; + + return name.equals(that.name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public String toString() { + return "BuildablePropertySource{" + + "ordinal=" + ordinal + + ", name='" + name + '\'' + + ", properties=" + properties + + '}'; + } + + /** + * Builder builder. + * + * @return the builder + */ + public static Builder builder() { + return new Builder(); + } + + + /** + * The type Builder. + */ + public static final class Builder { + private int ordinal; + private String source = "<on-the-fly-build>"; + private String name = "PropertySource-"+ UUID.randomUUID().toString(); + private Map<String,PropertyValue> properties = new HashMap<>(); + + private Builder() { + } + + /** + * With ordinal builder. + * + * @param ordinal the ordinal + * @return the builder + */ + public Builder withOrdinal(int ordinal) { + this.ordinal = ordinal; + return this; + } + + /** + * With source builder. + * + * @param source the source + * @return the builder + */ + public Builder withSource(String source) { + this.source = Objects.requireNonNull(source); + return this; + } + + /** + * With name builder. + * + * @param name the name + * @return the builder + */ + public Builder withName(String name) { + this.name = Objects.requireNonNull(name); + return this; + } + + /** + * With simple property builder. + * + * @param key the key + * @param value the value + * @return the builder + */ + public Builder withSimpleProperty(String key, String value) { + return withProperties(PropertyValue.of(key, value, this.source)); + } + + /** + * With simple property builder. + * + * @param key the key + * @param value the value + * @param source the source + * @return the builder + */ + public Builder withSimpleProperty(String key, String value, String source) { + return withProperties(PropertyValue.of(key, value, source)); + } + + /** + * With properties builder. + * + * @param values the values + * @return the builder + */ + public Builder withProperties(PropertyValue... values) { + for(PropertyValue val:values){ + this.properties.put(val.getKey(), val); + } + return this; + } + + /** + * With properties builder. + * + * @param properties the properties + * @return the builder + */ + public Builder withProperties(Map<String, PropertyValue> properties) { + this.properties = Objects.requireNonNull(properties); + return this; + } + + /** + * With properties builder. + * + * @param properties the properties + * @param source the source + * @return the builder + */ + public Builder withProperties(Map<String, String> properties, String source) { + this.properties.putAll(PropertyValue.map(properties, source)); + return this; + } + + /** + * With simple properties builder. + * + * @param properties the properties + * @return the builder + */ + public Builder withSimpleProperties(Map<String, String> properties) { + this.properties.putAll(PropertyValue.map(properties, this.source)); + return this; + } + + /** + * But builder. + * + * @return the builder + */ + public Builder but() { + return builder().withOrdinal(ordinal).withName(name).withProperties(properties); + } + + /** + * Build buildable property source. + * + * @return the buildable property source + */ + public BuildablePropertySource build() { + BuildablePropertySource buildablePropertySource = new BuildablePropertySource(); + buildablePropertySource.name = this.name; + buildablePropertySource.properties = this.properties; + buildablePropertySource.ordinal = this.ordinal; + return buildablePropertySource; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/98d31b71/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BuildablePropertySourceProvider.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BuildablePropertySourceProvider.java b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BuildablePropertySourceProvider.java new file mode 100644 index 0000000..8341a44 --- /dev/null +++ b/modules/spi-support/src/main/java/org/apache/tamaya/spisupport/BuildablePropertySourceProvider.java @@ -0,0 +1,114 @@ +/* + * 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.spisupport; + +import org.apache.tamaya.spi.PropertySource; +import org.apache.tamaya.spi.PropertySourceProvider; + +import java.util.*; + +/** + * A Buildable property source. + */ +public class BuildablePropertySourceProvider implements PropertySourceProvider{ + + private List<PropertySource> sources = new ArrayList<>(); + + @Override + public Collection<PropertySource> getPropertySources() { + return Collections.unmodifiableCollection(sources); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + BuildablePropertySourceProvider that = (BuildablePropertySourceProvider) o; + + return sources.equals(that.sources); + } + + @Override + public int hashCode() { + return sources.hashCode(); + } + + @Override + public String toString() { + return "BuildablePropertySourceProvider{" + + "sources=" + sources + + '}'; + } + + /** + * Builder builder. + * + * @return the builder + */ + public static Builder builder() { + return new Builder(); + } + + + + + /** + * The type Builder. + */ + public static final class Builder { + private List<PropertySource> sources = new ArrayList<>(); + + private Builder() { + } + + /** + * With propertySources. + * + * @param propertySources the propertySources + * @return the builder + */ + public Builder withPropertySourcs(PropertySource... propertySources) { + this.sources.addAll(Arrays.asList(propertySources)); + return this; + } + + /** + * With property sources builder. + * + * @param sources the property sources + * @return the builder + */ + public Builder withPropertySourcs(Collection<PropertySource> sources) { + this.sources.addAll(sources); + return this; + } + + /** + * Build buildable property source. + * + * @return the buildable property source + */ + public BuildablePropertySourceProvider build() { + BuildablePropertySourceProvider buildablePropertySource = new BuildablePropertySourceProvider(); + buildablePropertySource.sources.addAll(this.sources); + return buildablePropertySource; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/98d31b71/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceProviderTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceProviderTest.java new file mode 100644 index 0000000..cab05d2 --- /dev/null +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceProviderTest.java @@ -0,0 +1,76 @@ +/* + * 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.spisupport; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BuildablePropertySourceProviderTest { + + @Test + public void getPropertySources() throws Exception { + BuildablePropertySource ps = BuildablePropertySource.builder() + .withName("test1").build(); + BuildablePropertySourceProvider prov = BuildablePropertySourceProvider.builder() + .withPropertySourcs(ps).build(); + assertNotNull(prov); + assertEquals(prov.getPropertySources().iterator().next(), ps); + } + + @Test + public void equals() throws Exception { + BuildablePropertySource ps = BuildablePropertySource.builder() + .withName("test1").build(); + BuildablePropertySourceProvider prov1 = BuildablePropertySourceProvider.builder() + .withPropertySourcs(ps).build(); + BuildablePropertySourceProvider prov2 = BuildablePropertySourceProvider.builder() + .withPropertySourcs(ps).build(); + assertEquals(prov1, prov2); + BuildablePropertySource ps2 = BuildablePropertySource.builder() + .withName("test12").build(); + prov2 = BuildablePropertySourceProvider.builder() + .withPropertySourcs(ps2).build(); + assertNotEquals(prov1, prov2); + } + + @Test + public void testHashCode() throws Exception { + BuildablePropertySource ps = BuildablePropertySource.builder() + .withName("test1").build(); + BuildablePropertySourceProvider prov1 = BuildablePropertySourceProvider.builder() + .withPropertySourcs(ps).build(); + BuildablePropertySourceProvider prov2 = BuildablePropertySourceProvider.builder() + .withPropertySourcs(ps).build(); + assertEquals(prov1.hashCode(), prov2.hashCode()); + BuildablePropertySource ps2 = BuildablePropertySource.builder() + .withName("test12").build(); + prov2 = BuildablePropertySourceProvider.builder() + .withPropertySourcs(ps2).build(); + assertNotEquals(prov1.hashCode(), prov2.hashCode()); + } + + + @Test + public void builder() throws Exception { + assertNotNull(BuildablePropertySource.builder()); + assertNotEquals(BuildablePropertySource.builder(), BuildablePropertySource.builder()); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/98d31b71/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java ---------------------------------------------------------------------- diff --git a/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java new file mode 100644 index 0000000..721216d --- /dev/null +++ b/modules/spi-support/src/test/java/org/apache/tamaya/spisupport/BuildablePropertySourceTest.java @@ -0,0 +1,88 @@ +/* + * 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.spisupport; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BuildablePropertySourceTest { + @Test + public void getOrdinal() throws Exception { + BuildablePropertySource ps1 = BuildablePropertySource.builder() + .withOrdinal(55).build(); + assertEquals(55, ps1.getOrdinal()); + } + + @Test + public void getName() throws Exception { + BuildablePropertySource ps1 = BuildablePropertySource.builder() + .withName("test1").build(); + assertEquals("test1", ps1.getName()); + ps1 = BuildablePropertySource.builder().build(); + assertNotNull(ps1.getName()); + } + + @Test + public void get() throws Exception { + BuildablePropertySource ps1 = BuildablePropertySource.builder() + .withSimpleProperty("a", "b").build(); + assertEquals("b", ps1.get("a").getValue()); + } + + @Test + public void getProperties() throws Exception { + BuildablePropertySource ps1 = BuildablePropertySource.builder() + .withSimpleProperty("a", "b").build(); + assertNotNull(ps1.getProperties()); + assertEquals(1, ps1.getProperties().size()); + assertEquals("b", ps1.getProperties().get("a").getValue()); + } + + @Test + public void equals() throws Exception { + BuildablePropertySource ps1 = BuildablePropertySource.builder() + .withName("test1").build(); + BuildablePropertySource ps2 = BuildablePropertySource.builder() + .withName("test1").build(); + assertEquals(ps1, ps2); + ps2 = BuildablePropertySource.builder() + .withName("test2").build(); + assertNotEquals(ps1, ps2); + } + + @Test + public void testHashCode() throws Exception { + BuildablePropertySource ps1 = BuildablePropertySource.builder() + .withName("test1").build(); + BuildablePropertySource ps2 = BuildablePropertySource.builder() + .withName("test1").build(); + assertEquals(ps1.hashCode(), ps2.hashCode()); + ps2 = BuildablePropertySource.builder() + .withName("test2").build(); + assertNotEquals(ps1.hashCode(), ps2.hashCode()); + } + + @Test + public void builder() throws Exception { + assertNotNull(BuildablePropertySource.builder()); + assertNotEquals(BuildablePropertySource.builder(), BuildablePropertySource.builder()); + } + +} \ No newline at end of file
