blob: bfee9288fa5f46d2dd55a0f820d3b7f93a6a2ba8 [file] [log] [blame]
Pierre De Ropfaca2892016-01-31 23:27: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.dm.lambda.itest;
20
21import static org.apache.felix.dm.lambda.DependencyManagerActivator.bundleAdapter;
22import static org.apache.felix.dm.lambda.DependencyManagerActivator.component;
23
24import org.apache.felix.dm.Component;
25import org.apache.felix.dm.DependencyManager;
26import org.junit.Assert;
27import org.osgi.framework.Bundle;
28
29/**
30 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
31 */
32public class BundleAdapterTest extends TestBase {
33 public void testBundleAdapter() {
34 DependencyManager m = getDM();
35 // create a bundle adapter service (one is created for each bundle)
36 Component adapter = bundleAdapter(m)
37 .mask(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE)
38 .impl(BundleAdapter.class)
39 .provides(BundleAdapter.class)
40 .build();
41
42 // create a service provider and consumer
43 Consumer c = new Consumer();
44 Component consumer = m.createComponent().setImplementation(c)
45 .add(m.createServiceDependency().setService(BundleAdapter.class).setCallbacks("add", "remove"));
46
47 // add the bundle adapter
48 m.add(adapter);
49 // add the service consumer
50 m.add(consumer);
51 // check if at least one bundle was found
52 c.check();
53 // remove the consumer again
54 m.remove(consumer);
55 // check if all bundles were removed correctly
56 c.doubleCheck();
57 // remove the bundle adapter
58 m.remove(adapter);
59 }
60
61 public void testBundleAdapterWithCallbackInstance() {
62 DependencyManager m = getDM();
63 // create a bundle adapter service (one is created for each bundle)
64 BundleAdapterWithCallback baWithCb = new BundleAdapterWithCallback();
65 BundleAdapterCallbackInstance cbInstance = new BundleAdapterCallbackInstance(baWithCb);
66
67 Component adapter = bundleAdapter(m)
68 .mask(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE)
Pierre De Rop11527502016-02-18 21:07:16 +000069 .callbackInstance(cbInstance)
70 .add("add").remove("remove")
Pierre De Ropfaca2892016-01-31 23:27:05 +000071 .impl(baWithCb)
72 .provides(BundleAdapter.class.getName())
73 .build();
74
75 // create a service provider and consumer
76 Consumer c = new Consumer();
77 Component consumer = component(m)
78 .impl(c)
Pierre De Rop11527502016-02-18 21:07:16 +000079 .withSvc(BundleAdapter.class, s->s.add("add").remove("remove"))
Pierre De Ropfaca2892016-01-31 23:27:05 +000080 .build();
81
82 // add the bundle adapter
83 m.add(adapter);
84 // add the service consumer
85 m.add(consumer);
86 // check if at least one bundle was found
87 c.check();
88 // remove the consumer again
89 m.remove(consumer);
90 // check if all bundles were removed correctly
91 c.doubleCheck();
92 // remove the bundle adapter
93 m.remove(adapter);
94 }
95
96 public void testBundleAdapterWithCallbackInstanceRef() {
97 DependencyManager m = getDM();
98 // create a bundle adapter service (one is created for each bundle)
99 BundleAdapterWithCallback baWithCb = new BundleAdapterWithCallback();
100 BundleAdapterCallbackInstance cbInstance = new BundleAdapterCallbackInstance(baWithCb);
101
102 Component adapter = bundleAdapter(m)
103 .mask(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE)
Pierre De Rop11527502016-02-18 21:07:16 +0000104 .add(cbInstance::addRef).remove(cbInstance::removeRef)
Pierre De Ropfaca2892016-01-31 23:27:05 +0000105 .impl(baWithCb)
106 .provides(BundleAdapter.class.getName())
107 .build();
108
109 // create a service provider and consumer
110 Consumer c = new Consumer();
111 Component consumer = component(m)
112 .impl(c)
Pierre De Rop11527502016-02-18 21:07:16 +0000113 .withSvc(BundleAdapter.class, s->s.add("add").remove("remove"))
Pierre De Ropfaca2892016-01-31 23:27:05 +0000114 .build();
115
116 // add the bundle adapter
117 m.add(adapter);
118 // add the service consumer
119 m.add(consumer);
120 // check if at least one bundle was found
121 c.check();
122 // remove the consumer again
123 m.remove(consumer);
124 // check if all bundles were removed correctly
125 c.doubleCheck();
126 // remove the bundle adapter
127 m.remove(adapter);
128 }
129
130 public static class BundleAdapter {
131 volatile Bundle m_bundle;
132
133 Bundle getBundle() {
134 return m_bundle;
135 }
136 }
137
138 public static class BundleAdapterWithCallback extends BundleAdapter {
139 void add(Bundle b) {
140 m_bundle = b;
141 }
142
143 void remove(Bundle b) {
144 m_bundle = null;
145 }
146 }
147
148 public static class BundleAdapterCallbackInstance {
149 final BundleAdapterWithCallback m_ba;
150
151 BundleAdapterCallbackInstance(BundleAdapterWithCallback ba) {
152 m_ba = ba;
153 }
154
Pierre De Rop11527502016-02-18 21:07:16 +0000155 void add(Component c, Bundle b) { // reflection callback
Pierre De Ropfaca2892016-01-31 23:27:05 +0000156 m_ba.add(b);
157 }
Pierre De Rop11527502016-02-18 21:07:16 +0000158
159 void addRef(Bundle b, Component c) { // method reference callback
160 add(c, b);
161 }
162
163 void remove(Component c, Bundle b) { // reflection callback
Pierre De Ropfaca2892016-01-31 23:27:05 +0000164 m_ba.remove(b);
165 }
Pierre De Rop11527502016-02-18 21:07:16 +0000166
167 void removeRef(Bundle b, Component c) { // method reference callback
168 remove (c, b);
169 }
Pierre De Ropfaca2892016-01-31 23:27:05 +0000170 }
171
172 static class Consumer {
173 private volatile int m_count = 0;
174
175 public void add(BundleAdapter ba) {
176 Bundle b = ba.getBundle();
177 System.out.println("Consumer.add(" + b.getSymbolicName() + ")");
178 Assert.assertNotNull("bundle instance must not be null", b);
179 m_count++;
180 }
181
182 public void check() {
183 Assert.assertTrue("we should have found at least one bundle", m_count > 0);
184 }
185
186 public void remove(BundleAdapter ba) {
187 Bundle b = ba.getBundle();
188 System.out.println("Consumer.remove(" + b.getSymbolicName() + ")");
189 m_count--;
190 }
191
192 public void doubleCheck() {
193 Assert.assertEquals("all bundles we found should have been removed again", 0, m_count);
194 }
195 }
196}