1 | /*
|
---|
2 | * Copyright 2001-2002,2004-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.taskdefs;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.Project;
|
---|
21 | import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter;
|
---|
22 | import org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory;
|
---|
23 | import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
|
---|
24 | import org.apache.tools.ant.taskdefs.compilers.Javac12;
|
---|
25 | import org.apache.tools.ant.taskdefs.compilers.Javac13;
|
---|
26 | import org.apache.tools.ant.taskdefs.compilers.JavacExternal;
|
---|
27 | import org.apache.tools.ant.util.JavaEnvUtils;
|
---|
28 |
|
---|
29 | import junit.framework.TestCase;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Testcase for <javac>.
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public class JavacTest extends TestCase {
|
---|
36 |
|
---|
37 | private Project project;
|
---|
38 | private Javac javac;
|
---|
39 |
|
---|
40 | public JavacTest(String name) {
|
---|
41 | super(name);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public void setUp() {
|
---|
45 | project = new Project();
|
---|
46 | project.init();
|
---|
47 | javac = new Javac();
|
---|
48 | javac.setProject(project);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Test setting the name of the javac executable.
|
---|
53 | */
|
---|
54 | public void testForkedExecutableName() {
|
---|
55 | assertNull("no fork means no executable", javac.getJavacExecutable());
|
---|
56 |
|
---|
57 | project.setProperty("build.compiler", "modern");
|
---|
58 | assertNull("no fork means no executable", javac.getJavacExecutable());
|
---|
59 |
|
---|
60 | javac.setFork(true);
|
---|
61 | assertNotNull("normal fork", javac.getJavacExecutable());
|
---|
62 | assertTrue("name should contain \"javac\"",
|
---|
63 | javac.getJavacExecutable().indexOf("javac") > -1);
|
---|
64 |
|
---|
65 | project.setProperty("build.compiler", "extJavac");
|
---|
66 | javac.setFork(false);
|
---|
67 | assertNotNull("fork via property", javac.getJavacExecutable());
|
---|
68 | assertTrue("name should contain \"javac\"",
|
---|
69 | javac.getJavacExecutable().indexOf("javac") > -1);
|
---|
70 |
|
---|
71 | project.setProperty("build.compiler", "whatever");
|
---|
72 | assertNull("no fork and not extJavac means no executable",
|
---|
73 | javac.getJavacExecutable());
|
---|
74 |
|
---|
75 | String myJavac = "Slartibartfast";
|
---|
76 | javac.setFork(true);
|
---|
77 | javac.setExecutable(myJavac);
|
---|
78 | assertEquals(myJavac, javac.getJavacExecutable());
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Test nested compiler args.
|
---|
83 | */
|
---|
84 | public void testCompilerArg() {
|
---|
85 | String[] args = javac.getCurrentCompilerArgs();
|
---|
86 | assertNotNull(args);
|
---|
87 | assertEquals("no args", 0, args.length);
|
---|
88 |
|
---|
89 | Javac.ImplementationSpecificArgument arg = javac.createCompilerArg();
|
---|
90 | String ford = "Ford";
|
---|
91 | String prefect = "Prefect";
|
---|
92 | String testArg = ford + " " + prefect;
|
---|
93 | arg.setValue(testArg);
|
---|
94 | args = javac.getCurrentCompilerArgs();
|
---|
95 | assertEquals("unconditional single arg", 1, args.length);
|
---|
96 | assertEquals(testArg, args[0]);
|
---|
97 |
|
---|
98 | arg.setCompiler("jikes");
|
---|
99 | args = javac.getCurrentCompilerArgs();
|
---|
100 | assertNotNull(args);
|
---|
101 | assertEquals("implementation is jikes but build.compiler is null",
|
---|
102 | 0, args.length);
|
---|
103 |
|
---|
104 | project.setProperty("build.compiler", "jvc");
|
---|
105 | args = javac.getCurrentCompilerArgs();
|
---|
106 | assertNotNull(args);
|
---|
107 | assertEquals("implementation is jikes but build.compiler is jvc",
|
---|
108 | 0, args.length);
|
---|
109 |
|
---|
110 | project.setProperty("build.compiler", "jikes");
|
---|
111 | args = javac.getCurrentCompilerArgs();
|
---|
112 | assertEquals("both are jikes", 1, args.length);
|
---|
113 | assertEquals(testArg, args[0]);
|
---|
114 |
|
---|
115 | arg.setLine(testArg);
|
---|
116 | args = javac.getCurrentCompilerArgs();
|
---|
117 | assertEquals("split at space", 2, args.length);
|
---|
118 | assertEquals(ford, args[0]);
|
---|
119 | assertEquals(prefect, args[1]);
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Test nested compiler args in the fork="true" and
|
---|
124 | * implementation="extJavac" case.
|
---|
125 | */
|
---|
126 | public void testCompilerArgForForkAndExtJavac() {
|
---|
127 | Javac.ImplementationSpecificArgument arg = javac.createCompilerArg();
|
---|
128 | String ford = "Ford";
|
---|
129 | String prefect = "Prefect";
|
---|
130 | String testArg = ford + " " + prefect;
|
---|
131 | arg.setValue(testArg);
|
---|
132 | arg.setCompiler("extJavac");
|
---|
133 | javac.setFork(true);
|
---|
134 | String[] args = javac.getCurrentCompilerArgs();
|
---|
135 | assertEquals("both are forked javac", 1, args.length);
|
---|
136 | assertEquals(testArg, args[0]);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Test compiler attribute.
|
---|
141 | */
|
---|
142 | public void testCompilerAttribute() {
|
---|
143 | // check defaults
|
---|
144 | String compiler = javac.getCompiler();
|
---|
145 | assertNotNull(compiler);
|
---|
146 | assertTrue("default value",
|
---|
147 | "javac1.1".equals(compiler)
|
---|
148 | || "javac1.2".equals(compiler)
|
---|
149 | || "javac1.3".equals(compiler)
|
---|
150 | || "javac1.4".equals(compiler)
|
---|
151 | || "javac1.5".equals(compiler)
|
---|
152 | || "classic".equals(compiler));
|
---|
153 |
|
---|
154 | javac.setFork(true);
|
---|
155 | assertNotNull(javac.getCompiler());
|
---|
156 | assertEquals("extJavac", javac.getCompiler());
|
---|
157 | assertEquals(compiler, javac.getCompilerVersion());
|
---|
158 |
|
---|
159 | // check build.compiler provides defaults
|
---|
160 | javac = new Javac();
|
---|
161 | javac.setProject(project);
|
---|
162 | project.setNewProperty("build.compiler", "jikes");
|
---|
163 | compiler = javac.getCompiler();
|
---|
164 | assertNotNull(compiler);
|
---|
165 | assertEquals("jikes", compiler);
|
---|
166 |
|
---|
167 | javac.setFork(true);
|
---|
168 | compiler = javac.getCompiler();
|
---|
169 | assertNotNull(compiler);
|
---|
170 | assertEquals("jikes", compiler);
|
---|
171 |
|
---|
172 | // check attribute overrides build.compiler
|
---|
173 | javac.setFork(false);
|
---|
174 | javac.setCompiler("jvc");
|
---|
175 | compiler = javac.getCompiler();
|
---|
176 | assertNotNull(compiler);
|
---|
177 | assertEquals("jvc", compiler);
|
---|
178 |
|
---|
179 | javac.setFork(true);
|
---|
180 | compiler = javac.getCompiler();
|
---|
181 | assertNotNull(compiler);
|
---|
182 | assertEquals("jvc", compiler);
|
---|
183 | }
|
---|
184 |
|
---|
185 | public void testCompilerAdapter() {
|
---|
186 | if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)
|
---|
187 | || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
|
---|
188 | || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
|
---|
189 | javac.setCompiler("javac1.1");
|
---|
190 | } else {
|
---|
191 | javac.setCompiler("javac1.4");
|
---|
192 | }
|
---|
193 |
|
---|
194 | javac.setDepend(true);
|
---|
195 | CompilerAdapter adapter =
|
---|
196 | CompilerAdapterFactory.getCompiler(javac.getCompiler(), javac);
|
---|
197 |
|
---|
198 | if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)
|
---|
199 | || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
|
---|
200 | || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
|
---|
201 | assertTrue(adapter instanceof Javac12);
|
---|
202 | } else {
|
---|
203 | assertTrue(adapter instanceof Javac13);
|
---|
204 | }
|
---|
205 |
|
---|
206 | javac.setFork(true);
|
---|
207 | adapter =
|
---|
208 | CompilerAdapterFactory.getCompiler(javac.getCompiler(), javac);
|
---|
209 | assertTrue(adapter instanceof JavacExternal);
|
---|
210 | }
|
---|
211 |
|
---|
212 | }
|
---|