More command-line output customizability: , , , .
[scons.git] / test / Java / JAVAC.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 import os
28 import string
29 import sys
30 import TestSCons
31
32 python = TestSCons.python
33
34 test = TestSCons.TestSCons()
35
36
37
38 test.write('myjavac.py', r"""
39 import sys
40 args = sys.argv[1:]
41 while args:
42     a = args[0]
43     if a == '-d':
44         args = args[1:]
45     elif a == '-sourcepath':
46         args = args[1:]
47     else:
48         break
49     args = args[1:]
50 for file in args:
51     infile = open(file, 'rb')
52     outfile = open(file[:-5] + '.class', 'wb')
53     for l in infile.readlines():
54         if l[:9] != '/*javac*/':
55             outfile.write(l)
56 sys.exit(0)
57 """)
58
59 test.write('SConstruct', """
60 env = Environment(tools = ['javac'],
61                   JAVAC = r'%s myjavac.py')
62 env.Java(target = '.', source = '.')
63 """ % (python))
64
65 test.write('test1.java', """\
66 test1.java
67 /*javac*/
68 line 3
69 """)
70
71 test.run(arguments = '.', stderr = None)
72
73 test.must_match('test1.class', "test1.java\nline 3\n")
74
75 if os.path.normcase('.java') == os.path.normcase('.JAVA'):
76
77     test.write('SConstruct', """\
78 env = Environment(tools = ['javac'],
79                   JAVAC = r'%s myjavac.py')
80 env.Java(target = '.', source = '.')
81 """ % python)
82
83     test.write('test2.JAVA', """\
84 test2.JAVA
85 /*javac*/
86 line 3
87 """)
88
89     test.run(arguments = '.', stderr = None)
90
91     test.must_match('test2.class', "test2.JAVA\nline 3\n")
92
93
94 if test.detect_tool('javac'):
95     where_javac = test.detect('JAVAC', 'javac')
96 else:
97     import SCons.Environment
98     env = SCons.Environment.Environment()
99     where_javac = env.WhereIs('javac', os.environ['PATH'])
100     if not where_javac:
101         where_javac = env.WhereIs('javac', '/usr/local/j2sdk1.3.1/bin')
102         if not where_javac:
103             print "Could not find Java, skipping test(s)."
104             test.pass_test(1)
105         
106
107
108 test.write("wrapper.py", """\
109 import os
110 import string
111 import sys
112 open('%s', 'ab').write("wrapper.py %%s\\n" %% string.join(sys.argv[1:]))
113 os.system(string.join(sys.argv[1:], " "))
114 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
115
116 test.write('SConstruct', """
117 foo = Environment(tools = ['javac'],
118                   JAVAC = '%s')
119 javac = foo.Dictionary('JAVAC')
120 bar = foo.Copy(JAVAC = r'%s wrapper.py ' + javac)
121 foo.Java(target = 'class1', source = 'com/sub/foo')
122 bar.Java(target = 'class2', source = 'com/sub/bar')
123 foo.Java(target = 'class3', source = ['src1', 'src2'])
124 """ % (where_javac, python))
125
126 test.subdir('com',
127             ['com', 'sub'],
128             ['com', 'sub', 'foo'],
129             ['com', 'sub', 'bar'],
130             'src1',
131             'src2')
132
133 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
134 package com.sub.foo;
135
136 public class Example1
137 {
138
139      public static void main(String[] args)
140      {
141
142      }
143
144 }
145 """)
146
147 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
148 package com.other;
149
150 public class Example2
151 {
152
153      public static void main(String[] args)
154      {
155
156      }
157
158 }
159 """)
160
161 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
162 package com.sub.foo;
163
164 public class Example3
165 {
166
167      public static void main(String[] args)
168      {
169
170      }
171
172 }
173 """)
174
175 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
176 package com.sub.bar;
177
178 public class Example4
179 {
180
181      public static void main(String[] args)
182      {
183
184      }
185
186 }
187 """)
188
189 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
190 package com.other;
191
192 public class Example5
193 {
194
195      public static void main(String[] args)
196      {
197
198      }
199
200 }
201 """)
202
203 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
204 package com.sub.bar;
205
206 public class Example6
207 {
208
209      public static void main(String[] args)
210      {
211
212      }
213
214 }
215 """)
216
217 test.write(['src1', 'Example7.java'], """\
218 public class Example7
219 {
220
221      public static void main(String[] args)
222      {
223
224      }
225
226 }
227 """)
228
229 # Acid-test file for parsing inner Java classes, courtesy Chad Austin.
230 test.write(['src2', 'Test.java'], """\
231 class Empty {
232 }
233
234 interface Listener {
235   public void execute();
236 }
237
238 public
239 class
240 Test {
241   class Inner {
242     void go() {
243       use(new Listener() {
244         public void execute() {
245           System.out.println("In Inner");
246         }
247       });
248     }
249     String s1 = "class A";
250     String s2 = "new Listener() { }";
251     /* class B */
252     /* new Listener() { } */
253   }
254
255   public static void main(String[] args) {
256     new Test().run();
257   }
258
259   void run() {
260     use(new Listener() {
261       public void execute() {
262         use(new Listener( ) {
263           public void execute() {
264             System.out.println("Inside execute()");
265           }
266         });
267       }
268     });
269
270     new Inner().go();
271   }
272
273   void use(Listener l) {
274     l.execute();
275   }
276 }
277
278 class Private {
279   void run() {
280     new Listener() {
281       public void execute() {
282       }
283     };
284   }
285 }
286 """)
287
288 test.run(arguments = '.')
289
290 test.must_match('wrapper.out', "wrapper.py %s -d class2 -sourcepath com/sub/bar com/sub/bar/Example4.java com/sub/bar/Example5.java com/sub/bar/Example6.java\n" % where_javac)
291
292 test.must_exist(test.workpath('class1', 'com', 'sub', 'foo', 'Example1.class'))
293 test.must_exist(test.workpath('class1', 'com', 'other', 'Example2.class'))
294 test.must_exist(test.workpath('class1', 'com', 'sub', 'foo', 'Example3.class'))
295
296 test.must_exist(test.workpath('class2', 'com', 'sub', 'bar', 'Example4.class'))
297 test.must_exist(test.workpath('class2', 'com', 'other', 'Example5.class'))
298 test.must_exist(test.workpath('class2', 'com', 'sub', 'bar', 'Example6.class'))
299
300 test.must_exist(test.workpath('class3', 'Example7.class'))
301
302 test.must_exist(test.workpath('class3', 'Empty.class'))
303 test.must_exist(test.workpath('class3', 'Listener.class'))
304 test.must_exist(test.workpath('class3', 'Private.class'))
305 test.must_exist(test.workpath('class3', 'Private$1.class'))
306 test.must_exist(test.workpath('class3', 'Test.class'))
307 test.must_exist(test.workpath('class3', 'Test$1.class'))
308 test.must_exist(test.workpath('class3', 'Test$2.class'))
309 test.must_exist(test.workpath('class3', 'Test$3.class'))
310 test.must_exist(test.workpath('class3', 'Test$Inner.class'))
311
312 test.up_to_date(arguments = '.')
313
314 test.pass_test()