diff --git a/server/src/org/apache/cloudstack/region/RegionManagerImpl.java b/server/src/org/apache/cloudstack/region/RegionManagerImpl.java index 046779f98fb..40ac46c176c 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; diff --git a/server/test/org/apache/cloudstack/region/RegionManagerTest.java b/server/test/org/apache/cloudstack/region/RegionManagerTest.java index db6bf20cfb7..d1d6de4ddb5 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 { - -public class RegionManagerTest extends TestCase { - private static final Logger s_logger = Logger.getLogger(RegionManagerTest.class); - - @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()); + Assert.assertTrue(regionManager.getId() != 0); + } } diff --git a/utils/src/com/cloud/utils/PropertiesUtil.java b/utils/src/com/cloud/utils/PropertiesUtil.java index e256fce9440..6db66ff484a 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); + } + } } diff --git a/utils/test/com/cloud/utils/PropertiesUtilsTest.java b/utils/test/com/cloud/utils/PropertiesUtilsTest.java new file mode 100644 index 00000000000..5ebe095d69d --- /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")); + } +}