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.
36 _python_ = TestSCons._python_
38 test = TestSCons.TestSCons()
40 where_javac, java_version = test.java_where_javac('1.6')
44 test.write('SConstruct', """
45 env = Environment(tools = ['javac'],
47 JAVAC = r'%(where_javac)s')
48 env.Java(target = 'class1', source = 'com/sub/foo')
49 env.Java(target = 'class2', source = 'com/sub/bar')
50 env.Java(target = 'class3', source = ['src1', 'src2'])
51 env.Java(target = 'class4', source = ['src4'])
52 env.Java(target = 'class5', source = ['src5'])
53 env.Java(target = 'class6', source = ['src6'])
58 ['com', 'sub', 'foo'],
59 ['com', 'sub', 'bar'],
66 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
72 public static void main(String[] args)
80 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
86 public static void main(String[] args)
94 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
100 public static void main(String[] args)
108 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
111 public class Example4
114 public static void main(String[] args)
122 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
125 public class Example5
128 public static void main(String[] args)
136 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
139 public class Example6
142 public static void main(String[] args)
150 test.write(['src1', 'Example7.java'], """\
151 public class Example7
154 public static void main(String[] args)
162 # Acid-test file for parsing inner Java classes, courtesy Chad Austin.
163 test.write(['src2', 'Test.java'], """\
168 public void execute();
177 public void execute() {
178 System.out.println("In Inner");
182 String s1 = "class A";
183 String s2 = "new Listener() { }";
185 /* new Listener() { } */
188 public static void main(String[] args) {
194 public void execute() {
195 use(new Listener( ) {
196 public void execute() {
197 System.out.println("Inside execute()");
206 void use(Listener l) {
214 public void execute() {
221 # Testing nested anonymous inner classes, courtesy Brandon Mansfield.
222 test.write(['src4', 'NestedExample.java'], """\
223 // import java.util.*;
225 public class NestedExample
227 public NestedExample()
235 try {Thread.sleep(200);}
236 catch (Exception e) {}
241 try {Thread.sleep(200);}
242 catch (Exception e) {}
249 public static void main(String argv[])
256 # Test not finding an anonymous class when the second token after a
257 # "new" is a closing brace. This duplicates a test from the unit tests,
258 # but lets us make sure that we correctly determine that everything is
259 # up-to-date after the build.
260 test.write(['src5', 'TestSCons.java'], """\
262 public static void main(String[] args) {
263 Foo[] fooArray = new Foo[] { new Foo() };
270 # Test private inner class instantiation, courtesy Tilo Prutz:
271 # http://scons.tigris.org/issues/show_bug.cgi?id=1594
272 test.write(['src6', 'TestSCons.java'], """\
290 test.run(arguments = '.')
293 test.workpath('class1', 'com', 'other', 'Example2.class'),
294 test.workpath('class1', 'com', 'sub', 'foo', 'Example1.class'),
295 test.workpath('class1', 'com', 'sub', 'foo', 'Example3.class'),
299 test.workpath('class2', 'com', 'other', 'Example5.class'),
300 test.workpath('class2', 'com', 'sub', 'bar', 'Example4.class'),
301 test.workpath('class2', 'com', 'sub', 'bar', 'Example6.class'),
305 test.workpath('class3', 'Empty.class'),
306 test.workpath('class3', 'Example7.class'),
307 test.workpath('class3', 'Listener.class'),
308 test.workpath('class3', 'Private$1.class'),
309 test.workpath('class3', 'Private.class'),
310 test.workpath('class3', 'Test$1$1.class'),
311 test.workpath('class3', 'Test$1.class'),
312 test.workpath('class3', 'Test$Inner$1.class'),
313 test.workpath('class3', 'Test$Inner.class'),
314 test.workpath('class3', 'Test.class'),
318 test.workpath('class4', 'NestedExample$1$1.class'),
319 test.workpath('class4', 'NestedExample$1.class'),
320 test.workpath('class4', 'NestedExample.class'),
324 test.workpath('class5', 'Foo.class'),
325 test.workpath('class5', 'TestSCons.class'),
329 test.workpath('class6', 'test$1.class'),
330 test.workpath('class6', 'test$inner.class'),
331 test.workpath('class6', 'test.class'),
336 def get_class_files(dir):
337 def find_class_files(arg, dirname, fnames):
339 if fname[-6:] == '.class':
340 arg.append(os.path.join(dirname, fname))
342 os.path.walk(dir, find_class_files, result)
346 def classes_must_match(dir, expect):
348 got = get_class_files(test.workpath(dir))
350 sys.stderr.write("Expected the following class files in '%s':\n" % dir)
352 sys.stderr.write(' %s\n' % c)
353 sys.stderr.write("Got the following class files in '%s':\n" % dir)
355 sys.stderr.write(' %s\n' % c)
358 def classes_must_not_exist(dir, expect):
360 present = filter(os.path.exists, expect)
362 sys.stderr.write("Found the following unexpected class files in '%s' after cleaning:\n" % dir)
364 sys.stderr.write(' %s\n' % c)
367 classes_must_match('class1', expect_1)
368 classes_must_match('class2', expect_2)
369 classes_must_match('class3', expect_3)
370 classes_must_match('class4', expect_4)
371 classes_must_match('class5', expect_5)
372 classes_must_match('class6', expect_6)
374 test.fail_test(failed)
376 test.up_to_date(options='--debug=explain', arguments = '.')
378 test.run(arguments = '-c .')
380 classes_must_not_exist('class1', expect_1)
381 classes_must_not_exist('class2', expect_2)
382 classes_must_not_exist('class3', expect_3)
383 classes_must_not_exist('class4', expect_4)
384 classes_must_not_exist('class5', expect_5)
385 # This test case should pass, but doesn't.
386 # The expect_6 list contains the class files that the Java compiler
387 # actually creates, apparently because of the "private" instantiation
388 # of the "inner" class. Our parser doesn't currently detect this, so
389 # it doesn't know to remove that generated class file.
390 #classes_must_not_exist('class6', expect_6)
392 test.fail_test(failed)