ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant/util JavaEnvUtilsTest.java
Date Thu, 28 Mar 2002 12:15:27 GMT
bodewig     02/03/28 04:15:27

  Modified:    src/main/org/apache/tools/ant Project.java
               src/main/org/apache/tools/ant/taskdefs Javac.java
                        Javadoc.java
               src/main/org/apache/tools/ant/types CommandlineJava.java
  Added:       src/main/org/apache/tools/ant/util JavaEnvUtils.java
               src/testcases/org/apache/tools/ant/util
                        JavaEnvUtilsTest.java
  Log:
  New utility class that deals with the specifics of a Java
  installation:
  
  * Moved JVM version detection code from Project to the new class.
  
  * Unified the "find executable" methods and factored them into the new
    class.
  
  Revision  Changes    Path
  1.100     +13 -38    jakarta-ant/src/main/org/apache/tools/ant/Project.java
  
  Index: Project.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
  retrieving revision 1.99
  retrieving revision 1.100
  diff -u -r1.99 -r1.100
  --- Project.java	5 Mar 2002 14:39:05 -0000	1.99
  +++ Project.java	28 Mar 2002 12:15:26 -0000	1.100
  @@ -68,6 +68,7 @@
   import org.apache.tools.ant.types.FilterSet; 
   import org.apache.tools.ant.types.FilterSetCollection; 
   import org.apache.tools.ant.util.FileUtils; 
  +import org.apache.tools.ant.util.JavaEnvUtils;
   
   /**
    * Central representation of an Ant project. This class defines an
  @@ -81,7 +82,7 @@
    *
    * @author duncan@x180.com
    *
  - * @version $Revision: 1.99 $
  + * @version $Revision: 1.100 $
    */
   
   public class Project {
  @@ -108,19 +109,16 @@
        */
       private final static String VISITED = "VISITED";
   
  -    /** Version of currently running VM. */
  -    private static String javaVersion;
  -
       /** Version constant for Java 1.0 */
  -    public final static String JAVA_1_0 = "1.0";
  +    public final static String JAVA_1_0 = JavaEnvUtils.JAVA_1_0;
       /** Version constant for Java 1.1 */
  -    public final static String JAVA_1_1 = "1.1";
  +    public final static String JAVA_1_1 = JavaEnvUtils.JAVA_1_1;
       /** Version constant for Java 1.2 */
  -    public final static String JAVA_1_2 = "1.2";
  +    public final static String JAVA_1_2 = JavaEnvUtils.JAVA_1_2;
       /** Version constant for Java 1.3 */
  -    public final static String JAVA_1_3 = "1.3";
  +    public final static String JAVA_1_3 = JavaEnvUtils.JAVA_1_3;
       /** Version constant for Java 1.4 */
  -    public final static String JAVA_1_4 = "1.4";
  +    public final static String JAVA_1_4 = JavaEnvUtils.JAVA_1_4;
   
       /** Default filter start token. */
       public final static String TOKEN_START = FilterSet.DEFAULT_TOKEN_START;
  @@ -183,31 +181,6 @@
       /** Records the latest task to be executed on a thread (Thread to Task). */ 
       private Hashtable threadTasks = new Hashtable();
       
  -    static {
  -
  -        // Determine the Java version by looking at available classes
  -        // java.lang.CharSequence was introduced in JDK 1.4
  -        // java.lang.StrictMath was introduced in JDK 1.3
  -        // java.lang.ThreadLocal was introduced in JDK 1.2
  -        // java.lang.Void was introduced in JDK 1.1
  -        // Count up version until a NoClassDefFoundError ends the try
  -
  -        try {
  -            javaVersion = JAVA_1_0;
  -            Class.forName("java.lang.Void");
  -            javaVersion = JAVA_1_1;
  -            Class.forName("java.lang.ThreadLocal");
  -            javaVersion = JAVA_1_2;
  -            Class.forName("java.lang.StrictMath");
  -            javaVersion = JAVA_1_3;
  -            Class.forName("java.lang.CharSequence");
  -            javaVersion = JAVA_1_4;
  -        } catch (ClassNotFoundException cnfe) {
  -            // swallow as we've hit the max class version that
  -            // we have
  -        }
  -    }
  -
       /** Instance of a utility class to use for file operations. */
       private FileUtils fileUtils;
   
  @@ -728,9 +701,10 @@
       /**
        * Returns the version of Java this class is running under.
        * @return the version of Java as a String, e.g. "1.1"
  +     * @see org.apache.tools.ant.util.JavaEnvUtils#getJavaVersion
        */
       public static String getJavaVersion() {
  -        return javaVersion;
  +        return JavaEnvUtils.getJavaVersion();
       }
   
       /**
  @@ -741,13 +715,14 @@
        *
        * @exception BuildException if this Java version is not supported
        * 
  -     * @see #getJavaVersion()
  +     * @see org.apache.tools.ant.util.JavaEnvUtils#getJavaVersion
        */
       public void setJavaVersionProperty() throws BuildException {
  +        String javaVersion = JavaEnvUtils.getJavaVersion();
           setPropertyInternal("ant.java.version", javaVersion);
   
           // sanity check
  -        if (javaVersion == JAVA_1_0) {
  +        if (javaVersion == JavaEnvUtils.JAVA_1_0) {
               throw new BuildException("Ant cannot work on Java 1.0");
           }
   
  @@ -1477,7 +1452,7 @@
        */
       public void setFileLastModified(File file, long time) 
            throws BuildException {
  -        if (getJavaVersion() == JAVA_1_1) {
  +        if (JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_1) {
               log("Cannot change the modification time of " + file
                   + " in JDK 1.1", Project.MSG_WARN);
               return;
  
  
  
  1.93      +5 -32     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javac.java
  
  Index: Javac.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javac.java,v
  retrieving revision 1.92
  retrieving revision 1.93
  diff -u -r1.92 -r1.93
  --- Javac.java	25 Mar 2002 10:37:03 -0000	1.92
  +++ Javac.java	28 Mar 2002 12:15:27 -0000	1.93
  @@ -61,10 +61,10 @@
   import org.apache.tools.ant.types.Path;
   import org.apache.tools.ant.types.Reference;
   import org.apache.tools.ant.util.GlobPatternMapper;
  +import org.apache.tools.ant.util.JavaEnvUtils;
   import org.apache.tools.ant.util.SourceFileScanner;
   import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter;
   import org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory;
  -import org.apache.tools.ant.taskdefs.condition.Os;
   
   import java.io.File;
   import java.util.Enumeration;
  @@ -103,7 +103,7 @@
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
    * @author <a href="mailto:jayglanville@home.com">J D Glanville</a>
    *
  - * @version $Revision: 1.92 $
  + * @version $Revision: 1.93 $
    *
    * @ant.task category="java"
    */
  @@ -697,34 +697,7 @@
       }
   
       protected String getSystemJavac() {
  -        // This is the most common extension case - exe for windows and OS/2,
  -        // nothing for *nix.
  -        String extension =  Os.isFamily("dos") ? ".exe" : "";
  -
  -        File jExecutable = null;
  -
  -        // On AIX using IBM's JDK 1.2 the javac executable is in
  -        // the java.home/../../sh directory
  -        if (Os.isName("aix")) {
  -            jExecutable = new File(System.getProperty("java.home") +
  -                                   "/../../sh/javac" + extension);
  -        }
  -
  -        if (jExecutable == null || !jExecutable.exists()) {
  -            // Look for javac in the java.home/../bin directory.  
  -            jExecutable = new File(System.getProperty("java.home") +
  -                                   "/../bin/javac" + extension);
  -        }
  -
  -        // Unfortunately
  -        // on Windows java.home doesn't always refer to the correct location,
  -        // so we need to fall back to assuming java is somewhere on the
  -        // PATH.
  -        if (jExecutable.exists() && !Os.isFamily("netware")) {
  -            return jExecutable.getAbsolutePath();
  -        } else {
  -            return "javac" + extension;
  -        }
  +        return JavaEnvUtils.getJdkExecutable("javac");
       }
   
       /**
  @@ -767,8 +740,8 @@
           }
   
           if (compilerImpl == null) {
  -            if (Project.getJavaVersion() != Project.JAVA_1_1 &&
  -                Project.getJavaVersion() != Project.JAVA_1_2) {
  +            if (JavaEnvUtils.getJavaVersion() != Project.JAVA_1_1 &&
  +                JavaEnvUtils.getJavaVersion() != Project.JAVA_1_2) {
                   compilerImpl = "modern";
               } else {
                   compilerImpl = "classic";
  
  
  
  1.81      +6 -44     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javadoc.java
  
  Index: Javadoc.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Javadoc.java,v
  retrieving revision 1.80
  retrieving revision 1.81
  diff -u -r1.80 -r1.81
  --- Javadoc.java	21 Mar 2002 08:19:07 -0000	1.80
  +++ Javadoc.java	28 Mar 2002 12:15:27 -0000	1.81
  @@ -67,13 +67,13 @@
   import org.apache.tools.ant.Project;
   
   import org.apache.tools.ant.Task;
  -import org.apache.tools.ant.taskdefs.condition.Os;
   import org.apache.tools.ant.types.Path;
   import org.apache.tools.ant.types.Reference;
   import org.apache.tools.ant.types.EnumeratedAttribute;
   import org.apache.tools.ant.types.Commandline;
   import org.apache.tools.ant.types.FileSet;
   import org.apache.tools.ant.util.FileUtils;
  +import org.apache.tools.ant.util.JavaEnvUtils;
   
   /**
    * This task makes it easy to generate Javadoc documentation for a collection
  @@ -223,12 +223,12 @@
   
       private Commandline cmd = new Commandline();
       private static boolean javadoc1 =
  -        (Project.getJavaVersion() == Project.JAVA_1_1);
  +        (JavaEnvUtils.getJavaVersion() == Project.JAVA_1_1);
   
       private static boolean javadoc4 =
  -        (Project.getJavaVersion() != Project.JAVA_1_1 &&
  -         Project.getJavaVersion() != Project.JAVA_1_2 &&
  -         Project.getJavaVersion() != Project.JAVA_1_3);
  +        (JavaEnvUtils.getJavaVersion() != Project.JAVA_1_1 &&
  +         JavaEnvUtils.getJavaVersion() != Project.JAVA_1_2 &&
  +         JavaEnvUtils.getJavaVersion() != Project.JAVA_1_3);
   
       private void addArgIf(boolean b, String arg) {
           if (b) {
  @@ -921,7 +921,7 @@
           }
   
           Commandline toExecute = (Commandline)cmd.clone();
  -        toExecute.setExecutable( getJavadocExecutableName() );
  +        toExecute.setExecutable(JavaEnvUtils.getJdkExecutable("javadoc"));
   
   // ------------------------------------------------ general javadoc arguments
           if (classpath == null) {
  @@ -1335,44 +1335,6 @@
        */
       protected String expand(String content) {
           return project.replaceProperties(content);
  -    }
  -
  -    private String getJavadocExecutableName()
  -    {
  -        // This is the most common extension case - exe for windows and OS/2,
  -        // nothing for *nix.
  -        String extension =  Os.isFamily("dos") ? ".exe" : "";
  -
  -        File jdocExecutable = null;
  -
  -        // On AIX using IBM's JDK 1.2 the javadoc executable is in
  -        // the java.home/../sh directory
  -        if (Os.isName("aix")) {
  -            jdocExecutable = new File(System.getProperty("java.home") +
  -                                      "/../sh/javadoc" + extension);
  -        }
  -        
  -        if (jdocExecutable == null || !jdocExecutable.exists()) {
  -            // Look for javadoc in the java.home/../bin directory.  
  -            jdocExecutable = new File(System.getProperty("java.home") +
  -                                      "/../bin/javadoc" + extension);
  -        }
  -
  -        // Unfortunately
  -        // on Windows java.home doesn't always refer to the correct location, 
  -        // so we need to fall back to assuming java is somewhere on the
  -        // PATH.
  -
  -        if (jdocExecutable.exists() && !Os.isFamily("netware")) {
  -            return jdocExecutable.getAbsolutePath();
  -        } else {
  -            if (!Os.isFamily("netware")) {
  -                log( "Unable to locate " + jdocExecutable.getAbsolutePath()
  -                     + ". Using \"javadoc" + extension
  -                     + "\" instead.", Project.MSG_VERBOSE );
  -            }
  -            return "javadoc" + extension;
  -        }
       }
   
   }
  
  
  
  1.31      +3 -38     jakarta-ant/src/main/org/apache/tools/ant/types/CommandlineJava.java
  
  Index: CommandlineJava.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/types/CommandlineJava.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- CommandlineJava.java	21 Mar 2002 08:19:07 -0000	1.30
  +++ CommandlineJava.java	28 Mar 2002 12:15:27 -0000	1.31
  @@ -60,7 +60,7 @@
   import java.util.Vector;
   import org.apache.tools.ant.Project;
   import org.apache.tools.ant.BuildException;
  -import org.apache.tools.ant.taskdefs.condition.Os;
  +import org.apache.tools.ant.util.JavaEnvUtils;
   
   /**
    * A representation of a Java command line that is nothing more
  @@ -151,8 +151,8 @@
       }
   
       public CommandlineJava() {
  -        setVm(getJavaExecutableName());
  -        setVmversion(Project.getJavaVersion());
  +        setVm(JavaEnvUtils.getJreExecutable("java"));
  +        setVmversion(JavaEnvUtils.getJavaVersion());
       }
   
       public Commandline.Argument createArgument() {
  @@ -358,39 +358,4 @@
           javaCommand.clearArgs();
       }
   
  -    private String getJavaExecutableName() {
  -        // This is the most common extension case - exe for windows and OS/2, 
  -        // nothing for *nix.
  -        String extension =  Os.isFamily("dos") ? ".exe" : "";
  -
  -        File jExecutable = null;
  -
  -        // On AIX using IBM's JDK 1.2 the java executable is in
  -        // the java.home/../sh directory
  -        if (Os.isName("aix")) {
  -            jExecutable = new File(System.getProperty("java.home") +
  -                                   "/../sh/java" + extension);
  -        }
  -        
  -        if (jExecutable == null || !jExecutable.exists()) {
  -            // Look for java in the java.home/../bin directory.  
  -            jExecutable = new File(System.getProperty("java.home") +
  -                                   "/../bin/java" + extension);
  -        }
  -
  -        // Unfortunately
  -        // on Windows java.home doesn't always refer to the correct location, 
  -        // so we need to fall back to assuming java is somewhere on the
  -        // PATH.
  -
  -        if (jExecutable.exists() && !Os.isFamily("netware")) {
  -            // NetWare may have a "java" in that directory, but 99% of
  -            // the time, you don't want to execute it -- Jeff Tulley
  -            // <JTULLEY@novell.com>
  -            return jExecutable.getAbsolutePath();
  -        } else {
  -            return "java" + extension;
  -        }
  -    }
  -    
   }
  
  
  
  1.1                  jakarta-ant/src/main/org/apache/tools/ant/util/JavaEnvUtils.java
  
  Index: JavaEnvUtils.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. 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.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Ant", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
   * ITS 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.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.tools.ant.util;
  
  import org.apache.tools.ant.taskdefs.condition.Os;
  import java.io.File;
  
  /**
   * A set of helper methods related to locating executables or checking
   * conditons of a given Java installation.
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   *
   * @since Ant 1.5
   */
  public class JavaEnvUtils {
  
      /** Are we on a DOS-based system */
      private static final boolean isDos = Os.isFamily("dos");
      /** Are we on Novell NetWare */
      private static final boolean isNetware = Os.isName("netware");
      /** Are we on AIX */
      private static final boolean isAix = Os.isName("aix");
  
      /** shortcut for System.getProperty("java.home") */
      private static final String javaHome = System.getProperty("java.home");
  
      /** FileUtils instance for path normalization */
      private static final FileUtils fileUtils = FileUtils.newFileUtils();
  
      /** Version of currently running VM. */
      private static String javaVersion;
  
      /** Version constant for Java 1.0 */
      public final static String JAVA_1_0 = "1.0";
      /** Version constant for Java 1.1 */
      public final static String JAVA_1_1 = "1.1";
      /** Version constant for Java 1.2 */
      public final static String JAVA_1_2 = "1.2";
      /** Version constant for Java 1.3 */
      public final static String JAVA_1_3 = "1.3";
      /** Version constant for Java 1.4 */
      public final static String JAVA_1_4 = "1.4";
  
      static {
  
          // Determine the Java version by looking at available classes
          // java.lang.CharSequence was introduced in JDK 1.4
          // java.lang.StrictMath was introduced in JDK 1.3
          // java.lang.ThreadLocal was introduced in JDK 1.2
          // java.lang.Void was introduced in JDK 1.1
          // Count up version until a NoClassDefFoundError ends the try
  
          try {
              javaVersion = JAVA_1_0;
              Class.forName("java.lang.Void");
              javaVersion = JAVA_1_1;
              Class.forName("java.lang.ThreadLocal");
              javaVersion = JAVA_1_2;
              Class.forName("java.lang.StrictMath");
              javaVersion = JAVA_1_3;
              Class.forName("java.lang.CharSequence");
              javaVersion = JAVA_1_4;
          } catch (ClassNotFoundException cnfe) {
              // swallow as we've hit the max class version that
              // we have
          }
      }
  
      /**
       * Returns the version of Java this class is running under.
       * @return the version of Java as a String, e.g. "1.1"
       */
      public static String getJavaVersion() {
          return javaVersion;
      }
  
      /**
       * Finds an executable that is part of a JRE installation based on
       * the java.home system property.
       *
       * <p><code>java</code>, <code>keytool</code>,
       * <code>policytool</code>, <code>orbd</code>, <code>rmid</code>,
       * <code>rmiregistry</code>, <code>servertool</code> and
       * <code>tnameserv</code> are JRE executables on Sun based
       * JRE's.</p>
       *
       * <p>You typically find them in <code>JAVA_HOME/jre/bin</code> if
       * <code>JAVA_HOME</code> points to your JDK installation.  JDK
       * &lt; 1.2 has them in the same directory as the JDK
       * executables.</p>
       *
       * @since Ant 1.5
       */
      public static String getJreExecutable(String command) {
          if (isNetware) {
              // Extrapolating from:
              // "NetWare may have a "java" in that directory, but 99% of
              // the time, you don't want to execute it" -- Jeff Tulley
              // <JTULLEY@novell.com>
              return command;
          }
  
          File jExecutable = findInDir(javaHome + "/bin", command);
  
          if (jExecutable == null && isAix) {
              // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
              // Sun's layout.
              jExecutable = findInDir(javaHome + "/sh", command);
          }
  
          if (jExecutable != null) {
              return jExecutable.getAbsolutePath();
          } else {
              // Unfortunately on Windows java.home doesn't always refer
              // to the correct location, so we need to fall back to
              // assuming java is somewhere on the PATH.
              return addExtension(command);
          }
      }
  
      /**
       * Finds an executable that is part of a JDK installation based on
       * the java.home system property.
       *
       * <p>You typically find them in <code>JAVA_HOME/bin</code> if
       * <code>JAVA_HOME</code> points to your JDK installation.</p>
       *
       * @since Ant 1.5
       */
      public static String getJdkExecutable(String command) {
          if (isNetware) {
              // Extrapolating from:
              // "NetWare may have a "java" in that directory, but 99% of
              // the time, you don't want to execute it" -- Jeff Tulley
              // <JTULLEY@novell.com>
              return command;
          }
  
          File jExecutable = findInDir(javaHome + "/../bin", command);
  
          if (jExecutable == null && isAix) {
              // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
              // Sun's layout.
              jExecutable = findInDir(javaHome + "/../sh", command);
          }
  
          if (jExecutable != null) {
              return jExecutable.getAbsolutePath();
          } else {
              // fall back to JRE bin directory, also catches JDK 1.0 and 1.1
              // where java.home points to the root of the JDK
              return getJreExecutable(command);
          }
      }
  
      /**
       * Adds a system specific extension to the name of an executable.
       *
       * @since Ant 1.5
       */
      private static String addExtension(String command) {
          // This is the most common extension case - exe for windows and OS/2,
          // nothing for *nix.
          return command + (isDos ? ".exe" : "");
      }
  
      /**
       * Look for an executable in a given directory.
       *
       * @return null if the executable cannot be found.
       */
      private static File findInDir(String dirName, String commandName) {
          File dir = fileUtils.normalize(dirName);
          File executable = null;
          if (dir.exists()) {
              executable = new File(dir, addExtension(commandName));
              if (!executable.exists()) {
                  executable = null;
              }
          }
          return executable;
      }
  }
  
  
  
  1.1                  jakarta-ant/src/testcases/org/apache/tools/ant/util/JavaEnvUtilsTest.java
  
  Index: JavaEnvUtilsTest.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. 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.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Ant", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 APACHE SOFTWARE FOUNDATION OR
   * ITS 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.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.tools.ant.util;
  
  import java.io.File;
  
  import junit.framework.AssertionFailedError;
  import junit.framework.TestCase;
  
  import org.apache.tools.ant.taskdefs.condition.Os;
  
  /**
   * TestCase for JavaEnvUtils.
   *
   * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
   */
  public class JavaEnvUtilsTest extends TestCase {
      public JavaEnvUtilsTest(String s) {
          super(s);
      }
  
      public void testGetExecutableNetware() {
          if (Os.isName("netware")) {
              assertEquals("java", JavaEnvUtils.getJreExecutable("java"));
              assertEquals("javac", JavaEnvUtils.getJdkExecutable("javac"));
              assertEquals("foo", JavaEnvUtils.getJreExecutable("foo"));
              assertEquals("foo", JavaEnvUtils.getJdkExecutable("foo"));
          }
      }
  
      public void testGetExecutableWindows() {
          if (Os.isFamily("windows")) {
              FileUtils fileUtils = FileUtils.newFileUtils();
              String javaHome = 
                  fileUtils.normalize(System.getProperty("java.home"))
                  .getAbsolutePath();
  
              String j = JavaEnvUtils.getJreExecutable("java");
              assertTrue(j.endsWith(".exe"));
              assertTrue(j+" is absolute", (new File(j)).isAbsolute());
              try {
                  assertTrue(j+" is normalized and in the JRE dir",
                             j.startsWith(javaHome));
              } catch (AssertionFailedError e) {
                  // java.home is bogus
                  assertEquals("java.exe", j);
              }
              
              j = JavaEnvUtils.getJdkExecutable("javac");
              assertTrue(j.endsWith(".exe"));
              assertTrue(j+" is absolute", (new File(j)).isAbsolute());
              try {
                  String javaHomeParent = 
                      fileUtils.normalize(javaHome+"/..").getAbsolutePath();
                  assertTrue(j+" is normalized and in the JDK dir",
                             j.startsWith(javaHomeParent));
                  
                  if (JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_0 ||
                      JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_1) {
                      assertTrue(j+" is normalized and in the JRE dir",
                                 j.startsWith(javaHome));
                  } else {
                      assertTrue(j+" is normalized and not in the JRE dir",
                                 !j.startsWith(javaHome));
                  }
                  
              } catch (AssertionFailedError e) {
                  // java.home is bogus
                  assertEquals("javac.exe", j);
              }
              
              assertEquals("foo.exe", JavaEnvUtils.getJreExecutable("foo"));
              assertEquals("foo.exe", JavaEnvUtils.getJdkExecutable("foo"));
          }
      }
  
      public void testGetExecutableMostPlatforms() {
          if (!Os.isName("netware") && !Os.isFamily("windows")) {
              FileUtils fileUtils = FileUtils.newFileUtils();
              String javaHome = 
                  fileUtils.normalize(System.getProperty("java.home"))
                  .getAbsolutePath();
  
              // could still be OS/2
              String extension = Os.isFamily("dos") ? ".exe" : "";
  
              String j = JavaEnvUtils.getJreExecutable("java");
              if (!extension.equals("")) {
                  assertTrue(j.endsWith(extension));
              }
              assertTrue(j+" is absolute", (new File(j)).isAbsolute());
              assertTrue(j+" is normalized and in the JRE dir",
                         j.startsWith(javaHome));
  
              j = JavaEnvUtils.getJdkExecutable("javac");
              if (!extension.equals("")) {
                  assertTrue(j.endsWith(extension));
              }
              assertTrue(j+" is absolute", (new File(j)).isAbsolute());
  
              String javaHomeParent = 
                  fileUtils.normalize(javaHome+"/..").getAbsolutePath();
              assertTrue(j+" is normalized and in the JDK dir",
                         j.startsWith(javaHomeParent));
  
              if (JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_0 ||
                  JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_1) {
                  assertTrue(j+" is normalized and in the JRE dir",
                             j.startsWith(javaHome));
              } else {
                  assertTrue(j+" is normalized and not in the JRE dir",
                             !j.startsWith(javaHome));
              }
  
              assertEquals("foo"+extension, 
                           JavaEnvUtils.getJreExecutable("foo"));
              assertEquals("foo"+extension, 
                           JavaEnvUtils.getJdkExecutable("foo"));
          }
          
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message