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(31, metaMethods.length);
62 String[] names = { "sleep", "sleep", "println", "println", "println", "find", "print", "print", "each", "invokeMethod",
63 "inspect", "is", "isCase", "identity", "getAt", "putAt", "dump", "getMetaPropertyValues", "getProperties",
64 "use", "use", "printf", "printf", "eachWithIndex", "every", "any", "grep", "collect", "collect", "findAll", "findIndexOf"
65 };
66 assertNameEquals(names, metaMethods);
67 String[] details = {"GROOVY","public","Object","void","println","Object","n/a"};
68 assertContains(metaMethods, details);
69 }
70
71 public void testStaticMetaMethods() {
72 Matcher matcher = Pattern.compile("").matcher("");
73 Inspector insp = new Inspector(matcher);
74 Object[] metaMethods = insp.getMetaMethods();
75 assertUnique(Inspector.sort(Arrays.asList(metaMethods)));
76 String[] details = {"GROOVY","public static","Matcher","Matcher","getLastMatcher","","n/a"};
77 assertContains(metaMethods, details);
78 }
79
80 public void testFields() {
81 Inspector insp = new Inspector(this);
82 Object[] fields = insp.getPublicFields();
83 assertEquals(2, fields.length);
84 String[] names = { "someField","SOME_CONST" };
85 assertNameEquals(names, fields);
86 String[] details = {"JAVA","public","InspectorTest","String","someField","\"only for testing\""};
87 assertContains(fields, details);
88 }
89 public void testProperties() {
90 Inspector insp = new Inspector(this);
91 Object[] fields = insp.getPropertyInfo();
92 assertEquals(4, fields.length);
93 String[] names = { "SOME_CONST","someField","class","name"};
94 assertNameEquals(names, fields);
95 String[] details = {"GROOVY","public","n/a","String","name","\"testProperties\""};
96 assertContains(fields, details);
97 }
98
99 private void assertNameEquals(String[] names, Object[] metaMethods) {
100 Set metaSet = new HashSet();
101 for (int i = 0; i < metaMethods.length; i++) {
102 String[] strings = (String[]) metaMethods[i];
103 metaSet.add(strings[Inspector.MEMBER_NAME_IDX]);
104 }
105 Set nameSet = new HashSet(Arrays.asList(names));
106 assertEquals(nameSet, metaSet);
107 }
108
109 private void assertContains(Object[] candidates, String[] sample) {
110 String sampleBuffer = concat(sample);
111 for (int i = 0; i < candidates.length; i++) {
112 String[] entry = (String[]) candidates[i];
113 if (sampleBuffer.equals(concat(entry))) return;
114 }
115 fail("should have found sample: " + sampleBuffer);
116 }
117
118 private void assertUnique(Collection sortedMembers){
119 if (sortedMembers.size() < 2) return;
120 Comparator comp = new Inspector.MemberComparator();
121 Iterator iter = sortedMembers.iterator();
122 Object last = iter.next();
123 while (iter.hasNext()) {
124 Object element = iter.next();
125 if (0 == comp.compare(last, element)){
126 fail("found duplication for element "+element);
127 }
128 last = element;
129 }
130 }
131
132 private String concat(String[] details) {
133 StringBuffer detailBuffer = new StringBuffer();
134 for (int i = 0; i < details.length; i++) {
135 detailBuffer.append(details[i]);
136 detailBuffer.append(" ");
137 }
138 return detailBuffer.toString();
139 }
140
141 }