KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > CommandlineJavaTest


1 /*
2  * Copyright 2000-2005 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
18 package org.apache.tools.ant.types;
19
20 import org.apache.tools.ant.Project;
21 import org.apache.tools.ant.util.JavaEnvUtils;
22
23 import junit.framework.TestCase;
24 import junit.framework.AssertionFailedError;
25
26 import java.io.File JavaDoc;
27
28 /**
29  * JUnit 3 testcases for org.apache.tools.ant.CommandlineJava
30  *
31  */

32 public class CommandlineJavaTest extends TestCase {
33
34     public CommandlineJavaTest(String JavaDoc name) {
35         super(name);
36     }
37
38     private Project project;
39
40     public void setUp() {
41         project = new Project();
42         project.setBasedir(".");
43         project.setProperty("build.sysclasspath", "ignore");
44     }
45
46     public void testGetCommandline() throws CloneNotSupportedException JavaDoc {
47         CommandlineJava c = new CommandlineJava();
48         c.createArgument().setValue("org.apache.tools.ant.CommandlineJavaTest");
49         c.setClassname("junit.textui.TestRunner");
50         c.createVmArgument().setValue("-Djava.compiler=NONE");
51         String JavaDoc[] s = c.getCommandline();
52         assertEquals("no classpath", 4, s.length);
53         /*
54          * After changing CommandlineJava to search for the java
55          * executable, I don't know, how to tests the value returned
56          * here without using the same logic as applied in the class
57          * itself.
58          *
59          * assertTrue("no classpath", "java", s[0]);
60          */

61         assertEquals("no classpath", "-Djava.compiler=NONE", s[1]);
62         assertEquals("no classpath", "junit.textui.TestRunner", s[2]);
63         assertEquals("no classpath",
64                      "org.apache.tools.ant.CommandlineJavaTest", s[3]);
65         try {
66             CommandlineJava c2 = (CommandlineJava) c.clone();
67         } catch (NullPointerException JavaDoc ex) {
68             fail("cloning should work without classpath specified");
69         }
70
71         c.createClasspath(project).setLocation(project.resolveFile("build.xml"));
72         c.createClasspath(project).setLocation(project.resolveFile(
73             System.getProperty("ant.home")+"/lib/ant.jar"));
74         s = c.getCommandline();
75         assertEquals("with classpath", 6, s.length);
76         // assertEquals("with classpath", "java", s[0]);
77
assertEquals("with classpath", "-Djava.compiler=NONE", s[1]);
78         assertEquals("with classpath", "-classpath", s[2]);
79         assertTrue("build.xml contained",
80                s[3].indexOf("build.xml"+java.io.File.pathSeparator) >= 0);
81         assertTrue("ant.jar contained", s[3].endsWith("ant.jar"));
82         assertEquals("with classpath", "junit.textui.TestRunner", s[4]);
83         assertEquals("with classpath",
84                      "org.apache.tools.ant.CommandlineJavaTest", s[5]);
85     }
86
87     public void testJarOption() throws Exception JavaDoc {
88         CommandlineJava c = new CommandlineJava();
89         c.createArgument().setValue("arg1");
90         c.setJar("myfile.jar");
91         c.createVmArgument().setValue("-classic");
92         c.createVmArgument().setValue("-Dx=y");
93         String JavaDoc[] s = c.getCommandline();
94         assertEquals("-classic", s[1]);
95         assertEquals("-Dx=y", s[2]);
96         assertEquals("-jar", s[3]);
97         assertEquals("myfile.jar", s[4]);
98         assertEquals("arg1", s[5]);
99     }
100
101     public void testSysproperties() {
102         String JavaDoc currentClasspath = System.getProperty("java.class.path");
103         assertNotNull(currentClasspath);
104         assertNull(System.getProperty("key"));
105         CommandlineJava c = new CommandlineJava();
106         Environment.Variable v = new Environment.Variable();
107         v.setKey("key");
108         v.setValue("value");
109         c.addSysproperty(v);
110
111         project.setProperty("key2", "value2");
112         PropertySet ps = new PropertySet();
113         ps.setProject(project);
114         ps.appendName("key2");
115         c.addSyspropertyset(ps);
116
117         try {
118             c.setSystemProperties();
119             String JavaDoc newClasspath = System.getProperty("java.class.path");
120             assertNotNull(newClasspath);
121             assertEquals(currentClasspath, newClasspath);
122             assertNotNull(System.getProperty("key"));
123             assertEquals("value", System.getProperty("key"));
124             assertTrue(System.getProperties().containsKey("java.class.path"));
125             assertNotNull(System.getProperty("key2"));
126             assertEquals("value2", System.getProperty("key2"));
127         } finally {
128             c.restoreSystemProperties();
129         }
130         assertNull(System.getProperty("key"));
131         assertNull(System.getProperty("key2"));
132     }
133
134     public void testAssertions() throws CloneNotSupportedException JavaDoc {
135         if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
136             || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
137             return;
138         }
139
140         CommandlineJava c = new CommandlineJava();
141         c.createArgument().setValue("org.apache.tools.ant.CommandlineJavaTest");
142         c.setClassname("junit.textui.TestRunner");
143         c.createVmArgument().setValue("-Djava.compiler=NONE");
144         Assertions a = new Assertions();
145         a.setProject(project);
146         Assertions.EnabledAssertion ea = new Assertions.EnabledAssertion();
147         ea.setClass("junit.textui.TestRunner");
148         a.addEnable(ea);
149         c.setAssertions(a);
150
151         String JavaDoc[] expected = new String JavaDoc[] {
152             null,
153             "-Djava.compiler=NONE",
154             "-ea:junit.textui.TestRunner",
155             "junit.textui.TestRunner",
156             "org.apache.tools.ant.CommandlineJavaTest",
157         };
158             
159         // only the second iteration would pass because of PR 27218
160
for (int i = 0; i < 3; i++) {
161             String JavaDoc[] s = c.getCommandline();
162             assertEquals(expected.length, s.length);
163             for (int j = 1; j < expected.length; j++) {
164                 assertEquals(expected[j], s[j]);
165             }
166         }
167         CommandlineJava c2 = (CommandlineJava) c.clone();
168         String JavaDoc[] s = c2.getCommandline();
169         assertEquals(expected.length, s.length);
170         for (int j = 1; j < expected.length; j++) {
171             assertEquals(expected[j], s[j]);
172         }
173     }
174
175 }
176
Popular Tags