PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/findbugs-1.3.9/src/junit/edu/umd/cs/findbugs/ba/ch/Subtypes2Test.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 339 lines | 216 code | 52 blank | 71 comment | 0 complexity | 384238049ec3ca3f51b0447c2d6e310a MD5 | raw file
  1. /*
  2. * FindBugs - Find Bugs in Java programs
  3. * Copyright (C) 2003-2007 University of Maryland
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. package edu.umd.cs.findbugs.ba.ch;
  20. import org.apache.bcel.generic.ArrayType;
  21. import org.apache.bcel.generic.ObjectType;
  22. import org.apache.bcel.generic.Type;
  23. import edu.umd.cs.findbugs.FindBugsTestCase;
  24. import edu.umd.cs.findbugs.RunnableWithExceptions;
  25. import edu.umd.cs.findbugs.ba.ObjectTypeFactory;
  26. import edu.umd.cs.findbugs.classfile.Global;
  27. import edu.umd.cs.findbugs.detect.FindRefComparison;
  28. /**
  29. * Tests for Subtypes2.
  30. *
  31. * @author Bill Pugh
  32. * @author David Hovemeyer
  33. */
  34. public class Subtypes2Test extends FindBugsTestCase {
  35. ObjectType typeSerializable;
  36. ObjectType typeClonable;
  37. ObjectType typeObject;
  38. ObjectType typeInteger;
  39. ObjectType typeString;
  40. ObjectType typeList;
  41. ObjectType typeCollection;
  42. ObjectType typeHashSet;
  43. ArrayType typeArrayClonable;
  44. ObjectType typeComparable;
  45. ArrayType typeArrayObject;
  46. ArrayType typeArrayInteger;
  47. ArrayType typeArrayString;
  48. ArrayType typeArrayComparable;
  49. ArrayType typeArrayArrayObject;
  50. ArrayType typeArrayArraySerializable;
  51. ArrayType typeArrayArrayString;
  52. ArrayType typeArrayInt;
  53. ArrayType typeArrayArrayInt;
  54. ArrayType typeArrayArrayArrayInt;
  55. ArrayType typeArrayChar;
  56. ArrayType typeArrayArrayChar;
  57. ArrayType typeArrayArrayArrayChar;
  58. ObjectType typeDynamicString;
  59. ObjectType typeStaticString;
  60. ObjectType typeParameterString;
  61. /* (non-Javadoc)
  62. * @see junit.framework.TestCase#setUp()
  63. */
  64. @Override
  65. protected void setUp() throws Exception {
  66. super.setUp();
  67. typeSerializable = ObjectTypeFactory.getInstance("java.io.Serializable");
  68. typeClonable = ObjectTypeFactory.getInstance("java.lang.Cloneable");
  69. typeObject = ObjectTypeFactory.getInstance("java.lang.Object");
  70. typeInteger = ObjectTypeFactory.getInstance("java.lang.Integer");
  71. typeString = ObjectTypeFactory.getInstance("java.lang.String");
  72. typeComparable = ObjectTypeFactory.getInstance("java.lang.Comparable");
  73. typeList = ObjectTypeFactory.getInstance("java.util.List");
  74. typeCollection = ObjectTypeFactory.getInstance("java.util.Collection");
  75. typeHashSet = ObjectTypeFactory.getInstance("java.util.HashSet");
  76. typeArrayClonable = new ArrayType(typeClonable,1);
  77. typeArrayComparable= new ArrayType(typeComparable,1);
  78. typeArrayObject = new ArrayType(typeObject,1);
  79. typeArrayInteger = new ArrayType(typeInteger,1);
  80. typeArrayString = new ArrayType(typeString, 1);
  81. typeArrayArrayObject = new ArrayType(typeObject, 2);
  82. typeArrayArraySerializable = new ArrayType(typeSerializable, 2);
  83. typeArrayArrayString = new ArrayType(typeString, 2);
  84. typeArrayInt = new ArrayType(Type.INT, 1);
  85. typeArrayArrayInt = new ArrayType(Type.INT, 2);
  86. typeArrayArrayArrayInt = new ArrayType(Type.INT, 3);
  87. typeArrayChar = new ArrayType(Type.CHAR, 1);
  88. typeArrayArrayChar = new ArrayType(Type.CHAR, 2);
  89. typeArrayArrayArrayChar = new ArrayType(Type.CHAR, 3);
  90. typeDynamicString = new FindRefComparison.DynamicStringType();
  91. typeStaticString = new FindRefComparison.StaticStringType();
  92. typeParameterString = new FindRefComparison.ParameterStringType();
  93. }
  94. private static Subtypes2 getSubtypes2() {
  95. return Global.getAnalysisCache().getDatabase(Subtypes2.class);
  96. }
  97. public void testStringSubtypeOfObject() throws Throwable {
  98. executeFindBugsTest(new RunnableWithExceptions(){
  99. /* (non-Javadoc)
  100. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  101. */
  102. public void run() throws Throwable {
  103. Subtypes2 test = getSubtypes2();
  104. assertTrue(test.isSubtype(typeString, typeObject));
  105. }
  106. });
  107. }
  108. public void testStringSubtypeOfSerializable() throws Throwable {
  109. executeFindBugsTest(new RunnableWithExceptions(){
  110. /* (non-Javadoc)
  111. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  112. */
  113. public void run() throws Throwable {
  114. Subtypes2 test = getSubtypes2();
  115. assertTrue(test.isSubtype(typeString, typeSerializable));
  116. }
  117. });
  118. }
  119. public void testIdentitySubtype() throws Throwable {
  120. executeFindBugsTest(new RunnableWithExceptions() {
  121. /* (non-Javadoc)
  122. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  123. */
  124. public void run() throws Throwable {
  125. Subtypes2 test = getSubtypes2();
  126. assertTrue(test.isSubtype(typeObject, typeObject));
  127. assertTrue(test.isSubtype(typeSerializable, typeSerializable));
  128. assertTrue(test.isSubtype(typeArrayClonable, typeArrayClonable));
  129. }
  130. });
  131. }
  132. public void testInterfaceIsSubtypeOfObject() throws Throwable {
  133. executeFindBugsTest(new RunnableWithExceptions() {
  134. public void run() throws ClassNotFoundException {
  135. Subtypes2 test = getSubtypes2();
  136. assertTrue(test.isSubtype(typeClonable, typeObject));
  137. }
  138. });
  139. }
  140. public void testArrays() throws Throwable {
  141. executeFindBugsTest(new RunnableWithExceptions() {
  142. /* (non-Javadoc)
  143. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  144. */
  145. public void run() throws Throwable {
  146. Subtypes2 test = getSubtypes2();
  147. assertTrue(test.isSubtype(typeArrayClonable, typeObject));
  148. assertTrue(test.isSubtype(typeArrayClonable, typeArrayObject));
  149. }
  150. });
  151. }
  152. public void testUnrelatedTypes() throws Throwable {
  153. executeFindBugsTest(new RunnableWithExceptions(){
  154. /* (non-Javadoc)
  155. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  156. */
  157. public void run() throws Throwable {
  158. Subtypes2 test = getSubtypes2();
  159. assertFalse(test.isSubtype(typeInteger, typeString));
  160. }
  161. });
  162. }
  163. public void testArraysWrongDimension() throws Throwable {
  164. executeFindBugsTest(new RunnableWithExceptions(){
  165. /* (non-Javadoc)
  166. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  167. */
  168. public void run() throws Throwable {
  169. Subtypes2 test = getSubtypes2();
  170. assertFalse(test.isSubtype(typeArrayArrayString, typeArrayString));
  171. }
  172. });
  173. }
  174. public void testMultidimensionalArrayIsSubtypeOfObjectArray() throws Throwable {
  175. executeFindBugsTest(new RunnableWithExceptions() {
  176. /* (non-Javadoc)
  177. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  178. */
  179. public void run() throws Throwable {
  180. Subtypes2 test = getSubtypes2();
  181. assertTrue(test.isSubtype(typeArrayArrayString, typeArrayObject));
  182. assertTrue(test.isSubtype(typeArrayArraySerializable, typeArrayObject));
  183. assertTrue(test.isSubtype(typeArrayArrayInt, typeArrayObject));
  184. }
  185. });
  186. }
  187. public void testArrayOfPrimitiveIsSubtypeOfObject() throws Throwable {
  188. executeFindBugsTest(new RunnableWithExceptions(){
  189. /* (non-Javadoc)
  190. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  191. */
  192. public void run() throws Exception {
  193. Subtypes2 test = getSubtypes2();
  194. assertTrue(test.isSubtype(typeArrayInt, typeObject));
  195. }
  196. });
  197. }
  198. public void testSpecialStringSubclasses() throws Exception {
  199. executeFindBugsTest(new RunnableWithExceptions() {
  200. /* (non-Javadoc)
  201. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  202. */
  203. public void run() throws Exception {
  204. Subtypes2 test = getSubtypes2();
  205. assertTrue(test.isSubtype(typeDynamicString, typeString));
  206. assertTrue(test.isSubtype(typeStaticString, typeString));
  207. assertTrue(test.isSubtype(typeParameterString, typeString));
  208. }
  209. });
  210. }
  211. public void testEasyFirstCommonSuperclass() throws Exception {
  212. executeFindBugsTest(new RunnableWithExceptions() {
  213. /* (non-Javadoc)
  214. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  215. */
  216. public void run() throws Throwable {
  217. Subtypes2 test = getSubtypes2();
  218. assertEquals(typeObject, test.getFirstCommonSuperclass(typeObject, typeObject));
  219. assertEquals(typeString, test.getFirstCommonSuperclass(typeString, typeString));
  220. assertEquals(typeObject, test.getFirstCommonSuperclass(typeString, typeObject));
  221. assertEquals(typeObject, test.getFirstCommonSuperclass(typeObject, typeString));
  222. // Slightly harder one
  223. assertEquals(typeComparable, test.getFirstCommonSuperclass(typeString, typeInteger));
  224. }
  225. });
  226. }
  227. public void testInterfaceFirstCommonSuperclass() throws Exception {
  228. executeFindBugsTest(new RunnableWithExceptions(){
  229. /* (non-Javadoc)
  230. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  231. */
  232. public void run() throws Throwable {
  233. Subtypes2 test = getSubtypes2();
  234. assertEquals(typeObject, test.getFirstCommonSuperclass(typeSerializable, typeObject));
  235. assertEquals(typeObject, test.getFirstCommonSuperclass(typeObject, typeSerializable));
  236. assertEquals(typeObject, test.getFirstCommonSuperclass(typeSerializable, typeClonable));
  237. assertEquals(typeSerializable, test.getFirstCommonSuperclass(typeSerializable, typeSerializable));
  238. }
  239. });
  240. }
  241. public void testArrayFirstCommonSuperclass() throws Exception {
  242. executeFindBugsTest(new RunnableWithExceptions(){
  243. /* (non-Javadoc)
  244. * @see edu.umd.cs.findbugs.RunnableWithExceptions#run()
  245. */
  246. public void run() throws Throwable {
  247. Subtypes2 test = getSubtypes2();
  248. assertEquals(typeObject, test.getFirstCommonSuperclass(typeArrayInteger, typeObject));
  249. assertEquals(typeObject, test.getFirstCommonSuperclass(typeSerializable, typeArrayClonable));
  250. assertEquals(typeArrayComparable, test.getFirstCommonSuperclass(typeArrayString, typeArrayInteger));
  251. assertEquals(typeArrayInt, test.getFirstCommonSuperclass(typeArrayInt, typeArrayInt));
  252. assertEquals(typeObject, test.getFirstCommonSuperclass(typeArrayChar, typeArrayInt));
  253. assertEquals(typeObject, test.getFirstCommonSuperclass(typeArrayString, typeArrayInt));
  254. assertEquals(typeArrayObject, test.getFirstCommonSuperclass(typeArrayArraySerializable, typeArrayString));
  255. assertEquals(typeObject, test.getFirstCommonSuperclass(typeArrayArrayString, typeArrayInt));
  256. }
  257. });
  258. }
  259. public void testArrayFirstCommonSuperclassTricky() throws Exception {
  260. executeFindBugsTest(new RunnableWithExceptions() {
  261. public void run() throws Throwable {
  262. Subtypes2 test = getSubtypes2();
  263. assertEquals(typeArrayObject, test.getFirstCommonSuperclass(typeArrayArrayInt, typeArrayArrayChar));
  264. assertEquals(typeArrayObject, test.getFirstCommonSuperclass(typeArrayArrayInt, typeArrayArrayArrayChar));
  265. assertEquals(typeArrayArrayObject, test.getFirstCommonSuperclass(typeArrayArrayArrayChar, typeArrayArrayArrayInt));
  266. // Sanity check
  267. assertEquals(typeArrayArrayArrayChar, test.getFirstCommonSuperclass(typeArrayArrayArrayChar, typeArrayArrayArrayChar));
  268. }
  269. });
  270. }
  271. public void testInterfaces() throws Exception {
  272. executeFindBugsTest(new RunnableWithExceptions() {
  273. public void run() throws Throwable {
  274. Subtypes2 test = getSubtypes2();
  275. assertEquals(typeCollection, test.getFirstCommonSuperclass(typeCollection, typeHashSet));
  276. assertEquals(typeCollection, test.getFirstCommonSuperclass(typeHashSet, typeCollection));
  277. assertEquals(typeCollection, test.getFirstCommonSuperclass(typeList, typeHashSet));
  278. }
  279. });
  280. /*
  281. ObjectType typeList;
  282. ObjectType typeMap;
  283. ObjectType typeCollection;
  284. ObjectType typeHashSet;
  285. */
  286. }
  287. }