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 java.lang.reflect.Field;
20:
21: import org.drools.DroolsTestCase;
22:
23: public class ObjectSourceTest extends DroolsTestCase {
24:
25: public void testObjectSourceConstructor() {
26: final MockObjectSource source = new MockObjectSource(15);
27: assertEquals(15, source.getId());
28:
29: assertEquals(0, source.getAttached());
30: source.attach();
31: assertEquals(1, source.getAttached());
32: }
33:
34: public void testAddObjectSink() throws Exception {
35: final MockObjectSource source = new MockObjectSource(15);
36:
37: // We need to re-assign this var each time the sink changes references
38: final Field field = ObjectSource.class.getDeclaredField("sink");
39: field.setAccessible(true);
40: ObjectSinkPropagator sink = (ObjectSinkPropagator) field
41: .get(source);
42:
43: assertSame(EmptyObjectSinkAdapter.getInstance(), sink);
44:
45: final MockObjectSink sink1 = new MockObjectSink();
46: source.addObjectSink(sink1);
47: sink = (ObjectSinkPropagator) field.get(source);
48: assertSame(SingleObjectSinkAdapter.class, sink.getClass());
49: assertEquals(1, sink.getSinks().length);
50:
51: final MockObjectSink sink2 = new MockObjectSink();
52: source.addObjectSink(sink2);
53: sink = (ObjectSinkPropagator) field.get(source);
54: assertSame(CompositeObjectSinkAdapter.class, sink.getClass());
55: assertEquals(2, sink.getSinks().length);
56:
57: final MockObjectSink sink3 = new MockObjectSink();
58: source.addObjectSink(sink3);
59: assertSame(CompositeObjectSinkAdapter.class, sink.getClass());
60: assertEquals(3, sink.getSinks().length);
61:
62: source.removeObjectSink(sink2);
63: assertSame(CompositeObjectSinkAdapter.class, sink.getClass());
64: assertEquals(2, sink.getSinks().length);
65:
66: source.removeObjectSink(sink1);
67: sink = (ObjectSinkPropagator) field.get(source);
68: assertSame(SingleObjectSinkAdapter.class, sink.getClass());
69: assertEquals(1, sink.getSinks().length);
70:
71: source.removeObjectSink(sink3);
72: sink = (ObjectSinkPropagator) field.get(source);
73: assertSame(EmptyObjectSinkAdapter.getInstance(), sink);
74: assertEquals(0, sink.getSinks().length);
75: }
76: }
|