Package: StubEventAdmin

StubEventAdmin

nameinstructionbranchcomplexitylinemethod
StubEventAdmin()
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
awaitEvent(Event, List, long)
M: 0 C: 43
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
awaitEventOnTopic(String, List, long)
M: 0 C: 42
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
awaitPostingOfEvent(Event, long)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
awaitPostingOfEvent(String, long)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
awaitSendingOfEvent(Event, long)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
awaitSendingOfEvent(String, long)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
postEvent(Event)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
removeEvent(Event, List)
M: 0 C: 21
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
removeEventOnTopic(String, List)
M: 0 C: 22
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
sendEvent(Event)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.test.stubs.service.event;
13:
14: import java.util.ArrayList;
15: import java.util.Iterator;
16: import java.util.List;
17:
18: import org.osgi.service.event.Event;
19: import org.osgi.service.event.EventAdmin;
20:
21: import org.eclipse.virgo.test.stubs.service.event.internal.EventUtils;
22:
23: /**
24: * A stub implementation of {@link EventAdmin}.
25: * <p />
26: *
27: * <strong>Concurrent Semantics</strong><br />
28: *
29: * Thread-safe.
30: *
31: */
32: public class StubEventAdmin implements EventAdmin {
33:
34: private final List<Event> postedEvents = new ArrayList<Event>();
35:
36: private final List<Event> sentEvents = new ArrayList<Event>();
37:
38: private final Object monitor = new Object();
39:
40: /**
41: * {@inheritDoc}
42: */
43: public void postEvent(Event event) {
44: synchronized (this.monitor) {
45: this.postedEvents.add(event);
46: this.monitor.notifyAll();
47: }
48: }
49:
50: /**
51: * {@inheritDoc}
52: */
53: public void sendEvent(Event event) {
54: synchronized (this.monitor) {
55: this.sentEvents.add(event);
56: this.monitor.notifyAll();
57: }
58: }
59:
60: /**
61: * Waits for up to the supplied millisecond timeout period for the {@link EventAdmin#sendEvent(Event) sending} of an
62: * {@link Event} that {@link Event#equals equals} the supplied <code>event</code>. Successfully waiting for an
63: * <code>Event</code> removes it from the list of events such that a subsequent invocation would block until another
64: * matching event was sent.
65: *
66: * @param event The <code>Event</code> to await.
67: * @param msTimeout The timeout period
68: * @return <code>true</code> if the <code>Event</code> has been sent, otherwise <code>false</code>.
69: */
70: public boolean awaitSendingOfEvent(Event event, long msTimeout) {
71: return awaitEvent(event, this.sentEvents, msTimeout);
72: }
73:
74: /**
75: * Waits for up to the supplied millisecond timeout period for the {@link EventAdmin#postEvent(Event) posting} of an
76: * {@link Event} that {@link Event#equals equals} the supplied <code>event</code>. Successfully waiting for an
77: * <code>Event</code> removes it from the list of events such that a subsequent invocation would block until another
78: * matching event was posted.
79: *
80: * @param event The <code>Event</code> to await.
81: * @param msTimeout The timeout period
82: * @return <code>true</code> if the <code>Event</code> has been posted, otherwise <code>false</code>.
83: */
84: public boolean awaitPostingOfEvent(Event event, long msTimeout) {
85: return awaitEvent(event, this.postedEvents, msTimeout);
86: }
87:
88: /**
89: * Waits for up to the supplied millisecond timeout period for the {@link EventAdmin#sendEvent(Event) sending} of an
90: * {@link Event} that has the supplied <code>topic</code>. Successfully waiting for an <code>Event</code> removes it
91: * from the list of events such that a subsequent invocation would block until another matching event was sent.
92: *
93: * @param topic The <code>topic</code> to await.
94: * @param msTimeout The timeout period
95: * @return the matching <code>Event</code>, or <code>null</code> if no <code>Event</code> is received.
96: */
97: public Event awaitSendingOfEvent(String topic, long msTimeout) {
98: return awaitEventOnTopic(topic, this.sentEvents, msTimeout);
99: }
100:
101: /**
102: * Waits for up to the supplied millisecond timeout period for the {@link EventAdmin#postEvent(Event) posting} of an
103: * {@link Event} that has the supplied <code>topic</code>. Successfully waiting for an <code>Event</code> removes it
104: * from the list of events such that a subsequent invocation would block until another matching event was sent.
105: *
106: * @param topic The <code>topic</code> to await.
107: * @param msTimeout The timeout period
108: * @return the matching <code>Event</code>, or <code>null</code> if no <code>Event</code> is received.
109: */
110: public Event awaitPostingOfEvent(String topic, long msTimeout) {
111: return awaitEventOnTopic(topic, this.postedEvents, msTimeout);
112: }
113:
114: private boolean awaitEvent(Event event, List<Event> eventList, long timeout) {
115: long endTime = System.currentTimeMillis() + timeout;
116:
117: synchronized (this.monitor) {
118: try {
119:• while (!removeEvent(event, eventList)) {
120: long waitTime = endTime - System.currentTimeMillis();
121:
122:• if (waitTime > 0) {
123: this.monitor.wait(waitTime);
124: } else {
125: return false;
126: }
127: }
128: return true;
129: } catch (InterruptedException e) {
130: Thread.interrupted();
131: }
132:
133: return false;
134: }
135: }
136:
137: private Event awaitEventOnTopic(String topic, List<Event> eventList, long timeout) {
138: long endTime = System.currentTimeMillis() + timeout;
139:
140: synchronized (this.monitor) {
141: try {
142: Event event = null;
143:• while (event == null) {
144: long waitTime = endTime - System.currentTimeMillis();
145:
146:• if (waitTime > 0) {
147: this.monitor.wait(waitTime);
148: event = removeEventOnTopic(topic, eventList);
149: } else {
150: break;
151: }
152: }
153: return event;
154: } catch (InterruptedException e) {
155: Thread.interrupted();
156: }
157:
158: return null;
159: }
160: }
161:
162: private boolean removeEvent(Event event, List<Event> eventList) {
163:
164: Iterator<Event> candidates = eventList.iterator();
165:
166:• while (candidates.hasNext()) {
167: Event candidate = candidates.next();
168:
169:• if (EventUtils.eventsAreEqual(candidate, event)) {
170: candidates.remove();
171: return true;
172: }
173: }
174:
175: return false;
176: }
177:
178: private Event removeEventOnTopic(String topic, List<Event> eventList) {
179:
180: Iterator<Event> candidates = eventList.iterator();
181:
182:• while (candidates.hasNext()) {
183: Event candidate = candidates.next();
184:
185:• if (topic.equals(candidate.getTopic())) {
186: candidates.remove();
187: return candidate;
188: }
189: }
190:
191: return null;
192: }
193: }