Stuart McCulloch | 798b456 | 2009-04-16 09:32:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | package org.apache.felix.das; |
| 20 | |
| 21 | |
| 22 | import java.util.Dictionary; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | import java.util.Properties; |
| 26 | |
| 27 | import org.mockito.Mock; |
| 28 | import org.mockito.Mockito; |
| 29 | import org.mockito.MockitoAnnotations; |
| 30 | import org.mockito.invocation.InvocationOnMock; |
| 31 | import org.mockito.stubbing.Answer; |
| 32 | import org.osgi.framework.Bundle; |
| 33 | import org.osgi.framework.BundleContext; |
| 34 | import org.osgi.framework.Constants; |
| 35 | import 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 | */ |
| 45 | public 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 Offermans | b8f9bcb | 2011-04-11 20:46:19 +0000 | [diff] [blame] | 103 | Mockito.when( ref.getUsingBundles() ).thenReturn( new Bundle[0] ); |
| 104 | |
Stuart McCulloch | 798b456 | 2009-04-16 09:32:05 +0000 | [diff] [blame] | 105 | 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 | } |