changeset 73:b024cd568e5f

add tests for the distribution identity
author Omair Majid <omajid@redhat.com>
date Wed, 15 Feb 2012 14:18:44 -0500
parents 977911dfde32
children 61fb3c8f88b8
files agent/src/main/java/com/redhat/thermostat/backend/system/DistributionIdentity.java agent/src/test/java/com/redhat/thermostat/TestUtils.java agent/src/test/java/com/redhat/thermostat/backend/system/DistributionIdentityTest.java
diffstat 3 files changed, 138 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/DistributionIdentity.java	Thu Feb 09 17:21:46 2012 +0100
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/DistributionIdentity.java	Wed Feb 15 14:18:44 2012 -0500
@@ -50,6 +50,9 @@
  */
 public class DistributionIdentity {
 
+    public static final String UNKNOWN_NAME = "Unknown Distribution";
+    public static final String UNKNOWN_VERSION = "Unknown Version";
+
     private static final String DISTRIBUTION_NAME = "distributor id";
     private static final String DISTRIBUTION_VERSION = "release";
 
@@ -59,8 +62,8 @@
     private final String version;
 
     public DistributionIdentity() {
-        String tempName = "Unknown Distribution";
-        String tempVersion = "Unknown";
+        String tempName = UNKNOWN_NAME;
+        String tempVersion = UNKNOWN_VERSION;
         BufferedReader reader = null;
         try {
             Process lsbProc = Runtime.getRuntime().exec(new String[] { "lsb_release", "-a" });
@@ -78,8 +81,14 @@
                     }
                 }
             }
+            int exitValue = lsbProc.waitFor();
+            if (exitValue != 0) {
+                logger.log(Level.WARNING, "unable to identify distribution, problems running 'lsb_release'");
+            }
         } catch (IOException e) {
-            logger.log(Level.WARNING, "unable to identify distribution");
+            logger.log(Level.WARNING, "unable to identify distribution", e);
+        } catch (InterruptedException e) {
+            logger.log(Level.WARNING, "unable to identify distribution", e);
         } finally {
             if (reader != null) {
                 try {
@@ -97,7 +106,7 @@
     }
 
     /**
-     * @return the name of the distrbution, or {@code null} if it can not be
+     * @return the name of the distribution, or {@link #UNKNOWN_NAME} if it can not be
      * identified
      */
     public String getName() {
@@ -105,7 +114,7 @@
     }
 
     /**
-     * @return the release of the distribution or {@code null} if it can not be
+     * @return the release of the distribution or {@link #UNKNOWN_VERSION} if it can not be
      * identified
      */
     public String getVersion() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/test/java/com/redhat/thermostat/TestUtils.java	Wed Feb 15 14:18:44 2012 -0500
@@ -0,0 +1,53 @@
+/*
+ * 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;
+
+import java.lang.management.ManagementFactory;
+
+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"));
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/test/java/com/redhat/thermostat/backend/system/DistributionIdentityTest.java	Wed Feb 15 14:18:44 2012 -0500
@@ -0,0 +1,71 @@
+/*
+ * 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.*;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.TestUtils;
+
+public class DistributionIdentityTest {
+
+    @Test
+    public void testName() {
+        if (TestUtils.isLinux()) {
+            DistributionIdentity identifier = new DistributionIdentity();
+            String name = identifier.getName();
+            assertNotNull(name);
+            assertTrue(name.length() > 0);
+            assertFalse(name.startsWith(":"));
+            assertFalse(name.equals(DistributionIdentity.UNKNOWN_NAME));
+        }
+    }
+
+    @Test
+    public void testVersion() {
+        if (TestUtils.isLinux()) {
+            DistributionIdentity identifier = new DistributionIdentity();
+            String version = identifier.getVersion();
+            assertNotNull(version);
+            assertTrue(version.length()> 0);
+            assertFalse(version.startsWith(":"));
+            assertFalse(version.equals(DistributionIdentity.UNKNOWN_VERSION));
+        }
+    }
+
+}