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 | import static org.apache.felix.dm.lambda.DependencyManagerActivator.factoryPidAdapter; |
| 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.util.Dictionary; |
| 26 | import java.util.Hashtable; |
| 27 | import java.util.Map; |
| 28 | |
| 29 | import org.apache.felix.dm.Component; |
| 30 | import org.apache.felix.dm.DependencyManager; |
| 31 | import org.junit.Assert; |
| 32 | import org.osgi.service.cm.ConfigurationAdmin; |
| 33 | |
| 34 | /** |
| 35 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 36 | */ |
| 37 | @SuppressWarnings({"unchecked", "rawtypes"}) |
| 38 | public class FactoryConfigurationAdapterTest extends TestBase |
| 39 | { |
| 40 | private static Ensure m_ensure; |
| 41 | |
| 42 | public void testFactoryConfigurationAdapter() { |
| 43 | testFactoryConfigurationAdapter(Adapter.class, "updated"); |
| 44 | } |
| 45 | |
| 46 | public void testFactoryConfigurationAdapterWithUpdatedCallbackThatTakesComponentAsParameter() { |
| 47 | testFactoryConfigurationAdapter(AdapterWithUpdateMethodThatTakesComponentAsParameter.class, "updatedWithComponent"); |
| 48 | } |
| 49 | |
| 50 | public void testFactoryConfigurationAdapter(Class<?> adapterImplClass, String adapterUpdate) { |
| 51 | DependencyManager m = getDM(); |
| 52 | // helper class that ensures certain steps get executed in sequence |
| 53 | m_ensure = new Ensure(); |
| 54 | |
| 55 | // Create a Configuration instance, which will create/update/remove a configuration for factoryPid "MyFactoryPid" |
| 56 | ConfigurationCreator configurator = new ConfigurationCreator("MyFactoryPid", "key", "value1"); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 57 | Component s1 = component(m).impl(configurator).withSvc(ConfigurationAdmin.class).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 58 | |
| 59 | // Create an Adapter that will be instantiated, once the configuration is created. |
| 60 | // This Adapter provides an AdapterService, and depends on an AdapterExtraDependency service. |
| 61 | Component s2 = factoryPidAdapter(m) |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 62 | .factoryPid("MyFactoryPid").impl(adapterImplClass).update(adapterUpdate).propagate().provides(AdapterService.class, "foo", "bar") |
| 63 | .withSvc(AdapterExtraDependency.class) |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 64 | .build(); |
| 65 | |
| 66 | // Create extra adapter service dependency upon which our adapter depends on. |
| 67 | Component s3 = component(m) |
| 68 | .impl(new AdapterExtraDependency()).provides(AdapterExtraDependency.class).build(); |
| 69 | |
| 70 | // Create an AdapterService Consumer |
| 71 | Component s4 = component(m) |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 72 | .impl(AdapterServiceConsumer.class).withSvc(AdapterService.class, srv -> srv.add("bind").change("change").remove("remove")).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 73 | |
| 74 | // Start services |
| 75 | m.add(s1); |
| 76 | m.add(s2); |
| 77 | m.add(s3); |
| 78 | m.add(s4); |
| 79 | |
| 80 | // Wait for step 8: the AdapterService consumer has been injected with the AdapterService, and has called the doService method. |
| 81 | m_ensure.waitForStep(8, 10000); |
| 82 | |
| 83 | // Modify configuration. |
| 84 | configurator.update("key", "value2"); |
| 85 | |
| 86 | // Wait for step 13: the AdapterService has been updated, and the AdapterService consumer has seen the change |
| 87 | m_ensure.waitForStep(13, 10000); |
| 88 | |
| 89 | // Remove the configuration |
| 90 | m.remove(s1); // The stop method will remove the configuration |
| 91 | m_ensure.waitForStep(16, 10000); |
| 92 | m.clear(); |
| 93 | } |
| 94 | |
| 95 | public static class ConfigurationCreator { |
| 96 | private volatile ConfigurationAdmin m_ca; |
| 97 | private String m_key; |
| 98 | private String m_value; |
| 99 | private org.osgi.service.cm.Configuration m_conf; |
| 100 | private String m_factoryPid; |
| 101 | |
| 102 | public ConfigurationCreator(String factoryPid, String key, String value) { |
| 103 | m_factoryPid = factoryPid; |
| 104 | m_key = key; |
| 105 | m_value = value; |
| 106 | } |
| 107 | |
| 108 | public void start() { |
| 109 | try { |
| 110 | m_ensure.step(1); |
| 111 | m_conf = m_ca.createFactoryConfiguration(m_factoryPid, null); |
| 112 | Hashtable props = new Hashtable(); |
| 113 | props.put(m_key, m_value); |
| 114 | m_conf.update(props); |
| 115 | } |
| 116 | catch (IOException e) { |
| 117 | Assert.fail("Could not create configuration: " + e.getMessage()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public void update(String key, String val) { |
| 122 | Hashtable props = new Hashtable(); |
| 123 | props.put(key, val); |
| 124 | try { |
| 125 | m_conf.update(props); |
| 126 | } |
| 127 | catch (IOException e) { |
| 128 | Assert.fail("Could not update configuration: " + e.getMessage()); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public void stop() { |
| 133 | try |
| 134 | { |
| 135 | m_conf.delete(); |
| 136 | } |
| 137 | catch (IOException e) |
| 138 | { |
| 139 | Assert.fail("Could not remove configuration: " + e.toString()); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | public interface AdapterService { |
| 145 | public void doService(); |
| 146 | } |
| 147 | |
| 148 | public static class AdapterExtraDependency { |
| 149 | } |
| 150 | |
| 151 | public static class Adapter implements AdapterService { |
| 152 | volatile AdapterExtraDependency m_extraDependency; // extra dependency. |
| 153 | private int updateCount; |
| 154 | |
| 155 | void updated(Dictionary settings) { |
| 156 | updateCount ++; |
| 157 | if (updateCount == 1) { |
| 158 | m_ensure.step(2); |
| 159 | Assert.assertEquals(true, "value1".equals(settings.get("key"))); |
| 160 | m_ensure.step(3); |
| 161 | } else if (updateCount == 2) { |
| 162 | m_ensure.step(9); |
| 163 | Assert.assertEquals(true, "value2".equals(settings.get("key"))); |
| 164 | m_ensure.step(10); |
| 165 | } else { |
| 166 | Assert.fail("wrong call to updated method: count=" + updateCount); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | public void doService() { |
| 171 | m_ensure.step(8); |
| 172 | } |
| 173 | |
| 174 | public void start() { |
| 175 | m_ensure.step(4); |
| 176 | Assert.assertNotNull(m_extraDependency); |
| 177 | m_ensure.step(5); |
| 178 | } |
| 179 | |
| 180 | public void stop() { |
| 181 | m_ensure.step(16); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public static class AdapterWithUpdateMethodThatTakesComponentAsParameter extends Adapter { |
| 186 | void updatedWithComponent(Component component, Dictionary settings) { |
| 187 | Assert.assertNotNull(component); |
| 188 | Assert.assertEquals(this, component.getInstance()); |
| 189 | super.updated(settings); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | public static class AdapterServiceConsumer { |
| 194 | private AdapterService m_adapterService; |
| 195 | private Map m_adapterServiceProperties; |
| 196 | |
| 197 | void bind(Map serviceProperties, AdapterService adapterService) { |
| 198 | m_ensure.step(6); |
| 199 | m_adapterService = adapterService; |
| 200 | m_adapterServiceProperties = serviceProperties; |
| 201 | } |
| 202 | |
| 203 | void change(Map serviceProperties, AdapterService adapterService) { |
| 204 | m_ensure.step(11); |
| 205 | Assert.assertEquals(true, "value2".equals(m_adapterServiceProperties.get("key"))); |
| 206 | m_ensure.step(12); |
| 207 | Assert.assertEquals(true, "bar".equals(m_adapterServiceProperties.get("foo"))); |
| 208 | m_ensure.step(13); |
| 209 | } |
| 210 | |
| 211 | public void start() { |
| 212 | m_ensure.step(7); |
| 213 | Assert.assertNotNull(m_adapterService); |
| 214 | Assert.assertEquals(true, "value1".equals(m_adapterServiceProperties.get("key"))); |
| 215 | Assert.assertEquals(true, "bar".equals(m_adapterServiceProperties.get("foo"))); |
| 216 | m_adapterService.doService(); |
| 217 | } |
| 218 | |
| 219 | public void stop() { |
| 220 | m_ensure.step(14); |
| 221 | } |
| 222 | |
| 223 | void remove(AdapterService adapterService) { |
| 224 | m_ensure.step(15); |
| 225 | } |
| 226 | } |
| 227 | } |