blob: 5924d9326c3a5c24c3ca17020ecf7e0521988246 [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.component;
22
23import org.apache.felix.dm.Component;
24import org.apache.felix.dm.DependencyManager;
25import org.junit.Assert;
26import org.osgi.framework.Bundle;
27
28/**
29 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
30 */
31public class BundleDependencyTest extends TestBase {
32 private final static String BSN = "org.apache.felix.metatype";
33
34 public void testBundleDependencies() {
35 DependencyManager m = getDM();
36 // create a service provider and consumer
37 MyConsumer c = new MyConsumer();
Pierre De Rop11527502016-02-18 21:07:16 +000038 Component consumer = component(m, comp -> comp.impl(c).withBundle(bundle -> bundle.add("add").remove("remove")));
Pierre De Ropfaca2892016-01-31 23:27:05 +000039
40 // check if at least one bundle was found
41 c.check();
42 // remove the consumer again
43 m.remove(consumer);
44 // check if all bundles were removed correctly
45 c.doubleCheck();
46
47 // helper class that ensures certain steps get executed in sequence
48 Ensure e = new Ensure();
49 String filter = "(Bundle-SymbolicName=" + BSN + ")";
50 Component consumerWithFilter = component(m, comp -> comp.impl(new FilteredConsumer(e))
Pierre De Rop11527502016-02-18 21:07:16 +000051 .withBundle(bundle-> bundle.filter(filter).add("add").remove("remove")));
Pierre De Ropfaca2892016-01-31 23:27:05 +000052 e.step(2);
53 // remove the consumer again
54 m.remove(consumerWithFilter);
55 e.step(4);
56 }
57
58 public void testBundleDependenciesRef() {
59 DependencyManager m = getDM();
60 // create a service provider and consumer
61 MyConsumer c = new MyConsumer();
Pierre De Rop11527502016-02-18 21:07:16 +000062 Component consumer = component(m, comp -> comp.impl(c).withBundle(bundle -> bundle.add(MyConsumer::add).remove(MyConsumer::remove)));
Pierre De Ropfaca2892016-01-31 23:27:05 +000063
64 // check if at least one bundle was found
65 c.check();
66 // remove the consumer again
67 m.remove(consumer);
68 // check if all bundles were removed correctly
69 c.doubleCheck();
70
71 // helper class that ensures certain steps get executed in sequence
72 Ensure e = new Ensure();
73 String filter = "(Bundle-SymbolicName=" + BSN + ")";
74 Component consumerWithFilter = component(m, comp -> comp.impl(new FilteredConsumer(e))
Pierre De Rop11527502016-02-18 21:07:16 +000075 .withBundle(bundle-> bundle.filter(filter).add(FilteredConsumer::add).remove(FilteredConsumer::remove)));
Pierre De Ropfaca2892016-01-31 23:27:05 +000076 e.step(2);
77 // remove the consumer again
78 m.remove(consumerWithFilter);
79 e.step(4);
80 }
81
82 public void testRequiredBundleDependency() {
83 DependencyManager m = getDM();
84
85 // helper class that ensures certain steps get executed in sequence
86 Ensure e = new Ensure();
87 Component consumerWithFilter = component(m, c -> c.impl(new FilteredConsumerRequired(e))
Pierre De Rop11527502016-02-18 21:07:16 +000088 .withBundle(b -> b.filter("(Bundle-SymbolicName=" + BSN + ")").add("add").remove("remove")));
Pierre De Ropfaca2892016-01-31 23:27:05 +000089 e.waitForStep(1, 5000);
90 // remove the consumer again
91 m.remove(consumerWithFilter);
92 e.waitForStep(2, 5000);
93 }
94
95 public void testRequiredBundleDependencyRef() {
96 DependencyManager m = getDM();
97
98 // helper class that ensures certain steps get executed in sequence
99 Ensure e = new Ensure();
100 FilteredConsumerRequired impl = new FilteredConsumerRequired(e);
101 Component consumerWithFilter = component(m, c -> c.impl(impl)
Pierre De Rop11527502016-02-18 21:07:16 +0000102 .withBundle(b -> b.filter("(Bundle-SymbolicName=" + BSN + ")").add(impl::add).remove(impl::remove)));
Pierre De Ropfaca2892016-01-31 23:27:05 +0000103 e.waitForStep(1, 5000);
104 // remove the consumer again
105 m.remove(consumerWithFilter);
106 e.waitForStep(2, 5000);
107 }
108
109 public void testRequiredBundleDependencyWithComponentArgInCallbackMethod() {
110 DependencyManager m = getDM();
111
112 // helper class that ensures certain steps get executed in sequence
113 Ensure e = new Ensure();
114 // add a consumer with a filter
115 FilteredConsumerRequiredWithComponentArg impl = new FilteredConsumerRequiredWithComponentArg(e);
116 Component consumerWithFilter = component(m, c -> c.impl(impl)
Pierre De Rop11527502016-02-18 21:07:16 +0000117 .withBundle(b -> b.filter("(Bundle-SymbolicName=" + BSN + ")").add("add").remove("remove")));
Pierre De Ropfaca2892016-01-31 23:27:05 +0000118 e.waitForStep(1, 5000);
119 // remove the consumer again
120 m.remove(consumerWithFilter);
121 e.waitForStep(2, 5000);
122 }
123
124 public void testRequiredBundleDependencyWithComponentArgInCallbackMethodRef() {
125 DependencyManager m = getDM();
126
127 // helper class that ensures certain steps get executed in sequence
128 Ensure e = new Ensure();
129 FilteredConsumerRequiredWithComponentArg impl = new FilteredConsumerRequiredWithComponentArg(e);
130 Component consumerWithFilter = component(m).impl(impl)
Pierre De Rop11527502016-02-18 21:07:16 +0000131 .withBundle(b -> b.filter("(Bundle-SymbolicName=" + BSN + ")").add(impl::addRef).remove(impl::removeRef)).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +0000132 // add a consumer with a filter
133 m.add(consumerWithFilter);
134 e.waitForStep(1, 5000);
135 // remove the consumer again
136 m.remove(consumerWithFilter);
137 e.waitForStep(2, 5000);
138 }
139
140 static class MyConsumer {
141 private volatile int m_count = 0;
142
143 public void add(Bundle b) {
144 System.out.println("Consumer.add(" + b.getSymbolicName() + ")");
145 Assert.assertNotNull("bundle instance must not be null", b);
146 m_count++;
147 }
148
149 public void check() {
150 Assert.assertTrue("we should have found at least one bundle", m_count > 0);
151 }
152
153 public void remove(Bundle b) {
154 System.out.println("Consumer.remove(" + b.getSymbolicName() + ")");
155 m_count--;
156 }
157
158 public void doubleCheck() {
159 Assert.assertEquals("all bundles we found should have been removed again", 0, m_count);
160 }
161 }
162
163 static class FilteredConsumer {
164 private final Ensure m_ensure;
165
166 public FilteredConsumer(Ensure e) {
167 m_ensure = e;
168 }
169
170 public void add(Bundle b) {
171 m_ensure.step(1);
172 }
173
174 public void remove(Bundle b) {
175 m_ensure.step(3);
176 }
177 }
178
179 static class FilteredConsumerRequired {
180 private final Ensure m_ensure;
181
182 public FilteredConsumerRequired(Ensure e) {
183 m_ensure = e;
184 }
185
186 public void add(Bundle b) {
187 System.out.println("Bundle is " + b);
188// Assert.assertNotNull(b);
189 if (b.getSymbolicName().equals(BSN)) {
190 m_ensure.step(1);
191 }
192 }
193
194 public void remove(Bundle b) {
195 Assert.assertNotNull(b);
196 if (b.getSymbolicName().equals(BSN)) {
197 m_ensure.step(2);
198 }
199 }
200 }
201
202 static class FilteredConsumerRequiredWithComponentArg {
203 private final Ensure m_ensure;
204
205 public FilteredConsumerRequiredWithComponentArg(Ensure e) {
206 m_ensure = e;
207 }
208
Pierre De Rop11527502016-02-18 21:07:16 +0000209 public void add(Component component, Bundle b) { // method ref callback
210 Assert.assertNotNull(component);
Pierre De Ropfaca2892016-01-31 23:27:05 +0000211 if (b.getSymbolicName().equals(BSN)) {
212 m_ensure.step(1);
213 }
214 }
215
Pierre De Rop11527502016-02-18 21:07:16 +0000216 public void addRef(Bundle b, Component component) { // method ref callback
217 add(component, b);
218 }
219
220 public void removeRef(Bundle b, Component component) { // method ref callback
221 remove(component, b);
222 }
223
224 public void remove(Component component, Bundle b) { // method ref callback
225 Assert.assertNotNull(component);
Pierre De Ropfaca2892016-01-31 23:27:05 +0000226 Assert.assertNotNull(b);
227 if (b.getSymbolicName().equals(BSN)) {
228 m_ensure.step(2);
229 }
230 }
231 }
232}