Package: StubServiceReference

StubServiceReference

nameinstructionbranchcomplexitylinemethod
StubServiceReference(Long, Integer, StubServiceRegistration)
M: 56 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
StubServiceReference(StubServiceRegistration)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
addUsingBundles(StubBundle[])
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
compareTo(Object)
M: 44 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
equals(Object)
M: 29 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getBundle()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getProperty(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getPropertyKeys()
M: 26 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getServiceId()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getServiceRanking()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getServiceRegistration()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getUsingBundles()
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
hashCode()
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
isAssignableTo(Bundle, String)
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
putAssignableTo(Bundle, String[])
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
removeUsingBundles(StubBundle[])
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
setBundle(StubBundle)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toString()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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.framework;
13:
14: import static org.eclipse.virgo.test.stubs.internal.Assert.assertNotNull;
15:
16: import java.util.ArrayList;
17: import java.util.Arrays;
18: import java.util.Enumeration;
19: import java.util.HashMap;
20: import java.util.List;
21: import java.util.Map;
22:
23: import org.osgi.framework.Bundle;
24: import org.osgi.framework.ServiceReference;
25: import org.osgi.framework.ServiceRegistration;
26:
27: /**
28: * A stub testing implementation of {@link ServiceReference} as defined in section 6.1.23 of the OSGi Service Platform Core
29: * Specification.
30: * <p />
31: *
32: * <strong>Concurrent Semantics</strong><br />
33: *
34: * Threadsafe
35: *
36: * @param <S> type of ServiceReference
37: *
38: */
39: public final class StubServiceReference<S> implements ServiceReference<S> {
40:
41: private static final Long DEFAULT_SERVICE_ID = Long.valueOf(1);
42:
43: private static final Integer DEFAULT_SERVICE_RANKING = Integer.valueOf(0);
44:
45: private final Long serviceId;
46:
47: private final Integer serviceRanking;
48:
49: private final StubServiceRegistration<S> serviceRegistration;
50:
51: private volatile StubBundle bundle;
52:
53: private final Object bundleMonitor = new Object();
54:
55: private final List<StubBundle> usingBundles = new ArrayList<StubBundle>();
56:
57: private final Object usingBundlesMonitor = new Object();
58:
59: private final Map<Bundle, List<String>> assignableTo = new HashMap<Bundle, List<String>>();
60:
61: private final Object assignableToMonitor = new Object();
62:
63: /**
64: * Creates a new {@link StubServiceRegistration} and sets its initial state. This constructor sets
65: * <code>service.id</code> to <code>1</code> and <code>service.ranking</code> to <code>0</code>.
66: *
67: * @param serviceRegistration The service registration behind this {@link ServiceReference}
68: */
69: public StubServiceReference(StubServiceRegistration<S> serviceRegistration) {
70: this(DEFAULT_SERVICE_ID, DEFAULT_SERVICE_RANKING, serviceRegistration);
71: }
72:
73: /**
74: * Creates a new {@link StubServiceRegistration} and sets its initial state
75: *
76: * @param serviceId The service id to use
77: * @param serviceRanking The service ranking to use
78: * @param serviceRegistration The service registration behind this {@link ServiceReference}
79: */
80: public StubServiceReference(Long serviceId, Integer serviceRanking, StubServiceRegistration<S> serviceRegistration) {
81: assertNotNull(serviceId, "serviceId");
82: assertNotNull(serviceRanking, "serviceRanking");
83: assertNotNull(serviceRegistration, "serviceRegistration");
84:
85: this.serviceId = serviceId;
86: this.serviceRanking = serviceRanking;
87: this.serviceRegistration = serviceRegistration;
88: this.serviceRegistration.setServiceReference(this);
89: this.bundle = serviceRegistration.getBundleContext().getContextBundle();
90: }
91:
92: /**
93: * Gets this {@link ServiceReference}'s <code>service.id</code>
94: *
95: * @return This {@link ServiceReference}'s <code>service.id</code>
96: */
97: public Long getServiceId() {
98: return this.serviceId;
99: }
100:
101: /**
102: * Gets this {@link ServiceReference}'s <code>service.ranking</code>
103: *
104: * @return This {@link ServiceReference}'s <code>service.ranking</code>
105: */
106: public Integer getServiceRanking() {
107: return this.serviceRanking;
108: }
109:
110: /**
111: * Gets this {@link ServiceReference}'s {@link ServiceRegistration}
112: *
113: * @return This {@link ServiceReference}'s {@link ServiceRegistration}
114: */
115: public StubServiceRegistration<S> getServiceRegistration() {
116: return this.serviceRegistration;
117: }
118:
119: /**
120: * {@inheritDoc}
121: */
122: public int compareTo(Object reference) {
123:• if (reference == null) {
124: throw new IllegalArgumentException("input cannot be null");
125: }
126:
127:• if (!(reference instanceof StubServiceReference<?>)) {
128: throw new IllegalArgumentException("input must be StubServiceReference");
129: }
130:
131: StubServiceReference<?> other = (StubServiceReference<?>) reference;
132: int idComparison = serviceId.compareTo(other.serviceId);
133: int rankingComparison = serviceRanking.compareTo(other.serviceRanking);
134:
135:• if (serviceId.equals(other.serviceId)) {
136: return 0;
137:• } else if (rankingComparison != 0) {
138: return rankingComparison;
139: } else {
140: return idComparison;
141: }
142: }
143:
144: /**
145: * {@inheritDoc}
146: */
147: public Bundle getBundle() {
148: synchronized (this.bundleMonitor) {
149: return this.bundle;
150: }
151: }
152:
153: /**
154: * Sets the {@link Bundle} to return for all subsequent calls to {@link #getBundle()}.
155: *
156: * @param bundle The bundle to return
157: *
158: * @return <code>this</code> instance of the {@link StubServiceReference}
159: */
160: public StubServiceReference<S> setBundle(StubBundle bundle) {
161: synchronized (this.bundleMonitor) {
162: this.bundle = bundle;
163: return this;
164: }
165: }
166:
167: /**
168: * {@inheritDoc}
169: */
170: public Object getProperty(String key) {
171: return this.serviceRegistration.getProperties().get(key);
172: }
173:
174: /**
175: * {@inheritDoc}
176: */
177: public String[] getPropertyKeys() {
178: List<String> properties = new ArrayList<String>();
179: Enumeration<String> keys = this.serviceRegistration.getProperties().keys();
180:• while (keys.hasMoreElements()) {
181: properties.add((String) keys.nextElement());
182: }
183:
184: return properties.toArray(new String[properties.size()]);
185: }
186:
187: /**
188: * {@inheritDoc}
189: */
190: public Bundle[] getUsingBundles() {
191: synchronized (this.usingBundlesMonitor) {
192:• if (this.usingBundles.isEmpty()) {
193: return null;
194: }
195: return this.usingBundles.toArray(new Bundle[this.usingBundles.size()]);
196: }
197: }
198:
199: /**
200: * Adds a collection of {@link Bundle}s to this {@link ServiceReference} to be returned for all subsequent calls to
201: * {@link #getUsingBundles()}.
202: *
203: * @param bundles The bundles to add
204: * @return <code>this</code> instance of the {@link StubServiceReference}
205: */
206: public StubServiceReference<S> addUsingBundles(StubBundle... bundles) {
207: synchronized (this.usingBundlesMonitor) {
208: this.usingBundles.addAll(Arrays.asList(bundles));
209: return this;
210: }
211: }
212:
213: /**
214: * Removes a collection of {@link Bundle}s from this {@link ServiceReference} to be returned for all subsequent
215: * calls to {@link #getUsingBundles()}.
216: *
217: * @param bundles The bundles to remove
218: * @return <code>this</code> instance of the {@link StubServiceReference}
219: */
220: public StubServiceReference<S> removeUsingBundles(StubBundle... bundles) {
221: synchronized (this.usingBundlesMonitor) {
222: this.usingBundles.removeAll(Arrays.asList(bundles));
223: return this;
224: }
225: }
226:
227: /**
228: * {@inheritDoc}
229: */
230: public boolean isAssignableTo(Bundle bundle, String className) {
231: synchronized (this.assignableToMonitor) {
232:• if (!this.assignableTo.containsKey(bundle)) {
233: return false;
234: }
235: return this.assignableTo.get(bundle).contains(className);
236: }
237: }
238:
239: /**
240: * Adds a mapping from a {@link Bundle} to a collection of class names that the bundle is assignable to for all
241: * subsequent calls to {@link #isAssignableTo(Bundle, String)}.
242: *
243: * @param bundle The bundle that the class names will be assignable to
244: * @param classNames The class names that this bundle is assignable from
245: * @return <code>this</code> instance of the {@link StubServiceReference}
246: */
247: public StubServiceReference<S> putAssignableTo(Bundle bundle, String... classNames) {
248: synchronized (this.assignableToMonitor) {
249: this.assignableTo.put(bundle, Arrays.asList(classNames));
250: return this;
251: }
252: }
253:
254: /**
255: * {@inheritDoc}
256: */
257: @Override
258: public int hashCode() {
259: final int prime = 31;
260: int result = 1;
261: result = prime * result + serviceRegistration.hashCode();
262: return result;
263: }
264:
265: /**
266: * {@inheritDoc}
267: */
268: @Override
269: public boolean equals(Object obj) {
270:• if (this == obj) {
271: return true;
272: }
273:• if (obj == null) {
274: return false;
275: }
276:• if (getClass() != obj.getClass()) {
277: return false;
278: }
279: StubServiceReference<?> other = (StubServiceReference<?>) obj;
280:
281:• if (!serviceRegistration.equals(other.serviceRegistration)) {
282: return false;
283: }
284: return true;
285: }
286:
287: /**
288: * {@inheritDoc}
289: */
290: @Override
291: public String toString() {
292: return String.format("id: %d, ranking: %d", this.serviceId, this.serviceRanking);
293: }
294: }