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          // ctors are not considered static !
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; // 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());
63          Object[] metaMethods = insp.getMetaMethods();
64          assertEquals(28, metaMethods.length);
65          String[] 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[] details = {"GROOVY","public","Object","void","println","Object","n/a"};
70          assertContains(metaMethods, details);
71      }
72  
73      public void testStaticMetaMethods() {
74          Matcher matcher = Pattern.compile("").matcher("");
75          Inspector insp = new Inspector(matcher);
76          Object[] 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[] 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[] fields = insp.getPublicFields();
89          assertEquals(2, fields.length);
90          String[] names = { "someField","SOME_CONST" };
91          assertNameEquals(names, fields);
92          String[] 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[] fields = insp.getProperties();
98          assertEquals(4, fields.length);
99          String[] names = { "SOME_CONST","someField","class","name"};
100         assertNameEquals(names, fields);
101         String[] details = {"GROOVY","public","n/a","String","name","testProperties"};
102         assertContains(fields, details);
103     }
104 
105     private void assertNameEquals(String[] names, Object[] metaMethods) {
106         Set metaSet = new HashSet();
107         for (int i = 0; i < metaMethods.length; i++) {
108             String[] strings = (String[]) metaMethods[i];
109             metaSet.add(strings[Inspector.MEMBER_NAME_IDX]);
110         }
111         Set nameSet = new HashSet(Arrays.asList(names));
112         assertEquals(nameSet, metaSet);
113     }
114 
115     private void assertContains(Object[] candidates, String[] sample) {
116         String sampleBuffer = concat(sample);
117         for (int i = 0; i < candidates.length; i++) {
118             String[] entry = (String[]) candidates[i];
119             if (sampleBuffer.equals(concat(entry))) return;
120         }
121         fail("should have found sample: " + sampleBuffer);
122     }
123 
124     private void assertUnique(Object[] sortedMembers){
125         Comparator 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 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 }