001: package org.drools.util.asm;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.lang.reflect.Method;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import junit.framework.Assert;
025: import junit.framework.TestCase;
026:
027: public class ClassFieldInspectorTest extends TestCase {
028:
029: public void testIt() throws Exception {
030: final ClassFieldInspector ext = new ClassFieldInspector(
031: Person.class);
032: assertEquals(7, ext.getPropertyGetters().size());
033: assertEquals("getAge", ((Method) ext.getPropertyGetters()
034: .get(0)).getName());
035: assertEquals("isHappy", ((Method) ext.getPropertyGetters().get(
036: 1)).getName());
037: assertEquals("getName", ((Method) ext.getPropertyGetters().get(
038: 2)).getName());
039:
040: final Map names = ext.getFieldNames();
041: assertNotNull(names);
042: assertEquals(7, names.size());
043: assertEquals(0, ((Integer) names.get("age")).intValue());
044: assertEquals(1, ((Integer) names.get("happy")).intValue());
045: assertEquals(2, ((Integer) names.get("name")).intValue());
046: assertNull(names.get("nAme"));
047:
048: }
049:
050: public void testInterface() throws Exception {
051: final ClassFieldInspector ext = new ClassFieldInspector(
052: TestInterface.class);
053: assertEquals(2, ext.getPropertyGetters().size());
054: assertEquals("getSomething", ((Method) ext.getPropertyGetters()
055: .get(0)).getName());
056: assertEquals("getAnother", ((Method) ext.getPropertyGetters()
057: .get(1)).getName());
058:
059: final Map names = ext.getFieldNames();
060: assertNotNull(names);
061: assertEquals(2, names.size());
062: assertEquals(0, ((Integer) names.get("something")).intValue());
063: assertEquals(1, ((Integer) names.get("another")).intValue());
064:
065: }
066:
067: public void testAbstract() throws Exception {
068: final ClassFieldInspector ext = new ClassFieldInspector(
069: TestAbstract.class);
070: assertEquals(5, ext.getPropertyGetters().size());
071: assertEquals("getSomething", ((Method) ext.getPropertyGetters()
072: .get(0)).getName());
073: assertEquals("getAnother", ((Method) ext.getPropertyGetters()
074: .get(1)).getName());
075:
076: final Map names = ext.getFieldNames();
077: assertNotNull(names);
078: assertEquals(5, names.size());
079: assertEquals(0, ((Integer) names.get("something")).intValue());
080: assertEquals(1, ((Integer) names.get("another")).intValue());
081:
082: }
083:
084: public void testInheritedFields() throws Exception {
085: ClassFieldInspector ext = new ClassFieldInspector(
086: BeanInherit.class);
087: assertEquals(5, ext.getPropertyGetters().size());
088:
089: ext = new ClassFieldInspector(InterfaceChildImpl.class);
090: assertEquals(8, ext.getPropertyGetters().size());
091: // test inheritence from abstract class
092: assertEquals(4, ((Integer) ext.getFieldNames().get("HTML"))
093: .intValue());
094:
095: // check normal field on child class
096: assertEquals(1, ((Integer) ext.getFieldNames().get("baz"))
097: .intValue());
098:
099: // test inheritence from an interface
100: assertEquals(3, ((Integer) ext.getFieldNames().get("URI"))
101: .intValue());
102: }
103:
104: public void testIntefaceInheritance() throws Exception {
105: final ClassFieldInspector ext = new ClassFieldInspector(
106: InterfaceChild.class);
107: final Map fields = ext.getFieldNames();
108: assertTrue(fields.containsKey("foo"));
109: assertTrue(fields.containsKey("bar"));
110: assertTrue(fields.containsKey("baz"));
111: assertTrue(fields.containsKey("URI"));
112: }
113:
114: public void testFieldIndexCalculation() {
115: try {
116: final ClassFieldInspector ext = new ClassFieldInspector(
117: SubPerson.class);
118: final Map map = ext.getFieldNames();
119: final String[] fields = new String[map.size()];
120: for (final Iterator i = map.entrySet().iterator(); i
121: .hasNext();) {
122: final Map.Entry entry = (Map.Entry) i.next();
123: final String fieldName = (String) entry.getKey();
124: final int fieldIndex = ((Integer) entry.getValue())
125: .intValue();
126: if (fields[fieldIndex] == null) {
127: fields[fieldIndex] = fieldName;
128: } else {
129: Assert
130: .fail("Duplicate index found for 2 fields: index["
131: + fieldIndex
132: + "] = ["
133: + fields[fieldIndex]
134: + "] and ["
135: + fieldName + "]");
136: }
137: }
138: } catch (final IOException e) {
139: e.printStackTrace();
140: Assert.fail("Unexpected exception thrown");
141: }
142: }
143:
144: public void testGetReturnTypes() throws Exception {
145: final ClassFieldInspector ext = new ClassFieldInspector(
146: Person.class);
147: final Map types = ext.getFieldTypes();
148: assertNotNull(types);
149: assertEquals(boolean.class, types.get("happy"));
150: assertEquals(int.class, types.get("age"));
151: assertEquals(String.class, types.get("name"));
152: }
153:
154: public void testGetMethodForField() throws Exception {
155: final ClassFieldInspector ext = new ClassFieldInspector(
156: Person.class);
157: final Map methods = ext.getGetterMethods();
158: assertNotNull(methods);
159: assertEquals("isHappy", ((Method) methods.get("happy"))
160: .getName());
161: assertEquals("getName", ((Method) methods.get("name"))
162: .getName());
163: // test case sensitive
164: assertNull(methods.get("nAme"));
165: assertEquals("getAge", ((Method) methods.get("age")).getName());
166:
167: }
168:
169: public void testNonGetter() throws Exception {
170: final ClassFieldInspector ext = new ClassFieldInspector(
171: NonGetter.class);
172: final Map methods = ext.getGetterMethods();
173: assertEquals("getFoo", ((Method) methods.get("foo")).getName());
174: assertEquals(5, methods.size());
175: assertTrue(ext.getFieldNames().containsKey("foo"));
176: assertTrue(ext.getFieldNames().containsKey("baz"));
177: assertEquals(String.class, ext.getFieldTypes().get("foo"));
178:
179: }
180:
181: public void testWierdCapsForField() throws Exception {
182: final ClassFieldInspector ext = new ClassFieldInspector(
183: Person.class);
184: final Map methods = ext.getGetterMethods();
185: assertEquals("getURI", ((Method) methods.get("URI")).getName());
186: assertEquals(7, methods.size());
187: }
188:
189: static class NonGetter {
190:
191: public int foo() {
192: return 42;
193: }
194:
195: public String getFoo() {
196: return "foo";
197: }
198:
199: public String baz() {
200: return "";
201: }
202:
203: public void bas() {
204:
205: }
206: }
207:
208: static class Person {
209: public static String aStaticString;
210: private boolean happy;
211: private String name;
212: private int age;
213: private String URI;
214:
215: static {
216: aStaticString = "A static String";
217: }
218:
219: public int getAge() {
220: return this .age;
221: }
222:
223: public void setAge(final int age) {
224: this .age = age;
225: }
226:
227: public boolean isHappy() {
228: return this .happy;
229: }
230:
231: public void setHappy(final boolean happy) {
232: this .happy = happy;
233: }
234:
235: public String getName() {
236: return this .name;
237: }
238:
239: public void setName(final String name) {
240: this .name = name;
241: }
242:
243: //ignore this as it returns void type
244: public void getNotAGetter() {
245: return;
246: }
247:
248: //ignore this as private
249: private boolean isBogus() {
250: return false;
251: }
252:
253: //this will not show up as it is a getter that takes an argument
254: public String getAlsoBad(final String s) {
255: return "ignored";
256: }
257:
258: //this should show up, as its a getter, but all CAPS
259: public String getURI() {
260: return this .URI;
261: }
262:
263: public void setURI(final String URI) {
264: this .URI = URI;
265: }
266: }
267:
268: static class SubPerson {
269: private int childField;
270:
271: /**
272: * @return the childField
273: */
274: public int getChildField() {
275: return this .childField;
276: }
277:
278: /**
279: * @param childField the childField to set
280: */
281: public void setChildField(final int childField) {
282: this.childField = childField;
283: }
284:
285: }
286:
287: }
|