Fix the Java parser's handling of backslashes. (Leanid Nazdrynau)
[scons.git] / src / engine / SCons / Tool / JavaCommonTests.py
1 #
2 # __COPYRIGHT__
3 #
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:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
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.
22 #
23
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
25
26 import os.path
27 import sys
28 import unittest
29
30 import SCons.Tool.JavaCommon
31
32 class parse_javaTestCase(unittest.TestCase):
33
34     def test_bare_bones(self):
35         """Test a bare-bones class"""
36
37         pkg_dir, classes = SCons.Tool.JavaCommon.parse_java("""\
38 package com.sub.bar;
39
40 public class Foo
41 {
42
43      public static void main(String[] args)
44      {
45
46         /* This tests a former bug where strings would eat later code. */
47         String hello1 = new String("Hello, world!");
48
49      }
50
51 }
52 """)
53         assert pkg_dir == os.path.join('com', 'sub', 'bar'), pkg_dir
54         assert classes == ['Foo'], classes
55
56     def test_inner_classes(self):
57         """Test parsing various forms of inner classes"""
58
59         pkg_dir, classes = SCons.Tool.JavaCommon.parse_java("""\
60 class Empty {
61 }
62
63 interface Listener {
64   public void execute();
65 }
66
67 public
68 class
69 Test {
70   class Inner {
71     void go() {
72       use(new Listener() {
73         public void execute() {
74           System.out.println("In Inner");
75         }
76       });
77     }
78     String s1 = "class A";
79     String s2 = "new Listener() { }";
80     /* class B */
81     /* new Listener() { } */
82   }
83
84   public static void main(String[] args) {
85     new Test().run();
86   }
87
88   void run() {
89     use(new Listener() {
90       public void execute() {
91         use(new Listener( ) {
92           public void execute() {
93             System.out.println("Inside execute()");
94           }
95         });
96       }
97     });
98
99     new Inner().go();
100   }
101
102   void use(Listener l) {
103     l.execute();
104   }
105 }
106
107 class Private {
108   void run() {
109     new Listener() {
110       public void execute() {
111       }
112     };
113   }
114 }
115 """)
116
117         assert pkg_dir is None, pkg_dir
118         expect = [
119                    'Empty',
120                    'Listener',
121                    'Test$1',
122                    'Test$Inner',
123                    'Test$2',
124                    'Test$3',
125                    'Test',
126                    'Private$1',
127                    'Private',
128                  ]
129         assert classes == expect, classes
130
131     def test_comments(self):
132         """Test a class with comments"""
133
134         pkg_dir, classes = SCons.Tool.JavaCommon.parse_java("""\
135 package com.sub.foo;
136
137 import java.rmi.Naming;
138 import java.rmi.RemoteException;
139 import java.rmi.RMISecurityManager;
140 import java.rmi.server.UnicastRemoteObject;
141
142 public class Example1 extends UnicastRemoteObject implements Hello {
143
144     public Example1() throws RemoteException {
145         super();
146     }
147
148     public String sayHello() {
149         return "Hello World!";
150     }
151
152     public static void main(String args[]) {
153         if (System.getSecurityManager() == null) {
154             System.setSecurityManager(new RMISecurityManager());
155         }
156         // a comment
157         try {
158             Example1 obj = new Example1();
159
160             Naming.rebind("//myhost/HelloServer", obj);
161
162             System.out.println("HelloServer bound in registry");
163         } catch (Exception e) {
164             System.out.println("Example1 err: " + e.getMessage());
165             e.printStackTrace();
166         }
167     }
168 }
169 """)
170
171         assert pkg_dir == os.path.join('com', 'sub', 'foo'), pkg_dir
172         assert classes == ['Example1'], classes
173
174     def test_arrays(self):
175         """Test arrays of class instances"""
176
177         pkg_dir, classes = SCons.Tool.JavaCommon.parse_java("""\
178 public class Test {
179     MyClass abc = new MyClass();
180     MyClass xyz = new MyClass();
181     MyClass _array[] = new MyClass[] {
182         abc,
183         xyz
184     }
185 }
186 """)
187         assert pkg_dir == None, pkg_dir
188         assert classes == ['Test'], classes
189
190     def test_backslash(self):
191         """Test backslash handling"""
192
193         pkg_dir, classes = SCons.Tool.JavaCommon.parse_java("""\
194 public class MyTabs
195 {
196         private class MyInternal
197         {
198         }
199         private final static String PATH = "images\\\\";
200 }
201 """)
202         assert pkg_dir == None, pkg_dir
203         assert classes == ['MyTabs$MyInternal', 'MyTabs'], classes
204
205 if __name__ == "__main__":
206     suite = unittest.TestSuite()
207     tclasses = [ parse_javaTestCase ]
208     for tclass in tclasses:
209         names = unittest.getTestCaseNames(tclass, 'test_')
210         suite.addTests(map(tclass, names))
211     if not unittest.TextTestRunner().run(suite).wasSuccessful():
212         sys.exit(1)