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