You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:19:42 UTC

[sling-org-apache-sling-caconfig-spi] 11/23: SLING-6338 enhance nested configuration handling refactor SPI metadata classes to make them fluent

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.caconfig.spi-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-caconfig-spi.git

commit b068471bb00d69708fa3fec9836a44ed5b5e30e0
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Dec 1 18:05:41 2016 +0000

    SLING-6338 enhance nested configuration handling
    refactor SPI metadata classes to make them fluent
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/contextaware-config/spi@1772244 13f79535-47bb-0310-9956-ffa450edef68
---
 .../spi/ConfigurationPersistenceStrategy.java      |  2 +-
 .../caconfig/spi/metadata/AbstractMetadata.java    | 17 +++++++---
 .../spi/metadata/ConfigurationMetadata.java        | 39 ++++++++++++----------
 .../caconfig/spi/metadata/PropertyMetadata.java    | 19 +++++++++--
 .../spi/metadata/ConfigurationMetadataTest.java    | 35 +++++++++++++++----
 .../spi/metadata/PropertyMetadataTest.java         | 14 ++++----
 6 files changed, 87 insertions(+), 39 deletions(-)

diff --git a/src/main/java/org/apache/sling/caconfig/spi/ConfigurationPersistenceStrategy.java b/src/main/java/org/apache/sling/caconfig/spi/ConfigurationPersistenceStrategy.java
index eddba38..909b7e2 100644
--- a/src/main/java/org/apache/sling/caconfig/spi/ConfigurationPersistenceStrategy.java
+++ b/src/main/java/org/apache/sling/caconfig/spi/ConfigurationPersistenceStrategy.java
@@ -43,7 +43,7 @@ public interface ConfigurationPersistenceStrategy {
     /**
      * Allows the strategy to transform the given configuration resource path according to it's persistent strategies,
      * e.g. fetching the data from a child resource instead of the given resource. 
-     * @param resource Configuration resource path
+     * @param resource Configuration resource path or part of it (e.g. config name)
      * @return Transformed configuration resource path. If null is returned this strategy does not support the given configuration resource path.
      */
     @CheckForNull String getResourcePath(@Nonnull String resourcePath);
diff --git a/src/main/java/org/apache/sling/caconfig/spi/metadata/AbstractMetadata.java b/src/main/java/org/apache/sling/caconfig/spi/metadata/AbstractMetadata.java
index 1db599c..dc69fb6 100644
--- a/src/main/java/org/apache/sling/caconfig/spi/metadata/AbstractMetadata.java
+++ b/src/main/java/org/apache/sling/caconfig/spi/metadata/AbstractMetadata.java
@@ -25,7 +25,7 @@ import javax.annotation.Nonnull;
 /**
  * Common properties for configuration and properties.
  */
-abstract class AbstractMetadata {
+abstract class AbstractMetadata<T> {
 
     private final String name;
     private String label;
@@ -55,9 +55,12 @@ abstract class AbstractMetadata {
 
     /**
      * @param label Label
+     * @return this;
      */
-    public void setLabel(String label) {
+    @SuppressWarnings("unchecked")
+    public T label(String label) {
         this.label = label;
+        return (T)this;
     }
 
     /**
@@ -69,9 +72,12 @@ abstract class AbstractMetadata {
 
     /**
      * @param description Description
+     * @return this;
      */
-    public void setDescription(String description) {
+    @SuppressWarnings("unchecked")
+    public T description(String description) {
         this.description = description;
+        return (T)this;
     }
 
     /**
@@ -83,9 +89,12 @@ abstract class AbstractMetadata {
     
     /**
      * @param properties Further properties for documentation and configuration of behavior in configuration editor.
+     * @return this;
      */
-    public void setProperties(Map<String,String> properties) {
+    @SuppressWarnings("unchecked")
+    public T properties(Map<String,String> properties) {
         this.properties = properties;
+        return (T)this;
     }
     
     @Override
diff --git a/src/main/java/org/apache/sling/caconfig/spi/metadata/ConfigurationMetadata.java b/src/main/java/org/apache/sling/caconfig/spi/metadata/ConfigurationMetadata.java
index d9ec56a..d036713 100644
--- a/src/main/java/org/apache/sling/caconfig/spi/metadata/ConfigurationMetadata.java
+++ b/src/main/java/org/apache/sling/caconfig/spi/metadata/ConfigurationMetadata.java
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.caconfig.spi.metadata;
 
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
 
 import javax.annotation.Nonnull;
@@ -28,16 +30,31 @@ import org.osgi.annotation.versioning.ProviderType;
  * Defines a configuration.
  */
 @ProviderType
-public final class ConfigurationMetadata extends AbstractMetadata {
+public final class ConfigurationMetadata extends AbstractMetadata<ConfigurationMetadata> {
 
-    private boolean collection;
-    private Map<String,PropertyMetadata<?>> propertyMetadata;
+    private final Map<String,PropertyMetadata<?>> propertyMetadata;
+    private final boolean collection;
 
     /**
      * @param name Configuration name
      */
-    public ConfigurationMetadata(@Nonnull String name) {
+    public ConfigurationMetadata(@Nonnull String name,
+            Collection<PropertyMetadata<?>> propertyMetadata,
+            boolean collection) {
         super(name);
+        this.propertyMetadata = toMap(propertyMetadata);
+        this.collection = collection;
+    }
+    
+    private static Map<String,PropertyMetadata<?>> toMap(Collection<PropertyMetadata<?>> propertyMetadata) {
+        Map<String,PropertyMetadata<?>> map = new HashMap<>();
+        for (PropertyMetadata<?> item : propertyMetadata) {
+            if (map.containsKey(item.getName())) {
+                throw new IllegalArgumentException("Duplicate property name: " + item.getName());
+            }
+            map.put(item.getName(), item);
+        }
+        return map;
     }
     
     /**
@@ -55,24 +72,10 @@ public final class ConfigurationMetadata extends AbstractMetadata {
     }
 
     /**
-     * @param isList true if configuration is collection
-     */
-    public void setCollection(boolean value) {
-        this.collection = value;
-    }
-
-    /**
      * @return Configuration properties
      */
     public Map<String,PropertyMetadata<?>> getPropertyMetadata() {
         return this.propertyMetadata;
     }
 
-    /**
-     * @param propertyMetadata Configuration properties
-     */
-    public void setPropertyMetadata(Map<String,PropertyMetadata<?>> propertyMetadata) {
-        this.propertyMetadata = propertyMetadata;
-    }
-
 }
diff --git a/src/main/java/org/apache/sling/caconfig/spi/metadata/PropertyMetadata.java b/src/main/java/org/apache/sling/caconfig/spi/metadata/PropertyMetadata.java
index 447c8ec..e936077 100644
--- a/src/main/java/org/apache/sling/caconfig/spi/metadata/PropertyMetadata.java
+++ b/src/main/java/org/apache/sling/caconfig/spi/metadata/PropertyMetadata.java
@@ -33,7 +33,7 @@ import org.osgi.annotation.versioning.ProviderType;
  * @param <T> Property value type
  */
 @ProviderType
-public final class PropertyMetadata<T> extends AbstractMetadata {
+public final class PropertyMetadata<T> extends AbstractMetadata<PropertyMetadata<T>> {
 
     // these are all types supported for fields of annotation classes (plus class which indicates nested configurations)
     private static final Class<?>[] SUPPORTED_TYPES_ARRAY = {
@@ -119,9 +119,11 @@ public final class PropertyMetadata<T> extends AbstractMetadata {
     
     /**
      * @param value Default value if parameter is not set for configuration
+     * @return this;
      */
-    public void setDefaultValue(T value) {
+    public PropertyMetadata<T> defaultValue(T value) {
         this.defaultValue = value;
+        return this;
     }
     
     /**
@@ -133,9 +135,20 @@ public final class PropertyMetadata<T> extends AbstractMetadata {
 
     /**
      * @param configurationMetadata Metadata for nested configuration
+     * @return this;
      */
-    public void setConfigurationMetadata(ConfigurationMetadata configurationMetadata) {
+    public PropertyMetadata<T> configurationMetadata(ConfigurationMetadata configurationMetadata) {
         this.configurationMetadata = configurationMetadata;
+        return this;
+    }
+    
+    /**
+     * @return true if this property describes a nested configuration.
+     *   In this case it is ensured configuration metadata is present, and the type is ConfigurationMetadata or ConfigurationMetadata[].
+     */
+    public boolean isNestedConfiguration() {
+        return configurationMetadata != null
+                && (this.type.equals(ConfigurationMetadata.class) || this.type.equals(ConfigurationMetadata[].class));
     }
 
     @Override
diff --git a/src/test/java/org/apache/sling/caconfig/spi/metadata/ConfigurationMetadataTest.java b/src/test/java/org/apache/sling/caconfig/spi/metadata/ConfigurationMetadataTest.java
index 6477b59..83bd8f5 100644
--- a/src/test/java/org/apache/sling/caconfig/spi/metadata/ConfigurationMetadataTest.java
+++ b/src/test/java/org/apache/sling/caconfig/spi/metadata/ConfigurationMetadataTest.java
@@ -26,28 +26,51 @@ import java.util.Map;
 
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
 public class ConfigurationMetadataTest {
 
     @Test
     public void testProps() {
-        ConfigurationMetadata underTest = new ConfigurationMetadata("name1");
+        ConfigurationMetadata underTest = new ConfigurationMetadata("name1", ImmutableList.<PropertyMetadata<?>>of(), false);
         assertEquals("name1", underTest.getName());
         assertTrue(underTest.isSingleton());
         assertFalse(underTest.isCollection());
         
-        underTest.setLabel("label1");
-        underTest.setDescription("desc1");
-        underTest.setCollection(true);
         Map<String,String> props = ImmutableMap.of("p1", "v1");
-        underTest.setProperties(props);
+        underTest.label("label1")
+            .description("desc1")
+            .properties(props);
         
         assertEquals("label1", underTest.getLabel());
         assertEquals("desc1", underTest.getDescription());
+        assertEquals(props, underTest.getProperties());
+    }
+
+    @Test
+    public void testCollectionProps() {
+        ConfigurationMetadata underTest = new ConfigurationMetadata("name1", ImmutableList.<PropertyMetadata<?>>of(), true);
+        assertEquals("name1", underTest.getName());
         assertFalse(underTest.isSingleton());
         assertTrue(underTest.isCollection());
-        assertEquals(props, underTest.getProperties());
+    }
+
+    @Test
+    public void testPropertyMap() {
+        ConfigurationMetadata underTest = new ConfigurationMetadata("name1", ImmutableList.<PropertyMetadata<?>>of(
+                new PropertyMetadata<>("prop1", "devValue"),
+                new PropertyMetadata<>("prop2", 5)), false);
+        assertEquals(2, underTest.getPropertyMetadata().size());
+        assertTrue(underTest.getPropertyMetadata().containsKey("prop1"));
+        assertTrue(underTest.getPropertyMetadata().containsKey("prop2"));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testDuplicateKey() {
+        new ConfigurationMetadata("name1", ImmutableList.<PropertyMetadata<?>>of(
+                new PropertyMetadata<>("prop1", "devValue"),
+                new PropertyMetadata<>("prop1", 5)), false);
     }
 
 }
diff --git a/src/test/java/org/apache/sling/caconfig/spi/metadata/PropertyMetadataTest.java b/src/test/java/org/apache/sling/caconfig/spi/metadata/PropertyMetadataTest.java
index 42b1563..a51b948 100644
--- a/src/test/java/org/apache/sling/caconfig/spi/metadata/PropertyMetadataTest.java
+++ b/src/test/java/org/apache/sling/caconfig/spi/metadata/PropertyMetadataTest.java
@@ -25,6 +25,7 @@ import java.util.Map;
 
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
 public class PropertyMetadataTest {
@@ -35,14 +36,13 @@ public class PropertyMetadataTest {
         assertEquals("name1", underTest.getName());
         assertEquals(String.class, underTest.getType());
         
-        underTest.setLabel("label1");
-        underTest.setDescription("desc1");
-        underTest.setDefaultValue("value1");
+        ConfigurationMetadata configMetadata = new ConfigurationMetadata("test", ImmutableList.<PropertyMetadata<?>>of(), false);
         Map<String,String> props = ImmutableMap.of("p1", "v1");
-        underTest.setProperties(props);
-        
-        ConfigurationMetadata configMetadata = new ConfigurationMetadata("test");
-        underTest.setConfigurationMetadata(configMetadata);
+        underTest.label("label1")
+            .description("desc1")
+            .defaultValue("value1")
+            .properties(props)
+            .configurationMetadata(configMetadata);
         
         assertEquals("label1", underTest.getLabel());
         assertEquals("desc1", underTest.getDescription());

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.