Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / Java / JAR.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
29 import TestSCons
30
31 _python_ = TestSCons._python_
32
33 test = TestSCons.TestSCons()
34
35 test.write('myjar.py', r"""
36 import sys
37 args = sys.argv[1:]
38 while args:
39     a = args[0]
40     if a == 'cf':
41         out = args[1]
42         args = args[1:]
43     else:
44         break
45     args = args[1:]
46 outfile = open(out, 'wb')
47 for file in args:
48     infile = open(file, 'rb')
49     for l in infile.readlines():
50         if l[:7] != '/*jar*/':
51             outfile.write(l)
52 sys.exit(0)
53 """)
54
55 test.write('SConstruct', """
56 env = Environment(tools = ['jar'],
57                   JAR = r'%(_python_)s myjar.py')
58 env.Jar(target = 'test1.jar', source = 'test1.class')
59 """ % locals())
60
61 test.write('test1.class', """\
62 test1.class
63 /*jar*/
64 line 3
65 """)
66
67 test.run(arguments = '.', stderr = None)
68
69 test.must_match('test1.jar', "test1.class\nline 3\n")
70
71 if os.path.normcase('.class') == os.path.normcase('.CLASS'):
72
73     test.write('SConstruct', """
74 env = Environment(tools = ['jar'],
75                   JAR = r'%(_python_)s myjar.py')
76 env.Jar(target = 'test2.jar', source = 'test2.CLASS')
77 """ % locals())
78
79     test.write('test2.CLASS', """\
80 test2.CLASS
81 /*jar*/
82 line 3
83 """)
84
85     test.run(arguments = '.', stderr = None)
86
87     test.must_match('test2.jar', "test2.CLASS\nline 3\n")
88
89 test.write('myjar2.py', r"""
90 import sys
91 f=open(sys.argv[2], 'wb')
92 f.write(" ".join(sys.argv[1:]))
93 f.write("\n")
94 f.close()
95 sys.exit(0)
96 """)
97
98 test.write('SConstruct', """
99 env = Environment(tools = ['jar'],
100                   JAR = r'%(_python_)s myjar2.py',
101                   JARFLAGS='cvf')
102 env.Jar(target = 'classes.jar', source = [ 'testdir/bar.class',
103                                            'foo.mf' ],
104         TESTDIR='testdir',
105         JARCHDIR='$TESTDIR')
106 """ % locals())
107
108 test.subdir('testdir')
109 test.write([ 'testdir', 'bar.class' ], 'foo')
110 test.write('foo.mf',
111            """Manifest-Version : 1.0
112            blah
113            blah
114            blah
115            """)
116 test.run(arguments='classes.jar')
117 test.must_match('classes.jar',
118                 'cvfm classes.jar foo.mf -C testdir bar.class\n')
119
120
121
122 where_javac, java_version = test.java_where_javac()
123 where_jar = test.java_where_jar()
124
125
126
127 test.write("wrapper.py", """\
128 import os
129 import sys
130 open('%s', 'ab').write("wrapper.py %%s\\n" %% " ".join(sys.argv[1:]))
131 os.system(" ".join(sys.argv[1:]))
132 """ % test.workpath('wrapper.out').replace('\\', '\\\\'))
133
134 test.write('SConstruct', """
135 foo = Environment(tools = ['javac', 'jar'],
136                   JAVAC = r'%(where_javac)s',
137                   JAR = r'%(where_jar)s')
138 jar = foo.Dictionary('JAR')
139 bar = foo.Clone(JAR = r'%(_python_)s wrapper.py ' + jar)
140 foo.Java(target = 'classes', source = 'com/sub/foo')
141 bar.Java(target = 'classes', source = 'com/sub/bar')
142 foo.Jar(target = 'foo', source = 'classes/com/sub/foo')
143 bar.Jar(target = 'bar', source = 'classes/com/sub/bar')
144 """ % locals())
145
146 test.subdir('com',
147             ['com', 'sub'],
148             ['com', 'sub', 'foo'],
149             ['com', 'sub', 'bar'])
150
151 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
152 package com.sub.foo;
153
154 public class Example1
155 {
156
157      public static void main(String[] args)
158      {
159
160      }
161
162 }
163 """)
164
165 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
166 package com.sub.foo;
167
168 public class Example2
169 {
170
171      public static void main(String[] args)
172      {
173
174      }
175
176 }
177 """)
178
179 test.write(['com', 'sub', 'foo', 'Example3.java'], """\
180 package com.sub.foo;
181
182 public class Example3
183 {
184
185      public static void main(String[] args)
186      {
187
188      }
189
190 }
191 """)
192
193 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
194 package com.sub.bar;
195
196 public class Example4
197 {
198
199      public static void main(String[] args)
200      {
201
202      }
203
204 }
205 """)
206
207 test.write(['com', 'sub', 'bar', 'Example5.java'], """\
208 package com.sub.bar;
209
210 public class Example5
211 {
212
213      public static void main(String[] args)
214      {
215
216      }
217
218 }
219 """)
220
221 test.write(['com', 'sub', 'bar', 'Example6.java'], """\
222 package com.sub.bar;
223
224 public class Example6
225 {
226
227      public static void main(String[] args)
228      {
229
230      }
231
232 }
233 """)
234
235 test.run(arguments = '.')
236
237 expected_wrapper_out = "wrapper.py %(where_jar)s cf bar.jar classes/com/sub/bar\n"
238 expected_wrapper_out = expected_wrapper_out.replace('/', os.sep)
239 test.must_match('wrapper.out',
240                 expected_wrapper_out % locals())
241
242 test.must_exist('foo.jar')
243 test.must_exist('bar.jar')
244
245 test.up_to_date(arguments = '.')
246
247 test.pass_test()
248
249 # Local Variables:
250 # tab-width:4
251 # indent-tabs-mode:nil
252 # End:
253 # vim: set expandtab tabstop=4 shiftwidth=4: