PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/machinelearning/5.0/drools-core/src/test/java/org/drools/util/asm/ClassFieldInspectorTest.java

https://github.com/etirelli/droolsjbpm-contributed-experiments
Java | 277 lines | 198 code | 50 blank | 29 comment | 4 complexity | 3913f3694e85d48a33074043012283cd MD5 | raw file
  1. package org.drools.util.asm;
  2. /*
  3. * Copyright 2005 JBoss Inc
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.io.IOException;
  18. import java.lang.reflect.Method;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import junit.framework.Assert;
  22. import junit.framework.TestCase;
  23. public class ClassFieldInspectorTest extends TestCase {
  24. public void testIt() throws Exception {
  25. final ClassFieldInspector ext = new ClassFieldInspector( Person.class );
  26. assertEquals( 7,
  27. ext.getFieldNames().size() );
  28. assertEquals( "getAge" ,
  29. ext.getGetterMethods().get( "age" ).getName() );
  30. assertEquals( "isHappy" ,
  31. ext.getGetterMethods().get( "happy" ).getName() );
  32. assertEquals( "getName" ,
  33. ext.getGetterMethods().get( "name" ).getName() );
  34. final Map<String, Integer> names = ext.getFieldNames();
  35. assertNotNull( names );
  36. assertEquals( 7,
  37. names.size() );
  38. assertNull( names.get( "nAme" ) );
  39. }
  40. public void testInterface() throws Exception {
  41. final ClassFieldInspector ext = new ClassFieldInspector( TestInterface.class );
  42. assertEquals( 2,
  43. ext.getFieldNames().size() );
  44. assertEquals( "getSomething" ,
  45. ext.getGetterMethods().get( "something" ).getName() );
  46. assertEquals( "getAnother" ,
  47. ext.getGetterMethods().get( "another" ).getName() );
  48. final Map<String, Integer> names = ext.getFieldNames();
  49. assertNotNull( names );
  50. assertEquals( 2,
  51. names.size() );
  52. }
  53. public void testAbstract() throws Exception {
  54. final ClassFieldInspector ext = new ClassFieldInspector( TestAbstract.class );
  55. assertEquals( 5,
  56. ext.getFieldNames().size() );
  57. assertEquals( "getSomething" ,
  58. ext.getGetterMethods().get( "something" ).getName() );
  59. assertEquals( "getAnother" ,
  60. ext.getGetterMethods().get( "another" ).getName() );
  61. final Map<String, Integer> names = ext.getFieldNames();
  62. assertNotNull( names );
  63. assertEquals( 5,
  64. names.size() );
  65. }
  66. public void testInheritedFields() throws Exception {
  67. ClassFieldInspector ext = new ClassFieldInspector( BeanInherit.class );
  68. assertEquals( 5,
  69. ext.getFieldNames().size() );
  70. ext = new ClassFieldInspector( InterfaceChildImpl.class );
  71. assertEquals( 8,
  72. ext.getFieldNames().size() );
  73. // test inheritence from abstract class
  74. assertNotNull( ext.getFieldNames().get( "HTML" ) );
  75. // check normal field on child class
  76. assertNotNull( ext.getFieldNames().get( "baz" ) );
  77. // test inheritence from an interface
  78. assertNotNull( ext.getFieldNames().get( "URI" ) );
  79. }
  80. public void testIntefaceInheritance() throws Exception {
  81. final ClassFieldInspector ext = new ClassFieldInspector( InterfaceChild.class );
  82. final Map fields = ext.getFieldNames();
  83. assertTrue( fields.containsKey( "foo" ) );
  84. assertTrue( fields.containsKey( "bar" ) );
  85. assertTrue( fields.containsKey( "baz" ) );
  86. assertTrue( fields.containsKey( "URI" ) );
  87. }
  88. public void testFieldIndexCalculation() {
  89. try {
  90. final ClassFieldInspector ext = new ClassFieldInspector( SubPerson.class );
  91. final Map map = ext.getFieldNames();
  92. final String[] fields = new String[map.size()];
  93. for ( final Iterator i = map.entrySet().iterator(); i.hasNext(); ) {
  94. final Map.Entry entry = (Map.Entry) i.next();
  95. final String fieldName = (String) entry.getKey();
  96. final int fieldIndex = ((Integer) entry.getValue()).intValue();
  97. if ( fields[fieldIndex] == null ) {
  98. fields[fieldIndex] = fieldName;
  99. } else {
  100. Assert.fail( "Duplicate index found for 2 fields: index[" + fieldIndex + "] = [" + fields[fieldIndex] + "] and [" + fieldName + "]" );
  101. }
  102. }
  103. } catch ( final IOException e ) {
  104. e.printStackTrace();
  105. Assert.fail( "Unexpected exception thrown" );
  106. }
  107. }
  108. public void testGetReturnTypes() throws Exception {
  109. final ClassFieldInspector ext = new ClassFieldInspector( Person.class );
  110. final Map types = ext.getFieldTypes();
  111. assertNotNull( types );
  112. assertEquals( boolean.class,
  113. types.get( "happy" ) );
  114. assertEquals( int.class,
  115. types.get( "age" ) );
  116. assertEquals( String.class,
  117. types.get( "name" ) );
  118. }
  119. public void testGetMethodForField() throws Exception {
  120. final ClassFieldInspector ext = new ClassFieldInspector( Person.class );
  121. final Map methods = ext.getGetterMethods();
  122. assertNotNull( methods );
  123. assertEquals( "isHappy",
  124. ((Method) methods.get( "happy" )).getName() );
  125. assertEquals( "getName",
  126. ((Method) methods.get( "name" )).getName() );
  127. // test case sensitive
  128. assertNull( methods.get( "nAme" ) );
  129. assertEquals( "getAge",
  130. ((Method) methods.get( "age" )).getName() );
  131. }
  132. public void testNonGetter() throws Exception {
  133. final ClassFieldInspector ext = new ClassFieldInspector( NonGetter.class );
  134. final Map methods = ext.getGetterMethods();
  135. assertEquals( "getFoo",
  136. ((Method) methods.get( "foo" )).getName() );
  137. assertEquals( 5,
  138. methods.size() );
  139. assertTrue( ext.getFieldNames().containsKey( "foo" ) );
  140. assertTrue( ext.getFieldNames().containsKey( "baz" ) );
  141. assertEquals( String.class,
  142. ext.getFieldTypes().get( "foo" ) );
  143. }
  144. public void testWierdCapsForField() throws Exception {
  145. final ClassFieldInspector ext = new ClassFieldInspector( Person.class );
  146. final Map methods = ext.getGetterMethods();
  147. assertEquals( "getURI",
  148. ((Method) methods.get( "URI" )).getName() );
  149. assertEquals( 7,
  150. methods.size() );
  151. }
  152. static class NonGetter {
  153. public int foo() {
  154. return 42;
  155. }
  156. public String getFoo() {
  157. return "foo";
  158. }
  159. public String baz() {
  160. return "";
  161. }
  162. public void bas() {
  163. }
  164. }
  165. static class Person {
  166. public static String aStaticString;
  167. private boolean happy;
  168. private String name;
  169. private int age;
  170. private String URI;
  171. static {
  172. aStaticString = "A static String";
  173. }
  174. public int getAge() {
  175. return this.age;
  176. }
  177. public void setAge(final int age) {
  178. this.age = age;
  179. }
  180. public boolean isHappy() {
  181. return this.happy;
  182. }
  183. public void setHappy(final boolean happy) {
  184. this.happy = happy;
  185. }
  186. public String getName() {
  187. return this.name;
  188. }
  189. public void setName(final String name) {
  190. this.name = name;
  191. }
  192. //ignore this as it returns void type
  193. public void getNotAGetter() {
  194. return;
  195. }
  196. //ignore this as private
  197. private boolean isBogus() {
  198. return false;
  199. }
  200. //this will not show up as it is a getter that takes an argument
  201. public String getAlsoBad(final String s) {
  202. return "ignored";
  203. }
  204. //this should show up, as its a getter, but all CAPS
  205. public String getURI() {
  206. return this.URI;
  207. }
  208. public void setURI(final String URI) {
  209. this.URI = URI;
  210. }
  211. }
  212. static class SubPerson {
  213. private int childField;
  214. /**
  215. * @return the childField
  216. */
  217. public int getChildField() {
  218. return this.childField;
  219. }
  220. /**
  221. * @param childField the childField to set
  222. */
  223. public void setChildField(final int childField) {
  224. this.childField = childField;
  225. }
  226. }
  227. }