/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. package com.google.test.metric;
  17. import java.io.File;
  18. import java.net.InetAddress;
  19. import java.util.Arrays;
  20. import java.util.BitSet;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import com.google.classpath.DirectoryClassPath;
  26. import com.google.test.metric.method.op.turing.Operation;
  27. public class ClassInfoTest extends AutoFieldClearTestCase {
  28. private final ClassRepository repo = new JavaClassRepository();
  29. public void testNonExistingClass() throws Exception {
  30. try {
  31. repo.getClass("IDontExistClass");
  32. fail();
  33. } catch (ClassNotFoundException e) {
  34. assertTrue(e.getMessage().contains("IDontExistClass"));
  35. assertEquals("IDontExistClass", e.getClassName());
  36. }
  37. }
  38. public static class EmptyClass {
  39. }
  40. public void testParseEmptyClass() throws Exception {
  41. ClassInfo clazz = repo.getClass(EmptyClass.class.getCanonicalName());
  42. assertEquals(EmptyClass.class.getCanonicalName(), clazz.getName());
  43. assertEquals(EmptyClass.class.getCanonicalName(), clazz.toString());
  44. assertSame(clazz, repo.getClass(EmptyClass.class.getCanonicalName()));
  45. }
  46. public void testMethodNotFoundException() throws Exception {
  47. ClassInfo clazz = repo.getClass(EmptyClass.class.getCanonicalName());
  48. try {
  49. clazz.getMethod("IDontExistMethod()V");
  50. fail();
  51. } catch (MethodNotFoundException e) {
  52. assertTrue(e.getMessage().contains("IDontExistMethod()V"));
  53. assertTrue(e.getMessage().contains(EmptyClass.class.getCanonicalName()));
  54. assertEquals("IDontExistMethod()V", e.getMethodName());
  55. assertEquals(clazz, e.getClassInfo());
  56. }
  57. }
  58. public static class SingleMethodClass {
  59. public void methodA() {
  60. }
  61. }
  62. public void testParseSingleMethodClass() throws Exception {
  63. ClassInfo clazz = repo.getClass(SingleMethodClass.class.getCanonicalName());
  64. MethodInfo method = clazz.getMethod("void methodA()");
  65. assertEquals("void methodA()", method.toString());
  66. assertSame(method, clazz.getMethod("void methodA()"));
  67. }
  68. public void testFiledNotFound() throws Exception {
  69. ClassInfo clazz = repo.getClass(EmptyClass.class.getCanonicalName());
  70. try {
  71. clazz.getField("IDontExistField");
  72. fail();
  73. } catch (FieldNotFoundException e) {
  74. assertTrue(e.getMessage().contains("IDontExistField"));
  75. assertTrue(e.getMessage().contains(EmptyClass.class.getCanonicalName()));
  76. assertEquals("IDontExistField", e.getFieldName());
  77. assertEquals(clazz, e.getClassInfo());
  78. }
  79. }
  80. public static class SingleFieldClass {
  81. Object fieldA;
  82. }
  83. public void testParseFields() throws Exception {
  84. ClassInfo clazz = repo.getClass(SingleFieldClass.class.getCanonicalName());
  85. FieldInfo field = clazz.getField("fieldA");
  86. assertEquals("fieldA", field.getName());
  87. assertEquals(SingleFieldClass.class.getCanonicalName()
  88. + ".fieldA{java.lang.Object}", field.toString());
  89. assertSame(field, clazz.getField("fieldA"));
  90. }
  91. public static class LocalVarsClass {
  92. public void method() {
  93. }
  94. public static void staticMethod() {
  95. }
  96. public void method3(Object a, int b, int[] c) {
  97. Object d = null;
  98. a = d;
  99. }
  100. public static void staticMethod3(Object a, int b, int[] c) {
  101. Object d = null;
  102. a = d;
  103. }
  104. }
  105. public void testLocalVarsMethod() throws Exception {
  106. assertLocalVars("void method()", params(), locals("this"));
  107. }
  108. public void testLocalVarsStaticMethod() throws Exception {
  109. assertLocalVars("void staticMethod()", params(), locals());
  110. }
  111. public void testLocalVarsMethod3() throws Exception {
  112. assertLocalVars("void method3(java.lang.Object, int, int[])",
  113. params("a", "b", "c"), locals("this", "d"));
  114. }
  115. public void testLocalVarsStaticMethod3() throws Exception {
  116. assertLocalVars("void staticMethod3(java.lang.Object, int, int[])", params("a",
  117. "b", "c"), locals("d"));
  118. }
  119. private void assertLocalVars(String method, String[] params, String[] locals) {
  120. ClassInfo classInfo = repo.getClass(LocalVarsClass.class.getCanonicalName());
  121. MethodInfo methodInfo = classInfo.getMethod(method);
  122. List<ParameterInfo> paramsParse = methodInfo.getParameters();
  123. List<LocalVariableInfo> localsParse = methodInfo.getLocalVariables();
  124. assertEquals("Expecting " + Arrays.toString(params) + " found "
  125. + paramsParse, params.length, paramsParse.size());
  126. assertEquals("Expecting " + Arrays.toString(locals) + " found "
  127. + localsParse, locals.length, localsParse.size());
  128. for (int i = 0; i < params.length; i++) {
  129. assertEquals(params[i], paramsParse.get(i).getName());
  130. }
  131. for (int i = 0; i < locals.length; i++) {
  132. assertEquals(locals[i], localsParse.get(i).getName());
  133. }
  134. }
  135. private String[] params(String... strings) {
  136. return strings;
  137. }
  138. private String[] locals(String... strings) {
  139. return strings;
  140. }
  141. public void testJavaLangObject() throws Exception {
  142. repo.getClass(Object.class.getCanonicalName());
  143. }
  144. public void testJavaLangString() throws Exception {
  145. repo.getClass(String.class.getCanonicalName());
  146. }
  147. public void testJavaUtilBitSet() throws Exception {
  148. repo.getClass(BitSet.class.getCanonicalName());
  149. }
  150. static class BitSetGetMethod {
  151. private long[] bits;
  152. public BitSetGetMethod get(int fromIndex, int toIndex) {
  153. int startBitIndex = 0;
  154. while (true) {
  155. bits[0] = bits[1] | (startBitIndex == 2 ? 0 : 2);
  156. }
  157. }
  158. }
  159. public void testJavaUtilBitSetGetMethod() throws Exception {
  160. repo.getClass(BitSetGetMethod.class.getCanonicalName());
  161. }
  162. private static class Monitor {
  163. public void method() {
  164. synchronized (this) {
  165. hashCode();
  166. }
  167. }
  168. public void method2() {
  169. hashCode();
  170. synchronized (this) {
  171. hashCode();
  172. }
  173. hashCode();
  174. }
  175. }
  176. public void testMonitor() throws Exception {
  177. repo.getClass(Monitor.class.getCanonicalName());
  178. }
  179. public void testJSRinstructionInTryCatchFinally() throws Exception {
  180. repo.getClass(InetAddress.class.getCanonicalName());
  181. }
  182. interface TestInterface {
  183. Object get(Object o);
  184. }
  185. interface SubTestInterface extends TestInterface {
  186. }
  187. class ImplementsSubTestInterface implements SubTestInterface {
  188. public Object get(Object o) {
  189. return null;
  190. }
  191. }
  192. public void testMethodInSuperInterface() throws Exception {
  193. ClassInfo interfaceClassInfo = repo.getClass(SubTestInterface.class.getCanonicalName());
  194. assertEquals(repo.getClass(Object.class.getCanonicalName()), interfaceClassInfo.getSuperClass());
  195. List<ClassInfo> superInterfaces = interfaceClassInfo.getInterfaces();
  196. assertEquals(1, superInterfaces.size());
  197. assertEquals(repo.getClass(TestInterface.class.getCanonicalName()), superInterfaces.get(0));
  198. assertNotNull(interfaceClassInfo.getMethod("java.lang.Object get(java.lang.Object)"));
  199. }
  200. public void testPickConcreteMethodOverInterfaceMethod() throws Exception {
  201. ClassInfo classInfo = repo.getClass(ImplementsSubTestInterface.class.getCanonicalName());
  202. ClassInfo interfaceClassInfo = repo.getClass(SubTestInterface.class.getCanonicalName());
  203. MethodInfo method = classInfo.getMethod("java.lang.Object get(java.lang.Object)");
  204. assertSame(classInfo, method.getClassInfo());
  205. assertNotSame(interfaceClassInfo, method.getClassInfo());
  206. }
  207. public void testReadInvalidByteCodeClassFile() throws Exception {
  208. ClassRepository repo = new JavaClassRepository(new DirectoryClassPath(new File("classes-for-test")));
  209. try {
  210. repo.getClass("invalidByteCode");
  211. fail();
  212. } catch (ClassNotFoundException e) {
  213. }
  214. }
  215. public void testGetSettersShouldReturnItemsInAlphabeticalOrderAndIncludeSuperClasses() throws Exception {
  216. List<ClassInfo> emptyInterfaces = Collections.emptyList();
  217. ClassInfo superClass = new ClassInfo("super", false, null, emptyInterfaces, null);
  218. List<ParameterInfo> params = Collections.emptyList();
  219. List<LocalVariableInfo> locals = Collections.emptyList();
  220. List<Operation> operations = Collections.emptyList();
  221. superClass.addMethod(new MethodInfo(superClass, "void setB()", -1, null, params, locals,Visibility.PUBLIC, operations, false, false, Collections.<Integer>emptyList()));
  222. superClass.addMethod(new MethodInfo(superClass, "void setA()", -1, null, params, locals,Visibility.PRIVATE, operations, false, false, Collections.<Integer>emptyList()));
  223. superClass.addMethod(new MethodInfo(superClass, "voidX()", -1, null, params, locals,Visibility.PUBLIC, operations, false, false, Collections.<Integer>emptyList()));
  224. ClassInfo clazz = new ClassInfo("super", false, superClass, emptyInterfaces, null);
  225. clazz.addMethod(new MethodInfo(clazz, "void setD()", -1, null, params, locals,Visibility.PUBLIC, operations, false, false, Collections.<Integer>emptyList()));
  226. clazz.addMethod(new MethodInfo(clazz, "void setC()", -1, null, params, locals,Visibility.PUBLIC, operations, false, false, Collections.<Integer>emptyList()));
  227. Collection<MethodInfo> setters = clazz.getSetters();
  228. assertEquals(3, setters.size());
  229. Iterator<MethodInfo> iterator = setters.iterator();
  230. assertEquals("void setB()", iterator.next().getName());
  231. assertEquals("void setC()", iterator.next().getName());
  232. assertEquals("void setD()", iterator.next().getName());
  233. }
  234. }