added release.txt blurb. Fixed spelling typo in Defaults.xml
[scons.git] / test / Java / JAR.py
blob2d67717fe39d04b4252adbde421ab4a1ac271106
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 """
27 Test Jar builder.
29 These tests require a findable/working Java subsystem.
30 """
32 import os
33 import TestSCons
35 _python_ = TestSCons._python_
37 test = TestSCons.TestSCons()
39 where_javac, java_version = test.java_where_javac()
40 where_jar = test.java_where_jar()
42 test.file_fixture('wrapper_with_args.py')
44 test.write('SConstruct', """\
45 DefaultEnvironment(tools=[])
46 foo = Environment(tools=['javac', 'jar'])
47 # jar = foo.Dictionary('JAR')
48 bar = foo.Clone(JAR=r'%(_python_)s wrapper_with_args.py jar')
49 foo.Java(target='classes', source='com/sub/foo')
50 bar.Java(target='classes', source='com/sub/bar')
51 foo.Jar(target='foo', source='classes/com/sub/foo')
52 bar.Jar(target='bar', source=Dir('classes/com/sub/bar'))
53 """ % locals())
55 test.subdir('com', ['com', 'sub'], ['com', 'sub', 'foo'], ['com', 'sub', 'bar'])
57 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
58 package com.sub.foo;
60 public class Example1
63 public static void main(String[] args)
69 """)
71 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
72 package com.sub.foo;
74 public class Example2
77 public static void main(String[] args)
83 """)
85 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
86 package com.sub.foo;
88 public class Example3
91 public static void main(String[] args)
97 """)
99 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
100 package com.sub.bar;
102 public class Example4
105 public static void main(String[] args)
111 """)
113 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
114 package com.sub.bar;
116 public class Example5
119 public static void main(String[] args)
125 """)
127 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
128 package com.sub.bar;
130 public class Example6
133 public static void main(String[] args)
139 """)
141 test.run(arguments = '.')
143 expected_wrapper_out = "wrapper_with_args.py jar cf bar.jar classes/com/sub/bar\n"
144 expected_wrapper_out = expected_wrapper_out.replace('/', os.sep)
145 test.must_match('wrapper.out', expected_wrapper_out % locals(), mode='r')
147 test.must_exist('foo.jar')
148 test.must_exist('bar.jar')
150 test.up_to_date(arguments='.')
152 #######
153 # test java source files as source to Jar builder
155 # make some directories to test in
156 test.subdir('testdir2',
157 ['testdir2', 'com'],
158 ['testdir2', 'com', 'javasource'])
160 # simple SConstruct which passes the 3 .java as source
161 # and extracts the jars back to classes
162 test.write(['testdir2', 'SConstruct'], """
163 DefaultEnvironment(tools=[])
165 foo = Environment()
166 foo.Jar(
167 target='foobar',
168 source=[
169 'com/javasource/JavaFile1.java',
170 'com/javasource/JavaFile2.java',
171 'com/javasource/JavaFile3.java',
174 foo.Jar(
175 target=['foo', 'bar'],
176 source=[
177 'com/javasource/JavaFile1.java',
178 'com/javasource/JavaFile2.java',
179 'com/javasource/JavaFile3.java',
182 foo.Command("foobarTest", [], Mkdir("foobarTest"))
183 foo.Command(
184 'foobarTest/com/javasource/JavaFile3.java',
185 'foobar.jar',
186 foo['JAR'] + ' xvf ../foobar.jar',
187 chdir='foobarTest',
189 foo.Command("fooTest", [], Mkdir("fooTest"))
190 foo.Command(
191 'fooTest/com/javasource/JavaFile3.java',
192 'foo.jar',
193 foo['JAR'] + ' xvf ../foo.jar',
194 chdir='fooTest',
196 foo.Command("barTest", [], Mkdir("barTest"))
197 foo.Command(
198 'barTest/com/javasource/JavaFile3.java',
199 'bar.jar',
200 foo['JAR'] + ' xvf ../bar.jar',
201 chdir='barTest',
203 """)
205 test.write(['testdir2', 'com', 'javasource', 'JavaFile1.java'], """\
206 package com.javasource;
208 public class JavaFile1
210 public static void main(String[] args)
215 """)
217 test.write(['testdir2', 'com', 'javasource', 'JavaFile2.java'], """\
218 package com.javasource;
220 public class JavaFile2
222 public static void main(String[] args)
227 """)
229 test.write(['testdir2', 'com', 'javasource', 'JavaFile3.java'], """\
230 package com.javasource;
232 public class JavaFile3
234 public static void main(String[] args)
239 """)
242 # check the output and make sure the java files got converted to classes
243 # use regex . for dirsep so this will work on both windows and other platforms.
244 expect = ".*jar cf foo.jar -C com.javasource.JavaFile1 com.javasource.JavaFile1.class -C com.javasource.JavaFile2 com.javasource.JavaFile2.class -C com.javasource.JavaFile3 com.javasource.JavaFile3.class.*"
246 test.run(chdir='testdir2', match=TestSCons.match_re_dotall, stdout=expect)
248 #test single target jar
249 test.must_exist(['testdir2','foobar.jar'])
250 test.must_exist(['testdir2', 'foobarTest', 'com', 'javasource', 'JavaFile1.class'])
251 test.must_exist(['testdir2', 'foobarTest', 'com', 'javasource', 'JavaFile2.class'])
252 test.must_exist(['testdir2', 'foobarTest', 'com', 'javasource', 'JavaFile3.class'])
254 # make sure there are class in the jar
255 test.must_exist(['testdir2','foo.jar'])
256 test.must_exist(['testdir2', 'fooTest', 'com', 'javasource', 'JavaFile1.class'])
257 test.must_exist(['testdir2', 'fooTest', 'com', 'javasource', 'JavaFile2.class'])
258 test.must_exist(['testdir2', 'fooTest', 'com', 'javasource', 'JavaFile3.class'])
260 # make sure both jars got createds
261 test.must_exist(['testdir2','bar.jar'])
262 test.must_exist(['testdir2', 'barTest', 'com', 'javasource', 'JavaFile1.class'])
263 test.must_exist(['testdir2', 'barTest', 'com', 'javasource', 'JavaFile2.class'])
264 test.must_exist(['testdir2', 'barTest', 'com', 'javasource', 'JavaFile3.class'])
267 #######
268 # test list of lists
270 # make some directories to test in
271 test.subdir(
272 'listOfLists',
273 ['manifest_dir'],
274 ['listOfLists', 'src'],
275 ['listOfLists', 'src', 'com'],
276 ['listOfLists', 'src', 'com', 'javasource'],
277 ['listOfLists', 'src', 'com', 'resource'],
280 # test varient dir and lists of lists
281 test.write(['listOfLists', 'SConstruct'], """
282 DefaultEnvironment(tools=[])
284 foo = Environment()
285 foo.VariantDir('build', 'src', duplicate=0)
286 foo.VariantDir('test', '../manifest_dir', duplicate=0)
287 sourceFiles = [
288 "src/com/javasource/JavaFile1.java",
289 "src/com/javasource/JavaFile2.java",
290 "src/com/javasource/JavaFile3.java",
292 list_of_class_files = foo.Java('build', source=sourceFiles)
293 resources = ['build/com/resource/resource1.txt', 'build/com/resource/resource2.txt']
294 for resource in resources:
295 foo.Command(
296 resource, list_of_class_files, Copy(resource, resource.replace('build', 'src'))
298 contents = [list_of_class_files, resources]
299 foo.Jar(target='lists', source=contents + ['test/MANIFEST.mf'], JARCHDIR='build')
300 foo.Command("listsTest", [], Mkdir("listsTest"))
301 foo.Command(
302 'listsTest/src/com/javasource/JavaFile3.java',
303 'lists.jar',
304 foo['JAR'] + ' xvf ../lists.jar',
305 chdir='listsTest',
307 """)
309 test.write(['listOfLists', 'src', 'com', 'javasource', 'JavaFile1.java'], """\
310 package com.javasource;
312 public class JavaFile1
314 public static void main(String[] args)
319 """)
321 test.write(['listOfLists', 'src', 'com', 'javasource', 'JavaFile2.java'], """\
322 package com.javasource;
324 public class JavaFile2
326 public static void main(String[] args)
331 """)
333 test.write(['listOfLists', 'src', 'com', 'javasource', 'JavaFile3.java'], """\
334 package com.javasource;
336 public class JavaFile3
338 public static void main(String[] args)
343 """)
345 test.write(['manifest_dir','MANIFEST.mf'],
346 """Manifest-Version: 1.0
347 MyManifestTest: Test
348 """)
350 test.write(['listOfLists', 'src', 'com', 'resource', 'resource1.txt'], """\
351 this is a resource file
352 """)
354 test.write(['listOfLists', 'src', 'com', 'resource', 'resource2.txt'], """\
355 this is another resource file
356 """)
359 test.run(chdir='listOfLists')
361 #test single target jar
362 test.must_exist(['listOfLists','lists.jar'])
364 # make sure there are class in the jar
365 test.must_exist(['listOfLists', 'listsTest', 'com', 'javasource', 'JavaFile1.class'])
366 test.must_exist(['listOfLists', 'listsTest', 'com', 'javasource', 'JavaFile2.class'])
367 test.must_exist(['listOfLists', 'listsTest', 'com', 'javasource', 'JavaFile3.class'])
368 test.must_exist(['listOfLists', 'listsTest', 'com', 'resource', 'resource1.txt'])
369 test.must_exist(['listOfLists', 'listsTest', 'com', 'resource', 'resource2.txt'])
370 test.must_exist(['listOfLists', 'listsTest', 'META-INF', 'MANIFEST.MF'])
371 test.must_contain(['listOfLists', 'listsTest', 'META-INF', 'MANIFEST.MF'], b"MyManifestTest: Test" )
373 #######
374 # test different style of passing in dirs
376 # make some directories to test in
377 test.subdir(
378 'testdir3',
379 ['testdir3', 'com'],
380 ['testdir3', 'com', 'sub'],
381 ['testdir3', 'com', 'sub', 'foo'],
382 ['testdir3', 'com', 'sub', 'bar'],
385 # Create the jars then extract them back to check contents
386 test.write(['testdir3', 'SConstruct'], """
387 DefaultEnvironment(tools=[])
389 foo = Environment()
390 foo_cls = foo.Java(target='classes', source='com/sub/foo')
391 foo_res = 'classes/com/sub/foo/NonJava.txt'
392 foo_res_src = 'com/sub/foo/NonJava.txt'
393 foo.Command(foo_res, foo_cls, Copy(foo_res, foo_res_src))
394 foo.Jar(target='foo', source='classes/com/sub/foo', JARCHDIR='classes')
395 foo.Command("fooTest", 'foo.jar', Mkdir("fooTest"))
396 foo.Command('doesnt_exist1', "fooTest", foo['JAR'] + ' xvf ../foo.jar', chdir='fooTest')
398 bar = foo.Clone()
399 bar_cls = bar.Java(target='classes', source='com/sub/bar')
400 bar_res = 'classes/com/sub/bar/NonJava.txt'
401 bar_res_src = 'com/sub/bar/NonJava.txt'
402 bar.Command(bar_res, bar_cls, Copy(bar_res, bar_res_src))
403 bar.Jar(target='bar', source=Dir('classes/com/sub/bar'), JARCHDIR='classes')
404 bar.Command("barTest", 'bar.jar', Mkdir("barTest"))
405 bar.Command('doesnt_exist2', 'barTest', bar['JAR'] + ' xvf ../bar.jar', chdir='barTest')
406 """)
408 test.write(['testdir3', 'com', 'sub', 'foo', 'Example1.java'], """\
409 package com.sub.foo;
411 public class Example1
414 public static void main(String[] args)
420 """)
422 test.write(['testdir3', 'com', 'sub', 'foo', 'Example2.java'], """\
423 package com.sub.foo;
425 public class Example2
428 public static void main(String[] args)
434 """)
436 test.write(['testdir3', 'com', 'sub', 'foo', 'Example3.java'], """\
437 package com.sub.foo;
439 public class Example3
442 public static void main(String[] args)
448 """)
450 test.write(['testdir3', 'com', 'sub', 'foo', 'NonJava.txt'], """\
451 testfile
452 """)
454 test.write(['testdir3', 'com', 'sub', 'bar', 'Example4.java'], """\
455 package com.sub.bar;
457 public class Example4
460 public static void main(String[] args)
466 """)
468 test.write(['testdir3', 'com', 'sub', 'bar', 'Example5.java'], """\
469 package com.sub.bar;
471 public class Example5
474 public static void main(String[] args)
480 """)
482 test.write(['testdir3', 'com', 'sub', 'bar', 'Example6.java'], """\
483 package com.sub.bar;
485 public class Example6
488 public static void main(String[] args)
494 """)
496 test.write(['testdir3', 'com', 'sub', 'bar', 'NonJava.txt'], """\
497 testfile
498 """)
500 test.run(chdir='testdir3')
502 # check the output and make sure the java files got converted to classes
504 # make sure there are class in the jar
505 test.must_exist(['testdir3','foo.jar'])
506 test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'Example1.class'])
507 test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'Example2.class'])
508 test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'Example3.class'])
509 # TODO: determine expected behavior with resource files, should they be
510 # automatically copied in or specified in seperate commands
511 test.must_exist(['testdir3', 'fooTest', 'com', 'sub', 'foo', 'NonJava.txt'])
513 # make sure both jars got createds
514 test.must_exist(['testdir3','bar.jar'])
515 test.must_exist(['testdir3', 'barTest', 'com', 'sub', 'bar', 'Example4.class'])
516 test.must_exist(['testdir3', 'barTest', 'com', 'sub', 'bar', 'Example5.class'])
517 test.must_exist(['testdir3', 'barTest', 'com', 'sub', 'bar', 'Example6.class'])
518 # TODO: determine expected behavior with resource files, should they be
519 # automatically copied in or specified in seperate commands
520 test.must_exist(['testdir3', 'barTest', 'com', 'sub', 'bar', 'NonJava.txt'])
522 test.pass_test()
524 # Local Variables:
525 # tab-width:4
526 # indent-tabs-mode:nil
527 # End:
528 # vim: set expandtab tabstop=4 shiftwidth=4: