geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject [2/3] geode git commit: Refactor code to use SystemUtils for OS system properties
Date Tue, 14 Feb 2017 02:04:14 GMT
Refactor code to use SystemUtils for OS system properties


Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/93f8e326
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/93f8e326
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/93f8e326

Branch: refs/heads/feature/GEODE-2474
Commit: 93f8e326bd280d88a7e9057ef933ac2cfe8f3820
Parents: 3209c63
Author: Kirk Lund <klund@apache.org>
Authored: Mon Feb 13 17:54:44 2017 -0800
Committer: Kirk Lund <klund@apache.org>
Committed: Mon Feb 13 17:54:44 2017 -0800

----------------------------------------------------------------------
 .../apache/geode/internal/GemFireVersion.java   | 13 ++--
 .../apache/geode/internal/lang/SystemUtils.java | 39 +++++++++-
 .../internal/cli/functions/NetstatFunction.java | 26 ++-----
 .../internal/lang/SystemUtilsJUnitTest.java     | 78 ++++++++++++++------
 4 files changed, 105 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/93f8e326/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java b/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java
index 26d4fb3..04b4443 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/GemFireVersion.java
@@ -14,6 +14,8 @@
  */
 package org.apache.geode.internal;
 
+import static org.apache.geode.internal.lang.SystemUtils.*;
+
 import java.io.File;
 import java.io.InputStream;
 import java.io.PrintStream;
@@ -120,8 +122,7 @@ public class GemFireVersion {
     }
     // fix for bug 33274 - null CodeSource from protection domain in Sybase
     URL csLoc = null;
