blob: aaaa5916934db1a504608e8b88f4ceaed0488b26 [file] [log] [blame]
Stuart McCulloch798b4562009-04-16 09:32:05 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.das;
20
21
22import java.util.Dictionary;
23import java.util.HashMap;
24import java.util.Map;
25import java.util.Properties;
26
27import org.mockito.Mock;
28import org.mockito.Mockito;
29import org.mockito.MockitoAnnotations;
30import org.mockito.invocation.InvocationOnMock;
31import org.mockito.stubbing.Answer;
32import org.osgi.framework.Bundle;
33import org.osgi.framework.BundleContext;
34import org.osgi.framework.Constants;
35import org.osgi.framework.ServiceReference;
36
37
38/**
39 *
40 * a very simple mock of an osgi framework. enables the registration of services.
41 * automatically generates mocked service references for them.
42 *
43 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
44 */
45public class OSGiMock
46{
47
48 @Mock
49 private BundleContext m_context;
50
51 private Map<Object, ServiceReference> m_references;
52
53 private Map<ServiceReference, Bundle> m_bundles;
54
55 private int m_serviceIndex = 1;
56
57 public OSGiMock()
58 {
59 MockitoAnnotations.initMocks(this);
60 m_references = new HashMap<Object, ServiceReference>();
61 m_bundles = new HashMap<ServiceReference, Bundle>();
62 }
63
64 public static ServiceReference createReference(final Properties p)
65 {
66 ServiceReference ref = Mockito.mock( ServiceReference.class );
67
68 Mockito.when(ref.getProperty(Mockito.anyString())).thenAnswer(new Answer<Object>() {
69 public Object answer(InvocationOnMock invocation) throws Throwable {
70 return p.get(invocation.getArguments()[0].toString());
71 }
72 });
73
74 Mockito.when(ref.getPropertyKeys()).thenAnswer(new Answer<Object>() {
75 public Object answer(InvocationOnMock invocation) throws Throwable {
76 return p.keySet().toArray(new String[0]);
77 }
78 });
79
80
81 return ref;
82 }
83
84
85
86 public BundleContext getBundleContext()
87 {
88 return m_context;
89 }
90
91
92 @SuppressWarnings("all")
93 public ServiceReference registerService( String[] ifaces, Object impl, Properties props )
94 {
95
96 ServiceReference ref = createReference( ifaces, props );
97
98 Mockito.when( m_context.registerService( ifaces, impl, props ) )
99 .thenReturn( null );
100
101 Mockito.when( m_context.getService( ref ) ).thenReturn( impl );
102
Marcel Offermansb8f9bcb2011-04-11 20:46:19 +0000103 Mockito.when( ref.getUsingBundles() ).thenReturn( new Bundle[0] );
104
Stuart McCulloch798b4562009-04-16 09:32:05 +0000105 m_references.put( impl, ref );
106
107 return ref;
108 }
109
110
111 public ServiceReference getReference( Object service )
112 {
113 return m_references.get( service );
114 }
115
116
117 public Bundle getBundle( ServiceReference ref )
118 {
119 return m_bundles.get( ref );
120 }
121
122
123
124 @SuppressWarnings("all")
125 public ServiceReference createReference( String[] ifaces, Properties props )
126 {
127
128 final ServiceReference ref = Mockito.mock( ServiceReference.class );
129
130 RefPropAnswer answer = new RefPropAnswer( props, ifaces );
131
132 Mockito.when( ref.getProperty( Mockito.anyString() ) )
133 .thenAnswer( answer );
134
135 Mockito.when( ref.getPropertyKeys() )
136 .thenReturn( props.keySet().toArray( new String[0] ) );
137
138 Bundle bundle = Mockito.mock( Bundle.class );
139
140 Mockito.when( ref.getBundle() ).thenReturn( bundle );
141
142 m_bundles.put( ref, bundle );
143
144 return ref;
145 }
146
147 @SuppressWarnings({ "unchecked" })
148 private class RefPropAnswer implements Answer<Object>
149 {
150 private final Dictionary m_p;
151
152
153 public RefPropAnswer( Dictionary p, String[] iface )
154 {
155 m_p = p;
156 m_p.put( Constants.OBJECTCLASS, iface );
157 m_p.put( Constants.SERVICE_ID, m_serviceIndex++ );
158 }
159
160
161 public Object answer(InvocationOnMock invocation) throws Throwable
162 {
163 String key = (String)invocation.getArguments()[0];
164 return m_p.get( key );
165 }
166
167 }
168}