KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > services > TestEventLinker


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package hivemind.test.services;
16
17 import java.beans.PropertyChangeEvent JavaDoc;
18
19 import org.apache.hivemind.ErrorLog;
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.service.EventLinker;
22 import org.apache.hivemind.service.impl.EventLinkerImpl;
23 import org.apache.hivemind.test.HiveMindTestCase;
24
25 /**
26  * Tests for the {@link org.apache.hivemind.service.impl.EventLinkerImpl}class, used by the
27  * {@link org.apache.hivemind.service.impl.BuilderFactory}.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class TestEventLinker extends HiveMindTestCase
32 {
33     public void testNoName()
34     {
35         EventProducer p = new EventProducer();
36         EventConsumer c = new EventConsumer();
37         EventLinker l = new EventLinkerImpl(null);
38
39         l.addEventListener(p, null, c, null);
40
41         PropertyChangeEvent JavaDoc e = new PropertyChangeEvent JavaDoc(this, "whatever", "foo", "bar");
42
43         p.fire(e);
44
45         assertSame(e, c.getLastEvent());
46
47         // Exercise code paths for when the producer has already
48
// been scanned.
49

50         EventConsumer c2 = new EventConsumer();
51
52         l.addEventListener(p, null, c2, null);
53
54         p.fire(e);
55
56         assertSame(e, c2.getLastEvent());
57     }
58
59     public void testNameMatch()
60     {
61         EventProducer p = new EventProducer();
62         EventConsumer c = new EventConsumer();
63         EventLinker l = new EventLinkerImpl(null);
64
65         l.addEventListener(p, "propertyChange", c, null);
66
67         PropertyChangeEvent JavaDoc e = new PropertyChangeEvent JavaDoc(this, "whatever", "foo", "bar");
68
69         p.fire(e);
70
71         assertSame(e, c.getLastEvent());
72     }
73
74     public void testNoMatch() throws Exception JavaDoc
75     {
76         Location location = newLocation();
77         EventProducer p = new EventProducer();
78         Object JavaDoc c = "NeverwinterNights";
79
80         ErrorLog el = (ErrorLog) newMock(ErrorLog.class);
81
82         el
83                 .error(
84                         "NeverwinterNights does not implement any listener interfaces compatible with EventProducer.",
85                         location,
86                         null);
87
88         replayControls();
89
90         EventLinker l = new EventLinkerImpl(el);
91
92         l.addEventListener(p, null, c, location);
93
94         assertEquals(0, p.getListenerCount());
95
96         verifyControls();
97     }
98
99     public void testNoMatchingName() throws Exception JavaDoc
100     {
101         Location location = newLocation();
102         EventProducer p = new EventProducer();
103         Object JavaDoc c = "SoulCailiburII";
104
105         ErrorLog el = (ErrorLog) newMock(ErrorLog.class);
106
107         el.error("EventProducer does not implement an event set named 'fred'.", location, null);
108
109         replayControls();
110
111         EventLinker l = new EventLinkerImpl(el);
112
113         l.addEventListener(p, "fred", c, location);
114
115         assertEquals(0, p.getListenerCount());
116
117         verifyControls();
118     }
119
120     public void testIncompatible() throws Exception JavaDoc
121     {
122         Location location = newLocation();
123         EventProducer p = new EventProducer();
124         Object JavaDoc c = "SplinterCell";
125
126         ErrorLog el = (ErrorLog) newMock(ErrorLog.class);
127
128         el
129                 .error(
130                         "SplinterCell does not implement the java.beans.PropertyChangeListener listener interface (for event set 'propertyChange' of EventProducer).",
131                         location,
132                         null);
133
134         replayControls();
135
136         EventLinker l = new EventLinkerImpl(el);
137
138         l.addEventListener(p, "propertyChange", c, location);
139
140         assertEquals(0, p.getListenerCount());
141
142         verifyControls();
143     }
144
145     public void testNoProducer() throws Exception JavaDoc
146     {
147         Location location = newLocation();
148         Object JavaDoc p = "DanceDanceRevolution";
149         Object JavaDoc c = "SplinterCell";
150
151         ErrorLog el = (ErrorLog) newMock(ErrorLog.class);
152
153         el
154                 .error(
155                         "SplinterCell does not implement any listener interfaces compatible with DanceDanceRevolution.",
156                         location,
157                         null);
158
159         replayControls();
160
161         EventLinker l = new EventLinkerImpl(el);
162
163         l.addEventListener(p, null, c, location);
164
165         verifyControls();
166     }
167
168 }
Popular Tags