001: package org.drools.base;
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 junit.framework.TestCase;
020:
021: import org.drools.RuntimeDroolsException;
022: import org.drools.util.asm.BeanInherit;
023: import org.drools.util.asm.InterfaceChild;
024: import org.drools.util.asm.TestAbstract;
025: import org.drools.util.asm.TestAbstractImpl;
026: import org.drools.util.asm.TestBean;
027: import org.drools.util.asm.TestInterface;
028: import org.drools.util.asm.TestInterfaceImpl;
029:
030: public class ClassFieldExtractorTest extends TestCase {
031:
032: public void testBasic() throws Exception {
033: final Object[] objArray = new Object[1];
034:
035: final TestBean obj = new TestBean();
036: obj.setBlah(false);
037: obj.setSomething("no");
038: obj.setObjArray(objArray);
039:
040: final ClassFieldExtractor ext = ClassFieldExtractorCache
041: .getExtractor(TestBean.class, "blah", getClass()
042: .getClassLoader());
043: assertEquals(false, ((Boolean) ext.getValue(null, obj))
044: .booleanValue());
045:
046: final ClassFieldExtractor ext2 = ClassFieldExtractorCache
047: .getExtractor(TestBean.class, "fooBar", getClass()
048: .getClassLoader());
049: assertEquals("fooBar", ext2.getValue(null, obj));
050:
051: final ClassFieldExtractor ext3 = ClassFieldExtractorCache
052: .getExtractor(TestBean.class, "objArray", getClass()
053: .getClassLoader());
054: assertEquals(objArray, ext3.getValue(null, obj));
055:
056: }
057:
058: public void testInterface() throws Exception {
059:
060: final TestInterface obj = new TestInterfaceImpl();
061: final ClassFieldExtractor ext = ClassFieldExtractorCache
062: .getExtractor(TestInterface.class, "something",
063: getClass().getClassLoader());
064:
065: assertEquals("foo", (String) ext.getValue(null, obj));
066:
067: }
068:
069: public void testAbstract() throws Exception {
070:
071: final ClassFieldExtractor ext = ClassFieldExtractorCache
072: .getExtractor(TestAbstract.class, "something",
073: getClass().getClassLoader());
074: final TestAbstract obj = new TestAbstractImpl();
075: assertEquals("foo", (String) ext.getValue(null, obj));
076:
077: }
078:
079: public void testInherited() throws Exception {
080: final ClassFieldExtractor ext = ClassFieldExtractorCache
081: .getExtractor(BeanInherit.class, "text", getClass()
082: .getClassLoader());
083: final BeanInherit obj = new BeanInherit();
084: assertEquals("hola", (String) ext.getValue(null, obj));
085:
086: }
087:
088: public void testMultipleInterfaces() throws Exception {
089: final ConcreteChild obj = new ConcreteChild();
090: final ClassFieldExtractor ext = ClassFieldExtractorCache
091: .getExtractor(InterfaceChild.class, "foo", getClass()
092: .getClassLoader());
093: assertEquals(42, ((Number) ext.getValue(null, obj)).intValue());
094: }
095:
096: public void testLong() throws Exception {
097: final ClassFieldExtractor ext = ClassFieldExtractorCache
098: .getExtractor(TestBean.class, "longField", getClass()
099: .getClassLoader());
100: final TestBean bean = new TestBean();
101: assertEquals(424242, ((Number) ext.getValue(null, bean))
102: .longValue());
103: }
104:
105: public void testNonExistentField() throws Exception {
106: final Object[] objArray = new Object[1];
107:
108: final TestBean obj = new TestBean();
109: obj.setBlah(false);
110: obj.setSomething("no");
111: obj.setObjArray(objArray);
112:
113: try {
114: final ClassFieldExtractor ext = ClassFieldExtractorCache
115: .getExtractor(TestBean.class, "xyz", getClass()
116: .getClassLoader());
117: fail("A RuntimeDroolsException should have been raised");
118: } catch (RuntimeDroolsException e) {
119: e.printStackTrace();
120: // everything is fine, since field does not exist
121: } catch (Exception e) {
122: fail("A RuntimeDroolsException should have been raised");
123: }
124:
125: }
126:
127: }
|