KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > JavaEnvUtilsTest


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.tools.ant.util;
18
19 import java.io.File JavaDoc;
20
21 import junit.framework.AssertionFailedError;
22 import junit.framework.TestCase;
23
24 import org.apache.tools.ant.taskdefs.condition.Os;
25
26 /**
27  * TestCase for JavaEnvUtils.
28  *
29  */

30 public class JavaEnvUtilsTest extends TestCase {
31     public JavaEnvUtilsTest(String JavaDoc s) {
32         super(s);
33     }
34
35     public void testGetExecutableNetware() {
36         if (Os.isName("netware")) {
37             assertEquals("java", JavaEnvUtils.getJreExecutable("java"));
38             assertEquals("javac", JavaEnvUtils.getJdkExecutable("javac"));
39             assertEquals("foo", JavaEnvUtils.getJreExecutable("foo"));
40             assertEquals("foo", JavaEnvUtils.getJdkExecutable("foo"));
41         }
42     }
43
44     public void testGetExecutableWindows() {
45         if (Os.isFamily("windows")) {
46             FileUtils fileUtils = FileUtils.newFileUtils();
47             String JavaDoc javaHome =
48                 fileUtils.normalize(System.getProperty("java.home"))
49                 .getAbsolutePath();
50
51             String JavaDoc j = JavaEnvUtils.getJreExecutable("java");
52             assertTrue(j.endsWith(".exe"));
53             assertTrue(j+" is absolute", (new File JavaDoc(j)).isAbsolute());
54             try {
55                 assertTrue(j+" is normalized and in the JRE dir",
56                            j.startsWith(javaHome));
57             } catch (AssertionFailedError e) {
58                 // java.home is bogus
59
assertEquals("java.exe", j);
60             }
61
62             j = JavaEnvUtils.getJdkExecutable("javac");
63             assertTrue(j.endsWith(".exe"));
64             try {
65                 assertTrue(j+" is absolute", (new File JavaDoc(j)).isAbsolute());
66                 String JavaDoc javaHomeParent =
67                     fileUtils.normalize(javaHome+"/..").getAbsolutePath();
68                 assertTrue(j+" is normalized and in the JDK dir",
69                            j.startsWith(javaHomeParent));
70
71                 if (JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_0 ||
72                     JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_1) {
73                     assertTrue(j+" is normalized and in the JRE dir",
74                                j.startsWith(javaHome));
75                 } else {
76                     assertTrue(j+" is normalized and not in the JRE dir",
77                                !j.startsWith(javaHome));
78                 }
79
80             } catch (AssertionFailedError e) {
81                 // java.home is bogus
82
assertEquals("javac.exe", j);
83             }
84
85             assertEquals("foo.exe", JavaEnvUtils.getJreExecutable("foo"));
86             assertEquals("foo.exe", JavaEnvUtils.getJdkExecutable("foo"));
87         }
88     }
89
90     public void testGetExecutableMostPlatforms() {
91         if (!Os.isName("netware") && !Os.isFamily("windows")) {
92             FileUtils fileUtils = FileUtils.newFileUtils();
93             String JavaDoc javaHome =
94                 fileUtils.normalize(System.getProperty("java.home"))
95                 .getAbsolutePath();
96
97             // could still be OS/2
98
String JavaDoc extension = Os.isFamily("dos") ? ".exe" : "";
99
100             String JavaDoc j = JavaEnvUtils.getJreExecutable("java");
101             if (!extension.equals("")) {
102                 assertTrue(j.endsWith(extension));
103             }
104             assertTrue(j+" is absolute", (new File JavaDoc(j)).isAbsolute());
105             assertTrue(j+" is normalized and in the JRE dir",
106                        j.startsWith(javaHome));
107
108             j = JavaEnvUtils.getJdkExecutable("javac");
109             if (!extension.equals("")) {
110                 assertTrue(j.endsWith(extension));
111             }
112             assertTrue(j+" is absolute", (new File JavaDoc(j)).isAbsolute());
113
114             String JavaDoc javaHomeParent =
115                 fileUtils.normalize(javaHome+"/..").getAbsolutePath();
116             assertTrue(j+" is normalized and in the JDK dir",
117                        j.startsWith(javaHomeParent));
118
119             if (JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_0 ||
120                 JavaEnvUtils.getJavaVersion() == JavaEnvUtils.JAVA_1_1 ||
121                 Os.isFamily("mac")) {
122                 assertTrue(j+" is normalized and in the JRE dir",
123                            j.startsWith(javaHome));
124             } else {
125                 assertTrue(j+" is normalized and not in the JRE dir",
126                            !j.startsWith(javaHome));
127             }
128
129             assertEquals("foo"+extension,
130                          JavaEnvUtils.getJreExecutable("foo"));
131             assertEquals("foo"+extension,
132                          JavaEnvUtils.getJdkExecutable("foo"));
133         }
134
135     }
136
137 }
138
Popular Tags