001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.util;
019:
020: import java.io.File;
021:
022: import junit.framework.AssertionFailedError;
023: import junit.framework.TestCase;
024:
025: import org.apache.tools.ant.taskdefs.condition.Os;
026:
027: /**
028: * TestCase for JavaEnvUtils.
029: *
030: */
031: public class JavaEnvUtilsTest extends TestCase {
032:
033: private static final FileUtils FILE_UTILS = FileUtils
034: .getFileUtils();
035:
036: public JavaEnvUtilsTest(String s) {
037: super (s);
038: }
039:
040: public void testGetExecutableNetware() {
041: if (Os.isName("netware")) {
042: assertEquals("java", JavaEnvUtils.getJreExecutable("java"));
043: assertEquals("javac", JavaEnvUtils
044: .getJdkExecutable("javac"));
045: assertEquals("foo", JavaEnvUtils.getJreExecutable("foo"));
046: assertEquals("foo", JavaEnvUtils.getJdkExecutable("foo"));
047: }
048: }
049:
050: public void testGetExecutableWindows() {
051: if (Os.isFamily("windows")) {
052: String javaHome = FILE_UTILS.normalize(
053: System.getProperty("java.home")).getAbsolutePath();
054:
055: String j = JavaEnvUtils.getJreExecutable("java");
056: assertTrue(j.endsWith(".exe"));
057: assertTrue(j + " is absolute", (new File(j)).isAbsolute());
058: try {
059: assertTrue(j + " is normalized and in the JRE dir", j
060: .startsWith(javaHome));
061: } catch (AssertionFailedError e) {
062: // java.home is bogus
063: assertEquals("java.exe", j);
064: }
065:
066: j = JavaEnvUtils.getJdkExecutable("javac");
067: assertTrue(j.endsWith(".exe"));
068: try {
069: assertTrue(j + " is absolute", (new File(j))
070: .isAbsolute());
071: String javaHomeParent = FILE_UTILS.normalize(
072: javaHome + "/..").getAbsolutePath();
073: assertTrue(j + " is normalized and in the JDK dir", j
074: .startsWith(javaHomeParent));
075: assertTrue(j + " is normalized and not in the JRE dir",
076: !j.startsWith(javaHome));
077:
078: } catch (AssertionFailedError e) {
079: // java.home is bogus
080: assertEquals("javac.exe", j);
081: }
082:
083: assertEquals("foo.exe", JavaEnvUtils
084: .getJreExecutable("foo"));
085: assertEquals("foo.exe", JavaEnvUtils
086: .getJdkExecutable("foo"));
087: }
088: }
089:
090: public void testGetExecutableMostPlatforms() {
091: if (!Os.isName("netware") && !Os.isFamily("windows")) {
092: String javaHome = FILE_UTILS.normalize(
093: System.getProperty("java.home")).getAbsolutePath();
094:
095: // could still be OS/2
096: String extension = Os.isFamily("dos") ? ".exe" : "";
097:
098: String j = JavaEnvUtils.getJreExecutable("java");
099: if (!extension.equals("")) {
100: assertTrue(j.endsWith(extension));
101: }
102: assertTrue(j + " is absolute", (new File(j)).isAbsolute());
103: assertTrue(j + " is normalized and in the JRE dir", j
104: .startsWith(javaHome));
105:
106: j = JavaEnvUtils.getJdkExecutable("javac");
107: if (!extension.equals("")) {
108: assertTrue(j.endsWith(extension));
109: }
110: assertTrue(j + " is absolute", (new File(j)).isAbsolute());
111:
112: String javaHomeParent = FILE_UTILS.normalize(
113: javaHome + "/..").getAbsolutePath();
114: assertTrue(j + " is normalized and in the JDK dir", j
115: .startsWith(javaHomeParent));
116:
117: if (Os.isFamily("mac")) {
118: assertTrue(j + " is normalized and in the JRE dir", j
119: .startsWith(javaHome));
120: } else {
121: assertTrue(j + " is normalized and not in the JRE dir",
122: !j.startsWith(javaHome));
123: }
124:
125: assertEquals("foo" + extension, JavaEnvUtils
126: .getJreExecutable("foo"));
127: assertEquals("foo" + extension, JavaEnvUtils
128: .getJdkExecutable("foo"));
129: }
130:
131: }
132:
133: public void testIsAtLeastJavaVersion() {
134: assertTrue(
135: "Current java version is not at least the current java version...",
136: JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils
137: .getJavaVersion()));
138: assertFalse(
139: "In case the current java version is higher than 9.0 definitely a new algorithem will be needed",
140: JavaEnvUtils.isAtLeastJavaVersion("9.0"));
141: }
142:
143: }
|