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.HashMap; |
| 24 | import java.util.Hashtable; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import org.apache.felix.dm.Component; |
| 28 | import org.apache.felix.dm.DependencyManager; |
| 29 | import org.junit.Assert; |
| 30 | import org.osgi.framework.Constants; |
| 31 | import org.osgi.framework.ServiceReference; |
| 32 | |
| 33 | /** |
| 34 | * This test does some injection tests on components being in INSTANTIATED_AND_WAITING_FOR_REQUIRED state. |
| 35 | * |
| 36 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 37 | */ |
| 38 | @SuppressWarnings({"unchecked", "rawtypes"}) |
| 39 | public class InstanceBoundDependencyTest extends TestBase { |
| 40 | Ensure m_e; |
| 41 | |
| 42 | public void testServiceInjection() { |
| 43 | DependencyManager m = getDM(); |
| 44 | m_e = new Ensure(); |
| 45 | |
| 46 | // Create a "C" component: it depends on some S1 services, and on some S2 instance-bound services (declared from C.init() method) |
| 47 | C cimpl = new C(); |
| 48 | Component c = component(m).impl(cimpl) |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 49 | .withSvc(S1.class, sb->sb.add("addS1").change("changeS1").remove("removeS1").autoConfig(true)).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 50 | m.add(c); |
| 51 | |
| 52 | // Add S1 (s1_1): C.add(S1 s1) is called, then init() is called where a dependency is declared on S2 |
| 53 | Hashtable s1_1_props = new Hashtable(); |
| 54 | s1_1_props.put("name", "s1_1"); |
| 55 | s1_1_props.put(Constants.SERVICE_RANKING, new Integer(10)); |
| 56 | S1Impl s1_1_impl = new S1Impl(); |
| 57 | Component s1_1 = component(m).impl(s1_1_impl).provides(S1.class.getName(), s1_1_props).build(); |
| 58 | m.add(s1_1); |
| 59 | m_e.waitForStep(1, 5000); // wait until C.init called |
| 60 | ServiceReference ref = cimpl.getS1("s1_1"); |
| 61 | Assert.assertNotNull(ref); |
| 62 | Assert.assertNotNull(cimpl.getS1()); |
| 63 | Assert.assertEquals(s1_1_impl, cimpl.getS1()); |
| 64 | |
| 65 | // At this point, MyComponent is in INSTANTIATED_AND_WAITING_FOR_REQUIRED state. |
| 66 | // add now add another higher ranked S1 (s1_2) instance. C.add(s1_2) method should be called (the S1 dependency |
| 67 | // is not instance bound), and m_s1 autoconfig field should be updated. |
| 68 | Hashtable s1_2_props = new Hashtable(); |
| 69 | s1_2_props.put(Constants.SERVICE_RANKING, new Integer(20)); |
| 70 | s1_2_props.put("name", "s1_2"); |
| 71 | S1Impl s1_2_impl = new S1Impl(); |
| 72 | Component s1_2 = component(m).impl(s1_2_impl).provides(S1.class.getName(), s1_2_props).build(); |
| 73 | m.add(s1_2); |
| 74 | ref = cimpl.getS1("s1_2"); |
| 75 | Assert.assertNotNull(ref); |
| 76 | Assert.assertNotNull(cimpl.getS1()); |
| 77 | Assert.assertEquals(s1_2_impl, cimpl.getS1()); // must return s1_2 with ranking = 20 |
| 78 | |
| 79 | // Now, change the s1_1 service properties: C.changed(s1_1) should be called, and C.m_s1AutoConfig should be updated |
| 80 | s1_1_props.put(Constants.SERVICE_RANKING, new Integer(30)); |
| 81 | s1_1.setServiceProperties(s1_1_props); |
| 82 | ref = cimpl.getS1("s1_1"); |
| 83 | Assert.assertNotNull(ref); |
| 84 | Assert.assertEquals(new Integer(30), ref.getProperty(Constants.SERVICE_RANKING)); |
| 85 | Assert.assertNotNull(cimpl.getS1()); |
| 86 | Assert.assertEquals(s1_1_impl, cimpl.getS1()); |
| 87 | |
| 88 | // Now, remove the s1_1: C.remove(s1_1) should be called, and C.m_s1AutoConfig should be updated |
| 89 | m.remove(s1_1); |
| 90 | ref = cimpl.getS1("s1_1"); |
| 91 | Assert.assertNull(cimpl.getS1("s1_1")); |
| 92 | Assert.assertNotNull(cimpl.getS1()); |
| 93 | Assert.assertEquals(s1_2_impl, cimpl.getS1()); |
| 94 | m.clear(); |
| 95 | } |
| 96 | |
| 97 | // C component depends on some S1 required services |
| 98 | public interface S1 { |
| 99 | } |
| 100 | |
| 101 | public class S1Impl implements S1 { |
| 102 | } |
| 103 | |
| 104 | public interface S2 { |
| 105 | } |
| 106 | |
| 107 | public class S2Impl implements S2 { |
| 108 | } |
| 109 | |
| 110 | // Our "C" component: it depends on S1 (required) and S2 (required/instance bound) |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 111 | // Class tested with reflection based callbacks |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 112 | class C { |
| 113 | final Map<String, ServiceReference> m_s1Map = new HashMap(); |
| 114 | final Map<String, ServiceReference> m_s2Map = new HashMap(); |
| 115 | volatile S1 m_s1; // auto configured |
| 116 | |
| 117 | S1 getS1() { |
| 118 | return m_s1; |
| 119 | } |
| 120 | |
| 121 | void addS1(ServiceReference s1) { |
| 122 | m_s1Map.put((String) s1.getProperty("name"), s1); |
| 123 | } |
| 124 | |
| 125 | void changeS1(ServiceReference s1) { |
| 126 | m_s1Map.put((String) s1.getProperty("name"), s1); |
| 127 | } |
| 128 | |
| 129 | void removeS1(ServiceReference s1) { |
| 130 | m_s1Map.remove((String) s1.getProperty("name")); |
| 131 | } |
| 132 | |
| 133 | void addS2(ServiceReference s2) { |
| 134 | m_s2Map.put((String) s2.getProperty("name"), s2); |
| 135 | } |
| 136 | |
| 137 | void changeS2(ServiceReference s2) { |
| 138 | m_s2Map.put((String) s2.getProperty("name"), s2); |
| 139 | } |
| 140 | |
| 141 | void removeS2(ServiceReference s2) { |
| 142 | m_s2Map.remove((String) s2.getProperty("name")); |
| 143 | } |
| 144 | |
| 145 | ServiceReference getS1(String name) { |
| 146 | return m_s1Map.get(name); |
| 147 | } |
| 148 | |
| 149 | ServiceReference getS2(String name) { |
| 150 | return m_s2Map.get(name); |
| 151 | } |
| 152 | |
| 153 | void init(Component c) { |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 154 | component(c, comp->comp.withSvc(S2.class, srv -> srv.add("addS2").change("changeS2").remove("removeS2"))); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 155 | m_e.step(1); |
| 156 | } |
| 157 | } |
| 158 | } |