Remove (lots) more unnecessary imports.
[scons.git] / test / Java / Java-1.6.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
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.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Test Java compilation with a live Java 1.6 "javac" compiler.
29 """
30
31 import os
32 import sys
33
34 import TestSCons
35
36 _python_ = TestSCons._python_
37
38 test = TestSCons.TestSCons()
39
40 where_javac, java_version = test.java_where_javac('1.6')
41
42
43
44 test.write('SConstruct', """
45 env = Environment(tools = ['javac'],
46                   JAVAVERSION = '1.6',
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'])
54 """ % locals())
55
56 test.subdir('com',
57             ['com', 'sub'],
58             ['com', 'sub', 'foo'],
59             ['com', 'sub', 'bar'],
60             'src1',
61             'src2',
62             'src4',
63             'src5',
64             'src6')
65
66 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
67 package com.sub.foo;
68
69 public class Example1
70 {
71
72      public static void main(String[] args)
73      {
74
75      }
76
77 }
78 """)
79
80 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
81 package com.other;
82
83 public class Example2
84 {
85
86      public static void main(String[] args)
87      {
88
89      }
90
91 }
92 """)
93
94 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
95 package com.sub.foo;
96
97 public class Example3
98 {
99
100      public static void main(String[] args)
101      {
102
103      }
104
105 }
106 """)
107
108 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
109 package com.sub.bar;
110
111 public class Example4
112 {
113
114      public static void main(String[] args)
115      {
116
117      }
118
119 }
120 """)
121
122 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
123 package com.other;
124
125 public class Example5
126 {
127
128      public static void main(String[] args)
129      {
130
131      }
132
133 }
134 """)
135
136 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
137 package com.sub.bar;
138
139 public class Example6
140 {
141
142      public static void main(String[] args)
143      {
144
145      }
146
147 }
148 """)
149
150 test.write(['src1', 'Example7.java'], """\
151 public class Example7
152 {
153
154      public static void main(String[] args)
155      {
156
157      }
158
159 }
160 """)
161
162 # Acid-test file for parsing inner Java classes, courtesy Chad Austin.
163 test.write(['src2', 'Test.java'], """\
164 class Empty {
165 }
166
167 interface Listener {
168   public void execute();
169 }
170
171 public
172 class
173 Test {
174   class Inner {
175     void go() {
176       use(new Listener() {
177         public void execute() {
178           System.out.println("In Inner");
179         }
180       });
181     }
182     String s1 = "class A";
183     String s2 = "new Listener() { }";
184     /* class B */
185     /* new Listener() { } */
186   }
187
188   public static void main(String[] args) {
189     new Test().run();
190   }
191
192   void run() {
193     use(new Listener() {
194       public void execute() {
195         use(new Listener( ) {
196           public void execute() {
197             System.out.println("Inside execute()");
198           }
199         });
200       }
201     });
202
203     new Inner().go();
204   }
205
206   void use(Listener l) {
207     l.execute();
208   }
209 }
210
211 class Private {
212   void run() {
213     new Listener() {
214       public void execute() {
215       }
216     };
217   }
218 }
219 """)
220
221 # Testing nested anonymous inner classes, courtesy Brandon Mansfield.
222 test.write(['src4', 'NestedExample.java'], """\
223 // import java.util.*;
224
225 public class NestedExample
226 {
227         public NestedExample()
228         {
229                 new Thread() {
230                         public void start()
231                         {
232                                 new Thread() {
233                                         public void start()
234                                         {
235                                                 try {Thread.sleep(200);}
236                                                 catch (Exception e) {}
237                                         }
238                                 };
239                                 while (true)
240                                 {
241                                         try {Thread.sleep(200);}
242                                         catch (Exception e) {}
243                                 }
244                         }
245                 };
246         }
247
248
249         public static void main(String argv[])
250         {
251                 new NestedExample();
252         }
253 }
254 """)
255
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'], """\
261 class TestSCons {
262     public static void main(String[] args) {
263         Foo[] fooArray = new Foo[] { new Foo() };
264     }
265 }
266
267 class Foo { }
268 """)
269
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'], """\
273 class test
274 {
275     test()
276     {
277         super();
278         new inner();
279     }
280
281     static class inner
282     {
283         private inner() {}
284     }
285 }
286 """)
287
288
289
290 test.run(arguments = '.')
291
292 expect_1 = [
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'),
296 ]
297
298 expect_2 = [
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'),
302 ]
303
304 expect_3 = [
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'),
315 ]
316
317 expect_4 = [
318     test.workpath('class4', 'NestedExample$1$1.class'),
319     test.workpath('class4', 'NestedExample$1.class'),
320     test.workpath('class4', 'NestedExample.class'),
321 ]
322
323 expect_5 = [
324     test.workpath('class5', 'Foo.class'),
325     test.workpath('class5', 'TestSCons.class'),
326 ]
327
328 expect_6 = [
329     test.workpath('class6', 'test$1.class'),
330     test.workpath('class6', 'test$inner.class'),
331     test.workpath('class6', 'test.class'),
332 ]
333
334 failed = None
335
336 def get_class_files(dir):
337     def find_class_files(arg, dirname, fnames):
338         for fname in fnames:
339             if fname[-6:] == '.class':
340                 arg.append(os.path.join(dirname, fname))
341     result = []
342     os.path.walk(dir, find_class_files, result)
343     result.sort()
344     return result
345
346 def classes_must_match(dir, expect):
347     global failed
348     got = get_class_files(test.workpath(dir))
349     if expect != got:
350         sys.stderr.write("Expected the following class files in '%s':\n" % dir)
351         for c in expect:
352             sys.stderr.write('    %s\n' % c)
353         sys.stderr.write("Got the following class files in '%s':\n" % dir)
354         for c in got:
355             sys.stderr.write('    %s\n' % c)
356         failed = 1
357
358 def classes_must_not_exist(dir, expect):
359     global failed
360     present = filter(os.path.exists, expect)
361     if present:
362         sys.stderr.write("Found the following unexpected class files in '%s' after cleaning:\n" % dir)
363         for c in present:
364             sys.stderr.write('    %s\n' % c)
365         failed = 1
366
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)
373
374 test.fail_test(failed)
375
376 test.up_to_date(options='--debug=explain', arguments = '.')
377
378 test.run(arguments = '-c .')
379
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)
391
392 test.fail_test(failed)
393
394 test.pass_test()