Add a skip_test() method to the infrastructure and use it for test scripts that skip...
[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
95 ENV = test.java_ENV()
96
97 if test.detect_tool('javac', ENV=ENV):
98     where_javac = test.detect('JAVAC', 'javac', ENV=ENV)
99 else:
100     where_javac = test.where_is('javac')
101 if not where_javac:
102     test.skip_test("Could not find Java javac, skipping test(s).\n")
103
104
105
106 test.write("wrapper.py", """\
107 import os
108 import string
109 import sys
110 open('%s', 'ab').write("wrapper.py %%s\\n" %% string.join(sys.argv[1:]))
111 os.system(string.join(sys.argv[1:], " "))
112 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
113
114 test.write('SConstruct', """
115 foo = Environment(tools = ['javac'],
116                   JAVAC = '%s')
117 javac = foo.Dictionary('JAVAC')
118 bar = foo.Copy(JAVAC = r'%s wrapper.py ' + javac)
119 foo.Java(target = 'class1', source = 'com/sub/foo')
120 bar.Java(target = 'class2', source = 'com/sub/bar')
121 foo.Java(target = 'class3', source = ['src1', 'src2'])
122 """ % (where_javac, python))
123
124 test.subdir('com',
125             ['com', 'sub'],
126             ['com', 'sub', 'foo'],
127             ['com', 'sub', 'bar'],
128             'src1',
129             'src2')
130
131 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
132 package com.sub.foo;
133
134 public class Example1
135 {
136
137      public static void main(String[] args)
138      {
139
140      }
141
142 }
143 """)
144
145 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
146 package com.other;
147
148 public class Example2
149 {
150
151      public static void main(String[] args)
152      {
153
154      }
155
156 }
157 """)
158
159 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
160 package com.sub.foo;
161
162 public class Example3
163 {
164
165      public static void main(String[] args)
166      {
167
168      }
169
170 }
171 """)
172
173 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
174 package com.sub.bar;
175
176 public class Example4
177 {
178
179      public static void main(String[] args)
180      {
181
182      }
183
184 }
185 """)
186
187 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
188 package com.other;
189
190 public class Example5
191 {
192
193      public static void main(String[] args)
194      {
195
196      }
197
198 }
199 """)
200
201 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
202 package com.sub.bar;
203
204 public class Example6
205 {
206
207      public static void main(String[] args)
208      {
209
210      }
211
212 }
213 """)
214
215 test.write(['src1', 'Example7.java'], """\
216 public class Example7
217 {
218
219      public static void main(String[] args)
220      {
221
222      }
223
224 }
225 """)
226
227 # Acid-test file for parsing inner Java classes, courtesy Chad Austin.
228 test.write(['src2', 'Test.java'], """\
229 class Empty {
230 }
231
232 interface Listener {
233   public void execute();
234 }
235
236 public
237 class
238 Test {
239   class Inner {
240     void go() {
241       use(new Listener() {
242         public void execute() {
243           System.out.println("In Inner");
244         }
245       });
246     }
247     String s1 = "class A";
248     String s2 = "new Listener() { }";
249     /* class B */
250     /* new Listener() { } */
251   }
252
253   public static void main(String[] args) {
254     new Test().run();
255   }
256
257   void run() {
258     use(new Listener() {
259       public void execute() {
260         use(new Listener( ) {
261           public void execute() {
262             System.out.println("Inside execute()");
263           }
264         });
265       }
266     });
267
268     new Inner().go();
269   }
270
271   void use(Listener l) {
272     l.execute();
273   }
274 }
275
276 class Private {
277   void run() {
278     new Listener() {
279       public void execute() {
280       }
281     };
282   }
283 }
284 """)
285
286 test.run(arguments = '.')
287
288 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)
289
290 test.must_exist(test.workpath('class1', 'com', 'sub', 'foo', 'Example1.class'))
291 test.must_exist(test.workpath('class1', 'com', 'other', 'Example2.class'))
292 test.must_exist(test.workpath('class1', 'com', 'sub', 'foo', 'Example3.class'))
293
294 test.must_exist(test.workpath('class2', 'com', 'sub', 'bar', 'Example4.class'))
295 test.must_exist(test.workpath('class2', 'com', 'other', 'Example5.class'))
296 test.must_exist(test.workpath('class2', 'com', 'sub', 'bar', 'Example6.class'))
297
298 test.must_exist(test.workpath('class3', 'Example7.class'))
299
300 test.must_exist(test.workpath('class3', 'Empty.class'))
301 test.must_exist(test.workpath('class3', 'Listener.class'))
302 test.must_exist(test.workpath('class3', 'Private.class'))
303 test.must_exist(test.workpath('class3', 'Private$1.class'))
304 test.must_exist(test.workpath('class3', 'Test.class'))
305 test.must_exist(test.workpath('class3', 'Test$1.class'))
306 test.must_exist(test.workpath('class3', 'Test$2.class'))
307 test.must_exist(test.workpath('class3', 'Test$3.class'))
308 test.must_exist(test.workpath('class3', 'Test$Inner.class'))
309
310 test.up_to_date(arguments = '.')
311
312 test.pass_test()