Move 2.0 changes collected in branches/pending back to trunk for further
[scons.git] / test / Java / RMIC.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('myrmic.py', r"""
36 import os
37 import sys
38 args = sys.argv[1:]
39 while args:
40     a = args[0]
41     if a == '-d':
42         outdir = args[1]
43         args = args[1:]
44     elif a == '-classpath':
45         args = args[1:]
46     elif a == '-sourcepath':
47         args = args[1:]
48     else:
49         break
50     args = args[1:]
51 for file in args:
52     infile = open(file, 'rb')
53     outfile = open(os.path.join(outdir, file[:-5] + '.class'), 'wb')
54     for l in infile.readlines():
55         if l[:8] != '/*rmic*/':
56             outfile.write(l)
57 sys.exit(0)
58 """)
59
60 test.write('SConstruct', """
61 env = Environment(tools = ['rmic'],
62                   RMIC = r'%(_python_)s myrmic.py')
63 env.RMIC(target = 'outdir', source = 'test1.java')
64 """ % locals())
65
66 test.write('test1.java', """\
67 test1.java
68 /*rmic*/
69 line 3
70 """)
71
72 test.run(arguments = '.', stderr = None)
73
74 test.fail_test(test.read(['outdir', 'test1.class']) != "test1.java\nline 3\n")
75
76 if os.path.normcase('.java') == os.path.normcase('.JAVA'):
77
78     test.write('SConstruct', """\
79 env = Environment(tools = ['rmic'],
80                   RMIC = r'%(_python_)s myrmic.py')
81 env.RMIC(target = 'outdir', source = 'test2.JAVA')
82 """ % locals())
83
84     test.write('test2.JAVA', """\
85 test2.JAVA
86 /*rmic*/
87 line 3
88 """)
89
90     test.run(arguments = '.', stderr = None)
91
92     test.fail_test(test.read(['outdir', 'test2.class']) != "test2.JAVA\nline 3\n")
93
94 where_javac, java_version = test.java_where_javac()
95 where_rmic = test.java_where_rmic()
96
97 test.write("wrapper.py", """\
98 import os
99 import sys
100 open('%s', 'ab').write("wrapper.py %%s\\n" %% " ".join(sys.argv[1:]))
101 os.system(" ".join(sys.argv[1:]))
102 """ % test.workpath('wrapper.out').replace('\\', '\\\\'))
103
104 test.write('SConstruct', """
105 foo = Environment(tools = ['javac', 'rmic'],
106                   JAVAC = r'%(where_javac)s',
107                   RMIC = r'%(where_rmic)s')
108 foo.Java(target = 'class1', source = 'com/sub/foo')
109 foo.RMIC(target = 'outdir1',
110           source = ['class1/com/sub/foo/Example1.class',
111                     'class1/com/sub/foo/Example2'],
112           JAVACLASSDIR = 'class1')
113
114 rmic = foo.Dictionary('RMIC')
115 bar = foo.Clone(RMIC = r'%(_python_)s wrapper.py ' + rmic)
116 bar_classes = bar.Java(target = 'class2', source = 'com/sub/bar')
117 # XXX This is kind of a Python brute-force way to do what Ant
118 # does with its "excludes" attribute.  We should probably find
119 # a similar friendlier way to do this.
120 bar_classes = [c for c in bar_classes if str(c).find('Hello') == -1]
121 bar.RMIC(target = Dir('outdir2'), source = bar_classes)
122 """ % locals() )
123
124 test.subdir('com',
125             ['com', 'other'],
126             ['com', 'sub'],
127             ['com', 'sub', 'foo'],
128             ['com', 'sub', 'bar'],
129             'src3a',
130             'src3b')
131
132 test.write(['com', 'sub', 'foo', 'Hello.java'], """\
133 package com.sub.foo;
134
135 import java.rmi.Remote;
136 import java.rmi.RemoteException;
137
138 public interface Hello extends Remote {
139     String sayHello() throws RemoteException;
140 }
141 """)
142
143 test.write(['com', 'sub', 'foo', 'Example1.java'], """\
144 package com.sub.foo;
145
146 import java.rmi.Naming;
147 import java.rmi.RemoteException;
148 import java.rmi.RMISecurityManager;
149 import java.rmi.server.UnicastRemoteObject;
150
151 public class Example1 extends UnicastRemoteObject implements Hello {
152
153     static final long serialVersionUID = 0;
154
155     public Example1() throws RemoteException {
156         super();
157     }
158
159     public String sayHello() {
160         return "Hello World!";
161     }
162
163     public static void main(String args[]) {
164         if (System.getSecurityManager() == null) {
165             System.setSecurityManager(new RMISecurityManager());
166         }
167
168         try {
169             Example1 obj = new Example1();
170
171             Naming.rebind("//myhost/HelloServer", obj);
172
173             System.out.println("HelloServer bound in registry");
174         } catch (Exception e) {
175             System.out.println("Example1 err: " + e.getMessage());
176             e.printStackTrace();
177         }
178     }
179 }
180 """)
181
182 test.write(['com', 'sub', 'foo', 'Example2.java'], """\
183 package com.sub.foo;
184
185 import java.rmi.Naming;
186 import java.rmi.RemoteException;
187 import java.rmi.RMISecurityManager;
188 import java.rmi.server.UnicastRemoteObject;
189
190 public class Example2 extends UnicastRemoteObject implements Hello {
191
192     static final long serialVersionUID = 0;
193
194     public Example2() throws RemoteException {
195         super();
196     }
197
198     public String sayHello() {
199         return "Hello World!";
200     }
201
202     public static void main(String args[]) {
203         if (System.getSecurityManager() == null) {
204             System.setSecurityManager(new RMISecurityManager());
205         }
206
207         try {
208             Example2 obj = new Example2();
209
210             Naming.rebind("//myhost/HelloServer", obj);
211
212             System.out.println("HelloServer bound in registry");
213         } catch (Exception e) {
214             System.out.println("Example2 err: " + e.getMessage());
215             e.printStackTrace();
216         }
217     }
218 }
219 """)
220
221 test.write(['com', 'sub', 'bar', 'Hello.java'], """\
222 package com.sub.bar;
223
224 import java.rmi.Remote;
225 import java.rmi.RemoteException;
226
227 public interface Hello extends Remote {
228     String sayHello() throws RemoteException;
229 }
230 """)
231
232 test.write(['com', 'sub', 'bar', 'Example3.java'], """\
233 package com.sub.bar;
234
235 import java.rmi.Naming;
236 import java.rmi.RemoteException;
237 import java.rmi.RMISecurityManager;
238 import java.rmi.server.UnicastRemoteObject;
239
240 public class Example3 extends UnicastRemoteObject implements Hello {
241
242     static final long serialVersionUID = 0;
243
244     public Example3() throws RemoteException {
245         super();
246     }
247
248     public String sayHello() {
249         return "Hello World!";
250     }
251
252     public static void main(String args[]) {
253         if (System.getSecurityManager() == null) {
254             System.setSecurityManager(new RMISecurityManager());
255         }
256
257         try {
258             Example3 obj = new Example3();
259
260             Naming.rebind("//myhost/HelloServer", obj);
261
262             System.out.println("HelloServer bound in registry");
263         } catch (Exception e) {
264             System.out.println("Example3 err: " + e.getMessage());
265             e.printStackTrace();
266         }
267     }
268 }
269 """)
270
271 test.write(['com', 'sub', 'bar', 'Example4.java'], """\
272 package com.sub.bar;
273
274 import java.rmi.Naming;
275 import java.rmi.RemoteException;
276 import java.rmi.RMISecurityManager;
277 import java.rmi.server.UnicastRemoteObject;
278
279 public class Example4 extends UnicastRemoteObject implements Hello {
280
281     static final long serialVersionUID = 0;
282
283     public Example4() throws RemoteException {
284         super();
285     }
286
287     public String sayHello() {
288         return "Hello World!";
289     }
290
291     public static void main(String args[]) {
292         if (System.getSecurityManager() == null) {
293             System.setSecurityManager(new RMISecurityManager());
294         }
295
296         try {
297             Example4 obj = new Example4();
298
299             Naming.rebind("//myhost/HelloServer", obj);
300
301             System.out.println("HelloServer bound in registry");
302         } catch (Exception e) {
303             System.out.println("Example4 err: " + e.getMessage());
304             e.printStackTrace();
305         }
306     }
307 }
308 """)
309
310 test.run(arguments = '.')
311
312 test.fail_test(test.read('wrapper.out') != "wrapper.py %s -d outdir2 -classpath class2 com.sub.bar.Example3 com.sub.bar.Example4\n" % where_rmic)
313
314 test.must_exist(test.workpath('outdir1', 'com', 'sub', 'foo', 'Example1_Stub.class'))
315 test.must_exist(test.workpath('outdir1', 'com', 'sub', 'foo', 'Example2_Stub.class'))
316 test.must_exist(test.workpath('outdir2', 'com', 'sub', 'bar', 'Example3_Stub.class'))
317 test.must_exist(test.workpath('outdir2', 'com', 'sub', 'bar', 'Example4_Stub.class'))
318
319 # We used to check for _Skel.class files as well, but they're not
320 # generated by default starting with Java 1.5, and they apparently
321 # haven't been needed for a while.  Don't bother looking, even if we're
322 # running Java 1.4.  If we think they're needed but they don't exist
323 # the test.up_to_date() call below will detect it.
324 #test.must_exist(test.workpath('outdir1', 'com', 'sub', 'foo', 'Example1_Skel.class'))
325 #test.must_exist(test.workpath('outdir1', 'com', 'sub', 'foo', 'Example2_Skel.class'))
326 #test.must_exist(test.workpath('outdir2', 'com', 'sub', 'bar', 'Example3_Skel.class'))
327 #test.must_exist(test.workpath('outdir2', 'com', 'sub', 'bar', 'Example4_Skel.class'))
328
329 test.up_to_date(arguments = '.')
330
331 test.pass_test()
332
333 # Local Variables:
334 # tab-width:4
335 # indent-tabs-mode:nil
336 # End:
337 # vim: set expandtab tabstop=4 shiftwidth=4: