use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.
the class InstancesHashcodedTest method testInstanceHashcodes.
@Test
public void testInstanceHashcodes() {
ClassDefinition cd = new ClassDefinition("br.com.auster.TestClass2", null, new String[] {});
cd.addField(new FieldDefinition("cutDate", "java.util.Date", true));
cd.addField(new FieldDefinition("dueDate", "java.util.Date", true));
cd.addField(new FieldDefinition("issueDate", "java.util.Date", false));
cd.addField(new FieldDefinition("cycleCode", "java.lang.String", false));
ClassBuilder cb = new TraitClassBuilderFactory().getBeanClassBuilder();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2006);
cal.set(Calendar.MONTH, Calendar.JUNE);
cal.set(Calendar.DAY_OF_MONTH, 01);
Date cut = cal.getTime();
cal.set(Calendar.MONTH, Calendar.JULY);
cal.set(Calendar.DAY_OF_MONTH, 13);
Date d1 = cal.getTime();
cal.set(Calendar.DAY_OF_MONTH, 15);
Date d2 = cal.getTime();
try {
Class klass = build(cb, cd);
Object o1 = klass.newInstance();
cd.getField("cutDate").getFieldAccessor().setValue(o1, cut);
cd.getField("dueDate").getFieldAccessor().setValue(o1, d1);
Object o2 = klass.newInstance();
cd.getField("cutDate").getFieldAccessor().setValue(o2, cut);
cd.getField("dueDate").getFieldAccessor().setValue(o2, d2);
// System.out.println(o1);
// System.out.println(o1.hashCode());
// System.out.println(o2);
// System.out.println(o2.hashCode());
//
// System.out.println(o1.equals(o2));
} catch (Exception e) {
// e.printStackTrace();
}
}
use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.
the class ClassBuilderTest method testConstructorWithFields.
@Test
public void testConstructorWithFields() {
try {
ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder();
ClassDefinition classDef = new ClassDefinition("org.drools.TestClass5", null, new String[] {});
String[] types = new String[] { "byte", "short", "int", "long", "float", "double", "char", "java.lang.String", "boolean" };
FieldDefinition[] fields = new FieldDefinition[types.length];
for (int i = 0; i < types.length; i++) {
String attrName = types[i].substring(types[i].lastIndexOf('.') + 1);
attrName = attrName.substring(0, 1).toLowerCase() + attrName.substring(1) + "Attr";
fields[i] = new // attr name
FieldDefinition(// attr name
attrName, // attr type
types[i], // half of them are key
i % 2 == 0);
classDef.addField(fields[i]);
}
Class clazz = build(builder, classDef);
for (FieldDefinition field : fields) {
field.setReadWriteAccessor(store.getAccessor(clazz, field.getName()));
}
Constructor<?>[] cons = clazz.getConstructors();
assertEquals(3, cons.length);
for (Constructor<?> c : cons) {
Class<?>[] ptypes = c.getParameterTypes();
if (ptypes.length == 0) {
// default constructor
} else if (ptypes.length == fields.length) {
// constructor with fields
for (int i = 0; i < ptypes.length; i++) {
if (!ptypes[i].equals(fields[i].getType())) {
fail("Wrong parameter in constructor. index=" + i + " expected=" + fields[i].getType() + " found=" + ptypes[i]);
}
}
// test actual invocation
Object instance = c.newInstance((byte) 1, (short) 2, 3, 4l, 5.0f, 6.0d, 'a', "xyz", true);
assertEquals((byte) 1, fields[0].getValue(instance));
assertEquals((short) 2, fields[1].getValue(instance));
assertEquals(3, fields[2].getValue(instance));
assertEquals(4l, fields[3].getValue(instance));
assertEquals(5.0f, fields[4].getValue(instance));
assertEquals(6.0d, fields[5].getValue(instance));
assertEquals('a', fields[6].getValue(instance));
assertEquals("xyz", fields[7].getValue(instance));
assertEquals(true, fields[8].getValue(instance));
} else if (ptypes.length == (fields.length / 2 + 1)) {
// as defined in the beginning of the test
// constructor with key fields
int i = 0;
for (FieldDefinition field : fields) {
if (field.isKey() && !ptypes[i++].equals(field.getType())) {
fail("Wrong parameter in constructor. index=" + i + " expected=" + field.getType() + " found=" + ptypes[i]);
}
}
// test actual invocation
Object instance = c.newInstance((byte) 1, 3, 5.0f, 'a', true);
assertEquals((byte) 1, fields[0].getValue(instance));
assertEquals(3, fields[2].getValue(instance));
assertEquals(5.0f, fields[4].getValue(instance));
assertEquals('a', fields[6].getValue(instance));
assertEquals(true, fields[8].getValue(instance));
} else {
fail("Unexpected constructor: " + c.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected Exception: " + e.getMessage());
}
}
use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.
the class ClassBuilderTest method testHashCode.
@Test
public void testHashCode() {
try {
ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder();
ClassDefinition classDef = new ClassDefinition("org.drools.TestClass3", null, new String[] {});
FieldDefinition intDef = new FieldDefinition("intAttr", "int", true);
FieldDefinition strDef = new FieldDefinition("stringAttr", "java.lang.String", false);
classDef.addField(intDef);
classDef.addField(strDef);
Class clazz = build(builder, classDef);
intDef.setReadWriteAccessor(store.getAccessor(clazz, intDef.getName()));
strDef.setReadWriteAccessor(store.getAccessor(clazz, strDef.getName()));
Object x = clazz.newInstance();
intDef.setValue(x, new Integer(10));
strDef.setValue(x, "abc");
assertEquals("Wrong hashcode calculation", 31 + 10, x.hashCode());
assertEquals("Wrong hashcode calculation", x.hashCode(), x.hashCode());
} catch (Exception e) {
e.printStackTrace();
fail("Exception not expected");
}
}
use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.
the class ClassBuilderTest method testEquals.
@Test
public void testEquals() {
try {
ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder();
ClassDefinition classDef = new ClassDefinition("org.drools.TestClass2", null, new String[] {});
FieldDefinition long1Def = new FieldDefinition("longAttr1", "long", true);
FieldDefinition long2Def = new FieldDefinition("longAttr2", "long", true);
FieldDefinition doubleDef = new FieldDefinition("doubleAttr", "double", true);
FieldDefinition intDef = new FieldDefinition("intAttr", "int", true);
FieldDefinition strDef = new FieldDefinition("stringAttr", "java.lang.String", true);
FieldDefinition dateDef = new FieldDefinition("dateAttr", "java.util.Date", true);
FieldDefinition str2Def = new FieldDefinition("stringAttr2", "java.lang.String");
classDef.addField(long1Def);
classDef.addField(long2Def);
classDef.addField(doubleDef);
classDef.addField(intDef);
classDef.addField(strDef);
classDef.addField(dateDef);
classDef.addField(str2Def);
Class clazz = build(builder, classDef);
long1Def.setReadWriteAccessor(store.getAccessor(clazz, long1Def.getName()));
long2Def.setReadWriteAccessor(store.getAccessor(clazz, long2Def.getName()));
doubleDef.setReadWriteAccessor(store.getAccessor(clazz, doubleDef.getName()));
intDef.setReadWriteAccessor(store.getAccessor(clazz, intDef.getName()));
strDef.setReadWriteAccessor(store.getAccessor(clazz, strDef.getName()));
dateDef.setReadWriteAccessor(store.getAccessor(clazz, dateDef.getName()));
str2Def.setReadWriteAccessor(store.getAccessor(clazz, str2Def.getName()));
Object x = clazz.newInstance();
Object y = clazz.newInstance();
long1Def.setValue(x, new Long(20));
long2Def.setValue(x, new Long(30));
doubleDef.setValue(x, new Double(50.0));
intDef.setValue(x, new Integer(10));
strDef.setValue(x, "abc");
dateDef.setValue(x, new Date(1000));
str2Def.setValue(x, "instance1");
long1Def.setValue(y, new Long(20));
long2Def.setValue(y, new Long(30));
doubleDef.setValue(y, new Double(50.0));
intDef.setValue(y, new Integer(10));
strDef.setValue(y, "abc");
dateDef.setValue(y, new Date(1000));
str2Def.setValue(y, "instance2");
Object o = new Object();
assertTrue(x.equals(x));
assertFalse(x.equals(null));
assertFalse(x.equals(o));
assertTrue(x.equals(y));
intDef.setValue(y, new Integer(1));
assertFalse(x.equals(y));
intDef.setValue(y, new Integer(10));
strDef.setValue(y, "xyz");
assertFalse(x.equals(y));
strDef.setValue(y, null);
assertFalse(x.equals(y));
strDef.setValue(y, "abc");
dateDef.setValue(y, new Date(1));
assertFalse(x.equals(y));
dateDef.setValue(y, null);
assertFalse(x.equals(y));
} catch (Exception e) {
e.printStackTrace();
fail("Exception not expected");
}
}
Aggregations