001: package org.drools.facttemplates;
002:
003: import junit.framework.TestCase;
004:
005: import org.drools.rule.Package;
006:
007: public class FactTemplateTest extends TestCase {
008: public void testFieldsAndGetters() {
009: final Package pkg = new Package("org.store");
010: final FieldTemplate cheeseName = new FieldTemplateImpl("name",
011: 0, String.class);
012: final FieldTemplate cheesePrice = new FieldTemplateImpl(
013: "price", 1, Integer.class);
014: final FieldTemplate[] fields = new FieldTemplate[] {
015: cheeseName, cheesePrice };
016: final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese",
017: fields);
018:
019: assertEquals("org.store", cheese.getPackage().getName());
020: assertEquals("Cheese", cheese.getName());
021:
022: assertEquals(2, cheese.getNumberOfFields());
023:
024: assertSame(fields, cheese.getAllFieldTemplates());
025:
026: assertSame(cheeseName, cheese.getFieldTemplate(0));
027: assertSame(cheesePrice, cheese.getFieldTemplate(1));
028:
029: assertSame(cheeseName, cheese.getFieldTemplate("name"));
030: assertSame(cheesePrice, cheese.getFieldTemplate("price"));
031:
032: assertEquals(0, cheese.getFieldTemplateIndex("name"));
033: assertEquals(1, cheese.getFieldTemplateIndex("price"));
034: }
035:
036: public void testEqualsAndHashCode() {
037: final Package pkg = new Package("org.store");
038:
039: // Create cheese1 with name and price fields
040: final FieldTemplate cheeseName = new FieldTemplateImpl("name",
041: 0, String.class);
042: final FieldTemplate cheesePrice = new FieldTemplateImpl(
043: "price", 1, Integer.class);
044: final FieldTemplate[] fields1 = new FieldTemplate[] {
045: cheeseName, cheesePrice };
046: final FactTemplate cheese1 = new FactTemplateImpl(pkg,
047: "Cheese", fields1);
048:
049: // Create cheese2 with type and price fields
050: final FieldTemplate cheeseType = new FieldTemplateImpl("type",
051: 0, String.class);
052: final FieldTemplate[] fields2 = new FieldTemplate[] {
053: cheeseType, cheesePrice };
054: final FactTemplate cheese2 = new FactTemplateImpl(pkg,
055: "Cheese", fields2);
056:
057: assertNotSame(cheese1, cheese2);
058:
059: assertFalse(cheese1.equals(cheese2));
060:
061: assertFalse(cheese1.hashCode() == cheese2.hashCode());
062:
063: // create cheese3 with name and price fields, using new instances
064: final FieldTemplate cheeseName2 = new FieldTemplateImpl("name",
065: 0, String.class);
066: final FieldTemplate cheesePrice2 = new FieldTemplateImpl(
067: "price", 1, Integer.class);
068: final FieldTemplate[] fields3 = new FieldTemplate[] {
069: cheeseName2, cheesePrice2 };
070: final FactTemplate cheese3 = new FactTemplateImpl(pkg,
071: "Cheese", fields3);
072:
073: assertNotSame(cheese1, cheese3);
074: assertNotSame(cheese1.getAllFieldTemplates(), cheese3
075: .getAllFieldTemplates());
076: assertEquals(cheese1, cheese3);
077: assertEquals(cheese1.hashCode(), cheese3.hashCode());
078: }
079:
080: public void testFacts() {
081: final Package pkg = new Package("org.store");
082: final FieldTemplate cheeseName = new FieldTemplateImpl("name",
083: 0, String.class);
084: final FieldTemplate cheesePrice = new FieldTemplateImpl(
085: "price", 1, Integer.class);
086: final FieldTemplate[] fields1 = new FieldTemplate[] {
087: cheeseName, cheesePrice };
088: final FactTemplate cheese1 = new FactTemplateImpl(pkg,
089: "Cheese", fields1);
090:
091: final Fact stilton1 = cheese1.createFact(10);
092: stilton1.setFieldValue("name", "stilton");
093: stilton1.setFieldValue("price", new Integer(200));
094:
095: final Fact stilton2 = cheese1.createFact(11);
096: stilton2.setFieldValue(0, "stilton");
097: stilton2.setFieldValue(1, new Integer(200));
098:
099: assertEquals(stilton1, stilton2);
100: assertEquals(stilton1.hashCode(), stilton2.hashCode());
101:
102: final Fact brie = cheese1.createFact(12);
103: brie.setFieldValue("name", "brie");
104: brie.setFieldValue("price", new Integer(55));
105:
106: assertFalse(stilton1.equals(brie));
107: assertFalse(stilton1.hashCode() == brie.hashCode());
108:
109: }
110: }
|