added release.txt blurb. Fixed spelling typo in Defaults.xml
[scons.git] / test / Java / Java-1.4.py
blob96e6368d852758f04721f753f140c835731684f3
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 """
27 Test Java compilation with a live Java 1.4 "javac" compiler.
28 """
30 import os
31 import sys
33 import TestSCons
35 _python_ = TestSCons._python_
37 test = TestSCons.TestSCons()
39 where_javac, java_version = test.java_where_javac('1.4')
43 test.write('SConstruct', """
44 env = Environment(tools = ['javac'],
45 JAVAVERSION = '1.4',
46 JAVAC = r'%(where_javac)s')
47 env.Java(target = 'class1', source = 'com/sub/foo')
48 env.Java(target = 'class2', source = 'com/sub/bar')
49 env.Java(target = 'class3', source = ['src1', 'src2'])
50 env.Java(target = 'class4', source = ['src4'])
51 env.Java(target = 'class5', source = ['src5'])
52 env.Java(target = 'class6', source = ['src6'])
53 """ % locals())
55 test.subdir('com',
56 ['com', 'sub'],
57 ['com', 'sub', 'foo'],
58 ['com', 'sub', 'bar'],
59 'src1',
60 'src2',
61 'src4',
62 'src5',
63 'src6')
65 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
66 package com.sub.foo;
68 public class Example1
71 public static void main(String[] args)
77 """)
79 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
80 package com.other;
82 public class Example2
85 public static void main(String[] args)
91 """)
93 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
94 package com.sub.foo;
96 public class Example3
99 public static void main(String[] args)
105 """)
107 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
108 package com.sub.bar;
110 public class Example4
113 public static void main(String[] args)
119 """)
121 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
122 package com.other;
124 public class Example5
127 public static void main(String[] args)
133 """)
135 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
136 package com.sub.bar;
138 public class Example6
141 public static void main(String[] args)
147 """)
149 test.write(['src1', 'Example7.java'], """\
150 public class Example7
153 public static void main(String[] args)
159 """)
161 # Acid-test file for parsing inner Java classes, courtesy Chad Austin.
162 test.write(['src2', 'Test.java'], """\
163 class Empty {
166 interface Listener {
167 public void execute();
170 public
171 class
172 Test {
173 class Inner {
174 void go() {
175 use(new Listener() {
176 public void execute() {
177 System.out.println("In Inner");
181 String s1 = "class A";
182 String s2 = "new Listener() { }";
183 /* class B */
184 /* new Listener() { } */
187 public static void main(String[] args) {
188 new Test().run();
191 void run() {
192 use(new Listener() {
193 public void execute() {
194 use(new Listener( ) {
195 public void execute() {
196 System.out.println("Inside execute()");
202 new Inner().go();
205 void use(Listener l) {
206 l.execute();
210 class Private {
211 void run() {
212 new Listener() {
213 public void execute() {
218 """)
220 # Testing nested anonymous inner classes, courtesy Brandon Mansfield.
221 test.write(['src4', 'NestedExample.java'], """\
222 // import java.util.*;
224 public class NestedExample
226 public NestedExample()
228 new Thread() {
229 public void start()
231 new Thread() {
232 public void start()
234 try {Thread.sleep(200);}
235 catch (Exception e) {}
238 while (true)
240 try {Thread.sleep(200);}
241 catch (Exception e) {}
248 public static void main(String argv[])
250 new NestedExample();
253 """)
255 # Test not finding an anonymous class when the second token after a
256 # "new" is a closing brace. This duplicates a test from the unit tests,
257 # but lets us make sure that we correctly determine that everything is
258 # up-to-date after the build.
259 test.write(['src5', 'TestSCons.java'], """\
260 class TestSCons {
261 public static void main(String[] args) {
262 new Foo();
266 class Foo { }
267 """)
269 # Test private inner class instantiation, courtesy Tilo Prutz:
270 # https://github.com/SCons/scons/issues/1594
271 test.write(['src6', 'TestSCons.java'], """\
272 class test
274 test()
276 super();
277 new inner();
280 static class inner
282 private inner() {}
285 """)
289 test.run(arguments = '.')
291 expect_1 = [
292 test.workpath('class1', 'com', 'other', 'Example2.class'),
293 test.workpath('class1', 'com', 'sub', 'foo', 'Example1.class'),
294 test.workpath('class1', 'com', 'sub', 'foo', 'Example3.class'),
297 expect_2 = [
298 test.workpath('class2', 'com', 'other', 'Example5.class'),
299 test.workpath('class2', 'com', 'sub', 'bar', 'Example4.class'),
300 test.workpath('class2', 'com', 'sub', 'bar', 'Example6.class'),
303 expect_3 = [
304 test.workpath('class3', 'Empty.class'),
305 test.workpath('class3', 'Example7.class'),
306 test.workpath('class3', 'Listener.class'),
307 test.workpath('class3', 'Private$1.class'),
308 test.workpath('class3', 'Private.class'),
309 test.workpath('class3', 'Test$1.class'),
310 test.workpath('class3', 'Test$2.class'),
311 test.workpath('class3', 'Test$3.class'),
312 test.workpath('class3', 'Test$Inner.class'),
313 test.workpath('class3', 'Test.class'),
316 expect_4 = [
317 test.workpath('class4', 'NestedExample$1.class'),
318 test.workpath('class4', 'NestedExample$2.class'),
319 test.workpath('class4', 'NestedExample.class'),
322 expect_5 = [
323 test.workpath('class5', 'Foo.class'),
324 test.workpath('class5', 'TestSCons.class'),
327 expect_6 = [
328 test.workpath('class6', 'test$1.class'),
329 test.workpath('class6', 'test$inner.class'),
330 test.workpath('class6', 'test.class'),
333 failed = None
335 def classes_must_match(dir, expect):
336 global failed
337 got = test.java_get_class_files(test.workpath(dir))
338 if expect != got:
339 missing = set(expect) - set(got)
340 if missing:
341 sys.stderr.write("Missing the following class files from '%s':\n" % dir)
342 for c in missing:
343 sys.stderr.write(' %s\n' % c)
344 unexpected = set(got) - set(expect)
345 if unexpected:
346 sys.stderr.write("Found the following unexpected class files in '%s':\n" % dir)
347 for c in unexpected:
348 sys.stderr.write(' %s\n' % c)
349 failed = 1
351 def classes_must_not_exist(dir, expect):
352 global failed
353 present = [path for path in expect if os.path.exists(path)]
354 if present:
355 sys.stderr.write("Found the following unexpected class files in '%s' after cleaning:\n" % dir)
356 for c in present:
357 sys.stderr.write(' %s\n' % c)
358 failed = 1
360 classes_must_match('class1', expect_1)
361 classes_must_match('class2', expect_2)
362 classes_must_match('class3', expect_3)
363 classes_must_match('class4', expect_4)
364 classes_must_match('class5', expect_5)
365 classes_must_match('class6', expect_6)
367 test.fail_test(failed)
369 test.up_to_date(options='--debug=explain', arguments = '.')
371 test.run(arguments = '-c .')
373 classes_must_not_exist('class1', expect_1)
374 classes_must_not_exist('class2', expect_2)
375 classes_must_not_exist('class3', expect_3)
376 classes_must_not_exist('class4', expect_4)
377 classes_must_not_exist('class5', expect_5)
378 # This test case should pass, but doesn't.
379 # The expect_6 list contains the class files that the Java compiler
380 # actually creates, apparently because of the "private" instantiation
381 # of the "inner" class. Our parser doesn't currently detect this, so
382 # it doesn't know to remove that generated class file.
383 #classes_must_not_exist('class6', expect_6)
385 test.fail_test(failed)
387 test.pass_test()
389 # Local Variables:
390 # tab-width:4
391 # indent-tabs-mode:nil
392 # End:
393 # vim: set expandtab tabstop=4 shiftwidth=4: