/api/src/test/java/org/openmrs/util/ReflectTest.java

https://github.com/babitha/openmrs · Java · 287 lines · 167 code · 48 blank · 72 comment · 0 complexity · 62ac63044e4620d83c627066dca1c5a0 MD5 · raw file

  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://license.openmrs.org
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  13. */
  14. package org.openmrs.util;
  15. import java.lang.reflect.Field;
  16. import java.lang.reflect.ParameterizedType;
  17. import java.lang.reflect.Type;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import junit.framework.Assert;
  22. import org.junit.Test;
  23. import org.openmrs.BaseOpenmrsObject;
  24. import org.openmrs.OpenmrsObject;
  25. import org.openmrs.Visit;
  26. import org.openmrs.VisitAttribute;
  27. import org.openmrs.test.Verifies;
  28. import org.springframework.util.ReflectionUtils;
  29. /**
  30. * Tests the {@link Reflect} class.
  31. */
  32. public class ReflectTest {
  33. /**
  34. * @see Reflect#hasField(Field)
  35. */
  36. @Test
  37. @Verifies(value = "should return true if given field is declared in parameterized class or its sub classes", method = "hasField(Field)")
  38. public void xhasField_shouldReturnTrueIfGivenFieldIsDeclaredInParameterizedClassOrSubClass() throws Exception {
  39. Reflect reflect = new Reflect(OpenmrsObject.class);
  40. List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
  41. Assert.assertEquals("genericCollectionField", allFields.get(0).getName());
  42. Assert.assertTrue(reflect.hasField(allFields.get(0)));
  43. }
  44. /**
  45. * @see Reflect#hasField(Field)
  46. */
  47. @Test
  48. @Verifies(value = "should return false if given field is not declared in parameterized class or its sub classes", method = "hasField(Field)")
  49. public void xhasField_shouldReturnFalseIfGivenFieldIsNotDeclaredInParameterizedClassOrItsSubClass() throws Exception {
  50. Reflect reflect = new Reflect(OpenmrsObject.class);
  51. List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
  52. Assert.assertEquals("normalClassField", allFields.get(3).getName());
  53. Assert.assertFalse(reflect.hasField(allFields.get(3)));
  54. }
  55. /**
  56. * @see {@link Reflect#getAllFields(Class<*>)}
  57. */
  58. @Test
  59. @Verifies(value = "should return all fields include private and super classes", method = "getAllFields(Class<*>)")
  60. public void getAllFields_shouldReturnAllFieldsIncludePrivateAndSuperClasses() throws Exception {
  61. List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
  62. Assert.assertEquals(4, allFields.size());
  63. Assert.assertEquals("subClassField", allFields.get(1).getName());
  64. Assert.assertEquals("normalClassField", allFields.get(3).getName());
  65. }
  66. /**
  67. * @see {@link Reflect#isCollection(Class<*>)}
  68. */
  69. @Test
  70. @Verifies(value = "should return false if given fieldClass is not a Collection class", method = "isCollection(Class<*>)")
  71. public void isCollection_shouldReturnFalseIfGivenFieldClassIsNotACollectionClass() throws Exception {
  72. Reflect reflect = new Reflect(OpenmrsObject.class);
  73. List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
  74. Assert.assertEquals("nonCollectionField", allFields.get(2).getName());
  75. Assert.assertFalse(reflect.isCollectionField(allFields.get(2)));
  76. }
  77. /**
  78. * @see {@link Reflect#isCollection(Class<*>)}
  79. */
  80. @Test
  81. @Verifies(value = "should return true if given fieldClass is Collection class", method = "isCollection(Class<*>)")
  82. public void isCollection_shouldReturnTrueIfGivenFieldClassIsCollectionClass() throws Exception {
  83. Assert.assertTrue(Reflect.isCollection(ArrayList.class));
  84. }
  85. /**
  86. * @see {@link Reflect#isCollection(Object)}
  87. */
  88. @Test
  89. @Verifies(value = "should return false if given object is not a Collection", method = "isCollection(Object)")
  90. public void isCollection_shouldReturnFalseIfGivenObjectIsNotACollection() throws Exception {
  91. Assert.assertFalse(Reflect.isCollection(new NormalClass()));
  92. }
  93. /**
  94. * @see {@link Reflect#isCollection(Object)}
  95. */
  96. @Test
  97. @Verifies(value = "should return true if given object is Collection class", method = "isCollection(Object)")
  98. public void isCollection_shouldReturnTrueIfGivenObjectIsCollectionClass() throws Exception {
  99. Assert.assertTrue(Reflect.isCollection(new ArrayList<Object>()));
  100. }
  101. /**
  102. * @see {@link Reflect#Reflect(Class)}
  103. */
  104. @Test(expected = NullPointerException.class)
  105. @Verifies(value = "should throw exception when null is passed", method = "Reflect(Class)")
  106. public void Reflect_shouldThrowExceptionWhenNullIsPassed() throws Exception {
  107. new Reflect(null);
  108. }
  109. /**
  110. * @see {@link Reflect#getInheritedFields(Class)}
  111. */
  112. @Test
  113. @Verifies(value = "should return only the sub class fields of given parameterized class", method = "getInheritedFields(Class)")
  114. public void getInheritedFields_shouldReturnOnlyTheSubClassFieldsOfGivenParameterizedClass() throws Exception {
  115. Reflect reflect = new Reflect(OpenmrsObject.class);
  116. List<Field> fields = reflect.getInheritedFields(OpenmrsObjectImp.class);
  117. List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
  118. Assert.assertEquals(3, fields.size());
  119. Assert.assertEquals("normalClassField", allFields.get(3).getName());
  120. Assert.assertFalse(fields.contains(allFields.get(3)));
  121. }
  122. /**
  123. * @see {@link Reflect#isCollectionField(Field)}
  124. */
  125. @Test
  126. @Verifies(value = "should return true if given field is Collection and its element type is given parameterized", method = "isCollectionField(Field)")
  127. public void isCollectionField_shouldReturnTrueIfGivenFieldIsCollectionAndItsElementTypeIsGivenParameterized()
  128. throws Exception {
  129. Reflect reflect = new Reflect(OpenmrsObject.class);
  130. List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
  131. Assert.assertEquals("subClassField", allFields.get(1).getName());
  132. Assert.assertTrue(reflect.isCollectionField(allFields.get(1)));
  133. }
  134. /**
  135. * @see {@link Reflect#isCollectionField(Field)}
  136. */
  137. @Test
  138. @Verifies(value = "should return false if given field is not a Collection", method = "isCollectionField(Field)")
  139. public void isCollectionField_shouldReturnFalseIfGivenFieldIsNotACollection() throws Exception {
  140. Assert.assertFalse(Reflect.isCollection(NormalClass.class));
  141. }
  142. /**
  143. * @see {@link Reflect#isCollectionField(Field)}
  144. */
  145. @Test
  146. @Verifies(value = "should return false if given field is Collection and element type is other than given", method = "isCollectionField(Field)")
  147. public void isCollectionField_shouldReturnFalseIfGivenFieldIsCollectionAndElementTypeIsOtherThanGiven() throws Exception {
  148. Reflect reflect = new Reflect(OpenmrsObject.class);
  149. List<Field> allFields = Reflect.getAllFields(OpenmrsObjectImp.class);
  150. Assert.assertEquals("genericCollectionField", allFields.get(0).getName());
  151. Assert.assertFalse(reflect.isCollectionField(allFields.get(0)));
  152. }
  153. /**
  154. * @see {@link Reflect#isSuperClass(Class)}
  155. */
  156. @Test
  157. @Verifies(value = "should return false if given subClass is not accessible from given parameterized class", method = "isSuperClass(Class)")
  158. public void isSuperClass_shouldReturnFalseIfGivenSubClassIsNotAccessibleFromGivenParameterizedClass() throws Exception {
  159. Reflect reflect = new Reflect(OpenmrsObject.class);
  160. Assert.assertFalse(reflect.isSuperClass(new NormalClass()));
  161. }
  162. /**
  163. * @see {@link Reflect#isSuperClass(Class)}
  164. */
  165. @Test
  166. @Verifies(value = "should return true if given subClass is accessible from given parameterized class", method = "isSuperClass(Class)")
  167. public void isSuperClass_shouldReturnTrueIfGivenSubClassIsAccessibleFromGivenParameterizedClass() throws Exception {
  168. Reflect reflect = new Reflect(OpenmrsObject.class);
  169. Assert.assertTrue(reflect.isSuperClass(OpenmrsObjectImp.class));
  170. }
  171. /**
  172. * @see {@link Reflect#isSuperClass(Object)}
  173. */
  174. @Test
  175. @Verifies(value = "should return false if given object is not accessible from given parameterized class", method = "isSuperClass(Object)")
  176. public void isSuperClass_shouldReturnFalseIfGivenObjectIsNotAccessibleFromGivenParameterizedClass() throws Exception {
  177. Reflect reflect = new Reflect(OpenmrsObject.class);
  178. Assert.assertFalse(reflect.isSuperClass(NormalClass.class));
  179. }
  180. /**
  181. * @see {@link Reflect#isSuperClass(Object)}
  182. */
  183. @Test
  184. @Verifies(value = "should return true if given object is accessible from given parameterized class", method = "isSuperClass(Object)")
  185. public void isSuperClass_shouldReturnTrueIfGivenObjectIsAccessibleFromGivenParameterizedClass() throws Exception {
  186. Reflect reflect = new Reflect(OpenmrsObject.class);
  187. Assert.assertTrue(reflect.isSuperClass(new OpenmrsObjectImp()));
  188. }
  189. /**
  190. * @see Reflect#isSuperClass(Type)
  191. * @verifies return true for a generic whose bound is a subclass
  192. */
  193. @Test
  194. public void isSuperClass_shouldReturnTrueForAGenericWhoseBoundIsASubclass() throws Exception {
  195. Reflect reflect = new Reflect(OpenmrsObject.class);
  196. Field field = ReflectionUtils.findField(Visit.class, "attributes");
  197. ParameterizedType setOfAttr = (ParameterizedType) field.getGenericType();
  198. Type genericType = setOfAttr.getActualTypeArguments()[0];
  199. Assert.assertTrue(reflect.isSuperClass(genericType));
  200. }
  201. /**
  202. * @see Reflect#isSuperClass(Type)
  203. * @verifies return false for a generic whose bound is not a subclass
  204. */
  205. @Test
  206. public void isSuperClass_shouldReturnFalseForAGenericWhoseBoundIsNotASubclass() throws Exception {
  207. Reflect reflect = new Reflect(Number.class);
  208. Field field = ReflectionUtils.findField(Visit.class, "attributes");
  209. ParameterizedType setOfAttr = (ParameterizedType) field.getGenericType();
  210. Type genericType = setOfAttr.getActualTypeArguments()[0];
  211. Assert.assertFalse(reflect.isSuperClass(genericType));
  212. }
  213. }
  214. class NormalClass {
  215. private String normalClassField;
  216. public String getNormalClassField() {
  217. return normalClassField;
  218. }
  219. public void setNormalClassField(String normalClassField) {
  220. this.normalClassField = normalClassField;
  221. }
  222. }
  223. class OpenmrsObjectImp extends NormalClass implements OpenmrsObject {
  224. protected Collection<BaseOpenmrsObject> subClassField;
  225. @SuppressWarnings("unused")
  226. private String nonCollectionField;
  227. @SuppressWarnings("unchecked")
  228. Collection genericCollectionField;
  229. public Integer getId() {
  230. return null;
  231. }
  232. public String getUuid() {
  233. return null;
  234. }
  235. public void setId(Integer id) {
  236. }
  237. public void setUuid(String uuid) {
  238. }
  239. }