01: package org.drools.base;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import junit.framework.TestCase;
20:
21: import org.drools.spi.FieldExtractor;
22: import org.drools.util.asm.BeanInherit;
23: import org.drools.util.asm.TestAbstract;
24: import org.drools.util.asm.TestAbstractImpl;
25: import org.drools.util.asm.TestInterface;
26: import org.drools.util.asm.TestInterfaceImpl;
27:
28: public class BaseClassFieldExtractorFactoryTest extends TestCase {
29:
30: public void testIt() throws Exception {
31: FieldExtractor ex = ClassFieldExtractorFactory
32: .getClassFieldExtractor(TestBean.class, "name", Thread
33: .currentThread().getContextClassLoader());
34: assertEquals(0, ex.getIndex());
35: assertEquals("michael", ex.getValue(null, new TestBean()));
36: ex = ClassFieldExtractorFactory.getClassFieldExtractor(
37: TestBean.class, "age", Thread.currentThread()
38: .getContextClassLoader());
39: assertEquals(1, ex.getIndex());
40: assertEquals(42, ((Number) ex.getValue(null, new TestBean()))
41: .intValue());
42:
43: }
44:
45: public void testInterface() throws Exception {
46: final FieldExtractor ex = ClassFieldExtractorCache
47: .getExtractor(TestInterface.class, "something",
48: getClass().getClassLoader());
49: assertEquals(0, ex.getIndex());
50: assertEquals("foo", ex.getValue(null, new TestInterfaceImpl()));
51: }
52:
53: public void testAbstract() throws Exception {
54: final FieldExtractor ex = ClassFieldExtractorCache
55: .getExtractor(TestAbstract.class, "something",
56: getClass().getClassLoader());
57: assertEquals(0, ex.getIndex());
58: assertEquals("foo", ex.getValue(null, new TestAbstractImpl()));
59: }
60:
61: public void testInherited() throws Exception {
62: final FieldExtractor ex = ClassFieldExtractorCache
63: .getExtractor(BeanInherit.class, "text", getClass()
64: .getClassLoader());
65: assertEquals("hola", ex.getValue(null, new BeanInherit()));
66: }
67:
68: public void testSelfReference() throws Exception {
69: final FieldExtractor ex = ClassFieldExtractorCache
70: .getExtractor(BeanInherit.class, "this", getClass()
71: .getClassLoader());
72: final TestBean bean = new TestBean();
73: assertEquals(bean, ex.getValue(null, bean));
74: }
75:
76: }
|