mirror of https://github.com/apache/cloudstack.git
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>
This commit is contained in:
parent
b4e972da03
commit
4b530c874f
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<String, Object>());
|
||||
Assert.assertTrue(regionManager.getId() != 0);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue