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