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