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 java.util.ArrayList; |
| 24 | import java.util.Dictionary; |
| 25 | import java.util.HashMap; |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.concurrent.ConcurrentHashMap; |
| 29 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 30 | |
| 31 | import org.apache.felix.dm.Component; |
| 32 | import org.apache.felix.dm.DependencyManager; |
| 33 | import org.junit.Assert; |
| 34 | import org.osgi.framework.Constants; |
| 35 | |
| 36 | /** |
| 37 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 38 | */ |
Pierre De Rop | 6e8f921 | 2016-02-20 21:44:59 +0000 | [diff] [blame] | 39 | @SuppressWarnings("rawtypes") |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 40 | public class AutoConfigTest extends TestBase { |
| 41 | private final Ensure m_ensure = new Ensure(); |
| 42 | |
| 43 | public void testField() throws Exception { |
| 44 | final DependencyManager dm = getDM(); |
| 45 | // Create a consumer, depending on some providers (autoconfig field). |
| 46 | ConsumeWithProviderField consumer = new ConsumeWithProviderField(); |
| 47 | Component c = createConsumer(dm, consumer); |
| 48 | // Create two providers |
| 49 | Component p1 = createProvider(dm, 10, new Provider() { |
| 50 | public String toString() { return "provider1"; } |
| 51 | public void run() { m_ensure.step(); } |
| 52 | }); |
| 53 | Component p2 = createProvider(dm, 20, new Provider() { |
| 54 | public String toString() { return "provider2"; } |
| 55 | public void run() { m_ensure.step(); } |
| 56 | }); |
| 57 | |
| 58 | // add the two providers |
| 59 | dm.add(p2); |
| 60 | dm.add(p1); |
| 61 | // add the consumer, which should have been injected with provider2 (highest rank) |
| 62 | dm.add(c); |
| 63 | m_ensure.waitForStep(1, 5000); |
| 64 | // remove the provider2, the consumer should now be injected with provider1 |
| 65 | dm.remove(p2); |
| 66 | Assert.assertNotNull(consumer.getProvider()); |
| 67 | Assert.assertEquals("provider1", consumer.getProvider().toString()); |
| 68 | // remove the provider1, the consumer should have been stopped |
| 69 | dm.remove(p1); |
| 70 | m_ensure.waitForStep(2, 5000); |
| 71 | dm.clear(); |
| 72 | } |
| 73 | |
| 74 | public void testIterableField() throws Exception { |
| 75 | final DependencyManager dm = getDM(); |
| 76 | ConsumerWithIterableField consumer = new ConsumerWithIterableField(); |
| 77 | Component c = createConsumer(dm, consumer); |
| 78 | Component p1 = createProvider(dm, 10, new Provider() { |
| 79 | public void run() { m_ensure.step(); } |
| 80 | public String toString() { return "provider1"; } |
| 81 | }); |
| 82 | Component p2 = createProvider(dm, 20, new Provider() { |
| 83 | public void run() { m_ensure.step();} |
| 84 | public String toString() { return "provider2"; } |
| 85 | }); |
| 86 | |
| 87 | dm.add(p2); |
| 88 | dm.add(p1); |
| 89 | dm.add(c); |
| 90 | // the consumer should have been injected with all providers. |
| 91 | m_ensure.waitForStep(3, 5000); |
| 92 | |
| 93 | // check if all providers are there |
| 94 | Assert.assertNotNull(consumer.getProvider("provider1")); |
| 95 | Assert.assertNotNull(consumer.getProvider("provider2")); |
| 96 | |
| 97 | // remove provider1 |
| 98 | dm.remove(p1); |
| 99 | |
| 100 | // check if provider1 has been removed and if provider2 is still there |
| 101 | Assert.assertNull(consumer.getProvider("provider1")); |
| 102 | Assert.assertNotNull(consumer.getProvider("provider2")); |
| 103 | |
| 104 | // remove provider2, the consumer should be stopped |
| 105 | dm.remove(p2); |
| 106 | m_ensure.waitForStep(4, 5000); |
| 107 | dm.clear(); |
| 108 | } |
| 109 | |
| 110 | public void testMapField() throws Exception { |
| 111 | final DependencyManager dm = getDM(); |
| 112 | ConsumerWithMapField consumer = new ConsumerWithMapField(); |
| 113 | Component c = createConsumer(dm, consumer); |
| 114 | Component p1 = createProvider(dm, 10, new Provider() { |
| 115 | public void run() { m_ensure.step(); } |
| 116 | public String toString() { return "provider1"; } |
| 117 | }); |
| 118 | Component p2 = createProvider(dm, 20, new Provider() { |
| 119 | public void run() { m_ensure.step();} |
| 120 | public String toString() { return "provider2"; } |
| 121 | }); |
| 122 | |
| 123 | dm.add(p2); |
| 124 | dm.add(p1); |
| 125 | dm.add(c); |
| 126 | // the consumer should have been injected with all providers. |
| 127 | m_ensure.waitForStep(3, 5000); |
| 128 | |
| 129 | // check if all providers are there |
| 130 | Assert.assertNotNull(consumer.getProvider("provider1")); |
| 131 | Assert.assertNotNull(consumer.getProvider("provider2")); |
| 132 | |
| 133 | // remove provider1 |
| 134 | dm.remove(p1); |
| 135 | |
| 136 | // check if provider1 has been removed and if provider2 is still there |
| 137 | Assert.assertNull(consumer.getProvider("provider1")); |
| 138 | Assert.assertNotNull(consumer.getProvider("provider2")); |
| 139 | |
| 140 | // remove provider2, the consumer should be stopped |
| 141 | dm.remove(p2); |
| 142 | m_ensure.waitForStep(4, 5000); |
| 143 | dm.clear(); |
| 144 | } |
| 145 | |
| 146 | private Component createProvider(DependencyManager dm, int rank, Provider provider) { |
| 147 | return component(dm).impl(provider).provides(Provider.class, Constants.SERVICE_RANKING, new Integer(rank)).build(); |
| 148 | } |
| 149 | |
| 150 | private Component createConsumer(DependencyManager dm, Object consumer) { |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 151 | return component(dm).impl(consumer).withSvc(Provider.class).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | public static interface Provider extends Runnable { |
| 155 | } |
| 156 | |
| 157 | public class ConsumeWithProviderField { |
| 158 | volatile Provider m_provider; |
| 159 | |
| 160 | void start() { |
| 161 | Assert.assertNotNull(m_provider); |
| 162 | Assert.assertEquals("provider2", m_provider.toString()); |
| 163 | m_ensure.step(1); |
| 164 | } |
| 165 | |
| 166 | public Provider getProvider() { |
| 167 | return m_provider; |
| 168 | } |
| 169 | |
| 170 | void stop() { |
| 171 | m_ensure.step(2); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public class ConsumerWithIterableField { |
| 176 | final Iterable<Provider> m_providers = new ConcurrentLinkedQueue<>(); |
| 177 | final List m_notInjectMe = new ArrayList(); |
| 178 | |
| 179 | void start() { |
| 180 | Assert.assertNotNull(m_providers); |
| 181 | int found = 0; |
| 182 | for (Provider provider : m_providers) { |
| 183 | provider.run(); |
| 184 | found ++; |
| 185 | } |
| 186 | Assert.assertTrue(found == 2); |
| 187 | // The "m_notInjectMe" should not be injected with anything |
| 188 | Assert.assertEquals(m_notInjectMe.size(), 0); |
| 189 | m_ensure.step(3); |
| 190 | } |
| 191 | |
| 192 | public Provider getProvider(String name) { |
| 193 | System.out.println("getProvider(" + name + ") : proviers=" + m_providers); |
| 194 | for (Provider provider : m_providers) { |
| 195 | if (provider.toString().equals(name)) { |
| 196 | return provider; |
| 197 | } |
| 198 | } |
| 199 | return null; |
| 200 | } |
| 201 | |
| 202 | void stop() { |
| 203 | m_ensure.step(4); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | public class ConsumerWithMapField { |
| 208 | final Map<Provider, Dictionary> m_providers = new ConcurrentHashMap<>(); |
| 209 | final Map m_notInjectMe = new HashMap<>(); |
| 210 | |
| 211 | void start() { |
| 212 | Assert.assertNotNull(m_providers); |
| 213 | System.out.println("ConsumerMap.start: injected providers=" + m_providers); |
| 214 | Assert.assertTrue(m_providers.size() == 2); |
| 215 | Assert.assertEquals(0, m_notInjectMe.size()); |
| 216 | for (Map.Entry<Provider, Dictionary> e : m_providers.entrySet()) { |
| 217 | Provider provider = e.getKey(); |
| 218 | Dictionary props = e.getValue(); |
| 219 | |
| 220 | provider.run(); |
| 221 | if (provider.toString().equals("provider1")) { |
| 222 | Assert.assertEquals(props.get(Constants.SERVICE_RANKING), 10); |
| 223 | } else if (provider.toString().equals("provider2")) { |
| 224 | Assert.assertEquals(props.get(Constants.SERVICE_RANKING), 20); |
| 225 | } else { |
| 226 | Assert.fail("Did not find any properties for provider " + provider); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | m_ensure.step(3); |
| 231 | } |
| 232 | |
| 233 | public Provider getProvider(String name) { |
| 234 | System.out.println("getProvider(" + name + ") : providers=" + m_providers); |
| 235 | for (Provider provider : m_providers.keySet()) { |
| 236 | if (provider.toString().equals(name)) { |
| 237 | return provider; |
| 238 | } |
| 239 | } |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | Map<Provider, Dictionary> getProviders() { |
| 244 | return m_providers; |
| 245 | } |
| 246 | |
| 247 | void stop() { |
| 248 | m_ensure.step(4); |
| 249 | } |
| 250 | } |
| 251 | } |