4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
30 import SCons.Tool.JavaCommon
33 # Adding trace=trace to any of the parse_jave() calls below will cause
34 # the parser to spit out trace messages of the tokens it sees and the
35 # attendant transitions.
37 def trace(token, newstate):
38 from SCons.Debug import Trace
39 statename = newstate.__class__.__name__
40 Trace('token = %s, state = %s\n' % (repr(token), statename))
42 class parse_javaTestCase(unittest.TestCase):
44 def test_bare_bones(self):
45 """Test a bare-bones class"""
53 public static void main(String[] args)
56 /* This tests a former bug where strings would eat later code. */
57 String hello1 = new String("Hello, world!");
63 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
64 assert pkg_dir == os.path.join('com', 'sub', 'bar'), pkg_dir
65 assert classes == ['Foo'], classes
68 def test_inner_classes(self):
69 """Test parsing various forms of inner classes"""
76 public void execute();
81 Test implements Listener {
85 public void execute() {
86 System.out.println("In Inner");
90 String s1 = "class A";
91 String s2 = "new Listener() { }";
93 /* new Listener() { } */
97 Inner2() { Listener l = new Listener(); }
100 /* Make sure this class doesn't get interpreted as an inner class of the previous one, when "new" is used in the previous class. */
105 public static void main(String[] args) {
111 public void execute() {
112 use(new Listener( ) {
113 public void execute() {
114 System.out.println("Inside execute()");
123 void use(Listener l) {
131 public void execute() {
138 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input, '1.4')
139 assert pkg_dir is None, pkg_dir
153 assert classes == expect, classes
155 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input, '1.5')
156 assert pkg_dir is None, pkg_dir
170 assert classes == expect, (expect, classes)
174 def test_comments(self):
175 """Test a class with comments"""
180 import java.rmi.Naming;
181 import java.rmi.RemoteException;
182 import java.rmi.RMISecurityManager;
183 import java.rmi.server.UnicastRemoteObject;
185 public class Example1 extends UnicastRemoteObject implements Hello {
187 public Example1() throws RemoteException {
191 public String sayHello() {
192 return "Hello World!";
195 public static void main(String args[]) {
196 if (System.getSecurityManager() == null) {
197 System.setSecurityManager(new RMISecurityManager());
201 Example1 obj = new Example1();
203 Naming.rebind("//myhost/HelloServer", obj);
205 System.out.println("HelloServer bound in registry");
206 } catch (Exception e) {
207 System.out.println("Example1 err: " + e.getMessage());
214 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
215 assert pkg_dir == os.path.join('com', 'sub', 'foo'), pkg_dir
216 assert classes == ['Example1'], classes
219 def test_arrays(self):
220 """Test arrays of class instances"""
224 MyClass abc = new MyClass();
225 MyClass xyz = new MyClass();
226 MyClass _array[] = new MyClass[] {
233 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
234 assert pkg_dir == None, pkg_dir
235 assert classes == ['Test'], classes
239 def test_backslash(self):
240 """Test backslash handling"""
245 private class MyInternal
248 private final static String PATH = "images\\\\";
252 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
253 assert pkg_dir == None, pkg_dir
254 assert classes == ['MyTabs$MyInternal', 'MyTabs'], classes
258 """Test the Java 1.5 enum keyword"""
265 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
266 assert pkg_dir == 'p', pkg_dir
267 assert classes == ['a'], classes
270 def test_anon_classes(self):
271 """Test anonymous classes"""
274 public abstract class TestClass
276 public void completed()
289 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
290 assert pkg_dir == None, pkg_dir
291 assert classes == ['TestClass$1', 'TestClass$2', 'TestClass'], classes
294 def test_closing_bracket(self):
295 """Test finding a closing bracket instead of an anonymous class"""
299 public static void main(String[] args) {
300 Foo[] fooArray = new Foo[] { new Foo() };
307 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
308 assert pkg_dir == None, pkg_dir
309 assert classes == ['TestSCons', 'Foo'], classes
312 def test_dot_class_attributes(self):
313 """Test handling ".class" attributes"""
316 public class Test extends Object
319 Class c = Object[].class;
320 Object[] s = new Object[] {};
325 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
326 assert classes == ['Test'], classes
331 public void F(Object[] o) {
332 F(new Object[] {Object[].class});
334 public void G(Object[] o) {
341 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input)
342 assert pkg_dir == None, pkg_dir
343 assert classes == ['A$B', 'A'], classes
345 def test_anonymous_classes_with_parentheses(self):
346 """Test finding anonymous classes marked by parentheses"""
352 public static void main(String[] args) {
355 public String toString() {
360 public String toString() {
368 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input, '1.4')
369 assert classes == ['Foo$1', 'Foo$2', 'Foo'], classes
371 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input, '1.5')
372 assert classes == ['Foo$1', 'Foo$1$1', 'Foo'], classes
376 def test_nested_anonymous_inner_classes(self):
377 """Test finding nested anonymous inner classes"""
380 // import java.util.*;
382 public class NestedExample
384 public NestedExample()
386 Thread t = new Thread() {
389 Thread t = new Thread() {
392 try {Thread.sleep(200);}
393 catch (Exception e) {}
398 try {Thread.sleep(200);}
399 catch (Exception e) {}
406 public static void main(String argv[])
408 NestedExample e = new NestedExample();
413 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input, '1.4')
414 expect = [ 'NestedExample$1', 'NestedExample$2', 'NestedExample' ]
415 assert expect == classes, (expect, classes)
417 pkg_dir, classes = SCons.Tool.JavaCommon.parse_java(input, '1.5')
418 expect = [ 'NestedExample$1', 'NestedExample$1$1', 'NestedExample' ]
419 assert expect == classes, (expect, classes)
423 if __name__ == "__main__":
424 suite = unittest.TestSuite()
425 tclasses = [ parse_javaTestCase ]
426 for tclass in tclasses:
427 names = unittest.getTestCaseNames(tclass, 'test_')
428 suite.addTests(map(tclass, names))
429 if not unittest.TextTestRunner().run(suite).wasSuccessful():