You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by st...@apache.org on 2012/08/28 13:18:30 UTC

svn commit: r1378079 - in /maven/shared/trunk/maven-shared-utils: ./ src/main/java/org/apache/maven/shared/utils/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/maven/ src/test/java/org/apache/maven/shared/ src/test/java/org/apac...

Author: struberg
Date: Tue Aug 28 11:18:29 2012
New Revision: 1378079

URL: http://svn.apache.org/viewvc?rev=1378079&view=rev
Log:
MSHARED-236 merge in changes from the sandbox pu rewrite

Added:
    maven/shared/trunk/maven-shared-utils/src/test/java/org/
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/
    maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/OsTest.java
      - copied, changed from r1195967, maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/OsTest.java
Modified:
    maven/shared/trunk/maven-shared-utils/pom.xml
    maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java

Modified: maven/shared/trunk/maven-shared-utils/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/pom.xml?rev=1378079&r1=1378078&r2=1378079&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/pom.xml (original)
+++ maven/shared/trunk/maven-shared-utils/pom.xml Tue Aug 28 11:18:29 2012
@@ -52,6 +52,12 @@
   </issueManagement>
 
   <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.9</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>

Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java?rev=1378079&r1=1378078&r2=1378079&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/Os.java Tue Aug 28 11:18:29 2012
@@ -20,69 +20,159 @@ package org.apache.maven.shared.utils;
  */
 
 
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Locale;
+import java.util.Set;
 
 /**
- * Condition that tests the OS type.
+ * <p>Condition that tests the OS type.</p>
+ *
+ * <p>This class got copied over from Apache ANT.
+ * Even the 'original' version from plexus-utils was
+ * only an ANT fork!<br/>
+ * The last time it got copied was on 2011-08-12</p>
+ *
+ * <p>When merging changes please take care of the special
+ * OS_FAMILY handling in this version of Os.java!</p>
  *
  * @author Stefan Bodewig
  * @author Magesh Umasankar
  * @author Brian Fox
+ * @author Mark Struberg
  * @since 1.0
  * @version $Revision$
  */
 public class Os
 {
-    // define the families for easier reference
-    public static final String FAMILY_DOS = "dos";
+    public static final String OS_NAME =
+            System.getProperty( "os.name" ).toLowerCase( Locale.ENGLISH );
+    public static final String OS_ARCH =
+            System.getProperty( "os.arch" ).toLowerCase( Locale.ENGLISH );
+    public static final String OS_VERSION =
+            System.getProperty( "os.version" ).toLowerCase( Locale.ENGLISH );
+    public static final String PATH_SEP =
+            System.getProperty( "path.separator" );
+    public static final String OS_FAMILY =
+            getOsFamily();
 
-    public static final String FAMILY_MAC = "mac";
+    // store the valid families
+    private static final Set validFamilies = getValidFamilies();
 
-    public static final String FAMILY_NETWARE = "netware";
 
+    /**
+     * OS family to look for
+     */
+    private String family;
+    /**
+     * Name of OS
+     */
+    private String name;
+    /**
+     * version of OS
+     */
+    private String version;
+    /**
+     * OS architecture
+     */
+    private String arch;
+    /**
+     * OS family that can be tested for. {@value}
+     */
+    public static final String FAMILY_WINDOWS = "windows";
+    /**
+     * OS family that can be tested for. {@value}
+     */
+    public static final String FAMILY_WIN9X = "win9x";
+    /**
+     * OS family that can be tested for. {@value}
+     */
+    public static final String FAMILY_NT = "winnt";
+    /**
+     * OS family that can be tested for. {@value}
+     */
     public static final String FAMILY_OS2 = "os/2";
-
+    /**
+     * OS family that can be tested for. {@value}
+     */
+    public static final String FAMILY_NETWARE = "netware";
+    /**
+     * OS family that can be tested for. {@value}
+     */
+    public static final String FAMILY_DOS = "dos";
+    /**
+     * OS family that can be tested for. {@value}
+     */
+    public static final String FAMILY_MAC = "mac";
+    /**
+     * OS family that can be tested for. {@value}
+     */
     public static final String FAMILY_TANDEM = "tandem";
-
+    /**
+     * OS family that can be tested for. {@value}
+     */
     public static final String FAMILY_UNIX = "unix";
-
-    public static final String FAMILY_WINDOWS = "windows";
-
-    public static final String FAMILY_WIN9X = "win9x";
-
+    /**
+     * OS family that can be tested for. {@value}
+     */
+    public static final String FAMILY_OPENVMS = "openvms";
+    /**
+     * OS family that can be tested for. {@value}
+     */
     public static final String FAMILY_ZOS = "z/os";
-
+    /**
+     * OS family that can be tested for. {@value}
+     */
     public static final String FAMILY_OS400 = "os/400";
 
-    public static final String FAMILY_OPENVMS = "openvms";
-
-    // get the current info
-    private static final String PATH_SEP = System.getProperty( "path.separator" );
-
-    public static final String OS_NAME = System.getProperty( "os.name" ).toLowerCase( Locale.US );
-
-    public static final String OS_ARCH = System.getProperty( "os.arch" ).toLowerCase( Locale.US );
-
-    public static final String OS_VERSION = System.getProperty( "os.version" ).toLowerCase( Locale.US );
+    /**
+     * OpenJDK is reported to call MacOS X "Darwin"
+     *
+     * @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=44889">bugzilla issue</a>
+     * @see <a href="https://issues.apache.org/jira/browse/HADOOP-3318">HADOOP-3318</a>
+     */
+    private static final String DARWIN = "darwin";
 
-    private String family;
 
-    private String name;
+    /**
+     * Initializes the set of valid families.
+     */
+    public static Set getValidFamilies()
+    {
+        if ( validFamilies != null )
+        {
+            return validFamilies;
+        }
 
-    private String version;
+        Set valid = new HashSet();
+        valid.add( FAMILY_DOS );
+        valid.add( FAMILY_MAC );
+        valid.add( FAMILY_NETWARE );
+        valid.add( FAMILY_NT );
+        valid.add( FAMILY_OPENVMS );
+        valid.add( FAMILY_OS2 );
+        valid.add( FAMILY_OS400 );
+        valid.add( FAMILY_TANDEM );
+        valid.add( FAMILY_UNIX );
+        valid.add( FAMILY_WIN9X );
+        valid.add( FAMILY_WINDOWS );
+        valid.add( FAMILY_ZOS );
 
-    private String arch;
+        return Collections.unmodifiableSet( valid );
+    }
 
     /**
      * Default constructor
      */
     public Os()
     {
+        //default
     }
 
     /**
      * Constructor that sets the family attribute
-     * 
+     *
      * @param family a String value
      */
     public Os( String family )
@@ -92,77 +182,77 @@ public class Os
 
     /**
      * Sets the desired OS family type
-     * 
+     *
      * @param f The OS family type desired<br />
-     *            Possible values:<br />
-     *            <ul>
-     *            <li>dos</li>
-     *            <li>mac</li>
-     *            <li>netware</li>
-     *            <li>os/2</li>
-     *            <li>tandem</li>
-     *            <li>unix</li>
-     *            <li>windows</li>
-     *            <li>win9x</li>
-     *            <li>z/os</li>
-     *            <li>os/400</li>
-     *            <li>openvms</li>
-     *            </ul>
+     *          Possible values:<br />
+     *          <ul>
+     *          <li>dos</li>
+     *          <li>mac</li>
+     *          <li>netware</li>
+     *          <li>os/2</li>
+     *          <li>tandem</li>
+     *          <li>unix</li>
+     *          <li>windows</li>
+     *          <li>win9x</li>
+     *          <li>z/os</li>
+     *          <li>os/400</li>
+     *          </ul>
      */
     public void setFamily( String f )
     {
-        family = f.toLowerCase( Locale.US );
+        family = f.toLowerCase( Locale.ENGLISH );
     }
 
     /**
      * Sets the desired OS name
-     * 
+     *
      * @param name The OS name
      */
     public void setName( String name )
     {
-        this.name = name.toLowerCase( Locale.US );
+        this.name = name.toLowerCase( Locale.ENGLISH );
     }
 
     /**
      * Sets the desired OS architecture
-     * 
+     *
      * @param arch The OS architecture
      */
     public void setArch( String arch )
     {
-        this.arch = arch.toLowerCase( Locale.US );
+        this.arch = arch.toLowerCase( Locale.ENGLISH );
     }
 
     /**
      * Sets the desired OS version
-     * 
+     *
      * @param version The OS version
      */
     public void setVersion( String version )
     {
-        this.version = version.toLowerCase( Locale.US );
+        this.version = version.toLowerCase( Locale.ENGLISH );
     }
 
     /**
-     * Determines if the current OS matches the type of that
-     * set in setFamily.
-     * 
+     * Determines if the OS on which Ant is executing matches the type of
+     * that set in setFamily.
+     *
+     * @return true if the os matches.
+     * @throws Exception if there is an error.
      * @see Os#setFamily(String)
      */
-    public boolean eval()
-        throws Exception
+    public boolean eval() throws Exception
     {
         return isOs( family, name, arch, version );
     }
 
     /**
-     * Determines if the current OS matches the given OS
-     * family.
-     * 
+     * Determines if the OS on which Ant is executing matches the
+     * given OS family.
+     *
      * @param family the family to check for
      * @return true if the OS matches
-     * @since 1.0
+     * @since 1.5
      */
     public static boolean isFamily( String family )
     {
@@ -170,12 +260,12 @@ public class Os
     }
 
     /**
-     * Determines if the current OS matches the given OS
-     * name.
-     * 
+     * Determines if the OS on which Ant is executing matches the
+     * given OS name.
+     *
      * @param name the OS name to check for
      * @return true if the OS matches
-     * @since 1.0
+     * @since 1.7
      */
     public static boolean isName( String name )
     {
@@ -183,12 +273,12 @@ public class Os
     }
 
     /**
-     * Determines if the current OS matches the given OS
-     * architecture.
-     * 
+     * Determines if the OS on which Ant is executing matches the
+     * given OS architecture.
+     *
      * @param arch the OS architecture to check for
      * @return true if the OS matches
-     * @since 1.0
+     * @since 1.7
      */
     public static boolean isArch( String arch )
     {
@@ -196,12 +286,12 @@ public class Os
     }
 
     /**
-     * Determines if the current OS matches the given OS
-     * version.
-     * 
+     * Determines if the OS on which Ant is executing matches the
+     * given OS version.
+     *
      * @param version the OS version to check for
      * @return true if the OS matches
-     * @since 1.0
+     * @since 1.7
      */
     public static boolean isVersion( String version )
     {
@@ -209,25 +299,23 @@ public class Os
     }
 
     /**
-     * Determines if the current OS matches the given OS
-     * family, name, architecture and version.
-     * 
-     * The name, archictecture and version are compared to
-     * the System properties os.name, os.version and os.arch
-     * in a case-independent way.
-     * 
-     * @param family The OS family
-     * @param name The OS name
-     * @param arch The OS architecture
+     * Determines if the OS on which Ant is executing matches the
+     * given OS family, name, architecture and version
+     *
+     * @param family  The OS family
+     * @param name    The OS name
+     * @param arch    The OS architecture
      * @param version The OS version
      * @return true if the OS matches
-     * @since 1.0
+     * @since 1.7
      */
-    public static boolean isOs( String family, String name, String arch, String version )
+    public static boolean isOs( String family, String name, String arch,
+                                String version )
     {
         boolean retValue = false;
 
-        if ( family != null || name != null || arch != null || version != null )
+        if ( family != null || name != null || arch != null
+                || version != null )
         {
 
             boolean isFamily = true;
@@ -237,73 +325,130 @@ public class Os
 
             if ( family != null )
             {
-                if ( family.equalsIgnoreCase( FAMILY_WINDOWS ) )
+
+                //windows probing logic relies on the word 'windows' in
+                //the OS
+                boolean isWindows = OS_NAME.indexOf( FAMILY_WINDOWS ) > -1;
+                boolean is9x = false;
+                boolean isNT = false;
+                if ( isWindows )
                 {
-                    isFamily = OS_NAME.indexOf( FAMILY_WINDOWS ) > -1;
+                    //there are only four 9x platforms that we look for
+                    is9x = (OS_NAME.indexOf( "95" ) >= 0
+                            || OS_NAME.indexOf( "98" ) >= 0
+                            || OS_NAME.indexOf( "me" ) >= 0
+                            //wince isn't really 9x, but crippled enough to
+                            //be a muchness. Ant doesnt run on CE, anyway.
+                            || OS_NAME.indexOf( "ce" ) >= 0);
+                    isNT = !is9x;
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_OS2 ) )
+                if ( family.equals( FAMILY_WINDOWS ) )
+                {
+                    isFamily = isWindows;
+                }
+                else if ( family.equals( FAMILY_WIN9X ) )
+                {
+                    isFamily = isWindows && is9x;
+                }
+                else if ( family.equals( FAMILY_NT ) )
+                {
+                    isFamily = isWindows && isNT;
+                }
+                else if ( family.equals( FAMILY_OS2 ) )
                 {
                     isFamily = OS_NAME.indexOf( FAMILY_OS2 ) > -1;
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_NETWARE ) )
+                else if ( family.equals( FAMILY_NETWARE ) )
                 {
                     isFamily = OS_NAME.indexOf( FAMILY_NETWARE ) > -1;
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_DOS ) )
+                else if ( family.equals( FAMILY_DOS ) )
                 {
                     isFamily = PATH_SEP.equals( ";" ) && !isFamily( FAMILY_NETWARE );
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_MAC ) )
+                else if ( family.equals( FAMILY_MAC ) )
                 {
-                    isFamily = OS_NAME.indexOf( FAMILY_MAC ) > -1;
+                    isFamily = OS_NAME.indexOf( FAMILY_MAC ) > -1
+                            || OS_NAME.indexOf( DARWIN ) > -1;
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_TANDEM ) )
+                else if ( family.equals( FAMILY_TANDEM ) )
                 {
                     isFamily = OS_NAME.indexOf( "nonstop_kernel" ) > -1;
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_UNIX ) )
+                else if ( family.equals( FAMILY_UNIX ) )
                 {
-                    isFamily = PATH_SEP.equals( ":" ) && !isFamily( FAMILY_OPENVMS )
-                        && ( !isFamily( FAMILY_MAC ) || OS_NAME.endsWith( "x" ) );
+                    isFamily = PATH_SEP.equals( ":" )
+                            && !isFamily( FAMILY_OPENVMS )
+                            && (!isFamily( FAMILY_MAC ) || OS_NAME.endsWith( "x" )
+                            || OS_NAME.indexOf( DARWIN ) > -1);
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_WIN9X ) )
+                else if ( family.equals( FAMILY_ZOS ) )
                 {
-                    isFamily = isFamily( FAMILY_WINDOWS )
-                        && ( OS_NAME.indexOf( "95" ) >= 0 || OS_NAME.indexOf( "98" ) >= 0
-                            || OS_NAME.indexOf( "me" ) >= 0 || OS_NAME.indexOf( "ce" ) >= 0 );
+                    isFamily = OS_NAME.indexOf( FAMILY_ZOS ) > -1
+                            || OS_NAME.indexOf( "os/390" ) > -1;
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_ZOS ) )
-                {
-                    isFamily = OS_NAME.indexOf( FAMILY_ZOS ) > -1 || OS_NAME.indexOf( "os/390" ) > -1;
-                }
-                else if ( family.equalsIgnoreCase( FAMILY_OS400 ) )
+                else if ( family.equals( FAMILY_OS400 ) )
                 {
                     isFamily = OS_NAME.indexOf( FAMILY_OS400 ) > -1;
                 }
-                else if ( family.equalsIgnoreCase( FAMILY_OPENVMS ) )
+                else if ( family.equals( FAMILY_OPENVMS ) )
                 {
                     isFamily = OS_NAME.indexOf( FAMILY_OPENVMS ) > -1;
                 }
                 else
                 {
-                    isFamily = OS_NAME.indexOf( family.toLowerCase( Locale.US ) ) > -1;
+                    throw new RuntimeException(
+                            "Don\'t know how to detect os family \""
+                                    + family + "\"" );
                 }
             }
             if ( name != null )
             {
-                isName = name.toLowerCase( Locale.US ).equals( OS_NAME );
+                isName = name.equals( OS_NAME );
             }
             if ( arch != null )
             {
-                isArch = arch.toLowerCase( Locale.US ).equals( OS_ARCH );
+                isArch = arch.equals( OS_ARCH );
             }
             if ( version != null )
             {
-                isVersion = version.toLowerCase( Locale.US ).equals( OS_VERSION );
+                isVersion = version.equals( OS_VERSION );
             }
             retValue = isFamily && isName && isArch && isVersion;
         }
         return retValue;
     }
 
+    /**
+     * Helper method to determine the current OS family.
+     *
+     * @return name of current OS family.
+     */
+    private static String getOsFamily()
+    {
+        Set families = getValidFamilies();
+
+        Iterator iter = families.iterator();
+        while ( iter.hasNext() )
+        {
+            String fam = (String) iter.next();
+            if ( Os.isFamily( fam ) )
+            {
+                return fam;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Test if the given family String represents a valid Family
+     * @param family
+     * @return <code>true</code> if 'family' represents a valid OS-Familiy, <code>false</code> otherwise.
+     */
+    public static boolean isValidFamily( String family )
+    {
+        return validFamilies.contains( family );
+    }
+
+
 }

Copied: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/OsTest.java (from r1195967, maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/OsTest.java)
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/OsTest.java?p2=maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/OsTest.java&p1=maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/OsTest.java&r1=1195967&r2=1378079&rev=1378079&view=diff
==============================================================================
--- maven/sandbox/trunk/plexus-utils-commons-bridge/plexus-utils-tck/src/test/java/org/codehaus/plexus/util/OsTest.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/OsTest.java Tue Aug 28 11:18:29 2012
@@ -1,4 +1,4 @@
-package org.codehaus.plexus.util;
+package org.apache.maven.shared.utils;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -19,8 +19,6 @@ package org.codehaus.plexus.util;
  * under the License.
  */
 
-import org.apache.maven.tck.FixPlexusBugs;
-import org.junit.Rule;
 import org.junit.Test;
 import org.junit.Before;
 import org.junit.After;
@@ -41,10 +39,6 @@ import static org.hamcrest.CoreMatchers.
  */
 public class OsTest extends Assert
 {
-
-    @Rule
-    public FixPlexusBugs fixPlexusBugs = new FixPlexusBugs();
-
     private String origOsName;
     private String origOsFamily;
     private String origOsArch;