blob: 9a8b950e61d820de8f3855d16f0892ab7c71042d [file] [log] [blame]
Pierre De Ropc40d93f2015-05-04 20:25:57 +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.dm.runtime.itest.components;
20
21import java.io.IOException;
22import java.util.Dictionary;
23import java.util.Hashtable;
24import java.util.Map;
25
26import org.apache.felix.dm.annotation.api.Component;
27import org.apache.felix.dm.annotation.api.ConfigurationDependency;
28import org.apache.felix.dm.annotation.api.FactoryConfigurationAdapterService;
29import org.apache.felix.dm.annotation.api.ServiceDependency;
30import org.apache.felix.dm.annotation.api.Start;
31import org.apache.felix.dm.annotation.api.Stop;
32import org.apache.felix.dm.itest.util.Ensure;
33import org.junit.Assert;
34import org.osgi.framework.ServiceReference;
35import org.osgi.service.cm.Configuration;
36import org.osgi.service.cm.ConfigurationAdmin;
37
38/**
39 * Tests various bind method signatures
40 */
41public class MethodSignatures {
42 // For Consumer service
43 public final static String ENSURE_SERVICE_DEPENDENCY = "MethodSignatures1";
44
45 // For FactoryPidComponent
46 public final static String ENSURE_FACTORYPID = "MethodSignatures2";
47
48 // This component configures the Consumer component.
49 @Component
50 public static class ConsumerConfigurator {
51 @ServiceDependency(filter="(name=" + ENSURE_SERVICE_DEPENDENCY + ")")
52 Ensure m_ensure;
53
54 @ServiceDependency
55 ConfigurationAdmin m_cm;
56
57 private Configuration m_conf;
58
59 @Start
60 void start() throws IOException {
61 m_conf = m_cm.getConfiguration(Consumer.class.getName());
62 Hashtable<String, Object> props = new Hashtable<>();
63 props.put("foo", "bar");
64 m_conf.update(props);
65 }
66
67 @Stop
68 void stop() throws IOException {
69 m_conf.delete();
70 }
71 }
72
73 // A simple provider service
74 @Component(provides=Provider.class)
75 public static class Provider {
76 @ServiceDependency(filter="(name=" + ENSURE_SERVICE_DEPENDENCY + ")")
77 Ensure m_ensure;
78 }
79
Pierre De Rop453344a2016-02-09 21:48:32 +000080 public interface ConsumerConfig {
81 String getFoo();
82 }
83
Pierre De Ropc40d93f2015-05-04 20:25:57 +000084 // This consumer depends on the configuration and on the provider, using multiple method signatures.
85 @Component
86 public static class Consumer {
87 Dictionary<String, Object> m_properties;
88 Dictionary<String, Object> m_properties2;
Pierre De Rop453344a2016-02-09 21:48:32 +000089 ConsumerConfig m_config;
90 ConsumerConfig m_config2;
Pierre De Ropc40d93f2015-05-04 20:25:57 +000091
92 @ConfigurationDependency
93 void updated(Dictionary<String, Object> properties) {
94 m_properties = properties;
95 }
96
97 @ConfigurationDependency
98 void updated2(org.apache.felix.dm.Component component, Dictionary<String, Object> properties) {
99 Assert.assertNotNull(component);
100 m_properties2 = properties;
101 }
Pierre De Rop453344a2016-02-09 21:48:32 +0000102
103 @ConfigurationDependency(pidClass=Consumer.class)
104 void updated3(ConsumerConfig cnf) {
105 Assert.assertNotNull(cnf);
106 m_config = cnf;
107 }
108
109 @ConfigurationDependency(pidClass=Consumer.class)
110 void updated4(org.apache.felix.dm.Component comp, ConsumerConfig cnf) {
111 Assert.assertNotNull(comp);
112 Assert.assertNotNull(cnf);
113 m_config2 = cnf;
114 }
Pierre De Ropc40d93f2015-05-04 20:25:57 +0000115
116 @ServiceDependency(filter="(name=" + ENSURE_SERVICE_DEPENDENCY + ")")
117 Ensure m_ensure;
118
119 @ServiceDependency
120 void bind(org.apache.felix.dm.Component component, ServiceReference ref, Provider provider) {
121 Assert.assertNotNull(component);
122 Assert.assertNotNull(ref);
123 Assert.assertNotNull(provider);
124 m_ensure.step();
125 }
126
127 @ServiceDependency
128 void bind(org.apache.felix.dm.Component component, Provider provider) {
129 Assert.assertNotNull(component);
130 Assert.assertNotNull(provider);
131 m_ensure.step();
132 }
133
134 @ServiceDependency
135 void bind(org.apache.felix.dm.Component component, Map<?, ?> properties, Provider provider) {
136 Assert.assertNotNull(component);
137 Assert.assertNotNull(properties);
138 Assert.assertNotNull(provider);
139 m_ensure.step();
140 }
141
142 @ServiceDependency
143 void bind(ServiceReference ref, Provider provider) {
144 Assert.assertNotNull(ref);
145 Assert.assertNotNull(provider);
146 m_ensure.step();
147 }
148
149 @ServiceDependency
150 void bind(Provider provider) {
151 Assert.assertNotNull(provider);
152 m_ensure.step();
153 }
154
155 @ServiceDependency
156 void bind(Provider provider, Map<?,?> properties) {
157 Assert.assertNotNull(provider);
158 Assert.assertNotNull(properties);
159 m_ensure.step();
160 }
161
162 @ServiceDependency
163 void bind(Map<?,?> properties, Provider provider) {
164 Assert.assertNotNull(properties);
165 Assert.assertNotNull(provider);
166 m_ensure.step();
167 }
168
169 @ServiceDependency
170 void bind(Provider provider, Dictionary<?,?> properties) {
171 Assert.assertNotNull(properties);
172 Assert.assertNotNull(provider);
173 m_ensure.step();
174 }
175
176 @ServiceDependency
177 void bind(Dictionary<?,?> properties, Provider provider) {
178 Assert.assertNotNull(properties);
179 Assert.assertNotNull(provider);
180 m_ensure.step();
181 }
182
183 @Start
184 void start() {
185 Assert.assertNotNull(m_properties);
186 Assert.assertNotNull(m_properties2);
187 Assert.assertEquals("bar", m_properties.get("foo"));
188 Assert.assertEquals("bar", m_properties2.get("foo"));
Pierre De Rop453344a2016-02-09 21:48:32 +0000189 Assert.assertNotNull(m_config);
190 Assert.assertEquals("bar", m_config.getFoo());
191 Assert.assertEquals("bar", m_config2.getFoo());
Pierre De Ropc40d93f2015-05-04 20:25:57 +0000192 m_ensure.step(10);
193 }
194 }
195
196 // This component configures the FactoryPidComponent / FactoryPidComponent2 components.
197 @Component
198 public static class FactoryPidConfigurator {
199 @ServiceDependency(filter="(name=" + ENSURE_FACTORYPID + ")")
200 Ensure m_ensure;
201
202 @ServiceDependency
203 ConfigurationAdmin m_cm;
204
205 private Configuration m_conf1;
206 private Configuration m_conf2;
207
208 @Start
209 void start() throws IOException {
210 m_conf1 = m_cm.createFactoryConfiguration(FactoryPidComponent.class.getName());
211 Hashtable<String, Object> props = new Hashtable<>();
212 props.put("foo", "bar");
213 m_conf1.update(props);
214
215 m_conf2 = m_cm.createFactoryConfiguration(FactoryPidComponent2.class.getName());
216 props = new Hashtable<>();
217 props.put("foo", "bar");
218 m_conf2.update(props);
219 }
220
221 @Stop
222 void stop() throws IOException {
223 m_conf1.delete();
224 m_conf2.delete();
225 }
226 }
227
228 // This is a factory pid component with an updated callback having the "updated(Dictionary)" signature
229 @FactoryConfigurationAdapterService
230 public static class FactoryPidComponent {
231 Dictionary<String, Object> m_properties;
232
233 void updated(Dictionary<String, Object> properties) {
234 m_properties = properties;
235 }
236
237 @ServiceDependency(filter="(name=" + ENSURE_FACTORYPID + ")")
238 Ensure m_ensure;
239
240 @Start
241 void start() {
242 Assert.assertNotNull(m_properties);
243 Assert.assertEquals("bar", m_properties.get("foo"));
244 m_ensure.step();
245 }
246 }
247
248 // This is a factory pid component with an updated callback having the "updated(Component, Dictionary)" signature
249 @FactoryConfigurationAdapterService
250 public static class FactoryPidComponent2 {
251 Dictionary<String, Object> m_properties;
252
253 void updated(org.apache.felix.dm.Component component, Dictionary<String, Object> properties) {
254 Assert.assertNotNull(component);
255 m_properties = properties;
256 }
257
258 @ServiceDependency(filter="(name=" + ENSURE_FACTORYPID + ")")
259 Ensure m_ensure;
260
261 @Start
262 void start() {
263 Assert.assertNotNull(m_properties);
264 Assert.assertEquals("bar", m_properties.get("foo"));
265 m_ensure.step();
266 }
267 }
268}