Pierre De Rop | faca289 | 2016-01-31 23:27: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.dm.lambda.itest; |
| 20 | |
| 21 | import static org.apache.felix.dm.lambda.DependencyManagerActivator.component; |
| 22 | |
| 23 | import org.apache.felix.dm.Component; |
| 24 | import org.apache.felix.dm.DependencyManager; |
| 25 | import org.junit.Assert; |
| 26 | import org.osgi.framework.Bundle; |
| 27 | |
| 28 | /** |
| 29 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 30 | */ |
| 31 | public 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 38 | Component consumer = component(m, comp -> comp.impl(c).withBundle(bundle -> bundle.add("add").remove("remove"))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 39 | |
| 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 51 | .withBundle(bundle-> bundle.filter(filter).add("add").remove("remove"))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 52 | 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 62 | Component consumer = component(m, comp -> comp.impl(c).withBundle(bundle -> bundle.add(MyConsumer::add).remove(MyConsumer::remove))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 63 | |
| 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 75 | .withBundle(bundle-> bundle.filter(filter).add(FilteredConsumer::add).remove(FilteredConsumer::remove))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 76 | 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 88 | .withBundle(b -> b.filter("(Bundle-SymbolicName=" + BSN + ")").add("add").remove("remove"))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 89 | 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 102 | .withBundle(b -> b.filter("(Bundle-SymbolicName=" + BSN + ")").add(impl::add).remove(impl::remove))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 103 | 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 117 | .withBundle(b -> b.filter("(Bundle-SymbolicName=" + BSN + ")").add("add").remove("remove"))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 118 | 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 131 | .withBundle(b -> b.filter("(Bundle-SymbolicName=" + BSN + ")").add(impl::addRef).remove(impl::removeRef)).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 132 | // 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 Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 209 | public void add(Component component, Bundle b) { // method ref callback |
| 210 | Assert.assertNotNull(component); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 211 | if (b.getSymbolicName().equals(BSN)) { |
| 212 | m_ensure.step(1); |
| 213 | } |
| 214 | } |
| 215 | |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 216 | 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 Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 226 | Assert.assertNotNull(b); |
| 227 | if (b.getSymbolicName().equals(BSN)) { |
| 228 | m_ensure.step(2); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |