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.Set;
9 import java.util.HashSet;
10 import java.util.Arrays;
11 import java.util.Comparator;
12
13 public class InspectorTest extends TestCase implements Serializable {
14 public String someField = "only for testing";
15 public static final String SOME_CONST = "only for testing";
16
17 public InspectorTest(String name) {
18 super(name);
19 }
20
21 public void testCtor() {
22 new Inspector(new Object());
23 try {
24 new Inspector(null);
25 fail("should have thown IllegalArgumentException");
26 } catch (Exception expected) {
27 }
28 }
29
30 public void testClassProps() {
31 Inspector insp = new Inspector(this);
32 String[] 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());
41 Object[] methods = insp.getMethods();
42 assertEquals(10, methods.length);
43 String[] names = {"hashCode","getClass","wait","wait","wait","equals","notify","notifyAll","toString","java.lang.Object"};
44 assertNameEquals(names, methods);
45 String[] details = {"JAVA","public final","Object","void","wait","long, int","InterruptedException"};
46 assertContains(methods, details);
47
48 String[] 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[] methods = insp.getMethods();
55 for (int i = 0; i < methods.length; i++) {
56 String[] strings = (String[]) methods[i];
57 if(strings[1].indexOf("static") > -1) return;
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());
63 Object[] metaMethods = insp.getMetaMethods();
64 assertEquals(32, metaMethods.length);
65 String[] names = { "sleep", "sleep", "println", "println", "println", "find", "print", "print", "each", "invokeMethod",
66 "inspect", "is", "isCase", "identity", "getAt", "putAt", "dump", "eachPropertyName", "eachProperty", "allProperties",
67 "use", "use", "printf", "printf", "eachWithIndex", "every", "any", "grep", "collect", "collect", "findAll", "findIndexOf"
68 };
69 assertNameEquals(names, metaMethods);
70 String[] details = {"GROOVY","public","Object","void","println","Object","n/a"};
71 assertContains(metaMethods, details);
72 }
73
74 public void testStaticMetaMethods() {
75 Matcher matcher = Pattern.compile("").matcher("");
76 Inspector insp = new Inspector(matcher);
77 Object[] metaMethods = insp.getMetaMethods();
78 assertUnique(Inspector.sort(metaMethods));
79 String[] details = {"GROOVY","public static","Matcher","Matcher","getLastMatcher","","n/a"};
80 assertContains(metaMethods, details);
81 }
82
83 public void testFields() {
84 Inspector insp = new Inspector(this);
85 Object[] fields = insp.getPublicFields();
86 assertEquals(2, fields.length);
87 String[] names = { "someField","SOME_CONST" };
88 assertNameEquals(names, fields);
89 String[] details = {"JAVA","public","InspectorTest","String","someField","only for testing"};
90 assertContains(fields, details);
91 }
92 public void testProperties() {
93 Inspector insp = new Inspector(this);
94 Object[] fields = insp.getProperties();
95 assertEquals(4, fields.length);
96 String[] names = { "SOME_CONST","someField","class","name"};
97 assertNameEquals(names, fields);
98 String[] details = {"GROOVY","public","n/a","String","name","testProperties"};
99 assertContains(fields, details);
100 }
101
102 private void assertNameEquals(String[] names, Object[] metaMethods) {
103 Set metaSet = new HashSet();
104 for (int i = 0; i < metaMethods.length; i++) {
105 String[] strings = (String[]) metaMethods[i];
106 metaSet.add(strings[Inspector.MEMBER_NAME_IDX]);
107 }
108 Set nameSet = new HashSet(Arrays.asList(names));
109 assertEquals(nameSet, metaSet);
110 }
111
112 private void assertContains(Object[] candidates, String[] sample) {
113 String sampleBuffer = concat(sample);
114 for (int i = 0; i < candidates.length; i++) {
115 String[] entry = (String[]) candidates[i];
116 if (sampleBuffer.equals(concat(entry))) return;
117 }
118 fail("should have found sample: " + sampleBuffer);
119 }
120
121 private void assertUnique(Object[] sortedMembers){
122 Comparator comp = new Inspector.MemberComparator();
123 for (int i = 1; i < sortedMembers.length; i++) {
124 if (0 == comp.compare(sortedMembers[i - 1], sortedMembers[i])){
125 Inspector.print(sortedMembers);
126 fail("found duplication at pos "+ (i-1)+" and "+i);
127 }
128 }
129 }
130
131 private String concat(String[] details) {
132 StringBuffer detailBuffer = new StringBuffer();
133 for (int i = 0; i < details.length; i++) {
134 detailBuffer.append(details[i]);
135 detailBuffer.append(" ");
136 }
137 return detailBuffer.toString();
138 }
139
140 }