5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
28 Test Java compilation with a live Java 1.6 "javac" compiler.
37 _python_ = TestSCons._python_
39 test = TestSCons.TestSCons()
42 ENV['PATH'] = '/usr/lib/jvm/java-6-sun-1.6.0.00/bin' + os.pathsep + os.environ['PATH']
44 if test.detect_tool('javac', ENV=ENV):
45 where_javac = test.detect('JAVAC', 'javac', ENV=ENV)
47 where_javac = test.where_is('javac')
49 test.skip_test("Could not find Java javac, skipping test(s).\n")
53 test.write('SConstruct', """
54 env = Environment(tools = ['javac'],
56 JAVAC = r'%(where_javac)s')
57 env.Java(target = 'class1', source = 'com/sub/foo')
58 env.Java(target = 'class2', source = 'com/sub/bar')
59 env.Java(target = 'class3', source = ['src1', 'src2'])
60 env.Java(target = 'class4', source = ['src4'])
61 env.Java(target = 'class5', source = ['src5'])
66 ['com', 'sub', 'foo'],
67 ['com', 'sub', 'bar'],
73 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
79 public static void main(String[] args)
87 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
93 public static void main(String[] args)
101 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
104 public class Example3
107 public static void main(String[] args)
115 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
118 public class Example4
121 public static void main(String[] args)
129 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
132 public class Example5
135 public static void main(String[] args)
143 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
146 public class Example6
149 public static void main(String[] args)
157 test.write(['src1', 'Example7.java'], """\
158 public class Example7
161 public static void main(String[] args)
169 # Acid-test file for parsing inner Java classes, courtesy Chad Austin.
170 test.write(['src2', 'Test.java'], """\
175 public void execute();
184 public void execute() {
185 System.out.println("In Inner");
189 String s1 = "class A";
190 String s2 = "new Listener() { }";
192 /* new Listener() { } */
195 public static void main(String[] args) {
201 public void execute() {
202 use(new Listener( ) {
203 public void execute() {
204 System.out.println("Inside execute()");
213 void use(Listener l) {
221 public void execute() {
228 # Testing nested anonymous inner classes, courtesy Brandon Mansfield.
229 test.write(['src4', 'NestedExample.java'], """\
230 // import java.util.*;
232 public class NestedExample
234 public NestedExample()
236 Thread t = new Thread() {
239 Thread t = new Thread() {
242 try {Thread.sleep(200);}
243 catch (Exception e) {}
248 try {Thread.sleep(200);}
249 catch (Exception e) {}
256 public static void main(String argv[])
258 NestedExample e = new NestedExample();
263 # Test not finding an anonymous class when the second token after a
264 # "new" is a closing brace. This duplicates a test from the unit tests,
265 # but lets us make sure that we correctly determine that everything is
266 # up-to-date after the build.
267 test.write(['src5', 'TestSCons.java'], """\
269 public static void main(String[] args) {
270 Foo[] fooArray = new Foo[] { new Foo() };
277 test.run(arguments = '.')
279 def get_class_files(dir):
280 def find_class_files(arg, dirname, fnames):
282 if fname[-6:] == '.class':
283 arg.append(os.path.join(dirname, fname))
285 os.path.walk(dir, find_class_files, result)
289 classes_1 = get_class_files(test.workpath('class1'))
290 classes_2 = get_class_files(test.workpath('class2'))
291 classes_3 = get_class_files(test.workpath('class3'))
292 classes_4 = get_class_files(test.workpath('class4'))
293 classes_5 = get_class_files(test.workpath('class5'))
296 test.workpath('class1', 'com', 'other', 'Example2.class'),
297 test.workpath('class1', 'com', 'sub', 'foo', 'Example1.class'),
298 test.workpath('class1', 'com', 'sub', 'foo', 'Example3.class'),
302 test.workpath('class2', 'com', 'other', 'Example5.class'),
303 test.workpath('class2', 'com', 'sub', 'bar', 'Example4.class'),
304 test.workpath('class2', 'com', 'sub', 'bar', 'Example6.class'),
308 test.workpath('class3', 'Empty.class'),
309 test.workpath('class3', 'Example7.class'),
310 test.workpath('class3', 'Listener.class'),
311 test.workpath('class3', 'Private$1.class'),
312 test.workpath('class3', 'Private.class'),
313 test.workpath('class3', 'Test$1$1.class'),
314 test.workpath('class3', 'Test$1.class'),
315 test.workpath('class3', 'Test$Inner$1.class'),
316 test.workpath('class3', 'Test$Inner.class'),
317 test.workpath('class3', 'Test.class'),
321 test.workpath('class4', 'NestedExample$1$1.class'),
322 test.workpath('class4', 'NestedExample$1.class'),
323 test.workpath('class4', 'NestedExample.class'),
327 test.workpath('class5', 'Foo.class'),
328 test.workpath('class5', 'TestSCons.class'),
333 def classes_must_match(dir, expect, got):
335 sys.stderr.write("Expected the following class files in '%s':\n" % dir)
337 sys.stderr.write(' %s\n' % c)
338 sys.stderr.write("Got the following class files in '%s':\n" % dir)
340 sys.stderr.write(' %s\n' % c)
343 classes_must_match('class1', expect_1, classes_1)
344 classes_must_match('class2', expect_2, classes_2)
345 classes_must_match('class3', expect_3, classes_3)
346 classes_must_match('class4', expect_4, classes_4)
348 test.fail_test(failed)
350 test.up_to_date(options='--debug=explain', arguments = '.')