directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject directory-kerberos git commit: DIRKRB-140 Section support in Configuration. Contributed by Lin Chen
Date Wed, 21 Jan 2015 02:33:05 GMT
Repository: directory-kerberos
Updated Branches:
  refs/heads/master 6b83abe96 -> dfddb4d3b


DIRKRB-140 Section support in Configuration. Contributed by Lin Chen


Project: http://git-wip-us.apache.org/repos/asf/directory-kerberos/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerberos/commit/dfddb4d3
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerberos/tree/dfddb4d3
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerberos/diff/dfddb4d3

Branch: refs/heads/master
Commit: dfddb4d3b07bf18e3601c18d663e7f403b5e2b06
Parents: 6b83abe
Author: drankye <drankye@gmail.com>
Authored: Wed Jan 21 18:29:54 2015 +0800
Committer: drankye <drankye@gmail.com>
Committed: Wed Jan 21 18:29:54 2015 +0800

----------------------------------------------------------------------
 .../main/java/org/apache/haox/config/Conf.java  |  2 +-
 .../java/org/apache/haox/config/ConfigImpl.java | 65 ++++++++++----------
 .../java/org/apache/haox/config/ConfTest.java   | 40 ++++++++++--
 .../org/apache/haox/config/ConfigImplTest.java  | 62 +++++++++++++++++++
 4 files changed, 132 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/dfddb4d3/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java b/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java
