Mercurial > hg > release > thermostat-0.11
changeset 157:a9f2b8a4dbbb
Big Refactoring: ConfigUtils and test utils.
Reviewed-by: rkennke, vanaltj
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-March/000456.html
author | Mario Torre <neugens.limasoftware@gmail.com> |
---|---|
date | Thu, 29 Mar 2012 16:52:29 +0200 |
parents | ddb36e8b9995 |
children | bf1cee600678 |
files | agent/src/test/java/com/redhat/thermostat/TestUtils.java common/src/main/java/com/redhat/thermostat/common/config/ConfigUtils.java common/src/main/java/com/redhat/thermostat/common/config/InvalidConfigurationException.java common/src/test/java/com/redhat/thermostat/common/TestUtils.java common/src/test/java/com/redhat/thermostat/common/config/ConfigUtilsTest.java |
diffstat | 5 files changed, 401 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/agent/src/test/java/com/redhat/thermostat/TestUtils.java Thu Mar 29 11:31:43 2012 -0400 +++ b/agent/src/test/java/com/redhat/thermostat/TestUtils.java Thu Mar 29 16:52:29 2012 +0200 @@ -36,7 +36,19 @@ package com.redhat.thermostat; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import java.lang.management.ManagementFactory; +import java.util.Properties; +import java.util.Random; + +import junit.framework.Assert; + +import org.junit.BeforeClass; + +import com.redhat.thermostat.agent.config.AgentProperties; +import com.redhat.thermostat.backend.BackendsProperties; public class TestUtils { @@ -49,5 +61,47 @@ public static boolean isLinux() { return (System.getProperty("os.name").toLowerCase().contains("linux")); } + + public static String setupAgentConfigs() throws IOException { + // need to create dummy config files for the tests + Random random = new Random(); + String tmpDir = System.getProperty("java.io.tmpdir") + File.separatorChar + + Math.abs(random.nextInt()) + File.separatorChar; + + System.setProperty("THERMOSTAT_HOME", tmpDir); + File agent = new File(tmpDir, "agent"); + agent.mkdirs(); + + File tmpConfigs = new File(agent, "agent.properties"); + + new File(agent, "run").mkdirs(); + new File(agent, "logs").mkdirs(); + + File backends = new File(tmpDir, "backends"); + File system = new File(backends, "system"); + system.mkdirs(); + + Properties props = new Properties(); + + props.setProperty(AgentProperties.BACKENDS.name(), "system"); + props.setProperty(AgentProperties.LOG_LEVEL.name(), "WARNING"); + + props.store(new FileOutputStream(tmpConfigs), "thermostat agent test properties"); + + // now write the configs for the backends + tmpConfigs = new File(system, "backend.properties"); + props = new Properties(); + props.setProperty(BackendsProperties.BACKEND_CLASS.name(), + "com.redhat.thermostat.backend.system.SystemBackend"); + props.setProperty(BackendsProperties.DESCRIPTION.name(), + "fluff backend for tests"); + props.setProperty(BackendsProperties.VENDOR.name(), + "Red Hat, Inc."); + props.setProperty(BackendsProperties.VERSION.name(), + "1.0"); + props.store(new FileOutputStream(tmpConfigs), "thermostat system backend properties"); + + return tmpDir; + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/main/java/com/redhat/thermostat/common/config/ConfigUtils.java Thu Mar 29 16:52:29 2012 +0200 @@ -0,0 +1,109 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.common.config; + +import java.io.File; + +public class ConfigUtils { + + public static String getThermostatHome() throws InvalidConfigurationException { + // allow this to be specified also as a property, especially for + // tests, this overrides the env setting + String home = System.getProperty("THERMOSTAT_HOME"); + if (home == null) { + home = System.getenv("THERMOSTAT_HOME"); + } + + if (home == null) { + throw new InvalidConfigurationException("THERMOSTAT_HOME not defined..."); + } + return home; + } + + public static File getBackendsBaseDirectory() throws InvalidConfigurationException { + String loc = getThermostatHome() + File.separatorChar + "backends"; + File file = new File(loc); + return file; + } + + public static File getStorageBaseDirectory() throws InvalidConfigurationException { + String loc = getThermostatHome() + File.separatorChar + "storage"; + File file = new File(loc); + return file; + } + + public static File getStorageDirectory() throws InvalidConfigurationException { + return new File(getStorageBaseDirectory(), "db"); + } + + public static File getStorageConfigurationFile() throws InvalidConfigurationException { + return new File(getStorageBaseDirectory(), "db.properties"); + } + + public static File getStorageLogFile() throws InvalidConfigurationException { + File logDir = new File(getStorageBaseDirectory(), "logs"); + File logFile = new File(logDir, "db.log"); + + return logFile; + } + + public static File getStoragePidFile() throws InvalidConfigurationException { + File logDir = new File(getStorageBaseDirectory(), "run"); + File logFile = new File(logDir, "db.pid"); + + return logFile; + } + + public static File getBackendPropertyFile(String backendName) + throws InvalidConfigurationException + { + File backendsConfigDir = ConfigUtils.getBackendsBaseDirectory(); + File backendConfig = new File(backendsConfigDir, backendName); + backendConfig = new File(backendConfig, "backend.properties"); + if (!backendConfig.exists()) + throw new InvalidConfigurationException("backends configuration " + + "directory doesn't exist: " + + backendConfig); + return backendConfig; + } + + public static File getAgentConfigurationFile() throws InvalidConfigurationException { + + File agent = new File(getThermostatHome(), "agent"); + return new File(agent, "agent.properties"); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/main/java/com/redhat/thermostat/common/config/InvalidConfigurationException.java Thu Mar 29 16:52:29 2012 +0200 @@ -0,0 +1,56 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.common.config; + +public class InvalidConfigurationException extends Exception { + + public InvalidConfigurationException() { + super(); + } + + public InvalidConfigurationException(String message) { + super(message); + } + + public InvalidConfigurationException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidConfigurationException(Throwable cause) { + super(cause); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/test/java/com/redhat/thermostat/common/TestUtils.java Thu Mar 29 16:52:29 2012 +0200 @@ -0,0 +1,100 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.common; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.util.Properties; +import java.util.Random; + +public class TestUtils { + + public static int getProcessId() { + String name = ManagementFactory.getRuntimeMXBean().getName(); + String pidPart = name.split("@")[0]; + return Integer.parseInt(pidPart); + } + + public static boolean isLinux() { + return (System.getProperty("os.name").toLowerCase().contains("linux")); + } + + public static String setupAgentConfigs() throws IOException { + // need to create dummy config files for the tests + Random random = new Random(); + + String tmpDir = System.getProperty("java.io.tmpdir") + File.separatorChar + + Math.abs(random.nextInt()) + File.separatorChar; + + System.setProperty("THERMOSTAT_HOME", tmpDir); + File agent = new File(tmpDir, "agent"); + agent.mkdirs(); + + File tmpConfigs = new File(agent, "agent.properties"); + + new File(agent, "run").mkdirs(); + new File(agent, "logs").mkdirs(); + + File backends = new File(tmpDir, "backends"); + File system = new File(backends, "system"); + system.mkdirs(); + + Properties props = new Properties(); + + props.setProperty("BACKENDS", "system"); + props.setProperty("LOG_LEVEL", "WARNING"); + + props.store(new FileOutputStream(tmpConfigs), "thermostat agent test properties"); + + // now write the configs for the backends + tmpConfigs = new File(system, "backend.properties"); + props = new Properties(); + props.setProperty("BACKEND_CLASS", + "com.redhat.thermostat.backend.system.SystemBackend"); + props.setProperty("DESCRIPTION", + "fluff backend for tests"); + props.setProperty("VENDOR", + "Red Hat, Inc."); + props.setProperty("VERSION", + "1.0"); + props.store(new FileOutputStream(tmpConfigs), "thermostat system backend properties"); + + return tmpDir; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/src/test/java/com/redhat/thermostat/common/config/ConfigUtilsTest.java Thu Mar 29 16:52:29 2012 +0200 @@ -0,0 +1,82 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.common.config; + +import java.io.File; +import java.io.IOException; +import java.util.Random; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.common.TestUtils; + +public class ConfigUtilsTest { + + private String oldLocation; + + @Before + public void setUp() throws IOException { + TestUtils.setupAgentConfigs(); + } + + @Test + public void testLocations() throws InvalidConfigurationException, IOException { + String path = System.getProperty("THERMOSTAT_HOME"); + char s = File.separatorChar; + + Assert.assertEquals(path, ConfigUtils.getThermostatHome()); + + Assert.assertEquals(path + "backends", ConfigUtils.getBackendsBaseDirectory().getCanonicalPath()); + Assert.assertEquals(path + "storage", ConfigUtils.getStorageBaseDirectory().getCanonicalPath()); + Assert.assertEquals(path + "storage" + s + "db.properties", + ConfigUtils.getStorageConfigurationFile().getCanonicalPath()); + Assert.assertEquals(path + "storage" + s + "db", + ConfigUtils.getStorageDirectory().getCanonicalPath()); + Assert.assertEquals(path + "storage" + s + "logs" + s + "db.log", + ConfigUtils.getStorageLogFile().getCanonicalPath()); + Assert.assertEquals(path + "storage" + s + "run" + s + "db.pid", + ConfigUtils.getStoragePidFile().getCanonicalPath()); + + Assert.assertEquals(path + "backends" + s + "system" + s + "backend.properties", + ConfigUtils.getBackendPropertyFile("system").getCanonicalPath()); + + } +}