cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ko...@apache.org
Subject [1/2] git commit: updated refs/heads/master to 4b530c8
Date Wed, 23 Oct 2013 18:50:55 GMT
Updated Branches:
  refs/heads/master 7edb4d377 -> 4b530c874


Safe properties loader

- new utility method introduced in PropertiesUtil to load properties objects from files
- RegionManagerImpl modified to use the utility method
- Tests added for both

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/4b530c87
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/4b530c87
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/4b530c87

Branch: refs/heads/master
Commit: 4b530c874f40242075b870bd49ecfc8994e74276
Parents: b4e972d
Author: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Authored: Wed Oct 23 20:26:19 2013 +0200
Committer: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Committed: Wed Oct 23 20:50:00 2013 +0200

----------------------------------------------------------------------
 .../cloudstack/region/RegionManagerImpl.java    |  2 +-
 .../cloudstack/region/RegionManagerTest.java    | 29 ++++++--------
 utils/src/com/cloud/utils/PropertiesUtil.java   | 16 ++++++++
 .../com/cloud/utils/PropertiesUtilsTest.java    | 41 ++++++++++++++++++++
 4 files changed, 69 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4b530c87/server/src/org/apache/cloudstack/region/RegionManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/org/apache/cloudstack/region/RegionManagerImpl.java b/server/src/org/apache/cloudstack/region/RegionManagerImpl.java
index 046779f..40ac46c 100755
--- a/server/src/org/apache/cloudstack/region/RegionManagerImpl.java
+++ b/server/src/org/apache/cloudstack/region/RegionManagerImpl.java
@@ -80,7 +80,7 @@ public class RegionManagerImpl extends ManagerBase implements RegionManager,
Man
             dbProps = new Properties();
         }
         try {
-            dbProps.load(new FileInputStream(dbPropsFile));
+            PropertiesUtil.loadFromFile(dbProps, dbPropsFile);
         } catch (IOException e) {
             s_logger.fatal("Unable to load db properties file, pl. check the classpath and
file path configuration", e);
             return false;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4b530c87/server/test/org/apache/cloudstack/region/RegionManagerTest.java
----------------------------------------------------------------------
diff --git a/server/test/org/apache/cloudstack/region/RegionManagerTest.java b/server/test/org/apache/cloudstack/region/RegionManagerTest.java
index db6bf20..d1d6de4 100644
--- a/server/test/org/apache/cloudstack/region/RegionManagerTest.java
+++ b/server/test/org/apache/cloudstack/region/RegionManagerTest.java
@@ -18,33 +18,20 @@
 package org.apache.cloudstack.region;
 
 
+import java.util.HashMap;
+
+import javax.naming.ConfigurationException;
+
 import junit.framework.Assert;
-import junit.framework.TestCase;
 
-import org.apache.cloudstack.api.command.admin.domain.DeleteDomainCmd;
-import org.apache.cloudstack.context.CallContext;
 import org.apache.cloudstack.region.dao.RegionDao;
-
-import org.apache.log4j.Logger;
-import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
 
 import com.cloud.exception.InvalidParameterValueException;
-import com.cloud.user.Account;
-import com.cloud.user.dao.AccountDao;
-
-
 
-public class RegionManagerTest extends TestCase {
-    private static final Logger s_logger = Logger.getLogger(RegionManagerTest.class);
+public class RegionManagerTest {
 
-    @Before
-    @Override
-    protected void setUp() {
-
-    }
-    
     @Test
     public void testUniqueName() {
     	RegionManagerImpl regionMgr = new RegionManagerImpl();
@@ -59,4 +46,10 @@ public class RegionManagerTest extends TestCase {
     	}
     }
 
+    @Test
+    public void configure() throws ConfigurationException {
+        RegionManagerImpl regionManager = new RegionManagerImpl();
+        regionManager.configure("foo", new HashMap<String, Object>());
+        Assert.assertTrue(regionManager.getId() != 0);
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4b530c87/utils/src/com/cloud/utils/PropertiesUtil.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/PropertiesUtil.java b/utils/src/com/cloud/utils/PropertiesUtil.java
index e256fce..6db66ff 100755
--- a/utils/src/com/cloud/utils/PropertiesUtil.java
+++ b/utils/src/com/cloud/utils/PropertiesUtil.java
@@ -27,6 +27,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.log4j.Logger;
 
 public class PropertiesUtil {
@@ -156,4 +157,19 @@ public class PropertiesUtil {
         }
         return configMap;
     }
+
+    /**
+     * Load a Properties object with contents from a File.
+     * @param properties the properties object to be loaded
+     * @param file  the file to load from
+     * @throws IOException 
+     */
+    public static void loadFromFile(Properties properties, File file) throws IOException
{
+        InputStream stream = new FileInputStream(file);
+        try {
+            properties.load(stream);
+        } finally {
+            IOUtils.closeQuietly(stream);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/4b530c87/utils/test/com/cloud/utils/PropertiesUtilsTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/PropertiesUtilsTest.java b/utils/test/com/cloud/utils/PropertiesUtilsTest.java
new file mode 100644
index 0000000..5ebe095
--- /dev/null
+++ b/utils/test/com/cloud/utils/PropertiesUtilsTest.java
@@ -0,0 +1,41 @@
+// 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 com.cloud.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Properties;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PropertiesUtilsTest {
+    @Test
+    public void findConfigFile() {
+        File configFile = PropertiesUtil.findConfigFile("notexistingresource");
+        Assert.assertNull(configFile);
+    }
+    @Test
+    public void loadFromFile() throws IOException {
+        File file = File.createTempFile("test", ".properties");
+        FileUtils.writeStringToFile(file, "a=b\nc=d\n");
+        Properties properties = new Properties();
+        PropertiesUtil.loadFromFile(properties, file);
+        Assert.assertEquals("b", properties.get("a"));
+    }
+}


Mime
View raw message