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 TupleSourceTest extends DroolsTestCase {
24:
25: public void testObjectTupleConstructor() {
26: final MockTupleSource source = new MockTupleSource(15);
27: assertEquals(15, source.getId());
28: }
29:
30: public void testAddTupleSink() throws Exception {
31: final MockTupleSource source = new MockTupleSource(15);
32:
33: // We need to re-assign this var each time the sink changes references
34: final Field field = TupleSource.class.getDeclaredField("sink");
35: field.setAccessible(true);
36: TupleSinkPropagator sink = (TupleSinkPropagator) field
37: .get(source);
38:
39: assertSame(EmptyTupleSinkAdapter.getInstance(), sink);
40:
41: final MockTupleSink sink1 = new MockTupleSink();
42: source.addTupleSink(sink1);
43: sink = (TupleSinkPropagator) field.get(source);
44: assertSame(SingleTupleSinkAdapter.class, sink.getClass());
45: assertEquals(1, sink.getSinks().length);
46:
47: final MockTupleSink sink2 = new MockTupleSink();
48: source.addTupleSink(sink2);
49: sink = (TupleSinkPropagator) field.get(source);
50: assertSame(CompositeTupleSinkAdapter.class, sink.getClass());
51: assertEquals(2, sink.getSinks().length);
52:
53: final MockTupleSink sink3 = new MockTupleSink();
54: source.addTupleSink(sink3);
55: assertSame(CompositeTupleSinkAdapter.class, sink.getClass());
56: assertEquals(3, sink.getSinks().length);
57:
58: source.removeTupleSink(sink2);
59: assertSame(CompositeTupleSinkAdapter.class, sink.getClass());
60: assertEquals(2, sink.getSinks().length);
61:
62: source.removeTupleSink(sink1);
63: sink = (TupleSinkPropagator) field.get(source);
64: assertSame(SingleTupleSinkAdapter.class, sink.getClass());
65: assertEquals(1, sink.getSinks().length);
66:
67: source.removeTupleSink(sink3);
68: sink = (TupleSinkPropagator) field.get(source);
69: assertSame(EmptyTupleSinkAdapter.getInstance(), sink);
70: assertEquals(0, sink.getSinks().length);
71: }
72:
73: }
|