01: package org.drools.facttemplates;
02:
03: import junit.framework.TestCase;
04:
05: import org.drools.rule.Pattern;
06: import org.drools.rule.Declaration;
07: import org.drools.rule.Package;
08: import org.drools.spi.Extractor;
09:
10: public class FactTemplateFieldExtractorTest extends TestCase {
11: public void testExtractor() {
12: final Package pkg = new Package("org.store");
13:
14: final FieldTemplate cheeseName = new FieldTemplateImpl("name",
15: 0, String.class);
16: final FieldTemplate cheesePrice = new FieldTemplateImpl(
17: "price", 1, Integer.class);
18: final FieldTemplate[] fields = new FieldTemplate[] {
19: cheeseName, cheesePrice };
20: final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese",
21: fields);
22:
23: final Extractor extractName = new FactTemplateFieldExtractor(
24: cheese, 0);
25: final Extractor extractPrice = new FactTemplateFieldExtractor(
26: cheese, 1);
27:
28: final Fact stilton = cheese.createFact(10);
29: stilton.setFieldValue("name", "stilton");
30: stilton.setFieldValue("price", new Integer(200));
31:
32: assertEquals("stilton", extractName.getValue(null, stilton));
33:
34: assertEquals(new Integer(200), extractPrice.getValue(null,
35: stilton));
36:
37: assertFalse(extractName.isNullValue(null, stilton));
38:
39: stilton.setFieldValue("name", null);
40:
41: assertTrue(extractName.isNullValue(null, stilton));
42: assertFalse(extractPrice.isNullValue(null, stilton));
43:
44: final Fact brie = cheese.createFact(12);
45: brie.setFieldValue("name", "brie");
46: brie.setFieldValue("price", new Integer(55));
47:
48: assertEquals("brie", extractName.getValue(null, brie));
49:
50: assertEquals(new Integer(55), extractPrice.getValue(null, brie));
51:
52: assertFalse(extractName.isNullValue(null, brie));
53:
54: brie.setFieldValue("name", null);
55:
56: assertTrue(extractName.isNullValue(null, brie));
57: assertFalse(extractPrice.isNullValue(null, stilton));
58: }
59:
60: public void testDeclaration() {
61: final Package pkg = new Package("org.store");
62:
63: final FieldTemplate cheeseName = new FieldTemplateImpl("name",
64: 0, String.class);
65: final FieldTemplate cheesePrice = new FieldTemplateImpl(
66: "price", 1, Integer.class);
67: final FieldTemplate[] fields = new FieldTemplate[] {
68: cheeseName, cheesePrice };
69: final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese",
70: fields);
71:
72: final Extractor extractName = new FactTemplateFieldExtractor(
73: cheese, 0);
74:
75: final Pattern pattern = new Pattern(0,
76: new FactTemplateObjectType(cheese));
77:
78: final Declaration declaration = new Declaration("typeOfCheese",
79: extractName, pattern);
80:
81: final Fact brie = cheese.createFact(12);
82: brie.setFieldValue("name", "brie");
83: brie.setFieldValue("price", new Integer(55));
84:
85: // Check we can extract Declarations correctly
86: assertEquals("brie", declaration.getValue(null, brie));
87: }
88: }
|