PageRenderTime 40ms CodeModel.GetById 14ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 1ms

/testability-explorer/src/test/java/com/google/test/metric/ClassInfoTest.java

http://testability-explorer.googlecode.com/
Java | 278 lines | 219 code | 44 blank | 15 comment | 4 complexity | 6440837f42c516ef9c029fd2054cfe3b MD5 | raw file
  1/*
  2 * Copyright 2007 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.google.test.metric;
 18
 19import java.io.File;
 20import java.net.InetAddress;
 21import java.util.Arrays;
 22import java.util.BitSet;
 23import java.util.Collection;
 24import java.util.Collections;
 25import java.util.Iterator;
 26import java.util.List;
 27
 28import com.google.classpath.DirectoryClassPath;
 29import com.google.test.metric.method.op.turing.Operation;
 30
 31public class ClassInfoTest extends AutoFieldClearTestCase {
 32
 33  private final ClassRepository repo = new JavaClassRepository();
 34
 35  public void testNonExistingClass() throws Exception {
 36    try {
 37      repo.getClass("IDontExistClass");
 38      fail();
 39    } catch (ClassNotFoundException e) {
 40      assertTrue(e.getMessage().contains("IDontExistClass"));
 41      assertEquals("IDontExistClass", e.getClassName());
 42    }
 43  }
 44
 45  public static class EmptyClass {
 46  }
 47
 48  public void testParseEmptyClass() throws Exception {
 49    ClassInfo clazz = repo.getClass(EmptyClass.class.getCanonicalName());
 50    assertEquals(EmptyClass.class.getCanonicalName(), clazz.getName());
 51    assertEquals(EmptyClass.class.getCanonicalName(), clazz.toString());
 52    assertSame(clazz, repo.getClass(EmptyClass.class.getCanonicalName()));
 53  }
 54
 55  public void testMethodNotFoundException() throws Exception {
 56    ClassInfo clazz = repo.getClass(EmptyClass.class.getCanonicalName());
 57    try {
 58      clazz.getMethod("IDontExistMethod()V");
 59      fail();
 60    } catch (MethodNotFoundException e) {
 61      assertTrue(e.getMessage().contains("IDontExistMethod()V"));
 62      assertTrue(e.getMessage().contains(EmptyClass.class.getCanonicalName()));
 63      assertEquals("IDontExistMethod()V", e.getMethodName());
 64      assertEquals(clazz, e.getClassInfo());
 65    }
 66  }
 67
 68  public static class SingleMethodClass {
 69    public void methodA() {
 70    }
 71  }
 72
 73  public void testParseSingleMethodClass() throws Exception {
 74    ClassInfo clazz = repo.getClass(SingleMethodClass.class.getCanonicalName());
 75    MethodInfo method = clazz.getMethod("void methodA()");
 76    assertEquals("void methodA()", method.toString());
 77    assertSame(method, clazz.getMethod("void methodA()"));
 78  }
 79
 80  public void testFiledNotFound() throws Exception {
 81    ClassInfo clazz = repo.getClass(EmptyClass.class.getCanonicalName());
 82    try {
 83      clazz.getField("IDontExistField");
 84      fail();
 85    } catch (FieldNotFoundException e) {
 86      assertTrue(e.getMessage().contains("IDontExistField"));
 87      assertTrue(e.getMessage().contains(EmptyClass.class.getCanonicalName()));
 88      assertEquals("IDontExistField", e.getFieldName());
 89      assertEquals(clazz, e.getClassInfo());
 90    }
 91  }
 92
 93  public static class SingleFieldClass {
 94    Object fieldA;
 95  }
 96
 97  public void testParseFields() throws Exception {
 98    ClassInfo clazz = repo.getClass(SingleFieldClass.class.getCanonicalName());
 99    FieldInfo field = clazz.getField("fieldA");
100    assertEquals("fieldA", field.getName());
101    assertEquals(SingleFieldClass.class.getCanonicalName()
102        + ".fieldA{java.lang.Object}", field.toString());
103    assertSame(field, clazz.getField("fieldA"));
104  }
105
106  public static class LocalVarsClass {
107    public void method() {
108    }
109
110    public static void staticMethod() {
111    }
112
113    public void method3(Object a, int b, int[] c) {
114      Object d = null;
115      a = d;
116    }
117
118    public static void staticMethod3(Object a, int b, int[] c) {
119      Object d = null;
120      a = d;
121    }
122  }
123
124  public void testLocalVarsMethod() throws Exception {
125    assertLocalVars("void method()", params(), locals("this"));
126  }
127
128  public void testLocalVarsStaticMethod() throws Exception {
129    assertLocalVars("void staticMethod()", params(), locals());
130  }
131
132  public void testLocalVarsMethod3() throws Exception {
133    assertLocalVars("void method3(java.lang.Object, int, int[])",
134        params("a", "b", "c"), locals("this", "d"));
135  }
136
137  public void testLocalVarsStaticMethod3() throws Exception {
138    assertLocalVars("void staticMethod3(java.lang.Object, int, int[])", params("a",
139        "b", "c"), locals("d"));
140  }
141
142  private void assertLocalVars(String method, String[] params, String[] locals) {
143    ClassInfo classInfo = repo.getClass(LocalVarsClass.class.getCanonicalName());
144    MethodInfo methodInfo = classInfo.getMethod(method);
145    List<ParameterInfo> paramsParse = methodInfo.getParameters();
146    List<LocalVariableInfo> localsParse = methodInfo.getLocalVariables();
147    assertEquals("Expecting " + Arrays.toString(params) + " found "
148        + paramsParse, params.length, paramsParse.size());
149    assertEquals("Expecting " + Arrays.toString(locals) + " found "
150        + localsParse, locals.length, localsParse.size());
151    for (int i = 0; i < params.length; i++) {
152      assertEquals(params[i], paramsParse.get(i).getName());
153    }
154    for (int i = 0; i < locals.length; i++) {
155      assertEquals(locals[i], localsParse.get(i).getName());
156    }
157  }
158
159  private String[] params(String... strings) {
160    return strings;
161  }
162
163  private String[] locals(String... strings) {
164    return strings;
165  }
166
167  public void testJavaLangObject() throws Exception {
168    repo.getClass(Object.class.getCanonicalName());
169  }
170
171  public void testJavaLangString() throws Exception {
172    repo.getClass(String.class.getCanonicalName());
173  }
174
175  public void testJavaUtilBitSet() throws Exception {
176    repo.getClass(BitSet.class.getCanonicalName());
177  }
178
179  static class BitSetGetMethod {
180    private long[] bits;
181
182    public BitSetGetMethod get(int fromIndex, int toIndex) {
183      int startBitIndex = 0;
184      while (true) {
185        bits[0] = bits[1] | (startBitIndex == 2 ? 0 : 2);
186      }
187    }
188  }
189
190  public void testJavaUtilBitSetGetMethod() throws Exception {
191    repo.getClass(BitSetGetMethod.class.getCanonicalName());
192  }
193
194  private static class Monitor {
195    public void method() {
196      synchronized (this) {
197        hashCode();
198      }
199    }
200
201    public void method2() {
202      hashCode();
203      synchronized (this) {
204        hashCode();
205      }
206      hashCode();
207    }
208  }
209
210  public void testMonitor() throws Exception {
211    repo.getClass(Monitor.class.getCanonicalName());
212  }
213
214  public void testJSRinstructionInTryCatchFinally() throws Exception {
215    repo.getClass(InetAddress.class.getCanonicalName());
216  }
217
218  interface TestInterface {
219    Object get(Object o);
220  }
221
222  interface SubTestInterface extends TestInterface {
223  }
224
225  class ImplementsSubTestInterface implements SubTestInterface {
226    public Object get(Object o) {
227      return null;
228    }
229  }
230
231  public void testMethodInSuperInterface() throws Exception {
232    ClassInfo interfaceClassInfo = repo.getClass(SubTestInterface.class.getCanonicalName());
233    assertEquals(repo.getClass(Object.class.getCanonicalName()), interfaceClassInfo.getSuperClass());
234    List<ClassInfo> superInterfaces = interfaceClassInfo.getInterfaces();
235    assertEquals(1, superInterfaces.size());
236    assertEquals(repo.getClass(TestInterface.class.getCanonicalName()), superInterfaces.get(0));
237    assertNotNull(interfaceClassInfo.getMethod("java.lang.Object get(java.lang.Object)"));
238  }
239
240  public void testPickConcreteMethodOverInterfaceMethod() throws Exception {
241    ClassInfo classInfo = repo.getClass(ImplementsSubTestInterface.class.getCanonicalName());
242    ClassInfo interfaceClassInfo = repo.getClass(SubTestInterface.class.getCanonicalName());
243    MethodInfo method = classInfo.getMethod("java.lang.Object get(java.lang.Object)");
244    assertSame(classInfo, method.getClassInfo());
245    assertNotSame(interfaceClassInfo, method.getClassInfo());
246  }
247
248  public void testReadInvalidByteCodeClassFile() throws Exception {
249    ClassRepository repo = new JavaClassRepository(new DirectoryClassPath(new File("classes-for-test")));
250    try {
251      repo.getClass("invalidByteCode");
252      fail();
253    } catch (ClassNotFoundException e) {
254    }
255  }
256
257  public void testGetSettersShouldReturnItemsInAlphabeticalOrderAndIncludeSuperClasses() throws Exception {
258    List<ClassInfo> emptyInterfaces = Collections.emptyList();
259    ClassInfo superClass = new ClassInfo("super", false, null, emptyInterfaces, null);
260    List<ParameterInfo> params = Collections.emptyList();
261    List<LocalVariableInfo> locals = Collections.emptyList();
262    List<Operation> operations = Collections.emptyList();
263    superClass.addMethod(new MethodInfo(superClass, "void setB()", -1, null, params, locals,Visibility.PUBLIC, operations, false, false, Collections.<Integer>emptyList()));
264    superClass.addMethod(new MethodInfo(superClass, "void setA()", -1, null, params, locals,Visibility.PRIVATE, operations, false, false, Collections.<Integer>emptyList()));
265    superClass.addMethod(new MethodInfo(superClass, "voidX()", -1, null, params, locals,Visibility.PUBLIC, operations, false, false, Collections.<Integer>emptyList()));
266    ClassInfo clazz = new ClassInfo("super", false, superClass, emptyInterfaces, null);
267    clazz.addMethod(new MethodInfo(clazz, "void setD()", -1, null, params, locals,Visibility.PUBLIC, operations, false, false, Collections.<Integer>emptyList()));
268    clazz.addMethod(new MethodInfo(clazz, "void setC()", -1, null, params, locals,Visibility.PUBLIC, operations, false, false, Collections.<Integer>emptyList()));
269
270    Collection<MethodInfo> setters = clazz.getSetters();
271    assertEquals(3, setters.size());
272    Iterator<MethodInfo> iterator = setters.iterator();
273    assertEquals("void setB()", iterator.next().getName());
274    assertEquals("void setC()", iterator.next().getName());
275    assertEquals("void setD()", iterator.next().getName());
276  }
277
278}