001: package org.drools.util;
002:
003: import junit.framework.TestCase;
004:
005: import org.drools.Cheese;
006: import org.drools.base.ClassFieldExtractor;
007: import org.drools.base.ClassFieldExtractorCache;
008: import org.drools.base.evaluators.Operator;
009: import org.drools.base.evaluators.StringFactory;
010: import org.drools.common.DefaultFactHandle;
011: import org.drools.common.InternalFactHandle;
012: import org.drools.util.AbstractHashTable.FactEntryImpl;
013: import org.drools.util.AbstractHashTable.FieldIndex;
014: import org.drools.util.AbstractHashTable.SingleIndex;
015: import org.drools.util.FactHandleIndexHashTable.FieldIndexEntry;
016:
017: public class FieldIndexEntryTest extends TestCase {
018:
019: public void testSingleEntry() {
020: final ClassFieldExtractor extractor = ClassFieldExtractorCache
021: .getExtractor(Cheese.class, "type", getClass()
022: .getClassLoader());
023:
024: final FieldIndex fieldIndex = new FieldIndex(extractor, null,
025: StringFactory.getInstance()
026: .getEvaluator(Operator.EQUAL));
027: final SingleIndex singleIndex = new SingleIndex(
028: new FieldIndex[] { fieldIndex }, 1);
029:
030: final FieldIndexEntry index = new FieldIndexEntry(singleIndex,
031: "stilton".hashCode());
032:
033: // Test initial construction
034: assertNull(index.getFirst());
035: assertEquals("stilton".hashCode(), index.hashCode());
036:
037: final Cheese stilton1 = new Cheese("stilton", 35);
038: final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
039:
040: // test add
041: index.add(h1);
042:
043: final FactEntryImpl entry1 = index.getFirst();
044: assertSame(h1, entry1.getFactHandle());
045: assertNull(entry1.getNext());
046: assertSame(entry1, index.get(h1));
047:
048: // test get
049: final FactEntryImpl entry2 = index.get(h1);
050: assertSame(entry1, entry2);
051:
052: // test remove
053: index.remove(h1);
054: assertNull(index.getFirst());
055: }
056:
057: public void testTwoEntries() {
058: final ClassFieldExtractor extractor = ClassFieldExtractorCache
059: .getExtractor(Cheese.class, "type", getClass()
060: .getClassLoader());
061: final FieldIndex fieldIndex = new FieldIndex(extractor, null,
062: StringFactory.getInstance()
063: .getEvaluator(Operator.EQUAL));
064: final SingleIndex singleIndex = new SingleIndex(
065: new FieldIndex[] { fieldIndex }, 1);
066:
067: final FieldIndexEntry index = new FieldIndexEntry(singleIndex,
068: "stilton".hashCode());
069:
070: final Cheese stilton1 = new Cheese("stilton", 35);
071: final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
072: final Cheese stilton2 = new Cheese("stilton", 59);
073: final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
074:
075: // test add
076: index.add(h1);
077: index.add(h2);
078: assertEquals(h2, index.getFirst().getFactHandle());
079: assertEquals(h1, ((FactEntryImpl) index.getFirst().getNext())
080: .getFactHandle());
081:
082: // test get
083: assertEquals(h1, index.get(h1).getFactHandle());
084: assertEquals(h2, index.get(h2).getFactHandle());
085:
086: // test removal for combinations
087: // remove first
088: index.remove(h2);
089: assertEquals(h1, index.getFirst().getFactHandle());
090:
091: // remove second
092: index.add(h2);
093: index.remove(h1);
094: assertEquals(h2, index.getFirst().getFactHandle());
095:
096: // check index type does not change, as this fact is removed
097: stilton1.setType("cheddar");
098: }
099:
100: public void testThreeEntries() {
101: final ClassFieldExtractor extractor = ClassFieldExtractorCache
102: .getExtractor(Cheese.class, "type", getClass()
103: .getClassLoader());
104: final FieldIndex fieldIndex = new FieldIndex(extractor, null,
105: StringFactory.getInstance()
106: .getEvaluator(Operator.EQUAL));
107: final SingleIndex singleIndex = new SingleIndex(
108: new FieldIndex[] { fieldIndex }, 1);
109:
110: final FieldIndexEntry index = new FieldIndexEntry(singleIndex,
111: "stilton".hashCode());
112:
113: final Cheese stilton1 = new Cheese("stilton", 35);
114: final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
115: final Cheese stilton2 = new Cheese("stilton", 59);
116: final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
117: final Cheese stilton3 = new Cheese("stilton", 59);
118: final InternalFactHandle h3 = new DefaultFactHandle(3, stilton3);
119:
120: // test add
121: index.add(h1);
122: index.add(h2);
123: index.add(h3);
124: assertEquals(h3, index.getFirst().getFactHandle());
125: assertEquals(h2, ((FactEntryImpl) index.getFirst().getNext())
126: .getFactHandle());
127: assertEquals(h1, ((FactEntryImpl) index.getFirst().getNext()
128: .getNext()).getFactHandle());
129:
130: // test get
131: assertEquals(h1, index.get(h1).getFactHandle());
132: assertEquals(h2, index.get(h2).getFactHandle());
133: assertEquals(h3, index.get(h3).getFactHandle());
134:
135: // test removal for combinations
136: //remove first
137: index.remove(h3);
138: assertEquals(h2, index.getFirst().getFactHandle());
139: assertEquals(h1, ((FactEntryImpl) index.getFirst().getNext())
140: .getFactHandle());
141:
142: index.add(h3);
143: index.remove(h2);
144: assertEquals(h3, index.getFirst().getFactHandle());
145: assertEquals(h1, ((FactEntryImpl) index.getFirst().getNext())
146: .getFactHandle());
147:
148: index.add(h2);
149: index.remove(h1);
150: assertEquals(h2, index.getFirst().getFactHandle());
151: assertEquals(h3, ((FactEntryImpl) index.getFirst().getNext())
152: .getFactHandle());
153:
154: index.remove(index.getFirst().getFactHandle());
155: // check index type does not change, as this fact is removed
156: stilton2.setType("cheddar");
157: }
158: }
|