index f6ca51c..caa9651 100644
--- a/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/Conf.java
@@ -98,7 +98,7 @@ public class Conf implements Config {
         } else {
             for (ConfigLoader loader : resourceConfigs) {
                 Config loaded = loader.load();
-                config.set(loaded.getResource(), loaded);
+                config.add(loaded);
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/dfddb4d3/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java
index 058d98b..470ee88 100644
--- a/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java
+++ b/contrib/haox-config/src/main/java/org/apache/haox/config/ConfigImpl.java
@@ -6,16 +6,16 @@
  *  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.haox.config;
 
@@ -25,23 +25,26 @@ import org.slf4j.LoggerFactory;
 import java.util.*;
 
 public class ConfigImpl implements Config {
-	private static final Logger logger = LoggerFactory.getLogger(Config.class);
+    private static final Logger logger = LoggerFactory.getLogger(Config.class);
 
     private String resource;
-	private Map<String, ConfigObject> properties;
-    private List<Config> subConfigs;
+    private Map<String, ConfigObject> properties;
+    /**
+     * Config resources
+     */
+    private List<Config> configs;
 
     private Set<String> propNames;
 
     protected ConfigImpl(String resource) {
         this.resource = resource;
         this.properties = new HashMap<String, ConfigObject>();
-        this.subConfigs = new ArrayList<Config>(0);
+        this.configs = new ArrayList<Config>(0);
     }
 
     protected void reset() {
         this.properties.clear();
-        this.subConfigs.clear();
+        this.configs.clear();
     }
 
     @Override
@@ -56,23 +59,23 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-	public String getString(String name) {
-		String result = null;
+    public String getString(String name) {
+        String result = null;
 
         ConfigObject co = properties.get(name);
-		if (co != null) {
+        if (co != null) {
             result = co.getPropertyValue();
-		}
+        }
 
         if (result == null) {
-            for (Config sub : subConfigs) {
-                result = sub.getString(name);
+            for (Config config : configs) {
+                result = config.getString(name);
                 if (result != null) break;
             }
         }
 
-		return result;
-	}
+        return result;
+    }
 
     @Override
     public String getString(ConfigKey name) {
@@ -214,14 +217,14 @@ public class ConfigImpl implements Config {
     }
 
     @Override
-	public List<String> getList(String name) {
+    public List<String> getList(String name) {
         List<String> results = null;
-		ConfigObject co = properties.get(name);
-		if (co != null) {
-			results = co.getListValues();
-		}
-		return results;
-	}
+        ConfigObject co = properties.get(name);
+        if (co != null) {
+            results = co.getListValues();
+        }
+        return results;
+    }
 
     @Override
     public List<String> getList(String name, String[] defaultValue) {
@@ -313,23 +316,21 @@ public class ConfigImpl implements Config {
     }
 
     protected void set(String name, String value) {
-		ConfigObject co = new ConfigObject(value);
-		set(name, co);
-	}
+        ConfigObject co = new ConfigObject(value);
+        set(name, co);
+    }
 
     protected void set(String name, Config value) {
         ConfigObject co = new ConfigObject(value);
         set(name, co);
-
-        addSubConfig(value);
     }
 
     protected void set(String name, ConfigObject value) {
         this.properties.put(name, value);
     }
 
-    private void addSubConfig(Config config) {
-        this.subConfigs.add(config);
+    protected void add(Config config) {
+        this.configs.add(config);
     }
 
     private void reloadNames() {
@@ -337,8 +338,8 @@ public class ConfigImpl implements Config {
             propNames.clear();
         }
         propNames = new HashSet<String>(properties.keySet());
-        for (Config sub : subConfigs) {
-            propNames.addAll(sub.getNames());
+        for (Config config : configs) {
+            propNames.addAll(config.getNames());
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/dfddb4d3/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java b/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java
index 2bbce0e..592ed3d 100644
--- a/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java
+++ b/contrib/haox-config/src/test/java/org/apache/haox/config/ConfTest.java
@@ -6,28 +6,32 @@
  *  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.haox.config;
 
-import junit.framework.Assert;
 import org.apache.haox.config.Conf;
 import org.apache.haox.config.ConfigKey;
+import org.junit.Assert;
 import org.junit.Test;
 
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
+/**
+ * The test is base on the Conf level.
+ * We hope users use the Conf object only, and don't need to care about its internal implementation.
+ */
 public class ConfTest {
 
     @Test
@@ -64,12 +68,40 @@ public class ConfTest {
         Assert.assertEquals(conf.getBoolean("boolProp"), boolProp);
     }
 
+    /**
+     * Test for whether can get right value form the conf which contains many config resources.
+     */
+    @Test
+    public void testMixedConfig() {
+        String mapStrProp = "hello map";
+        Integer intProp = 123456;
+        Map<String, String> mapConfig = new HashMap<String, String>();
+        mapConfig.put("mapStrProp", mapStrProp);
+        mapConfig.put("intProp", String.valueOf(intProp));
+
+        String propertiesStrProp = "hello properties";
+        Boolean boolProp = true;
+        Properties properties = new Properties();
+        properties.setProperty("propertiesStrProp", propertiesStrProp);
+        properties.setProperty("boolProp", String.valueOf(boolProp));
+
+        Conf conf = new Conf();
+        conf.addMapConfig(mapConfig);
+        conf.addPropertiesConfig(properties);
+        Assert.assertEquals(conf.getConfig("mapConfig"), null);
+        Assert.assertEquals(conf.getString("mapStrProp"), mapStrProp);
+        Assert.assertEquals(conf.getString("propertiesStrProp"), propertiesStrProp);
+        Assert.assertEquals(conf.getInt("intProp"), intProp);
+        Assert.assertEquals(conf.getBoolean("boolProp"), boolProp);
+    }
+
     static enum TestConfKey implements ConfigKey {
         ADDRESS("127.0.0.1"),
         PORT(8015),
         ENABLE(false);
 
         private Object defaultValue;
+
         private TestConfKey(Object defaultValue) {
             this.defaultValue = defaultValue;
         }

http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/dfddb4d3/contrib/haox-config/src/test/java/org/apache/haox/config/ConfigImplTest.java
----------------------------------------------------------------------
diff --git a/contrib/haox-config/src/test/java/org/apache/haox/config/ConfigImplTest.java
b/contrib/haox-config/src/test/java/org/apache/haox/config/ConfigImplTest.java
new file mode 100644
index 0000000..fd491e2
--- /dev/null
+++ b/contrib/haox-config/src/test/java/org/apache/haox/config/ConfigImplTest.java
@@ -0,0 +1,62 @@
+/**
+ *  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.haox.config;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * The test is on ConfigImpl level.
+ * ConfigImpl is the internal implementation of Conf, only visual by developers.
+ */
+public class ConfigImplTest {
+
+    /**
+     * Test for section config support.
+     */
+    @Test
+    public void testSectionConfig() {
+        ConfigImpl rootConfig = new ConfigImpl(null);
+        rootConfig.set("globalConfig", "true");
+
+        ConfigImpl sectionA = new ConfigImpl("libdefaults");
+        rootConfig.set("libdefaults", sectionA);
+        sectionA.set("default_realm", "EXAMPLE.COM");
+        sectionA.set("forwardable", "true");
+        sectionA.set("dns_lookup_realm", "false");
+
+        ConfigImpl sectionB = new ConfigImpl("logging");
+        rootConfig.set("logging", sectionB);
+        sectionB.set("kdc", "FILE:/var/log/krb5kdc.log");
+
+        Assert.assertEquals(rootConfig.getString("globalConfig"), "true");
+        Assert.assertEquals(rootConfig.getString("default_realm"), null);
+
+        Config subA = rootConfig.getConfig("libdefaults");
+        Assert.assertEquals(subA.getString("default_realm"), "EXAMPLE.COM");
+        Assert.assertEquals(subA.getString("globalConfig"), null);
+        Assert.assertEquals(subA.getString("kdc"), null);
+
+        Config subB = rootConfig.getConfig("logging");
+        Assert.assertEquals(subB.getString("kdc"), "FILE:/var/log/krb5kdc.log");
+        Assert.assertEquals(subB.getString("globalConfig"), null);
+        Assert.assertEquals(subB.getBoolean("forwardable"), null);
+    }
+}


Mime
View raw message