-    StringTokenizer tokenizer =
-        new StringTokenizer(System.getProperty("java.class.path"), File.pathSeparator);
+    StringTokenizer tokenizer = new StringTokenizer(getClassPath(), File.pathSeparator);
     while (tokenizer.hasMoreTokens()) {
       String jar = tokenizer.nextToken();
       if (jar.indexOf(getGemFireJarFileName()) != -1) {
@@ -137,7 +138,7 @@ public class GemFireVersion {
       return csLoc;
     }
     // try the boot class path to fix bug 37394
-    tokenizer = new StringTokenizer(System.getProperty("sun.boot.class.path"), File.pathSeparator);
+    tokenizer = new StringTokenizer(getBootClassPath(), File.pathSeparator);
     while (tokenizer.hasMoreTokens()) {
       String jar = tokenizer.nextToken();
       if (jar.indexOf(getGemFireJarFileName()) != -1) {
@@ -234,10 +235,10 @@ public class GemFireVersion {
     private void printHostInfo(PrintWriter pw)
         throws InternalGemFireError, Error, VirtualMachineError {
       try {
-        StringBuffer sb = new StringBuffer(SocketCreator.getLocalHost().toString()).append(",
")
+        StringBuilder sb = new StringBuilder(SocketCreator.getLocalHost().toString()).append(",
")
             .append(Runtime.getRuntime().availableProcessors()).append(" cpu(s), ")
-            .append(System.getProperty("os.arch")).append(' ').append(System.getProperty("os.name"))
-            .append(' ').append(System.getProperty("os.version")).append(' ');
+            .append(getOsArchitecture()).append(' ').append(getOsName()).append(' ')
+            .append(getOsVersion()).append(' ');
         pw.println(LocalizedStrings.GemFireVersion_RUNNING_ON_0.toLocalizedString(sb.toString()));
       } catch (VirtualMachineError err) {
         SystemFailure.initiateFailure(err);

http://git-wip-us.apache.org/repos/asf/geode/blob/93f8e326/geode-core/src/main/java/org/apache/geode/internal/lang/SystemUtils.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/internal/lang/SystemUtils.java b/geode-core/src/main/java/org/apache/geode/internal/lang/SystemUtils.java
index 66c158c..28f37ef 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/lang/SystemUtils.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/lang/SystemUtils.java
@@ -240,7 +240,7 @@ public class SystemUtils {
    * @throws MalformedURLException
    */
   public static boolean isInClassPath(URL location) throws MalformedURLException {
-    String classPath = System.getProperty("java.class.path");
+    String classPath = getClassPath();
     StringTokenizer st = new StringTokenizer(classPath, File.pathSeparator);
     while (st.hasMoreTokens()) {
       String path = st.nextToken();
@@ -251,9 +251,44 @@ public class SystemUtils {
     return false;
   }
 
+  /**
+   * Returns the value of {code System.getProperty("os.name")}.
+   */
+  public static String getOsName() {
+    return System.getProperty("os.name");
+  }
+
+  /**
+   * Returns the value of {code System.getProperty("os.version")}.
+   */
+  public static String getOsVersion() {
+    return System.getProperty("os.version");
+  }
+
+  /**
+   * Returns the value of {code System.getProperty("os.arch")}.
+   */
+  public static String getOsArchitecture() {
+    return System.getProperty("os.arch");
+  }
+
+  /**
+   * Returns the value of {code System.getProperty("java.class.path")}.
+   */
+  public static String getClassPath() {
+    return System.getProperty("java.class.path");
+  }
+
+  /**
+   * Returns the value of {code System.getProperty("sun.boot.class.path")}.
+   */
+  public static String getBootClassPath() {
+    return System.getProperty("sun.boot.class.path");
+  }
+
   // @see java.lang.System#getProperty(String) with "os.name".
   private static boolean isOS(final String expectedOsName) {
-    String osName = System.getProperty("os.name");
+    String osName = getOsName();
     return (osName != null && osName.contains(expectedOsName));
   }
 

http://git-wip-us.apache.org/repos/asf/geode/blob/93f8e326/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
index 5fa30f4..029723f 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/NetstatFunction.java
@@ -14,6 +14,8 @@
  */
 package org.apache.geode.management.internal.cli.functions;
 
+import static org.apache.geode.internal.lang.SystemUtils.*;
+
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
@@ -45,13 +47,6 @@ public class NetstatFunction implements Function, InternalEntity {
   private static final Logger logger = LogService.getLogger();
   private static final long serialVersionUID = 1L;
 
-  private static final String OS_NAME_LINUX = "Linux";
-  private static final String OS_NAME_MACOS = "darwin";
-  private static final String OS_NAME_SOLARIS = "SunOS";
-  private static final String OS_NAME_PROP = "os.name";
-  private static final String OS_ARCH_PROP = "os.arch";
-  private static final String OS_VERSION_PROP = "os.version";
-
   public static final NetstatFunction INSTANCE = new NetstatFunction();
 
   private static final String ID = NetstatFunction.class.getName();
@@ -92,9 +87,7 @@ public class NetstatFunction implements Function, InternalEntity {
   private static void addMemberHostHeader(final StringBuilder netstatInfo, final String id,
       final String host, final String lineSeparator) {
 
-    // TODO:GEODE-2474: replace with SystemUtils
-    String osInfo = System.getProperty(OS_NAME_PROP) + " " + System.getProperty(OS_VERSION_PROP)
-        + " " + System.getProperty(OS_ARCH_PROP);
+    String osInfo = getOsName() + " " + getOsVersion() + " " + getOsArchitecture();
 
     StringBuilder memberPlatFormInfo = new StringBuilder();
     memberPlatFormInfo.append(CliStrings.format(CliStrings.NETSTAT__MSG__FOR_HOST_1_OS_2_MEMBER_0,
@@ -113,17 +106,15 @@ public class NetstatFunction implements Function, InternalEntity {
   }
 
   private static void addNetstatDefaultOptions(final List<String> cmdOptionsList) {
-    // TODO:GEODE-2474: replace with SystemUtils
-    String osName = System.getProperty(OS_NAME_PROP);
-    if (OS_NAME_LINUX.equalsIgnoreCase(osName)) {
+    if (isLinux()) {
       cmdOptionsList.add("-v");
       cmdOptionsList.add("-a");
       cmdOptionsList.add("-e");
-    } else if (OS_NAME_MACOS.equalsIgnoreCase(osName)) {
+    } else if (isMacOSX()) {
       cmdOptionsList.add("-v");
       cmdOptionsList.add("-a");
       cmdOptionsList.add("-e");
-    } else if (OS_NAME_SOLARIS.equalsIgnoreCase(osName)) {
+    } else if (isSolaris()) {
       cmdOptionsList.add("-v");
       cmdOptionsList.add("-a");
     } else { // default to Windows
@@ -169,10 +160,7 @@ public class NetstatFunction implements Function, InternalEntity {
     existingNetstatInfo.append("################ ").append(LSOF_COMMAND)
         .append(" output ###################").append(lineSeparator);
 
-    // TODO:GEODE-2474: replace with SystemUtils
-    String osName = System.getProperty(OS_NAME_PROP);
-    if (OS_NAME_LINUX.equalsIgnoreCase(osName) || OS_NAME_MACOS.equalsIgnoreCase(osName)
-        || OS_NAME_SOLARIS.equalsIgnoreCase(osName)) {
+    if (isLinux() || isMacOSX() || isSolaris()) {
 
       ProcessBuilder procBuilder = new ProcessBuilder(LSOF_COMMAND);
       try {

http://git-wip-us.apache.org/repos/asf/geode/blob/93f8e326/geode-core/src/test/java/org/apache/geode/internal/lang/SystemUtilsJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/internal/lang/SystemUtilsJUnitTest.java
b/geode-core/src/test/java/org/apache/geode/internal/lang/SystemUtilsJUnitTest.java
index 48f176e..8a83dc0 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/lang/SystemUtilsJUnitTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/lang/SystemUtilsJUnitTest.java
@@ -14,7 +14,10 @@
  */
 package org.apache.geode.internal.lang;
 
+import static org.apache.geode.internal.lang.SystemUtils.*;
+import static org.assertj.core.api.Assertions.*;
 import static org.junit.Assert.*;
+import static org.junit.Assume.*;
 
 import java.lang.management.ManagementFactory;
 
@@ -38,73 +41,100 @@ public class SystemUtilsJUnitTest {
 
   // NOTE this test adds some maintenance overhead but ensure the correct functioning of
GemFire
   // code that relies on
-  // SystemUtils.isJavaVersionAtLeast
+  // isJavaVersionAtLeast
   @Test
   public void testIsJavaVersionAtLeast() {
     // note, the expected version value should be set to the minimum supported version of
the Java
     // Runtime Environment
     // (JRE) for GemFire
-    assertTrue(SystemUtils.isJavaVersionAtLeast("1.8"));
+    assertTrue(isJavaVersionAtLeast("1.8"));
     // note, the expected version value should be set to the next version of the Java Runtime
     // Environment (JRE)
     // not currently available.
-    assertFalse(SystemUtils.isJavaVersionAtLeast("1.9"));
+    assertFalse(isJavaVersionAtLeast("1.9"));
   }
 
   @Test
   public void testIsAppleJVM() {
-    final boolean expected = ManagementFactory.getRuntimeMXBean().getVmVendor()
-        .contains(SystemUtils.APPLE_JVM_VENDOR_NAME);
-    assertEquals(expected, SystemUtils.isAppleJVM());
+    final boolean expected =
+        ManagementFactory.getRuntimeMXBean().getVmVendor().contains(APPLE_JVM_VENDOR_NAME);
+    assertEquals(expected, isAppleJVM());
   }
 
   @Test
   public void testIsOracleJVM() {
-    final boolean expected = ManagementFactory.getRuntimeMXBean().getVmVendor()
-        .contains(SystemUtils.ORACLE_JVM_VENDOR_NAME);
-    assertEquals(expected, SystemUtils.isOracleJVM());
+    final boolean expected =
+        ManagementFactory.getRuntimeMXBean().getVmVendor().contains(ORACLE_JVM_VENDOR_NAME);
+    assertEquals(expected, isOracleJVM());
   }
 
   @Test
   public void testIsHotSpotVM() {
-    final boolean expected = ManagementFactory.getRuntimeMXBean().getVmName()
-        .contains(SystemUtils.JAVA_HOTSPOT_JVM_NAME);
-    assertEquals(expected, SystemUtils.isHotSpotVM());
+    final boolean expected =
+        ManagementFactory.getRuntimeMXBean().getVmName().contains(JAVA_HOTSPOT_JVM_NAME);
+    assertEquals(expected, isHotSpotVM());
   }
 
   @Test
   public void testIsJ9VM() {
     final boolean expected =
-        ManagementFactory.getRuntimeMXBean().getVmName().contains(SystemUtils.IBM_J9_JVM_NAME);
-    assertEquals(expected, SystemUtils.isJ9VM());
+        ManagementFactory.getRuntimeMXBean().getVmName().contains(IBM_J9_JVM_NAME);
+    assertEquals(expected, isJ9VM());
   }
 
   @Test
   public void testIsJRockitVM() {
-    final boolean expected = ManagementFactory.getRuntimeMXBean().getVmName()
-        .contains(SystemUtils.ORACLE_JROCKIT_JVM_NAME);
-    assertEquals(expected, SystemUtils.isJRockitVM());
+    final boolean expected =
+        ManagementFactory.getRuntimeMXBean().getVmName().contains(ORACLE_JROCKIT_JVM_NAME);
+    assertEquals(expected, isJRockitVM());
   }
 
   @Test
   public void testIsLinux() {
     final boolean expected =
-        ManagementFactory.getOperatingSystemMXBean().getName().contains(SystemUtils.LINUX_OS_NAME);
-    assertEquals(expected, SystemUtils.isLinux());
+        ManagementFactory.getOperatingSystemMXBean().getName().contains(LINUX_OS_NAME);
+    assertEquals(expected, isLinux());
   }
 
   @Test
   public void testIsMacOSX() {
     final boolean expected =
-        ManagementFactory.getOperatingSystemMXBean().getName().contains(SystemUtils.MAC_OSX_NAME);
-    assertEquals(expected, SystemUtils.isMacOSX());
+        ManagementFactory.getOperatingSystemMXBean().getName().contains(MAC_OSX_NAME);
+    assertEquals(expected, isMacOSX());
   }
 
   @Test
   public void testIsWindows() throws Exception {
-    final boolean expected = ManagementFactory.getOperatingSystemMXBean().getName()
-        .contains(SystemUtils.WINDOWS_OS_NAME);
-    assertEquals(expected, SystemUtils.isWindows());
+    final boolean expected =
+        ManagementFactory.getOperatingSystemMXBean().getName().contains(WINDOWS_OS_NAME);
+    assertEquals(expected, isWindows());
+  }
+
+  @Test
+  public void getOsNameShouldReturnOsNameValue() {
+    assertThat(getOsName()).isEqualTo(System.getProperty("os.name"));
+  }
+
+  @Test
+  public void getOsVersionShouldReturnOsVersionValue() {
+    assertThat(getOsVersion()).isEqualTo(System.getProperty("os.version"));
+  }
+
+  @Test
+  public void getOsArchitectureShouldReturnOsArchValue() {
+    assertThat(getOsArchitecture()).isEqualTo(System.getProperty("os.arch"));
+  }
+
+  @Test
+  public void getClassPathShouldReturnJavaClassPathValue() {
+    assertThat(getClassPath()).isEqualTo(System.getProperty("java.class.path"));
+  }
+
+  @Test
+  public void getBootClassPathShouldReturnSunBootClassPathValue() {
+    String value = System.getProperty("sun.boot.class.path");
+    assumeNotNull(value);
+    assertThat(getBootClassPath()).isEqualTo(value);
   }
 
 }


Mime
View raw message