Repository: incubator-tamaya-extensions Updated Branches: refs/heads/configjsr ddfdbbb93 -> 2abb7a772
Fixed some license refs. Renamed some artifacts for better compliance with JSR. Signed-off-by: Anatole Tresch <[email protected]> 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/319f435c Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/tree/319f435c Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/diff/319f435c Branch: refs/heads/configjsr Commit: 319f435c3d830f701d731033995ff81dbebe3443 Parents: ddfdbbb Author: Anatole Tresch <[email protected]> Authored: Sun Apr 22 18:24:04 2018 +0200 Committer: Anatole Tresch <[email protected]> Committed: Sun Apr 22 18:24:04 2018 +0200 ---------------------------------------------------------------------- .../tamaya/functions/FunctionalConfig.java | 130 +++++++++++ .../tamaya/mutableconfig/MutableConfig.java | 126 +++++++++++ .../mutableconfig/MutableConfigProvider.java | 216 +++++++++++++++++++ .../mutableconfig/MutableConfiguration.java | 126 ----------- .../MutableConfigurationProvider.java | 216 ------------------- .../internal/DefaultMutableConfiguration.java | 14 +- .../DefaultMutableConfigurationSpi.java | 10 +- .../spi/MutableConfigProviderSpi.java | 44 ++++ .../spi/MutableConfigurationProviderSpi.java | 43 ---- ...a.mutableconfig.spi.MutableConfigProviderSpi | 19 ++ ...leconfig.spi.MutableConfigurationProviderSpi | 19 -- .../MutableConfigProviderTest.java | 85 ++++++++ .../tamaya/mutableconfig/MutableConfigTest.java | 186 ++++++++++++++++ .../MutableConfigurationProviderTest.java | 85 -------- .../mutableconfig/MutableConfigurationTest.java | 186 ---------------- 15 files changed, 818 insertions(+), 687 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/functions/src/main/java/org/apache/tamaya/functions/FunctionalConfig.java ---------------------------------------------------------------------- diff --git a/modules/functions/src/main/java/org/apache/tamaya/functions/FunctionalConfig.java b/modules/functions/src/main/java/org/apache/tamaya/functions/FunctionalConfig.java new file mode 100644 index 0000000..0f36ed8 --- /dev/null +++ b/modules/functions/src/main/java/org/apache/tamaya/functions/FunctionalConfig.java @@ -0,0 +1,130 @@ +/* + * 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.functions; + +import org.apache.tamaya.base.ConfigContext; +import org.apache.tamaya.base.ConfigContextSupplier; +import org.apache.tamaya.base.TamayaConfigBuilder; + +import javax.config.Config; +import javax.config.ConfigValue; +import javax.config.spi.ConfigSource; +import java.util.Optional; +import java.util.Set; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.UnaryOperator; + + +/** + * Config interface with functional extension points. + */ +public interface FunctionalConfig extends Config, ConfigContextSupplier { + + /** + * Enriches a {@link Config} instance with functional access points. + * @param config the config, not null + * @return a functional config instance. + */ + static FunctionalConfig of(Config config){ + if(config instanceof FunctionalConfig){ + // Adapt it only once... + return (FunctionalConfig)config; + } + return new FunctionalConfig() { + @Override + public ConfigContext getConfigContext() { + return ConfigContext.from(config); + } + + @Override + public <T> T getValue(String key, Class<T> type) { + return config.getValue(key, type); + } + + @Override + public <T> Optional<T> getOptionalValue(String key, Class<T> type) { + return config.getOptionalValue(key, type); + } + + @Override + public ConfigValue<String> access(String key) { + return config.access(key); + } + + @Override + public Iterable<String> getPropertyNames() { + return config.getPropertyNames(); + } + + @Override + public Iterable<ConfigSource> getConfigSources() { + return config.getConfigSources(); + } + + @Override + public void registerConfigChangedListener(Consumer<Set<String>> consumer) { + config.registerConfigChangedListener(consumer); + } + + @Override + public String toString(){ + return "Functional:"+config.toString(); + } + }; + } + + /** + * Extension point for adjusting configuration. + * + * @param operator A configuration operator, e.g. a filter, or an adjuster + * combining configurations, never {@code null}. + * @return the new adjusted configuration returned by the {@code operator}, never {@code null}. + */ + default FunctionalConfig with(UnaryOperator<Config> operator){ + return FunctionalConfig.of(operator.apply(this)); + } + + /** + * Query a configuration. + * + * @param <T> the type of the configuration. + * @param query the query, not {@code null}. + * @return the result returned by the {@code query}. + */ + default <T> T query(Function<Config, T> query){ + return query.apply(this); + } + + /** + * Access a configuration's context. + * @return the configuration context, never null. + */ + default ConfigContext getContext(){ + return ConfigContext.from(this); + } + + /** + * Create a new builder using this instance as it's base. + * @return a new builder, never null. + */ + default TamayaConfigBuilder toBuilder() { + return TamayaConfigBuilder.create(this); + } +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfig.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfig.java b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfig.java new file mode 100644 index 0000000..29a54f9 --- /dev/null +++ b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfig.java @@ -0,0 +1,126 @@ +/* + * 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.mutableconfig; + +import org.apache.tamaya.mutableconfig.spi.MutableConfigSource; + +import javax.config.Config; +import java.util.Collection; +import java.util.Map; + + +/** + * This interface extends the Configuration interface hereby adding methods to change configuration entries. + * Hereby not all configuration entries are necessarily mutable, since some entries may be read from non + * mutable areas of configuration. Of course, it is always possible to add a mutable shadow layer on top of all + * property sources to persist/control any changes applied. The exact management and storage persistence algorithm + * should be transparent. + * + * As a consequence clients should first check, using the corresponding methods, if entries can be added/updated or + * removed. + * + * This class should only used in a single threaded context, though all methods inherited from {@link Config} + * must be thread-safe. Methods handling configuration changes are expected to be used in a single threaded environment + * only. For multi-threaded us create a new instance of {@link MutableConfig} for each thread. + */ +public interface MutableConfig extends Config { + + /** + * Storesd the changes. After a commit the change is not editable anymore. All changes applied will be written to + * the corresponding configuration backend. + * + * NOTE that changes applied must not necessarily be visible in the current {@link Config} instance, + * since visibility of changes also depends on the ordinals set on the {@link javax.config.spi.ConfigSource}s + * configured. + * @throws IllegalStateException if the request already has been committed or cancelled, or the commit fails. + */ + void store(); + + /** + * Access the current configuration change context, built up on all the change context of the participating + * {@link MutableConfigSource} instances. + * @return the colleted changes as one single config change for the current transaction, or null, if no transaction + * is active. + */ + ConfigChangeRequest getConfigChangeRequest(); + + /** + * Access the active {@link ChangePropagationPolicy}.This policy controls how configuration changes are written/published + * to the known {@link MutableConfigSource} instances of a {@link Config}. + * @return he active {@link ChangePropagationPolicy}, never null. + */ + ChangePropagationPolicy getChangePropagationPolicy(); + + /** + * Sets a property. + * + * @param key the property's key, not null. + * @param value the property's value, not null. + * @return the former property value, or null. + * @throws IllegalStateException if the key/value cannot be added, or the request is read-only. + */ + MutableConfig put(String key, String value); + + /** + * Puts all given configuration entries. This method should check that all given properties are + * basically removable, as defined by #isWritable. If any of the passed keys is not writable during this initial + * check, the operation should not perform any configuration changes and throw a + * {@link IllegalArgumentException}. If errors occur afterwards, when the properties are effectively + * written back to the backends, the errors should be collected and returned as part of the ConfigException + * payload. Nevertheless the operation should in that case remove all entries as far as possible and abort the + * writing operation. + * + * @param properties the properties tobe written, not null. + * @return the config change request + * @throws IllegalStateException if any of the given properties could not be written, or the request + * is read-only. + */ + MutableConfig putAll(Map<String, String> properties); + + /** + * Removes all given configuration entries. This method should check that all given properties are + * basically removable, as defined by #isRemovable. If any of the passed keys is not removable during this initial + * check, the operation should not perform any configuration changes and throw a + * {@link IllegalArgumentException}. If errors + * occur afterwards, when the properties are effectively written back to the backends, the errors should be + * collected and returned as part of the ConfigException payload. Nevertheless the operation should in that case + * remove all entries as far as possible and abort the writing operation. + * + * @param keys the property's keys to be removedProperties, not null. + * @return the config change request + * @throws IllegalStateException if any of the given keys could not be removedProperties, or the + * request is read-only. + */ + MutableConfig remove(Collection<String> keys); + + /** + * Removes all given configuration entries. This method should check that all given properties are + * basically removable, as defined by #isRemovable. If any of the passed keys is not removable during this initial + * check, the operation should not perform any configuration changes and throw a {@link IllegalArgumentException}. If errors + * occur afterwards, when the properties are effectively written back to the backends, the errors should be + * collected and returned as part of the ConfigException payload. Nevertheless the operation should in that case + * remove all entries as far as possible and abort the writing operation. + * + * @param keys the property's keys to be removedProperties, not null. + * @return the config change request + * @throws IllegalStateException if any of the given keys could not be removedProperties, or the request is read-only. + */ + MutableConfig remove(String... keys); + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfigProvider.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfigProvider.java b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfigProvider.java new file mode 100644 index 0000000..6cf277d --- /dev/null +++ b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfigProvider.java @@ -0,0 +1,216 @@ +/* + * 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.mutableconfig; + +import org.apache.tamaya.mutableconfig.spi.MutableConfigProviderSpi; +import org.apache.tamaya.mutableconfig.spi.MutableConfigSource; +import org.apache.tamaya.base.ServiceContextManager; + +import javax.config.Config; +import javax.config.ConfigProvider; +import javax.config.spi.ConfigSource; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.logging.Logger; + + +/** + * Accessor for creating {@link MutableConfig} instances to change configuration and commit changes. + */ +public final class MutableConfigProvider { + + private static final Logger LOG = Logger.getLogger(MutableConfigProvider.class.getName()); + /** + * URIs used by this query instance to identify the backends to use for write operations. + */ + private static MutableConfigProviderSpi spi(){ + MutableConfigProviderSpi spi = ServiceContextManager.getServiceContext().getService( + MutableConfigProviderSpi.class) ; + if(spi==null){ + throw new IllegalArgumentException("Failed to initialize MutableConfigurationProviderSpi - " + + "mutable configuration support."); + } + return spi; + } + + + /** Singleton constructor. */ + private MutableConfigProvider(){} + + /** + * Creates a new {@link MutableConfig} for the given default configuration, using all + * {@link MutableConfigSource} instances found in its context and {@code autoCommit = false}. + * + * @return a new MutableConfiguration instance + */ + public static MutableConfig createMutableConfig(){ + return spi().createMutableConfig( + ConfigProvider.getConfig(), getApplyMostSignificantOnlyChangePolicy()); + } + + /** + * Creates a new {@link MutableConfig} for the given default configuration, using all + * {@link MutableConfigSource} instances found in its context and {@code autoCommit = false}. + * @param changePropgationPolicy policy that defines how a change is written back and which property + * sources are finally eligible for a write operation. + * @return a new MutableConfiguration instance, with the given change policy active. + */ + public static MutableConfig createMutableConfig(ChangePropagationPolicy changePropgationPolicy){ + return spi().createMutableConfig( + ConfigProvider.getConfig(), changePropgationPolicy); + } + + + /** + * Creates a new {@link MutableConfig} for the given configuration, using all + * {@link MutableConfigSource} instances found in its context and {@code MOST_SIGNIFICANT_ONLY_POLICY} + * configuration writing policy. + * + * @param configuration the configuration to use to write the changes/config. + * @return a new MutableConfiguration instance + */ + public static MutableConfig createMutableConfig(Config configuration){ + return createMutableConfig(configuration, MOST_SIGNIFICANT_ONLY_POLICY); + } + + /** + * Creates a new {@link MutableConfig} for the given configuration, using all + * {@link MutableConfigSource} instances found in its context and {@code ALL_POLICY} + * configuration writing policy. + * + * @param configuration the configuration to use to write the changes/config. + * @param changePropagationPolicy the configuration writing policy. + * @return a new MutableConfiguration instance + */ + public static MutableConfig createMutableConfig(Config configuration, ChangePropagationPolicy changePropagationPolicy){ + return spi().createMutableConfig(configuration, changePropagationPolicy); + } + + /** + * This propagation policy writes through all changes to all mutable property sources, where applicable. + * This is also the default policy. + * @return default all policy. + */ + public static ChangePropagationPolicy getApplyAllChangePolicy(){ + return ALL_POLICY; + } + + /** + * This propagation policy writes changes only once to the most significant property source, where a change is + * applicable. + * @return a corresponding {@link ChangePropagationPolicy} implementation, never null. + */ + public static ChangePropagationPolicy getApplyMostSignificantOnlyChangePolicy(){ + return MOST_SIGNIFICANT_ONLY_POLICY; + } + + /** + * This propagation policy writes changes only once to the most significant property source, where a change is + * applicable. + * @param propertySourceNames the names of the mutable property sources to be considered for writing any changes to. + * @return a corresponding {@link ChangePropagationPolicy} implementation, never null. + */ + public static ChangePropagationPolicy getApplySelectiveChangePolicy(String... propertySourceNames){ + return new SelectiveChangeApplyPolicy(propertySourceNames); + } + + /** + * This propagation policy writes changes only once to the most significant property source, where a change is + * applicable. + * @return a corresponding {@link ChangePropagationPolicy} implementation, never null. + */ + public static ChangePropagationPolicy getApplyNonePolicy(){ + return NONE_POLICY; + } + + /** + * This propagation policy writes through all changes to all mutable property sources, where applicable. + */ + private static final ChangePropagationPolicy ALL_POLICY = (change, propertySources) -> { + for(ConfigSource propertySource: propertySources){ + if(propertySource instanceof MutableConfigSource){ + MutableConfigSource target = (MutableConfigSource)propertySource; + try{ + target.applyChange(change); + }catch(Exception e){ + LOG.warning("Failed to store changes '"+change+"' not applicable to "+target.getName() + +"("+target.getClass().getName()+")."); + } + } + } + }; + + /** + * This propagation policy writes changes only once to the most significant property source, where a change is + * applicable. + */ + private static final ChangePropagationPolicy MOST_SIGNIFICANT_ONLY_POLICY = (change, propertySources) -> { + for(ConfigSource propertySource: propertySources){ + if(propertySource instanceof MutableConfigSource){ + MutableConfigSource target = (MutableConfigSource)propertySource; + try{ + target.applyChange(change); + }catch(Exception e){ + LOG.warning("Failed to store changes '"+change+"' not applicable to "+target.getName() + +"("+target.getClass().getName()+")."); + } + break; + } + } + }; + + /** + * This propagation policy writes changes only once to the most significant property source, where a change is + * applicable. + */ + private static final ChangePropagationPolicy NONE_POLICY = (change, propertySources) -> LOG.warning("Cannot store changes '"+change+"': prohibited by change policy (read-only)."); + + /** + * This propagation policy writes through all changes to all mutable property sources, where applicable. + */ + private static final class SelectiveChangeApplyPolicy implements ChangePropagationPolicy { + + private Set<String> propertySourceNames = new HashSet<>(); + + SelectiveChangeApplyPolicy(String... propertySourceNames){ + this.propertySourceNames.addAll(Arrays.asList(propertySourceNames)); + } + + @Override + public void applyChange(ConfigChangeRequest change, Iterable<ConfigSource> propertySources) { + for(ConfigSource propertySource: propertySources){ + if(propertySource instanceof MutableConfigSource){ + if(this.propertySourceNames.contains(propertySource.getName())) { + MutableConfigSource target = (MutableConfigSource) propertySource; + try{ + target.applyChange(change); + }catch(Exception e){ + LOG.warning("Failed to store changes '"+change+"' not applicable to "+target.getName() + +"("+target.getClass().getName()+")."); + } + break; + } + } + } + } + } + + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfiguration.java b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfiguration.java deleted file mode 100644 index 580e283..0000000 --- a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfiguration.java +++ /dev/null @@ -1,126 +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.mutableconfig; - -import org.apache.tamaya.mutableconfig.spi.MutableConfigSource; - -import javax.config.Config; -import java.util.Collection; -import java.util.Map; - - -/** - * This interface extends the Configuration interface hereby adding methods to change configuration entries. - * Hereby not all configuration entries are necessarily mutable, since some entries may be read from non - * mutable areas of configuration. Of course, it is always possible to add a mutable shadow layer on top of all - * property sources to persist/control any changes applied. The exact management and storage persistence algorithm - * should be transparent. - * - * As a consequence clients should first check, using the corresponding methods, if entries can be added/updated or - * removed. - * - * This class should only used in a single threaded context, though all methods inherited from {@link Config} - * must be thread-safe. Methods handling configuration changes are expected to be used in a single threaded environment - * only. For multi-threaded us create a new instance of {@link MutableConfiguration} for each thread. - */ -public interface MutableConfiguration extends Config { - - /** - * Storesd the changes. After a commit the change is not editable anymore. All changes applied will be written to - * the corresponding configuration backend. - * - * NOTE that changes applied must not necessarily be visible in the current {@link Config} instance, - * since visibility of changes also depends on the ordinals set on the {@link javax.config.spi.ConfigSource}s - * configured. - * @throws IllegalStateException if the request already has been committed or cancelled, or the commit fails. - */ - void store(); - - /** - * Access the current configuration change context, built up on all the change context of the participating - * {@link MutableConfigSource} instances. - * @return the colleted changes as one single config change for the current transaction, or null, if no transaction - * is active. - */ - ConfigChangeRequest getConfigChangeRequest(); - - /** - * Access the active {@link ChangePropagationPolicy}.This policy controls how configuration changes are written/published - * to the known {@link MutableConfigSource} instances of a {@link Config}. - * @return he active {@link ChangePropagationPolicy}, never null. - */ - ChangePropagationPolicy getChangePropagationPolicy(); - - /** - * Sets a property. - * - * @param key the property's key, not null. - * @param value the property's value, not null. - * @return the former property value, or null. - * @throws IllegalStateException if the key/value cannot be added, or the request is read-only. - */ - MutableConfiguration put(String key, String value); - - /** - * Puts all given configuration entries. This method should check that all given properties are - * basically removable, as defined by #isWritable. If any of the passed keys is not writable during this initial - * check, the operation should not perform any configuration changes and throw a - * {@link IllegalArgumentException}. If errors occur afterwards, when the properties are effectively - * written back to the backends, the errors should be collected and returned as part of the ConfigException - * payload. Nevertheless the operation should in that case remove all entries as far as possible and abort the - * writing operation. - * - * @param properties the properties tobe written, not null. - * @return the config change request - * @throws IllegalStateException if any of the given properties could not be written, or the request - * is read-only. - */ - MutableConfiguration putAll(Map<String, String> properties); - - /** - * Removes all given configuration entries. This method should check that all given properties are - * basically removable, as defined by #isRemovable. If any of the passed keys is not removable during this initial - * check, the operation should not perform any configuration changes and throw a - * {@link IllegalArgumentException}. If errors - * occur afterwards, when the properties are effectively written back to the backends, the errors should be - * collected and returned as part of the ConfigException payload. Nevertheless the operation should in that case - * remove all entries as far as possible and abort the writing operation. - * - * @param keys the property's keys to be removedProperties, not null. - * @return the config change request - * @throws IllegalStateException if any of the given keys could not be removedProperties, or the - * request is read-only. - */ - MutableConfiguration remove(Collection<String> keys); - - /** - * Removes all given configuration entries. This method should check that all given properties are - * basically removable, as defined by #isRemovable. If any of the passed keys is not removable during this initial - * check, the operation should not perform any configuration changes and throw a {@link IllegalArgumentException}. If errors - * occur afterwards, when the properties are effectively written back to the backends, the errors should be - * collected and returned as part of the ConfigException payload. Nevertheless the operation should in that case - * remove all entries as far as possible and abort the writing operation. - * - * @param keys the property's keys to be removedProperties, not null. - * @return the config change request - * @throws IllegalStateException if any of the given keys could not be removedProperties, or the request is read-only. - */ - MutableConfiguration remove(String... keys); - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfigurationProvider.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfigurationProvider.java b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfigurationProvider.java deleted file mode 100644 index f5af5dc..0000000 --- a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/MutableConfigurationProvider.java +++ /dev/null @@ -1,216 +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.mutableconfig; - -import org.apache.tamaya.mutableconfig.spi.MutableConfigurationProviderSpi; -import org.apache.tamaya.mutableconfig.spi.MutableConfigSource; -import org.apache.tamaya.base.ServiceContextManager; - -import javax.config.Config; -import javax.config.ConfigProvider; -import javax.config.spi.ConfigSource; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import java.util.logging.Logger; - - -/** - * Accessor for creating {@link MutableConfiguration} instances to change configuration and commit changes. - */ -public final class MutableConfigurationProvider { - - private static final Logger LOG = Logger.getLogger(MutableConfigurationProvider.class.getName()); - /** - * URIs used by this query instance to identify the backends to use for write operations. - */ - private static MutableConfigurationProviderSpi spi(){ - MutableConfigurationProviderSpi spi = ServiceContextManager.getServiceContext().getService( - MutableConfigurationProviderSpi.class) ; - if(spi==null){ - throw new IllegalArgumentException("Failed to initialize MutableConfigurationProviderSpi - " + - "mutable configuration support."); - } - return spi; - } - - - /** Singleton constructor. */ - private MutableConfigurationProvider(){} - - /** - * Creates a new {@link MutableConfiguration} for the given default configuration, using all - * {@link MutableConfigSource} instances found in its context and {@code autoCommit = false}. - * - * @return a new MutableConfiguration instance - */ - public static MutableConfiguration createMutableConfiguration(){ - return spi().createMutableConfiguration( - ConfigProvider.getConfig(), getApplyMostSignificantOnlyChangePolicy()); - } - - /** - * Creates a new {@link MutableConfiguration} for the given default configuration, using all - * {@link MutableConfigSource} instances found in its context and {@code autoCommit = false}. - * @param changePropgationPolicy policy that defines how a change is written back and which property - * sources are finally eligible for a write operation. - * @return a new MutableConfiguration instance, with the given change policy active. - */ - public static MutableConfiguration createMutableConfiguration(ChangePropagationPolicy changePropgationPolicy){ - return spi().createMutableConfiguration( - ConfigProvider.getConfig(), changePropgationPolicy); - } - - - /** - * Creates a new {@link MutableConfiguration} for the given configuration, using all - * {@link MutableConfigSource} instances found in its context and {@code MOST_SIGNIFICANT_ONLY_POLICY} - * configuration writing policy. - * - * @param configuration the configuration to use to write the changes/config. - * @return a new MutableConfiguration instance - */ - public static MutableConfiguration createMutableConfiguration(Config configuration){ - return createMutableConfiguration(configuration, MOST_SIGNIFICANT_ONLY_POLICY); - } - - /** - * Creates a new {@link MutableConfiguration} for the given configuration, using all - * {@link MutableConfigSource} instances found in its context and {@code ALL_POLICY} - * configuration writing policy. - * - * @param configuration the configuration to use to write the changes/config. - * @param changePropagationPolicy the configuration writing policy. - * @return a new MutableConfiguration instance - */ - public static MutableConfiguration createMutableConfiguration(Config configuration, ChangePropagationPolicy changePropagationPolicy){ - return spi().createMutableConfiguration(configuration, changePropagationPolicy); - } - - /** - * This propagation policy writes through all changes to all mutable property sources, where applicable. - * This is also the default policy. - * @return default all policy. - */ - public static ChangePropagationPolicy getApplyAllChangePolicy(){ - return ALL_POLICY; - } - - /** - * This propagation policy writes changes only once to the most significant property source, where a change is - * applicable. - * @return a corresponding {@link ChangePropagationPolicy} implementation, never null. - */ - public static ChangePropagationPolicy getApplyMostSignificantOnlyChangePolicy(){ - return MOST_SIGNIFICANT_ONLY_POLICY; - } - - /** - * This propagation policy writes changes only once to the most significant property source, where a change is - * applicable. - * @param propertySourceNames the names of the mutable property sources to be considered for writing any changes to. - * @return a corresponding {@link ChangePropagationPolicy} implementation, never null. - */ - public static ChangePropagationPolicy getApplySelectiveChangePolicy(String... propertySourceNames){ - return new SelectiveChangeApplyPolicy(propertySourceNames); - } - - /** - * This propagation policy writes changes only once to the most significant property source, where a change is - * applicable. - * @return a corresponding {@link ChangePropagationPolicy} implementation, never null. - */ - public static ChangePropagationPolicy getApplyNonePolicy(){ - return NONE_POLICY; - } - - /** - * This propagation policy writes through all changes to all mutable property sources, where applicable. - */ - private static final ChangePropagationPolicy ALL_POLICY = (change, propertySources) -> { - for(ConfigSource propertySource: propertySources){ - if(propertySource instanceof MutableConfigSource){ - MutableConfigSource target = (MutableConfigSource)propertySource; - try{ - target.applyChange(change); - }catch(Exception e){ - LOG.warning("Failed to store changes '"+change+"' not applicable to "+target.getName() - +"("+target.getClass().getName()+")."); - } - } - } - }; - - /** - * This propagation policy writes changes only once to the most significant property source, where a change is - * applicable. - */ - private static final ChangePropagationPolicy MOST_SIGNIFICANT_ONLY_POLICY = (change, propertySources) -> { - for(ConfigSource propertySource: propertySources){ - if(propertySource instanceof MutableConfigSource){ - MutableConfigSource target = (MutableConfigSource)propertySource; - try{ - target.applyChange(change); - }catch(Exception e){ - LOG.warning("Failed to store changes '"+change+"' not applicable to "+target.getName() - +"("+target.getClass().getName()+")."); - } - break; - } - } - }; - - /** - * This propagation policy writes changes only once to the most significant property source, where a change is - * applicable. - */ - private static final ChangePropagationPolicy NONE_POLICY = (change, propertySources) -> LOG.warning("Cannot store changes '"+change+"': prohibited by change policy (read-only)."); - - /** - * This propagation policy writes through all changes to all mutable property sources, where applicable. - */ - private static final class SelectiveChangeApplyPolicy implements ChangePropagationPolicy { - - private Set<String> propertySourceNames = new HashSet<>(); - - SelectiveChangeApplyPolicy(String... propertySourceNames){ - this.propertySourceNames.addAll(Arrays.asList(propertySourceNames)); - } - - @Override - public void applyChange(ConfigChangeRequest change, Iterable<ConfigSource> propertySources) { - for(ConfigSource propertySource: propertySources){ - if(propertySource instanceof MutableConfigSource){ - if(this.propertySourceNames.contains(propertySource.getName())) { - MutableConfigSource target = (MutableConfigSource) propertySource; - try{ - target.applyChange(change); - }catch(Exception e){ - LOG.warning("Failed to store changes '"+change+"' not applicable to "+target.getName() - +"("+target.getClass().getName()+")."); - } - break; - } - } - } - } - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfiguration.java b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfiguration.java index 7d331aa..d8d137d 100644 --- a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfiguration.java +++ b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfiguration.java @@ -19,7 +19,7 @@ package org.apache.tamaya.mutableconfig.internal; import org.apache.tamaya.mutableconfig.ChangePropagationPolicy; -import org.apache.tamaya.mutableconfig.MutableConfiguration; +import org.apache.tamaya.mutableconfig.MutableConfig; import org.apache.tamaya.mutableconfig.ConfigChangeRequest; import org.apache.tamaya.mutableconfig.spi.MutableConfigSource; import org.osgi.service.component.annotations.Component; @@ -31,10 +31,10 @@ import java.util.logging.Logger; /** - * Default implementation of a {@link MutableConfiguration}. + * Default implementation of a {@link MutableConfig}. */ @Component -public class DefaultMutableConfiguration implements MutableConfiguration { +public class DefaultMutableConfiguration implements MutableConfig { private static final Logger LOG = Logger.getLogger(DefaultMutableConfiguration.class.getName()); private ConfigChangeRequest changeRequest = new ConfigChangeRequest(UUID.randomUUID().toString()); private final Config config; @@ -67,19 +67,19 @@ public class DefaultMutableConfiguration implements MutableConfiguration { @Override - public MutableConfiguration put(String key, String value) { + public MutableConfig put(String key, String value) { changeRequest.put(key, value); return this; } @Override - public MutableConfiguration putAll(Map<String, String> properties) { + public MutableConfig putAll(Map<String, String> properties) { changeRequest.putAll(properties); return this; } @Override - public MutableConfiguration remove(String... keys) { + public MutableConfig remove(String... keys) { changeRequest.removeAll(Arrays.asList(keys)); return this; } @@ -91,7 +91,7 @@ public class DefaultMutableConfiguration implements MutableConfiguration { } @Override - public MutableConfiguration remove(Collection<String> keys) { + public MutableConfig remove(Collection<String> keys) { for(MutableConfigSource target:getMutablePropertySources()) { changeRequest.removeAll(keys); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfigurationSpi.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfigurationSpi.java b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfigurationSpi.java index 6d7f57b..d21366a 100644 --- a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfigurationSpi.java +++ b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/internal/DefaultMutableConfigurationSpi.java @@ -19,8 +19,8 @@ package org.apache.tamaya.mutableconfig.internal; import org.apache.tamaya.mutableconfig.ChangePropagationPolicy; -import org.apache.tamaya.mutableconfig.MutableConfiguration; -import org.apache.tamaya.mutableconfig.spi.MutableConfigurationProviderSpi; +import org.apache.tamaya.mutableconfig.MutableConfig; +import org.apache.tamaya.mutableconfig.spi.MutableConfigProviderSpi; import org.osgi.service.component.annotations.Component; import javax.config.Config; @@ -31,11 +31,11 @@ import javax.config.Config; * each instance of {@link Config} a new instance has to be returned. */ @Component -public class DefaultMutableConfigurationSpi implements MutableConfigurationProviderSpi { +public class DefaultMutableConfigurationSpi implements MutableConfigProviderSpi { @Override - public MutableConfiguration createMutableConfiguration(Config configuration, - ChangePropagationPolicy propagationPolicy){ + public MutableConfig createMutableConfig(Config configuration, + ChangePropagationPolicy propagationPolicy){ return new DefaultMutableConfiguration(configuration, propagationPolicy); } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/spi/MutableConfigProviderSpi.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/spi/MutableConfigProviderSpi.java b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/spi/MutableConfigProviderSpi.java new file mode 100644 index 0000000..c3755a5 --- /dev/null +++ b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/spi/MutableConfigProviderSpi.java @@ -0,0 +1,44 @@ +/* + * 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.mutableconfig.spi; + +import org.apache.tamaya.mutableconfig.ChangePropagationPolicy; +import org.apache.tamaya.mutableconfig.MutableConfig; +import org.apache.tamaya.mutableconfig.MutableConfigProvider; + +import javax.config.Config; + + +/** + * Provider SPI used by {@link MutableConfigProvider}. Providers may override + * other providers registering with a higher {@link javax.annotation.Priority} value annotated. + */ +public interface MutableConfigProviderSpi { + + /** + * Creates a new {@link MutableConfig} with {@code autoCommit = false} as default. + * + * @param configuration the configuration, not null. + * @param propagationPolicy policy that defines how changes are published to the property + * sources. + * @return a new mutable configuration instance. + */ + MutableConfig createMutableConfig(Config configuration, + ChangePropagationPolicy propagationPolicy); +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/spi/MutableConfigurationProviderSpi.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/spi/MutableConfigurationProviderSpi.java b/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/spi/MutableConfigurationProviderSpi.java deleted file mode 100644 index bd1eca0..0000000 --- a/modules/mutable-config/src/main/java/org/apache/tamaya/mutableconfig/spi/MutableConfigurationProviderSpi.java +++ /dev/null @@ -1,43 +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.mutableconfig.spi; - -import org.apache.tamaya.mutableconfig.ChangePropagationPolicy; -import org.apache.tamaya.mutableconfig.MutableConfiguration; - -import javax.config.Config; - - -/** - * Provider SPI used by {@link org.apache.tamaya.mutableconfig.MutableConfigurationProvider}. Providers may override - * other providers registering with a higher {@link javax.annotation.Priority} value annotated. - */ -public interface MutableConfigurationProviderSpi { - - /** - * Creates a new {@link MutableConfiguration} with {@code autoCommit = false} as default. - * - * @param configuration the configuration, not null. - * @param propagationPolicy policy that defines how changes are published to the property - * sources. - * @return a new mutable configuration instance. - */ - MutableConfiguration createMutableConfiguration(Config configuration, - ChangePropagationPolicy propagationPolicy); -} http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/resources/META-INF/services/org.apache.tamaya.mutableconfig.spi.MutableConfigProviderSpi ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/resources/META-INF/services/org.apache.tamaya.mutableconfig.spi.MutableConfigProviderSpi b/modules/mutable-config/src/main/resources/META-INF/services/org.apache.tamaya.mutableconfig.spi.MutableConfigProviderSpi new file mode 100644 index 0000000..eb366fc --- /dev/null +++ b/modules/mutable-config/src/main/resources/META-INF/services/org.apache.tamaya.mutableconfig.spi.MutableConfigProviderSpi @@ -0,0 +1,19 @@ +# +# 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 current 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. +# +org.apache.tamaya.mutableconfig.internal.DefaultMutableConfigurationSpi \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/main/resources/META-INF/services/org.apache.tamaya.mutableconfig.spi.MutableConfigurationProviderSpi ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/main/resources/META-INF/services/org.apache.tamaya.mutableconfig.spi.MutableConfigurationProviderSpi b/modules/mutable-config/src/main/resources/META-INF/services/org.apache.tamaya.mutableconfig.spi.MutableConfigurationProviderSpi deleted file mode 100644 index eb366fc..0000000 --- a/modules/mutable-config/src/main/resources/META-INF/services/org.apache.tamaya.mutableconfig.spi.MutableConfigurationProviderSpi +++ /dev/null @@ -1,19 +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 current 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. -# -org.apache.tamaya.mutableconfig.internal.DefaultMutableConfigurationSpi \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigProviderTest.java b/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigProviderTest.java new file mode 100644 index 0000000..fbbe1ab --- /dev/null +++ b/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigProviderTest.java @@ -0,0 +1,85 @@ +/* + * 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.mutableconfig; + +import org.junit.Test; + +import javax.config.ConfigProvider; + +import static org.junit.Assert.*; + +/** + * Created by atsticks on 26.08.16. + */ +public class MutableConfigProviderTest { + @Test + public void createMutableConfiguration() throws Exception { + assertNotNull(MutableConfigProvider.createMutableConfig()); + } + + @Test + public void createMutableConfiguration1() throws Exception { + MutableConfig cfg = MutableConfigProvider + .createMutableConfig(ConfigProvider.getConfig()); + assertNotNull(cfg); + assertEquals(cfg.getChangePropagationPolicy(), + MutableConfigProvider.getApplyMostSignificantOnlyChangePolicy()); + } + + @Test + public void createMutableConfiguration2() throws Exception { + ChangePropagationPolicy policy = MutableConfigProvider.getApplySelectiveChangePolicy("blabla"); + MutableConfig cfg = MutableConfigProvider + .createMutableConfig(ConfigProvider.getConfig(), + policy); + assertNotNull(cfg); + assertEquals(cfg.getChangePropagationPolicy(), policy); + } + + @Test + public void createMutableConfiguration3() throws Exception { + ChangePropagationPolicy policy = MutableConfigProvider.getApplySelectiveChangePolicy("gugus"); + MutableConfig cfg = MutableConfigProvider + .createMutableConfig(policy); + assertNotNull(cfg); + assertEquals(cfg.getChangePropagationPolicy(), policy); + } + + @Test + public void getApplyAllChangePolicy() throws Exception { + assertNotNull(MutableConfigProvider.getApplyAllChangePolicy()); + } + + @Test + public void getApplyMostSignificantOnlyChangePolicy() throws Exception { + assertNotNull(MutableConfigProvider.getApplyMostSignificantOnlyChangePolicy()); + } + + @Test + public void getApplySelectiveChangePolicy() throws Exception { + assertNotNull(MutableConfigProvider.getApplySelectiveChangePolicy()); + } + + @Test + public void getApplyNonePolicy() throws Exception { + assertNotNull(MutableConfigProvider.getApplyNonePolicy()); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigTest.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigTest.java b/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigTest.java new file mode 100644 index 0000000..cde0e6f --- /dev/null +++ b/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigTest.java @@ -0,0 +1,186 @@ +/* + * 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.mutableconfig; + +import org.apache.tamaya.mutableconfig.internal.WritablePropertiesSource; +import org.apache.tamaya.mutableconfig.internal.WritableXmlPropertiesSource; +import org.junit.Test; + +import javax.config.ConfigProvider; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.junit.Assert.*; + +/** + * Tests for {@link MutableConfig}. + */ +public class MutableConfigTest { + + /** + * Test create change request. + * + * @throws Exception the exception + */ + @Test + public void testCreateMutableConfiguration() throws Exception { + File f = File.createTempFile("ConfigChangeRequest",".properties"); + MutableConfig cfg1 = MutableConfigProvider.createMutableConfig( + ConfigProvider.getConfig(), + MutableConfigProvider.getApplyAllChangePolicy()); + assertNotNull(cfg1); + assertNotNull(cfg1.getConfigChangeRequest()); + MutableConfig cfg2 = MutableConfigProvider.createMutableConfig( + ConfigProvider.getConfig()); + assertNotNull(cfg2); + assertNotNull(cfg2.getConfigChangeRequest()); + assertTrue(cfg1!=cfg2); + assertTrue(cfg1.getConfigChangeRequest()!=cfg2.getConfigChangeRequest()); + } + + /** + * Test null create change request. + * + * @throws Exception the exception + */ + @Test(expected=NullPointerException.class) + public void testNullCreateMutableConfiguration1() throws Exception { + MutableConfigProvider.createMutableConfig( + (javax.config.Config) null); + } + + /** + * Test null create change request. + * + * @throws Exception the exception + */ + @Test(expected=NullPointerException.class) + public void testNullCreateMutableConfiguration2() throws Exception { + MutableConfigProvider.createMutableConfig( + (ChangePropagationPolicy) null); + } + + /** + * Test read write properties with rollback. + * + * @throws IOException the io exception + */ + @Test + public void testReadWriteProperties_WithCancel() throws IOException { + WritablePropertiesSource.target.delete(); + MutableConfig mutConfig = MutableConfigProvider.createMutableConfig( + ConfigProvider.getConfig() + ); + mutConfig.put("key1", "value1"); + Map<String,String> cm = new HashMap<>(); + cm.put("key2", "value2"); + cm.put("key3", "value3"); + } + + /** + * Test read write properties with commit. + * + * @throws IOException the io exception + */ + @Test + public void testReadWriteProperties_WithCommit() throws IOException { + WritablePropertiesSource.target.delete(); + MutableConfig mutConfig = MutableConfigProvider.createMutableConfig( + ConfigProvider.getConfig() + ); + mutConfig.put("key1", "value1"); + Map<String,String> cm = new HashMap<>(); + cm.put("key2", "value2"); + cm.put("key3", "value3"); + mutConfig.putAll(cm); + mutConfig.store(); + assertTrue(WritablePropertiesSource.target.exists()); + MutableConfig mmutConfig2 = MutableConfigProvider.createMutableConfig( + ConfigProvider.getConfig() + ); + mmutConfig2.remove("foo"); + mmutConfig2.remove("key3"); + mmutConfig2.put("key1", "value1.2"); + mmutConfig2.put("key4", "value4"); + mmutConfig2.store(); + Properties props = new Properties(); + props.load(WritablePropertiesSource.target.toURL().openStream()); + assertEquals(3, props.size()); + assertEquals("value1.2", props.getProperty("key1")); + assertEquals("value2", props.getProperty("key2")); + assertEquals("value4", props.getProperty("key4")); + } + + /** + * Test read write xml properties with commit. + * + * @throws IOException the io exception + */ + @Test + public void testReadWriteXmlProperties_WithCommit() throws IOException { + WritableXmlPropertiesSource.target.delete(); + MutableConfig cfg = MutableConfigProvider.createMutableConfig( + ConfigProvider.getConfig(), MutableConfigProvider.getApplyAllChangePolicy()); + cfg.put("key1", "value1"); + Map<String,String> cm = new HashMap<>(); + cm.put("key2", "value2"); + cm.put("key3", "value3"); + cfg.putAll(cm); + cfg.store(); + assertTrue(WritableXmlPropertiesSource.target.exists()); + MutableConfig cfg2 = MutableConfigProvider.createMutableConfig( + ConfigProvider.getConfig()); + assertTrue(cfg != cfg2); + cfg2.remove("foo"); + cfg2.remove("key3"); + cfg2.put("key1", "value1.2"); + cfg2.put("key4", "value4"); + cfg2.store(); + Properties props = new Properties(); + props.loadFromXML( WritableXmlPropertiesSource.target.toURL().openStream()); + assertEquals(3, props.size()); + assertEquals("value1", props.getProperty("key1")); + assertEquals("value2", props.getProperty("key2")); + } + + /** + * Test read write xml properties with commit. + * + * @throws IOException the io exception + */ + @Test + public void testWriteWithNoChangePolicy() throws IOException { + WritableXmlPropertiesSource.target.delete(); + MutableConfig cfg = MutableConfigProvider.createMutableConfig( + ConfigProvider.getConfig(), + MutableConfigProvider.getApplyNonePolicy()); + cfg.put("key1", "value1"); + Map<String,String> cm = new HashMap<>(); + cm.put("key2", "value2"); + cm.put("key3", "value3"); + cfg.putAll(cm); + cfg.store(); + assertFalse(WritableXmlPropertiesSource.target.exists()); + } + + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigurationProviderTest.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigurationProviderTest.java b/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigurationProviderTest.java deleted file mode 100644 index a7adbff..0000000 --- a/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigurationProviderTest.java +++ /dev/null @@ -1,85 +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.mutableconfig; - -import org.junit.Test; - -import javax.config.ConfigProvider; - -import static org.junit.Assert.*; - -/** - * Created by atsticks on 26.08.16. - */ -public class MutableConfigurationProviderTest { - @Test - public void createMutableConfiguration() throws Exception { - assertNotNull(MutableConfigurationProvider.createMutableConfiguration()); - } - - @Test - public void createMutableConfiguration1() throws Exception { - MutableConfiguration cfg = MutableConfigurationProvider - .createMutableConfiguration(ConfigProvider.getConfig()); - assertNotNull(cfg); - assertEquals(cfg.getChangePropagationPolicy(), - MutableConfigurationProvider.getApplyMostSignificantOnlyChangePolicy()); - } - - @Test - public void createMutableConfiguration2() throws Exception { - ChangePropagationPolicy policy = MutableConfigurationProvider.getApplySelectiveChangePolicy("blabla"); - MutableConfiguration cfg = MutableConfigurationProvider - .createMutableConfiguration(ConfigProvider.getConfig(), - policy); - assertNotNull(cfg); - assertEquals(cfg.getChangePropagationPolicy(), policy); - } - - @Test - public void createMutableConfiguration3() throws Exception { - ChangePropagationPolicy policy = MutableConfigurationProvider.getApplySelectiveChangePolicy("gugus"); - MutableConfiguration cfg = MutableConfigurationProvider - .createMutableConfiguration(policy); - assertNotNull(cfg); - assertEquals(cfg.getChangePropagationPolicy(), policy); - } - - @Test - public void getApplyAllChangePolicy() throws Exception { - assertNotNull(MutableConfigurationProvider.getApplyAllChangePolicy()); - } - - @Test - public void getApplyMostSignificantOnlyChangePolicy() throws Exception { - assertNotNull(MutableConfigurationProvider.getApplyMostSignificantOnlyChangePolicy()); - } - - @Test - public void getApplySelectiveChangePolicy() throws Exception { - assertNotNull(MutableConfigurationProvider.getApplySelectiveChangePolicy()); - } - - @Test - public void getApplyNonePolicy() throws Exception { - assertNotNull(MutableConfigurationProvider.getApplyNonePolicy()); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya-extensions/blob/319f435c/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigurationTest.java ---------------------------------------------------------------------- diff --git a/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigurationTest.java b/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigurationTest.java deleted file mode 100644 index dae8478..0000000 --- a/modules/mutable-config/src/test/java/org/apache/tamaya/mutableconfig/MutableConfigurationTest.java +++ /dev/null @@ -1,186 +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.mutableconfig; - -import org.apache.tamaya.mutableconfig.internal.WritablePropertiesSource; -import org.apache.tamaya.mutableconfig.internal.WritableXmlPropertiesSource; -import org.junit.Test; - -import javax.config.ConfigProvider; -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import static org.junit.Assert.*; - -/** - * Tests for {@link MutableConfiguration}. - */ -public class MutableConfigurationTest { - - /** - * Test create change request. - * - * @throws Exception the exception - */ - @Test - public void testCreateMutableConfiguration() throws Exception { - File f = File.createTempFile("ConfigChangeRequest",".properties"); - MutableConfiguration cfg1 = MutableConfigurationProvider.createMutableConfiguration( - ConfigProvider.getConfig(), - MutableConfigurationProvider.getApplyAllChangePolicy()); - assertNotNull(cfg1); - assertNotNull(cfg1.getConfigChangeRequest()); - MutableConfiguration cfg2 = MutableConfigurationProvider.createMutableConfiguration( - ConfigProvider.getConfig()); - assertNotNull(cfg2); - assertNotNull(cfg2.getConfigChangeRequest()); - assertTrue(cfg1!=cfg2); - assertTrue(cfg1.getConfigChangeRequest()!=cfg2.getConfigChangeRequest()); - } - - /** - * Test null create change request. - * - * @throws Exception the exception - */ - @Test(expected=NullPointerException.class) - public void testNullCreateMutableConfiguration1() throws Exception { - MutableConfigurationProvider.createMutableConfiguration( - (javax.config.Config) null); - } - - /** - * Test null create change request. - * - * @throws Exception the exception - */ - @Test(expected=NullPointerException.class) - public void testNullCreateMutableConfiguration2() throws Exception { - MutableConfigurationProvider.createMutableConfiguration( - (ChangePropagationPolicy) null); - } - - /** - * Test read write properties with rollback. - * - * @throws IOException the io exception - */ - @Test - public void testReadWriteProperties_WithCancel() throws IOException { - WritablePropertiesSource.target.delete(); - MutableConfiguration mutConfig = MutableConfigurationProvider.createMutableConfiguration( - ConfigProvider.getConfig() - ); - mutConfig.put("key1", "value1"); - Map<String,String> cm = new HashMap<>(); - cm.put("key2", "value2"); - cm.put("key3", "value3"); - } - - /** - * Test read write properties with commit. - * - * @throws IOException the io exception - */ - @Test - public void testReadWriteProperties_WithCommit() throws IOException { - WritablePropertiesSource.target.delete(); - MutableConfiguration mutConfig = MutableConfigurationProvider.createMutableConfiguration( - ConfigProvider.getConfig() - ); - mutConfig.put("key1", "value1"); - Map<String,String> cm = new HashMap<>(); - cm.put("key2", "value2"); - cm.put("key3", "value3"); - mutConfig.putAll(cm); - mutConfig.store(); - assertTrue(WritablePropertiesSource.target.exists()); - MutableConfiguration mmutConfig2 = MutableConfigurationProvider.createMutableConfiguration( - ConfigProvider.getConfig() - ); - mmutConfig2.remove("foo"); - mmutConfig2.remove("key3"); - mmutConfig2.put("key1", "value1.2"); - mmutConfig2.put("key4", "value4"); - mmutConfig2.store(); - Properties props = new Properties(); - props.load(WritablePropertiesSource.target.toURL().openStream()); - assertEquals(3, props.size()); - assertEquals("value1.2", props.getProperty("key1")); - assertEquals("value2", props.getProperty("key2")); - assertEquals("value4", props.getProperty("key4")); - } - - /** - * Test read write xml properties with commit. - * - * @throws IOException the io exception - */ - @Test - public void testReadWriteXmlProperties_WithCommit() throws IOException { - WritableXmlPropertiesSource.target.delete(); - MutableConfiguration cfg = MutableConfigurationProvider.createMutableConfiguration( - ConfigProvider.getConfig(), MutableConfigurationProvider.getApplyAllChangePolicy()); - cfg.put("key1", "value1"); - Map<String,String> cm = new HashMap<>(); - cm.put("key2", "value2"); - cm.put("key3", "value3"); - cfg.putAll(cm); - cfg.store(); - assertTrue(WritableXmlPropertiesSource.target.exists()); - MutableConfiguration cfg2 = MutableConfigurationProvider.createMutableConfiguration( - ConfigProvider.getConfig()); - assertTrue(cfg != cfg2); - cfg2.remove("foo"); - cfg2.remove("key3"); - cfg2.put("key1", "value1.2"); - cfg2.put("key4", "value4"); - cfg2.store(); - Properties props = new Properties(); - props.loadFromXML( WritableXmlPropertiesSource.target.toURL().openStream()); - assertEquals(3, props.size()); - assertEquals("value1", props.getProperty("key1")); - assertEquals("value2", props.getProperty("key2")); - } - - /** - * Test read write xml properties with commit. - * - * @throws IOException the io exception - */ - @Test - public void testWriteWithNoChangePolicy() throws IOException { - WritableXmlPropertiesSource.target.delete(); - MutableConfiguration cfg = MutableConfigurationProvider.createMutableConfiguration( - ConfigProvider.getConfig(), - MutableConfigurationProvider.getApplyNonePolicy()); - cfg.put("key1", "value1"); - Map<String,String> cm = new HashMap<>(); - cm.put("key2", "value2"); - cm.put("key3", "value3"); - cfg.putAll(cm); - cfg.store(); - assertFalse(WritableXmlPropertiesSource.target.exists()); - } - - -} \ No newline at end of file
