01: package org.drools.reteoo;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import junit.framework.TestCase;
20:
21: import org.drools.common.DefaultFactHandle;
22:
23: public class DefaultFactHandleFactoryTest extends TestCase {
24:
25: /*
26: * Class under test for FactHandle newFactHandle()
27: */
28: public void testNewFactHandle() {
29: final ReteooFactHandleFactory factory = new ReteooFactHandleFactory();
30: DefaultFactHandle handle = (DefaultFactHandle) factory
31: .newFactHandle("cheese");
32: assertEquals(0, handle.getId());
33: assertEquals(0, handle.getRecency());
34:
35: // issue new handle
36: handle = (DefaultFactHandle) factory.newFactHandle("cheese");
37: assertEquals(1, handle.getId());
38: assertEquals(1, handle.getRecency());
39:
40: // issue new handle, under a different reference so we can destroy later
41: final DefaultFactHandle handle2 = (DefaultFactHandle) factory
42: .newFactHandle("cheese");
43: assertEquals(2, handle2.getId());
44: assertEquals(2, handle2.getRecency());
45:
46: // Check recency increasion works
47: factory.increaseFactHandleRecency(handle);
48: assertEquals(3, handle.getRecency());
49:
50: // issue new handle and make sure recency is still inline
51: handle = (DefaultFactHandle) factory.newFactHandle("cheese");
52: assertEquals(3, handle.getId());
53: assertEquals(4, handle.getRecency());
54:
55: // destroy handle
56: factory.destroyFactHandle(handle2);
57:
58: // issue new fact handle and make sure it recycled the id=2
59: handle = (DefaultFactHandle) factory.newFactHandle("cheese");
60: assertEquals(2, handle.getId());
61: assertEquals(5, handle.getRecency());
62:
63: // issue new handle making sure it correctly resumes ids and recency
64: handle = (DefaultFactHandle) factory.newFactHandle("cheese");
65: assertEquals(4, handle.getId());
66: assertEquals(6, handle.getRecency());
67:
68: }
69:
70: }
|