From 5c9a3e8e586134ca9b451a6a0c793e7e067ec44f Mon Sep 17 00:00:00 2001 From: Harald Kuhr Date: Thu, 21 Jun 2012 16:52:30 +0200 Subject: [PATCH] Added test case. Fixed some minor issues. --- .../java/com/twelvemonkeys/lang/Platform.java | 37 ++-- .../com/twelvemonkeys/lang/PlatformTest.java | 164 ++++++++++++++++++ 2 files changed, 191 insertions(+), 10 deletions(-) create mode 100644 common/common-lang/src/test/java/com/twelvemonkeys/lang/PlatformTest.java diff --git a/common/common-lang/src/main/java/com/twelvemonkeys/lang/Platform.java b/common/common-lang/src/main/java/com/twelvemonkeys/lang/Platform.java index 6c228fab..7fb98e4c 100755 --- a/common/common-lang/src/main/java/com/twelvemonkeys/lang/Platform.java +++ b/common/common-lang/src/main/java/com/twelvemonkeys/lang/Platform.java @@ -28,6 +28,8 @@ package com.twelvemonkeys.lang; +import java.util.Properties; + /** * Platform * @@ -42,7 +44,7 @@ public final class Platform { final OperatingSystem os; /** - * Unormalized operating system version constant (for completeness) + * Unnormalized operating system version constant (for completeness) */ final String version; @@ -54,18 +56,24 @@ public final class Platform { static final private Platform INSTANCE = new Platform(); private Platform() { - os = normalizeOperatingSystem(); - version = System.getProperty("os.version"); - architecture = normalizeArchitecture(os); + this(System.getProperties()); } - private static OperatingSystem normalizeOperatingSystem() { - String os = System.getProperty("os.name"); + Platform(final Properties properties) { + os = normalizeOperatingSystem(properties.getProperty("os.name")); + version = properties.getProperty("os.version"); + architecture = normalizeArchitecture(os, properties.getProperty("os.arch")); + } + + static OperatingSystem normalizeOperatingSystem(final String osName) { + String os = osName; + if (os == null) { throw new IllegalStateException("System property \"os.name\" == null"); } os = os.toLowerCase(); + if (os.startsWith("windows")) { return OperatingSystem.Windows; } @@ -82,13 +90,15 @@ public final class Platform { return OperatingSystem.Unknown; } - private static Architecture normalizeArchitecture(final OperatingSystem pOsName) { - String arch = System.getProperty("os.arch"); + static Architecture normalizeArchitecture(final OperatingSystem pOsName, final String osArch) { + String arch = osArch; + if (arch == null) { throw new IllegalStateException("System property \"os.arch\" == null"); } arch = arch.toLowerCase(); + if (pOsName == OperatingSystem.Windows && (arch.startsWith("x86") || arch.startsWith("i386"))) { return Architecture.X86; // TODO: 64 bit @@ -100,6 +110,9 @@ public final class Platform { else if (arch.startsWith("i686")) { return Architecture.I686; } + else if (arch.startsWith("power") || arch.startsWith("ppc")) { + return Architecture.PPC; + } // TODO: More Linux options? // TODO: 64 bit } @@ -107,9 +120,13 @@ public final class Platform { if (arch.startsWith("power") || arch.startsWith("ppc")) { return Architecture.PPC; } - else if (arch.startsWith("i386")) { - return Architecture.I386; + else if (arch.startsWith("x86")) { + return Architecture.X86; } + else if (arch.startsWith("i386")) { + return Architecture.X86; + } + // TODO: 64 bit } else if (pOsName == OperatingSystem.Solaris) { if (arch.startsWith("sparc")) { diff --git a/common/common-lang/src/test/java/com/twelvemonkeys/lang/PlatformTest.java b/common/common-lang/src/test/java/com/twelvemonkeys/lang/PlatformTest.java new file mode 100644 index 00000000..ed1aa5e7 --- /dev/null +++ b/common/common-lang/src/test/java/com/twelvemonkeys/lang/PlatformTest.java @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2012, Harald Kuhr + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name "TwelveMonkeys" nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.twelvemonkeys.lang; + +import org.junit.Ignore; +import org.junit.Test; + +import java.util.Properties; + +import static org.junit.Assert.*; + +/** + * PlatformTest + * + * @author Harald Kuhr + * @author last modified by $Author: haraldk$ + * @version $Id: PlatformTest.java,v 1.0 11.04.12 16:21 haraldk Exp$ + */ +public class PlatformTest { + // TODO: Make a decision: 32/64 bit part of architecture, or separate property? + // TODO: Should all i386/386/x86/i686 be normalized to x86? + // TODO: Create a version class, to allow testing for equal or greater version requirements etc + // Break down version strings to tokens, compare numbers to numbers, strings to strings, etc. + // - 10.0 > 2.1 (numeric, numeric) + // - beta > alpha (alpha) + // - 1A > 1 + // - 10 > 1A (what about hex numbering, does anyone use that?) + // - 1.0B > 1.0A (numeric, numeric, alpha) + // - 10.0RC > 10.0a > 10.0 ?? (numeric, numeric, alpha) + // - Feisty Fawn > Dapper Drake + // - special recognition of 'alpha', 'beta', 'rc' etc, to represent negative values?? + // Have a look at the Maven version scheme/algorithm, and the JAR file spec (Package#isCompatibleWith(String)) + + @Test + public void testGet() { + assertNotNull(Platform.get()); + } + + @Test + public void testOS() { + assertNotNull(Platform.os()); + assertEquals(Platform.get().getOS(), Platform.os()); + } + + @Test + public void testVersion() { + assertNotNull(Platform.version()); + assertEquals(Platform.get().getVersion(), Platform.version()); + assertEquals(System.getProperty("os.version"), Platform.version()); + } + + @Test + public void testArch() { + assertNotNull(Platform.arch()); + assertEquals(Platform.get().getArchitecture(), Platform.arch()); + } + + private static Properties createProperties(final String osName, final String osVersion, final String osArch) { + Properties properties = new Properties(); + properties.put("os.name", osName); + properties.put("os.version", osVersion); + properties.put("os.arch", osArch); + + return properties; + } + + @Test + public void testCreateOSXx86_64() { + Platform platform = new Platform(createProperties("Mac OS X", "10.7.3", "x86_64")); + assertEquals(Platform.OperatingSystem.MacOS, platform.getOS()); + assertEquals(Platform.Architecture.X86, platform.getArchitecture()); + } + + @Test + public void testCreateOSXDarwinx86() { + Platform platform = new Platform(createProperties("Darwin", "0.0.0", "x86")); + assertEquals(Platform.OperatingSystem.MacOS, platform.getOS()); + assertEquals(Platform.Architecture.X86, platform.getArchitecture()); + } + + @Test + public void testCreateOSXPPC() { + Platform platform = new Platform(createProperties("Mac OS X", "10.5.4", "PPC")); + assertEquals(Platform.OperatingSystem.MacOS, platform.getOS()); + assertEquals(Platform.Architecture.PPC, platform.getArchitecture()); + } + + @Test + public void testCreateWindows386() { + Platform platform = new Platform(createProperties("Windows", "7.0.1.1", "i386")); + assertEquals(Platform.OperatingSystem.Windows, platform.getOS()); + assertEquals(Platform.Architecture.X86, platform.getArchitecture()); + } + + @Ignore("Known issue, needs resolve") + @Test + public void testCreateWindows686() { + Platform platform = new Platform(createProperties("Windows", "5.1", "686")); + assertEquals(Platform.OperatingSystem.Windows, platform.getOS()); + assertEquals(Platform.Architecture.X86, platform.getArchitecture()); + } + + @Ignore("Known issue, needs resolve") + @Test + public void testCreateLinuxX86() { + Platform platform = new Platform(createProperties("Linux", "3.0.18", "x86")); + assertEquals(Platform.OperatingSystem.Linux, platform.getOS()); + assertEquals(Platform.Architecture.X86, platform.getArchitecture()); + } + + @Test + public void testCreateLinuxPPC() { + Platform platform = new Platform(createProperties("Linux", "2.6.11", "PPC")); + assertEquals(Platform.OperatingSystem.Linux, platform.getOS()); + assertEquals(Platform.Architecture.PPC, platform.getArchitecture()); + } + + @Test + public void testCreateSolarisSparc() { + Platform platform = new Platform(createProperties("SunOS", "6.0", "Sparc")); + assertEquals(Platform.OperatingSystem.Solaris, platform.getOS()); + assertEquals(Platform.Architecture.SPARC, platform.getArchitecture()); + } + + @Test + public void testCreateSolarisX86() { + Platform platform = new Platform(createProperties("Solaris", "5.0", "x86")); + assertEquals(Platform.OperatingSystem.Solaris, platform.getOS()); + assertEquals(Platform.Architecture.X86, platform.getArchitecture()); + } + + @Test + public void testCreateUnknownUnknown() { + Platform platform = new Platform(createProperties("Amiga OS", "5.0", "68k")); + assertEquals(Platform.OperatingSystem.Unknown, platform.getOS()); + assertEquals(Platform.Architecture.Unknown, platform.getArchitecture()); + } +}