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:
019: package org.apache.tools.ant.taskdefs;
020:
021: import org.apache.tools.ant.Project;
022: import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter;
023: import org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory;
024: import org.apache.tools.ant.taskdefs.compilers.Javac12;
025: import org.apache.tools.ant.taskdefs.compilers.Javac13;
026: import org.apache.tools.ant.taskdefs.compilers.JavacExternal;
027: import org.apache.tools.ant.util.JavaEnvUtils;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * Testcase for <javac>.
033: *
034: */
035: public class JavacTest extends TestCase {
036:
037: private Project project;
038: private Javac javac;
039:
040: public JavacTest(String name) {
041: super (name);
042: }
043:
044: public void setUp() {
045: project = new Project();
046: project.init();
047: javac = new Javac();
048: javac.setProject(project);
049: }
050:
051: /**
052: * Test setting the name of the javac executable.
053: */
054: public void testForkedExecutableName() {
055: assertNull("no fork means no executable", javac
056: .getJavacExecutable());
057:
058: project.setProperty("build.compiler", "modern");
059: assertNull("no fork means no executable", javac
060: .getJavacExecutable());
061:
062: javac.setFork(true);
063: assertNotNull("normal fork", javac.getJavacExecutable());
064: assertTrue("name should contain \"javac\"", javac
065: .getJavacExecutable().indexOf("javac") > -1);
066:
067: project.setProperty("build.compiler", "extJavac");
068: javac.setFork(false);
069: assertNotNull("fork via property", javac.getJavacExecutable());
070: assertTrue("name should contain \"javac\"", javac
071: .getJavacExecutable().indexOf("javac") > -1);
072:
073: project.setProperty("build.compiler", "whatever");
074: assertNull("no fork and not extJavac means no executable",
075: javac.getJavacExecutable());
076:
077: String myJavac = "Slartibartfast";
078: javac.setFork(true);
079: javac.setExecutable(myJavac);
080: assertEquals(myJavac, javac.getJavacExecutable());
081: }
082:
083: /**
084: * Test nested compiler args.
085: */
086: public void testCompilerArg() {
087: String[] args = javac.getCurrentCompilerArgs();
088: assertNotNull(args);
089: assertEquals("no args", 0, args.length);
090:
091: Javac.ImplementationSpecificArgument arg = javac
092: .createCompilerArg();
093: String ford = "Ford";
094: String prefect = "Prefect";
095: String testArg = ford + " " + prefect;
096: arg.setValue(testArg);
097: args = javac.getCurrentCompilerArgs();
098: assertEquals("unconditional single arg", 1, args.length);
099: assertEquals(testArg, args[0]);
100:
101: arg.setCompiler("jikes");
102: args = javac.getCurrentCompilerArgs();
103: assertNotNull(args);
104: assertEquals(
105: "implementation is jikes but build.compiler is null",
106: 0, args.length);
107:
108: project.setProperty("build.compiler", "jvc");
109: args = javac.getCurrentCompilerArgs();
110: assertNotNull(args);
111: assertEquals(
112: "implementation is jikes but build.compiler is jvc", 0,
113: args.length);
114:
115: project.setProperty("build.compiler", "jikes");
116: args = javac.getCurrentCompilerArgs();
117: assertEquals("both are jikes", 1, args.length);
118: assertEquals(testArg, args[0]);
119:
120: arg.setLine(testArg);
121: args = javac.getCurrentCompilerArgs();
122: assertEquals("split at space", 2, args.length);
123: assertEquals(ford, args[0]);
124: assertEquals(prefect, args[1]);
125: }
126:
127: /**
128: * Test nested compiler args in the fork="true" and
129: * implementation="extJavac" case.
130: */
131: public void testCompilerArgForForkAndExtJavac() {
132: Javac.ImplementationSpecificArgument arg = javac
133: .createCompilerArg();
134: String ford = "Ford";
135: String prefect = "Prefect";
136: String testArg = ford + " " + prefect;
137: arg.setValue(testArg);
138: arg.setCompiler("extJavac");
139: javac.setFork(true);
140: String[] args = javac.getCurrentCompilerArgs();
141: assertEquals("both are forked javac", 1, args.length);
142: assertEquals(testArg, args[0]);
143: }
144:
145: /**
146: * Test compiler attribute.
147: */
148: public void testCompilerAttribute() {
149: // check defaults
150: String compiler = javac.getCompiler();
151: assertNotNull(compiler);
152: if (System.getProperty("build.compiler") != null) {
153: assertEquals(System.getProperty("build.compiler"), compiler);
154: } else {
155: assertTrue("default value", "javac1.1".equals(compiler)
156: || "javac1.2".equals(compiler)
157: || "javac1.3".equals(compiler)
158: || "javac1.4".equals(compiler)
159: || "javac1.5".equals(compiler)
160: || "classic".equals(compiler));
161: }
162:
163: javac.setFork(true);
164: assertNotNull(javac.getCompiler());
165: assertEquals("extJavac", javac.getCompiler());
166: assertEquals(compiler, javac.getCompilerVersion());
167:
168: // check build.compiler provides defaults
169: javac = new Javac();
170: javac.setProject(project);
171: // setUserProperty to override system properties
172: project.setUserProperty("build.compiler", "jikes");
173: compiler = javac.getCompiler();
174: assertNotNull(compiler);
175: assertEquals("jikes", compiler);
176:
177: javac.setFork(true);
178: compiler = javac.getCompiler();
179: assertNotNull(compiler);
180: assertEquals("jikes", compiler);
181:
182: // check attribute overrides build.compiler
183: javac.setFork(false);
184: javac.setCompiler("jvc");
185: compiler = javac.getCompiler();
186: assertNotNull(compiler);
187: assertEquals("jvc", compiler);
188:
189: javac.setFork(true);
190: compiler = javac.getCompiler();
191: assertNotNull(compiler);
192: assertEquals("jvc", compiler);
193: }
194:
195: public void testCompilerAdapter() {
196: if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
197: || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
198: javac.setCompiler("javac1.1");
199: } else {
200: javac.setCompiler("javac1.4");
201: }
202:
203: javac.setDepend(true);
204: CompilerAdapter adapter = CompilerAdapterFactory.getCompiler(
205: javac.getCompiler(), javac);
206:
207: if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)
208: || JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
209: assertTrue(adapter instanceof Javac12);
210: } else {
211: assertTrue(adapter instanceof Javac13);
212: }
213:
214: javac.setFork(true);
215: adapter = CompilerAdapterFactory.getCompiler(javac
216: .getCompiler(), javac);
217: assertTrue(adapter instanceof JavacExternal);
218: }
219:
220: public void testSourceNoDefault() {
221: assertNull(javac.getSource());
222: }
223:
224: public void testSourceWithDefault() {
225: project.setNewProperty("ant.build.javac.source", "1.4");
226: assertEquals("1.4", javac.getSource());
227: }
228:
229: public void testSourceOverridesDefault() {
230: project.setNewProperty("ant.build.javac.source", "1.4");
231: javac.setSource("1.5");
232: assertEquals("1.5", javac.getSource());
233: }
234:
235: public void testTargetNoDefault() {
236: assertNull(javac.getTarget());
237: }
238:
239: public void testTargetWithDefault() {
240: project.setNewProperty("ant.build.javac.target", "1.4");
241: assertEquals("1.4", javac.getTarget());
242: }
243:
244: public void testTargetOverridesDefault() {
245: project.setNewProperty("ant.build.javac.target", "1.4");
246: javac.setTarget("1.5");
247: assertEquals("1.5", javac.getTarget());
248: }
249: }
|