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 "javac" compiler.
37 _python_ = TestSCons._python_
39 test = TestSCons.TestSCons()
43 if test.detect_tool('javac', ENV=ENV):
44 where_javac = test.detect('JAVAC', 'javac', ENV=ENV)
46 where_javac = test.where_is('javac')
48 test.skip_test("Could not find Java javac, skipping test(s).\n")
52 test.write('SConstruct', """
53 env = Environment(tools = ['javac'],
54 JAVAC = r'%(where_javac)s')
55 env.Java(target = 'class1', source = 'com/sub/foo')
56 env.Java(target = 'class2', source = 'com/sub/bar')
57 env.Java(target = 'class3', source = ['src1', 'src2'])
58 env.Java(target = 'class4', source = ['src4'])
59 env.Java(target = 'class5', source = ['src5'])
64 ['com', 'sub', 'foo'],
65 ['com', 'sub', 'bar'],
71 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
77 public static void main(String[] args)
85 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
91 public static void main(String[] args)
99 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
102 public class Example3
105 public static void main(String[] args)
113 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
116 public class Example4
119 public static void main(String[] args)
127 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
130 public class Example5
133 public static void main(String[] args)
141 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
144 public class Example6
147 public static void main(String[] args)
155 test.write(['src1', 'Example7.java'], """\
156 public class Example7
159 public static void main(String[] args)
167 # Acid-test file for parsing inner Java classes, courtesy Chad Austin.
168 test.write(['src2', 'Test.java'], """\
173 public void execute();
182 public void execute() {
183 System.out.println("In Inner");
187 String s1 = "class A";
188 String s2 = "new Listener() { }";
190 /* new Listener() { } */
193 public static void main(String[] args) {
199 public void execute() {
200 use(new Listener( ) {
201 public void execute() {
202 System.out.println("Inside execute()");
211 void use(Listener l) {
219 public void execute() {
226 # Testing nested anonymous inner classes, courtesy Brandon Mansfield.
227 test.write(['src4', 'NestedExample.java'], """\
228 // import java.util.*;
230 public class NestedExample
232 public NestedExample()
234 Thread t = new Thread() {
237 Thread t = new Thread() {
240 try {Thread.sleep(200);}
241 catch (Exception e) {}
246 try {Thread.sleep(200);}
247 catch (Exception e) {}
254 public static void main(String argv[])
256 NestedExample e = new NestedExample();
261 # Test not finding an anonymous class when the second token after a
262 # "new" is a closing brace. This duplicates a test from the unit tests,
263 # but lets us make sure that we correctly determine that everything is
264 # up-to-date after the build.
265 test.write(['src5', 'TestSCons.java'], """\
267 public static void main(String[] args) {
268 Foo[] fooArray = new Foo[] { new Foo() };
275 test.run(arguments = '.')
277 def get_class_files(dir):
278 def find_class_files(arg, dirname, fnames):
280 if fname[-6:] == '.class':
281 arg.append(os.path.join(dirname, fname))
283 os.path.walk(dir, find_class_files, result)
287 classes_1 = get_class_files(test.workpath('class1'))
288 classes_2 = get_class_files(test.workpath('class2'))
289 classes_3 = get_class_files(test.workpath('class3'))
290 classes_4 = get_class_files(test.workpath('class4'))
291 classes_5 = get_class_files(test.workpath('class5'))
294 test.workpath('class1', 'com', 'other', 'Example2.class'),
295 test.workpath('class1', 'com', 'sub', 'foo', 'Example1.class'),
296 test.workpath('class1', 'com', 'sub', 'foo', 'Example3.class'),
300 test.workpath('class2', 'com', 'other', 'Example5.class'),
301 test.workpath('class2', 'com', 'sub', 'bar', 'Example4.class'),
302 test.workpath('class2', 'com', 'sub', 'bar', 'Example6.class'),
306 test.workpath('class3', 'Empty.class'),
307 test.workpath('class3', 'Example7.class'),
308 test.workpath('class3', 'Listener.class'),
309 test.workpath('class3', 'Private$1.class'),
310 test.workpath('class3', 'Private.class'),
311 test.workpath('class3', 'Test$1.class'),
312 test.workpath('class3', 'Test$2.class'),
313 test.workpath('class3', 'Test$3.class'),
314 test.workpath('class3', 'Test$Inner.class'),
315 test.workpath('class3', 'Test.class'),
319 test.workpath('class4', 'NestedExample$1.class'),
320 test.workpath('class4', 'NestedExample$2.class'),
321 test.workpath('class4', 'NestedExample.class'),
325 test.workpath('class5', 'Foo.class'),
326 test.workpath('class5', 'TestSCons.class'),
329 def classes_must_match(dir, expect, got):
331 sys.stderr.write("Expected the following class files in '%s':\n" % dir)
333 sys.stderr.write(' %s\n' % c)
334 sys.stderr.write("Got the following class files in '%s':\n" % dir)
336 sys.stderr.write(' %s\n' % c)
339 classes_must_match('class1', expect_1, classes_1)
340 classes_must_match('class2', expect_2, classes_2)
341 classes_must_match('class3', expect_3, classes_3)
342 classes_must_match('class4', expect_4, classes_4)
344 test.up_to_date(arguments = '.')