KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > groovy > inspect > InspectorTest


1 package groovy.inspect;
2
3 import junit.framework.TestCase;
4
5 import java.io.Serializable JavaDoc;
6 import java.util.regex.Matcher JavaDoc;
7 import java.util.regex.Pattern JavaDoc;
8 import java.util.Set JavaDoc;
9 import java.util.HashSet JavaDoc;
10 import java.util.Arrays JavaDoc;
11 import java.util.Comparator JavaDoc;
12
13 public class InspectorTest extends TestCase implements Serializable JavaDoc {
14     public String JavaDoc someField = "only for testing";
15     public static final String JavaDoc SOME_CONST = "only for testing";
16
17     public InspectorTest(String JavaDoc name) {
18         super(name);
19     }
20
21     public void testCtor() {
22         new Inspector(new Object JavaDoc());
23         try {
24             new Inspector(null);
25             fail("should have thown IllegalArgumentException");
26         } catch (Exception JavaDoc expected) {
27         }
28     }
29
30     public void testClassProps() {
31         Inspector insp = new Inspector(this);
32         String JavaDoc[] classProps = insp.getClassProps();
33         assertEquals("package groovy.inspect",classProps[Inspector.CLASS_PACKAGE_IDX]);
34         assertEquals("public class InspectorTest",classProps[Inspector.CLASS_CLASS_IDX]);
35         assertEquals("implements Serializable ",classProps[Inspector.CLASS_INTERFACE_IDX]);
36         assertEquals("extends TestCase",classProps[Inspector.CLASS_SUPERCLASS_IDX]);
37         assertEquals("is Primitive: false, is Array: false, is Groovy: false",classProps[Inspector.CLASS_OTHER_IDX]);
38     }
39     public void testMethods() {
40         Inspector insp = new Inspector(new Object JavaDoc());
41         Object JavaDoc[] methods = insp.getMethods();
42         assertEquals(10, methods.length);
43         String JavaDoc[] names = {"hashCode","getClass","wait","wait","wait","equals","notify","notifyAll","toString","java.lang.Object"};
44         assertNameEquals(names, methods);
45         String JavaDoc[] details = {"JAVA","public final","Object","void","wait","long, int","InterruptedException"};
46         assertContains(methods, details);
47         // ctors are not considered static !
48
String JavaDoc[] ctorDetails = {"JAVA","public","Object","Object","java.lang.Object","",""};
49         assertContains(methods, ctorDetails);
50     }
51
52     public void testStaticMethods() {
53         Inspector insp = new Inspector(this);
54         Object JavaDoc[] methods = insp.getMethods();
55         for (int i = 0; i < methods.length; i++) {
56             String JavaDoc[] strings = (String JavaDoc[]) methods[i];
57             if(strings[1].indexOf("static") > -1) return; // ok, found one static method
58
}
59         fail("there should have been at least one static method in this TestCase, e.g. 'fail'.");
60     }
61     public void testMetaMethods() {
62         Inspector insp = new Inspector(new Object JavaDoc());
63         Object JavaDoc[] metaMethods = insp.getMetaMethods();
64         assertEquals(28, metaMethods.length);
65         String JavaDoc[] names = { "println", "println", "println", "find", "print", "print", "each", "invokeMethod",
66 "inspect", "isCase", "identity", "getAt", "putAt", "dump", "eachPropertyName", "eachProperty", "allProperties",
67 "use", "use", "printf", "eachWithIndex", "every", "any", "grep", "collect", "collect", "findAll", "findIndexOf"};
68         assertNameEquals(names, metaMethods);
69         String JavaDoc[] details = {"GROOVY","public","Object","void","println","Object","n/a"};
70         assertContains(metaMethods, details);
71     }
72
73     public void testStaticMetaMethods() {
74         Matcher JavaDoc matcher = Pattern.compile("").matcher("");
75         Inspector insp = new Inspector(matcher);
76         Object JavaDoc[] metaMethods = insp.getMetaMethods();
77
78         // todo: this currently fails under JDK 1.5 for whatever reason...
79
if (! System.getProperty("java.version").startsWith("1.5")){
80             assertUnique(Inspector.sort(metaMethods));
81         }
82         String JavaDoc[] details = {"GROOVY","public static","Matcher","Matcher","getLastMatcher","","n/a"};
83         assertContains(metaMethods, details);
84     }
85
86     public void testFields() {
87         Inspector insp = new Inspector(this);
88         Object JavaDoc[] fields = insp.getPublicFields();
89         assertEquals(2, fields.length);
90         String JavaDoc[] names = { "someField","SOME_CONST" };
91         assertNameEquals(names, fields);
92         String JavaDoc[] details = {"JAVA","public","InspectorTest","String","someField","only for testing"};
93         assertContains(fields, details);
94     }
95     public void testProperties() {
96         Inspector insp = new Inspector(this);
97         Object JavaDoc[] fields = insp.getProperties();
98         assertEquals(4, fields.length);
99         String JavaDoc[] names = { "SOME_CONST","someField","class","name"};
100         assertNameEquals(names, fields);
101         String JavaDoc[] details = {"GROOVY","public","n/a","String","name","testProperties"};
102         assertContains(fields, details);
103     }
104
105     private void assertNameEquals(String JavaDoc[] names, Object JavaDoc[] metaMethods) {
106         Set JavaDoc metaSet = new HashSet JavaDoc();
107         for (int i = 0; i < metaMethods.length; i++) {
108             String JavaDoc[] strings = (String JavaDoc[]) metaMethods[i];
109             metaSet.add(strings[Inspector.MEMBER_NAME_IDX]);
110         }
111         Set JavaDoc nameSet = new HashSet JavaDoc(Arrays.asList(names));
112         assertEquals(nameSet, metaSet);
113     }
114
115     private void assertContains(Object JavaDoc[] candidates, String JavaDoc[] sample) {
116         String JavaDoc sampleBuffer = concat(sample);
117         for (int i = 0; i < candidates.length; i++) {
118             String JavaDoc[] entry = (String JavaDoc[]) candidates[i];
119             if (sampleBuffer.equals(concat(entry))) return;
120         }
121         fail("should have found sample: " + sampleBuffer);
122     }
123
124     private void assertUnique(Object JavaDoc[] sortedMembers){
125         Comparator JavaDoc comp = new Inspector.MemberComparator();
126         for (int i = 1; i < sortedMembers.length; i++) {
127             if (0 == comp.compare(sortedMembers[i - 1], sortedMembers[i])){
128                 Inspector.print(sortedMembers);
129                 fail("found duplication at pos "+ (i-1)+" and "+i);
130             }
131         }
132     }
133
134     private String JavaDoc concat(String JavaDoc[] details) {
135         StringBuffer JavaDoc detailBuffer = new StringBuffer JavaDoc();
136         for (int i = 0; i < details.length; i++) {
137             detailBuffer.append(details[i]);
138             detailBuffer.append(" ");
139         }
140         return detailBuffer.toString();
141     }
142
143 }
Popular Tags