Merged revisions 2121-2135 via svnmerge from
[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 os.path
33 import string
34 import sys
35 import TestSCons
36
37 _python_ = TestSCons._python_
38
39 test = TestSCons.TestSCons()
40
41 ENV = test.java_ENV()
42 ENV['PATH'] = '/usr/lib/jvm/java-6-sun-1.6.0.00/bin' + os.pathsep + os.environ['PATH']
43
44 if test.detect_tool('javac', ENV=ENV):
45     where_javac = test.detect('JAVAC', 'javac', ENV=ENV)
46 else:
47     where_javac = test.where_is('javac')
48 if not where_javac:
49     test.skip_test("Could not find Java javac, skipping test(s).\n")
50
51
52
53 test.write('SConstruct', """
54 env = Environment(tools = ['javac'],
55                   JAVAVERSION = '1.6',
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'])
62 """ % locals())
63
64 test.subdir('com',
65             ['com', 'sub'],
66             ['com', 'sub', 'foo'],
67             ['com', 'sub', 'bar'],
68             'src1',
69             'src2',
70             'src4',
71             'src5')
72
73 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
74 package com.sub.foo;
75
76 public class Example1
77 {
78
79      public static void main(String[] args)
80      {
81
82      }
83
84 }
85 """)
86
87 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
88 package com.other;
89
90 public class Example2
91 {
92
93      public static void main(String[] args)
94      {
95
96      }
97
98 }
99 """)
100
101 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
102 package com.sub.foo;
103
104 public class Example3
105 {
106
107      public static void main(String[] args)
108      {
109
110      }
111
112 }
113 """)
114
115 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
116 package com.sub.bar;
117
118 public class Example4
119 {
120
121      public static void main(String[] args)
122      {
123
124      }
125
126 }
127 """)
128
129 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
130 package com.other;
131
132 public class Example5
133 {
134
135      public static void main(String[] args)
136      {
137
138      }
139
140 }
141 """)
142
143 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
144 package com.sub.bar;
145
146 public class Example6
147 {
148
149      public static void main(String[] args)
150      {
151
152      }
153
154 }
155 """)
156
157 test.write(['src1', 'Example7.java'], """\
158 public class Example7
159 {
160
161      public static void main(String[] args)
162      {
163
164      }
165
166 }
167 """)
168
169 # Acid-test file for parsing inner Java classes, courtesy Chad Austin.
170 test.write(['src2', 'Test.java'], """\
171 class Empty {
172 }
173
174 interface Listener {
175   public void execute();
176 }
177
178 public
179 class
180 Test {
181   class Inner {
182     void go() {
183       use(new Listener() {
184         public void execute() {
185           System.out.println("In Inner");
186         }
187       });
188     }
189     String s1 = "class A";
190     String s2 = "new Listener() { }";
191     /* class B */
192     /* new Listener() { } */
193   }
194
195   public static void main(String[] args) {
196     new Test().run();
197   }
198
199   void run() {
200     use(new Listener() {
201       public void execute() {
202         use(new Listener( ) {
203           public void execute() {
204             System.out.println("Inside execute()");
205           }
206         });
207       }
208     });
209
210     new Inner().go();
211   }
212
213   void use(Listener l) {
214     l.execute();
215   }
216 }
217
218 class Private {
219   void run() {
220     new Listener() {
221       public void execute() {
222       }
223     };
224   }
225 }
226 """)
227
228 # Testing nested anonymous inner classes, courtesy Brandon Mansfield.
229 test.write(['src4', 'NestedExample.java'], """\
230 // import java.util.*;
231
232 public class NestedExample
233 {
234         public NestedExample()
235         {
236                 Thread t = new Thread() {
237                         public void start()
238                         {
239                                 Thread t = new Thread() {
240                                         public void start()
241                                         {
242                                                 try {Thread.sleep(200);}
243                                                 catch (Exception e) {}
244                                         }
245                                 };
246                                 while (true)
247                                 {
248                                         try {Thread.sleep(200);}
249                                         catch (Exception e) {}
250                                 }
251                         }
252                 };
253         }
254
255
256         public static void main(String argv[])
257         {
258                 NestedExample e = new NestedExample();
259         }
260 }
261 """)
262
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'], """\
268 class TestSCons {
269     public static void main(String[] args) {
270         Foo[] fooArray = new Foo[] { new Foo() };
271     }
272 }
273
274 class Foo { }
275 """)
276
277 test.run(arguments = '.')
278
279 def get_class_files(dir):
280     def find_class_files(arg, dirname, fnames):
281         for fname in fnames:
282             if fname[-6:] == '.class':
283                 arg.append(os.path.join(dirname, fname))
284     result = []
285     os.path.walk(dir, find_class_files, result)
286     result.sort()
287     return result
288
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'))
294
295 expect_1 = [
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'),
299 ]
300
301 expect_2 = [
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'),
305 ]
306
307 expect_3 = [
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'),
318 ]
319
320 expect_4 = [
321     test.workpath('class4', 'NestedExample$1$1.class'),
322     test.workpath('class4', 'NestedExample$1.class'),
323     test.workpath('class4', 'NestedExample.class'),
324 ]
325
326 expect_5 = [
327     test.workpath('class5', 'Foo.class'),
328     test.workpath('class5', 'TestSCons.class'),
329 ]
330
331 failed = None
332
333 def classes_must_match(dir, expect, got):
334     if expect != got:
335         sys.stderr.write("Expected the following class files in '%s':\n" % dir)
336         for c in expect:
337             sys.stderr.write('    %s\n' % c)
338         sys.stderr.write("Got the following class files in '%s':\n" % dir)
339         for c in got:
340             sys.stderr.write('    %s\n' % c)
341         failed = 1
342
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)
347
348 test.fail_test(failed)
349
350 test.up_to_date(options='--debug=explain', arguments = '.')
351
352 test.pass_test()