blob: 56cef125d38b59814a2198e2e1da9a1af14283ad [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 org.junit.Assert;
23import org.junit.Before;
24import org.junit.Test;
25import org.mockito.Mock;
26import org.mockito.Mockito;
27import org.mockito.MockitoAnnotations;
28import org.osgi.framework.Bundle;
29import org.osgi.framework.Constants;
30import org.osgi.framework.ServiceReference;
31import org.osgi.service.device.Device;
32import org.osgi.service.device.Driver;
33
34/**
35 *
36 * Some simple tests for the DriverAttributes class.
37 *
38 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
39 *
40 */
41public class DriverAttributesTest {
42
43
44 private DriverAttributes m_attributes;
45
46 @Mock
47 private ServiceReference m_ref;
48
49 @Mock
50 private Driver m_driver;
51
52 @Mock
53 private Bundle m_bundle;
54
55 @Before
56 public void setUp() throws Exception {
57
58 MockitoAnnotations.initMocks(this);
59
60 Mockito.when(m_ref.getBundle()).thenReturn(m_bundle);
61
62 Mockito.when(m_bundle.getLocation()).thenReturn("_DD_test-driverbundle");
63
64 m_attributes = new DriverAttributes(m_ref, m_driver);
65 }
66
67
68 @Test
69 public void VerifyDriverReferenceReturned() throws Exception {
70
71 Assert.assertEquals(m_ref, m_attributes.getReference());
72 }
73
74 @Test
75 public void VerifyDriverInUseByDevice() throws Exception {
76
77 ServiceReference ref = Mockito.mock(ServiceReference.class);
78
79 Mockito.when(ref.getProperty(Constants.OBJECTCLASS))
80 .thenReturn(new String[]{Object.class.getName()});
81
82 Mockito.when(ref.getProperty(
83 org.osgi.service.device.Constants.DEVICE_CATEGORY))
84 .thenReturn(new String[]{"dummy"});
85
86 Mockito.when(m_bundle.getServicesInUse()).thenReturn(new ServiceReference[]{ref});
87
88 m_attributes.tryUninstall();
89
90 Mockito.verify(m_bundle).getLocation();
91 Mockito.verify(m_bundle).getServicesInUse();
92 Mockito.verifyNoMoreInteractions(m_bundle);
93
94 }
95
96 @Test
97 public void VerifyDriverInUseByDeviceInstance() throws Exception {
98
99 ServiceReference ref = Mockito.mock(ServiceReference.class);
100
101 Mockito.when(ref.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[]{Device.class.getName()});
102 Mockito.when(ref.getProperty(
103 org.osgi.service.device.Constants.DEVICE_CATEGORY))
104 .thenReturn(new String[]{"dummy"});
105
106 Mockito.when(m_bundle.getServicesInUse()).thenReturn(new ServiceReference[]{ref});
107
108 m_attributes.tryUninstall();
109
110 Mockito.verify(m_bundle).getLocation();
111 Mockito.verify(m_bundle).getServicesInUse();
112 Mockito.verifyNoMoreInteractions(m_bundle);
113
114 }
115
116
117 @Test
118 public void VerifyDriverInUseByNoDevice() throws Exception {
119
120 ServiceReference ref = Mockito.mock(ServiceReference.class);
121
122 Mockito.when(ref.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[]{Object.class.getName()});
123 Mockito.when(m_bundle.getServicesInUse()).thenReturn(new ServiceReference[]{ref});
124
125 m_attributes.tryUninstall();
126
127 Mockito.verify(m_bundle).getLocation();
128 Mockito.verify(m_bundle).getServicesInUse();
129 Mockito.verify(m_bundle).uninstall();
130
131 }
132 @Test
133 public void VerifyDriverNotInUseLeadsToUnInstall1() throws Exception {
134
135 Mockito.when(m_bundle.getServicesInUse()).thenReturn(new ServiceReference[0]);
136
137 m_attributes.tryUninstall();
138
139 Mockito.verify(m_bundle).uninstall();
140
141
142 }
143
144 @Test
145 public void VerifyDriverNotInUseLeadsToUnInstall2() throws Exception {
146
147 m_attributes.tryUninstall();
148
149 Mockito.verify(m_bundle).uninstall();
150
151 }
152
153 @Test
154 public void VerifyAttachCalledOnDriver() throws Exception {
155
156
157 ServiceReference ref = Mockito.mock(ServiceReference.class);
158 m_attributes.attach(ref);
159
160 Mockito.verify(m_driver).attach(Mockito.eq(ref));
161
162 }
163
164 @Test
165 public void VerifyMatchCalledOnDriver() throws Exception {
166
167
168 ServiceReference ref = Mockito.mock(ServiceReference.class);
169 m_attributes.match(ref);
170
171 Mockito.verify(m_driver).match(Mockito.eq(ref));
172
173 }
174}