changeset 174:6f1abf36bf35

Add some tests for SystemBackend and SampleBackend classes Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-March/000557.html
author Omair Majid <omajid@redhat.com>
date Thu, 29 Mar 2012 17:55:55 -0400
parents cbe6f5b7e43c
children 0b50d4b5e6c8 7180587aed68
files agent/src/main/java/com/redhat/thermostat/backend/Backend.java agent/src/main/java/com/redhat/thermostat/backend/sample/SampleBackend.java agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java agent/src/test/java/com/redhat/thermostat/backend/sample/SampleBackendTest.java agent/src/test/java/com/redhat/thermostat/backend/system/SystemBackendTest.java
diffstat 5 files changed, 241 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/backend/Backend.java	Thu Mar 29 23:56:44 2012 +0200
+++ b/agent/src/main/java/com/redhat/thermostat/backend/Backend.java	Thu Mar 29 17:55:55 2012 -0400
@@ -67,7 +67,7 @@
      * @param configMap a map containing the settings that this backend has been configured with.
      * @throws LaunchException if map contains values that this backend does not accept.
      */
-    public final void setInitialConfiguration(Map<String, String> configMap) throws BackendLoadException {
+    protected final void setInitialConfiguration(Map<String, String> configMap) throws BackendLoadException {
         if (initialConfigurationComplete) {
             throw new BackendLoadException("A backend may only receive intitial configuration once.");
         }
--- a/agent/src/main/java/com/redhat/thermostat/backend/sample/SampleBackend.java	Thu Mar 29 23:56:44 2012 +0200
+++ b/agent/src/main/java/com/redhat/thermostat/backend/sample/SampleBackend.java	Thu Mar 29 17:55:55 2012 -0400
@@ -37,6 +37,7 @@
 package com.redhat.thermostat.backend.sample;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -47,8 +48,8 @@
 import com.redhat.thermostat.common.storage.Category;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
-/** Just an example backend implementation.  This is really just to test the loading and configuration mechanisms
- *
+/**
+ * Just an example backend implementation.  This is really just to test the loading and configuration mechanisms
  */
 public class SampleBackend extends Backend {
     private final String NAME = "sample-backend";
@@ -59,6 +60,8 @@
 
     private Logger logger = LoggingUtils.getLogger(SampleBackend.class);
 
+    private final Map<String, String> config = new HashMap<>();
+
     public SampleBackend() {
         super();
     }
@@ -67,6 +70,7 @@
     protected void setConfigurationValue(String name, String value) {
         logger.log(Level.FINE, "Setting configuration value for backend: " + this.NAME);
         logger.log(Level.FINE, "key: " + name + "    value: " + value);
+        config.put(name, value);
     }
 
     @Override
@@ -91,12 +95,12 @@
 
     @Override
     public Map<String, String> getConfigurationMap() {
-        return new HashMap<String, String>();
+        return Collections.unmodifiableMap(config);
     }
 
     @Override
     public String getConfigurationValue(String key) {
-        throw new IllegalArgumentException("SampleBackend does not actually accept any configuration.");
+        return config.get(key);
     }
 
     @Override
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java	Thu Mar 29 23:56:44 2012 +0200
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java	Thu Mar 29 17:55:55 2012 -0400
@@ -79,6 +79,10 @@
 
     private static final Logger logger = LoggingUtils.getLogger(SystemBackend.class);
 
+    private final List<Category> categories = new ArrayList<Category>();
+
+    private final Set<Integer> pidsToMonitor = new CopyOnWriteArraySet<Integer>();
+
     private long procCheckInterval = 1000; // TODO make this configurable.
 
     private Timer timer = null;
@@ -87,21 +91,11 @@
     private MonitoredHost host = null;
     private JvmStatHostListener hostListener = new JvmStatHostListener();
 
-    private Set<Integer> pidsToMonitor = new CopyOnWriteArraySet<Integer>();
-
     private final VmCpuStatBuilder vmCpuBuilder;
 
-    private static List<Category> categories = new ArrayList<Category>();
-
     public SystemBackend() {
-        Clock clock = new SystemClock();
-        ProcessStatusInfoBuilder builder = new ProcessStatusInfoBuilder(new ProcDataSource());
-        long ticksPerSecond = SysConf.getClockTicksPerSecond();
-        vmCpuBuilder = new VmCpuStatBuilder(clock, ticksPerSecond, builder);
-    }
+        // Set up categories that will later be registered.
 
-    static {
-        // Set up categories that will later be registered.
         categories.add(CpuStatDAO.cpuStatCategory);
         categories.add(HostInfoDAO.hostInfoCategory);
         categories.add(MemoryStatDAO.memoryStatCategory);
@@ -111,6 +105,11 @@
         categories.add(VmGcStatDAO.vmGcStatsCategory);
         categories.add(VmInfoDAO.vmInfoCategory);
         categories.add(VmMemoryStatDAO.vmMemoryStatsCategory);
+
+        Clock clock = new SystemClock();
+        ProcessStatusInfoBuilder builder = new ProcessStatusInfoBuilder(new ProcDataSource());
+        long ticksPerSecond = SysConf.getClockTicksPerSecond();
+        vmCpuBuilder = new VmCpuStatBuilder(clock, ticksPerSecond, builder);
     }
 
     @Override
@@ -170,6 +169,8 @@
         timer.cancel();
         timer = null;
 
+        removeJvmStatusListener(this);
+
         try {
             host.removeHostListener(hostListener);
         } catch (MonitorException me) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/test/java/com/redhat/thermostat/backend/sample/SampleBackendTest.java	Thu Mar 29 17:55:55 2012 -0400
@@ -0,0 +1,110 @@
+/*
+ * 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.backend.sample;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.backend.Backend;
+import com.redhat.thermostat.common.storage.Category;
+
+public class SampleBackendTest {
+
+    @Test
+    public void testBackendInfo() {
+        Backend b = new SampleBackend();
+        assertNotNull(b.getName());
+        assertNotNull(b.getVersion());
+        assertNotNull(b.getVendor());
+        assertNotNull(b.getDescription());
+    }
+
+    @Test
+    public void testBasicBackend() {
+        Backend b = new SampleBackend();
+        assertFalse(b.isActive());
+        b.activate();
+        assertTrue(b.isActive());
+        b.deactivate();
+        assertFalse(b.isActive());
+    }
+
+    @Test
+    public void testActivateTwice() {
+        Backend b = new SampleBackend();
+        b.activate();
+        b.activate();
+        assert (b.isActive());
+    }
+
+    @Test
+    public void testDeactiateWhenNotActive() {
+        Backend b = new SampleBackend();
+        b.deactivate();
+        b.deactivate();
+        assertFalse(b.isActive());
+    }
+
+    @Test
+    public void testCategoriesAreSane() {
+        SampleBackend b = new SampleBackend();
+        Collection<Category> categories = b.getCategories();
+        assertEquals(0, categories.size());
+    }
+
+    @Test
+    public void testDefaultConfiguration() {
+        SampleBackend b = new SampleBackend();
+        assertTrue(b.getConfigurationMap().isEmpty());
+        assertTrue(b.getConfigurationValue("foo") == null);
+
+    }
+
+    @Test
+    public void testModifyConfiguration() {
+        SampleBackend b = new SampleBackend();
+        b.setConfigurationValue("speed", "fast");
+        assertEquals("fast", b.getConfigurationValue("speed"));
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/test/java/com/redhat/thermostat/backend/system/SystemBackendTest.java	Thu Mar 29 17:55:55 2012 -0400
@@ -0,0 +1,110 @@
+/*
+ * 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.backend.system;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import java.util.Collection;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.backend.Backend;
+import com.redhat.thermostat.common.dao.CpuStatDAO;
+import com.redhat.thermostat.common.dao.HostInfoDAO;
+import com.redhat.thermostat.common.dao.MemoryStatDAO;
+import com.redhat.thermostat.common.dao.NetworkInterfaceInfoDAO;
+import com.redhat.thermostat.common.dao.VmClassStatDAO;
+import com.redhat.thermostat.common.dao.VmCpuStatDAO;
+import com.redhat.thermostat.common.dao.VmGcStatDAO;
+import com.redhat.thermostat.common.dao.VmInfoDAO;
+import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
+import com.redhat.thermostat.common.storage.Category;
+import com.redhat.thermostat.common.storage.Storage;
+
+public class SystemBackendTest {
+
+    @Test
+    public void testBasicBackend() {
+        Backend b = new SystemBackend();
+        Storage s = mock(Storage.class);
+        b.setStorage(s);
+        assertFalse(b.isActive());
+        b.activate();
+        assertTrue(b.isActive());
+        b.deactivate();
+        assertFalse(b.isActive());
+    }
+
+    @Test
+    public void testActivateTwice() {
+        Backend b = new SystemBackend();
+        Storage s = mock(Storage.class);
+        b.setStorage(s);
+        b.activate();
+        b.activate();
+        assert(b.isActive());
+    }
+
+    @Test
+    public void testDeactiateWhenNotActive() {
+        Backend b = new SystemBackend();
+        Storage s = mock(Storage.class);
+        b.setStorage(s);
+        b.deactivate();
+        b.deactivate();
+        assertFalse(b.isActive());
+    }
+
+    @Test
+    public void testCategoriesAreSane() {
+        SystemBackend b = new SystemBackend();
+        Collection<Category> categories = b.getCategories();
+
+        assertTrue(categories.contains(CpuStatDAO.cpuStatCategory));
+        assertTrue(categories.contains(HostInfoDAO.hostInfoCategory));
+        assertTrue(categories.contains(MemoryStatDAO.memoryStatCategory));
+        assertTrue(categories.contains(NetworkInterfaceInfoDAO.networkInfoCategory));
+        assertTrue(categories.contains(VmClassStatDAO.vmClassStatsCategory));
+        assertTrue(categories.contains(VmCpuStatDAO.vmCpuStatCategory));
+        assertTrue(categories.contains(VmGcStatDAO.vmGcStatsCategory));
+        assertTrue(categories.contains(VmInfoDAO.vmInfoCategory));
+        assertTrue(categories.contains(VmMemoryStatDAO.vmMemoryStatsCategory));
+    }
+
+}