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.Properties; |
| 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.Configuration; |
| 33 | import org.osgi.service.cm.ConfigurationAdmin; |
| 34 | import org.osgi.service.cm.ConfigurationException; |
| 35 | import org.osgi.service.cm.ManagedService; |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 40 | */ |
| 41 | @SuppressWarnings({"unchecked", "rawtypes"}) |
| 42 | public class ConfigurationDependencyTest extends TestBase { |
| 43 | final static String PID = "ConfigurationDependencyTest.pid"; |
| 44 | |
| 45 | public void testComponentWithRequiredConfigurationAndServicePropertyPropagation() { |
| 46 | DependencyManager m = getDM(); |
| 47 | // helper class that ensures certain steps get executed in sequence |
| 48 | Ensure e = new Ensure(); |
| 49 | // create a service provider and consumer |
| 50 | Component s1 = m.createComponent().setImplementation(new ConfigurationConsumer(e)).setInterface(Runnable.class.getName(), null).add(m.createConfigurationDependency().setPid(PID).setPropagate(true)); |
| 51 | Component s2 = m.createComponent().setImplementation(new ConfigurationCreator(e)).add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true)); |
| 52 | Component s3 = m.createComponent().setImplementation(new ConfiguredServiceConsumer(e)).add(m.createServiceDependency().setService(Runnable.class, ("(testkey=testvalue)")).setRequired(true)); |
| 53 | m.add(s1); |
| 54 | m.add(s2); |
| 55 | m.add(s3); |
| 56 | e.waitForStep(4, 50000000); |
| 57 | m.remove(s1); |
| 58 | m.remove(s2); |
| 59 | m.remove(s3); |
| 60 | // ensure we executed all steps inside the component instance |
| 61 | e.step(6); |
| 62 | } |
| 63 | |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 64 | public void testComponentWithRequiredConfigurationWithComponentArgAndServicePropertyPropagation() { |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 65 | DependencyManager m = getDM(); |
| 66 | // helper class that ensures certain steps get executed in sequence |
| 67 | Ensure e = new Ensure(); |
| 68 | // create a service provider and consumer |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 69 | Component s1 = m.createComponent().setImplementation(new ConfigurationConsumerWithComponentArg(e)).setInterface(Runnable.class.getName(), null).add(m.createConfigurationDependency().setPid(PID).setPropagate(true)); |
| 70 | Component s2 = m.createComponent().setImplementation(new ConfigurationCreator(e)).add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true)); |
| 71 | Component s3 = m.createComponent().setImplementation(new ConfiguredServiceConsumer(e)).add(m.createServiceDependency().setService(Runnable.class, ("(testkey=testvalue)")).setRequired(true)); |
| 72 | m.add(s1); |
| 73 | m.add(s2); |
| 74 | m.add(s3); |
| 75 | e.waitForStep(4, 50000000); |
| 76 | m.remove(s1); |
| 77 | m.remove(s2); |
| 78 | m.remove(s3); |
| 79 | // ensure we executed all steps inside the component instance |
| 80 | e.step(6); |
| 81 | } |
| 82 | |
| 83 | public void testComponentWithRequiredConfigurationAndCallbackInstanceAndServicePropertyPropagation() { |
| 84 | Ensure e = new Ensure(); |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 85 | ConfigurationConsumerCallbackInstance callbackInstance = new ConfigurationConsumerCallbackInstance(e); |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 86 | testComponentWithRequiredConfigurationAndCallbackInstanceAndServicePropertyPropagation(callbackInstance, "updateConfiguration", e); |
| 87 | } |
| 88 | |
| 89 | public void testComponentWithRequiredConfigurationAndCallbackInstanceWithComponentArgAndServicePropertyPropagation() { |
| 90 | Ensure e = new Ensure(); |
| 91 | ConfigurationConsumerCallbackInstanceWithComponentArg callbackInstance = new ConfigurationConsumerCallbackInstanceWithComponentArg(e); |
| 92 | testComponentWithRequiredConfigurationAndCallbackInstanceAndServicePropertyPropagation(callbackInstance, "updateConfiguration", e); |
| 93 | } |
| 94 | |
| 95 | public void testComponentWithRequiredConfigurationAndCallbackInstanceAndServicePropertyPropagation |
| 96 | (Object callbackInstance, String updateMethod, Ensure e) { |
| 97 | DependencyManager m = getDM(); |
| 98 | // create a service provider and consumer |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 99 | Component s1 = m.createComponent().setImplementation(new ConfigurationConsumerWithCallbackInstance(e)) |
| 100 | .setInterface(Runnable.class.getName(), null) |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 101 | .add(m.createConfigurationDependency().setPid(PID).setPropagate(true).setCallback(callbackInstance, updateMethod)); |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 102 | Component s2 = m.createComponent().setImplementation(new ConfigurationCreator(e)) |
| 103 | .add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true)); |
| 104 | Component s3 = m.createComponent().setImplementation(new ConfiguredServiceConsumer(e)) |
| 105 | .add(m.createServiceDependency().setService(Runnable.class, ("(testkey=testvalue)")).setRequired(true)); |
| 106 | m.add(s1); |
| 107 | m.add(s2); |
| 108 | m.add(s3); |
| 109 | e.waitForStep(4, 5000); |
| 110 | m.remove(s1); |
| 111 | m.remove(s2); |
| 112 | m.remove(s3); |
| 113 | // ensure we executed all steps inside the component instance |
| 114 | e.step(6); |
| 115 | } |
| 116 | |
| 117 | public void testFELIX2987() { |
| 118 | // mimics testComponentWithRequiredConfigurationAndServicePropertyPropagation |
| 119 | DependencyManager m = getDM(); |
| 120 | // helper class that ensures certain steps get executed in sequence |
| 121 | Ensure e = new Ensure(); |
| 122 | // create a service provider and consumer |
| 123 | Component s1 = m.createComponent().setImplementation(new ConfigurationConsumer2(e)).setInterface(Runnable.class.getName(), null).add(m.createConfigurationDependency().setPid(PID).setPropagate(true)); |
| 124 | Component s2 = m.createComponent().setImplementation(new ConfigurationCreator(e)).add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true)); |
| 125 | Component s3 = m.createComponent().setImplementation(new ConfiguredServiceConsumer(e)).add(m.createServiceDependency().setService(Runnable.class, ("(testkey=testvalue)")).setRequired(true)); |
| 126 | m.add(s1); |
| 127 | m.add(s2); |
| 128 | m.add(s3); |
| 129 | e.waitForStep(4, 5000); |
| 130 | m.remove(s1); |
| 131 | m.remove(s2); |
| 132 | m.remove(s3); |
| 133 | // ensure we executed all steps inside the component instance |
| 134 | e.step(6); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | class ConfigurationCreator { |
| 139 | private volatile ConfigurationAdmin m_ca; |
| 140 | private final Ensure m_ensure; |
| 141 | Configuration m_conf; |
| 142 | |
| 143 | public ConfigurationCreator(Ensure e) { |
| 144 | m_ensure = e; |
| 145 | } |
| 146 | |
| 147 | public void init() { |
| 148 | try { |
| 149 | warn("ConfigurationCreator.init"); |
| 150 | Assert.assertNotNull(m_ca); |
| 151 | m_ensure.step(1); |
| 152 | m_conf = m_ca.getConfiguration(PID, null); |
| 153 | Hashtable props = new Properties(); |
| 154 | props.put("testkey", "testvalue"); |
| 155 | m_conf.update(props); |
| 156 | } |
| 157 | catch (IOException e) { |
| 158 | Assert.fail("Could not create configuration: " + e.getMessage()); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public void destroy() throws IOException { |
| 163 | warn("ConfigurationCreator.destroy"); |
| 164 | m_conf.delete(); |
| 165 | m_ensure.step(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | static class ConfigurationConsumer2 extends ConfigurationConsumer { |
| 170 | public ConfigurationConsumer2(Ensure e) { |
| 171 | super(e); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static class ConfigurationConsumer implements ManagedService, Runnable { |
| 176 | private final Ensure m_ensure; |
| 177 | |
| 178 | public ConfigurationConsumer(Ensure e) { |
| 179 | m_ensure = e; |
| 180 | } |
| 181 | |
| 182 | public void updated(Dictionary props) throws ConfigurationException { |
| 183 | if (props != null) { |
| 184 | m_ensure.step(2); |
| 185 | if (!"testvalue".equals(props.get("testkey"))) { |
| 186 | Assert.fail("Could not find the configured property."); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public void run() { |
| 192 | m_ensure.step(4); |
| 193 | } |
| 194 | } |
| 195 | |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 196 | static class ConfigurationConsumerWithComponentArg extends ConfigurationConsumer { |
| 197 | public ConfigurationConsumerWithComponentArg(Ensure e) { |
| 198 | super(e); |
| 199 | } |
| 200 | |
| 201 | public void updatedWithComponentArg(Component component, Dictionary props) throws ConfigurationException { |
| 202 | Assert.assertNotNull(component); |
| 203 | super.updated(props); |
| 204 | } |
| 205 | } |
| 206 | |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 207 | static class ConfigurationConsumerCallbackInstance { |
| 208 | private final Ensure m_ensure; |
| 209 | |
| 210 | public ConfigurationConsumerCallbackInstance(Ensure e) { |
| 211 | m_ensure = e; |
| 212 | } |
| 213 | |
| 214 | public void updateConfiguration(Dictionary props) throws Exception { |
| 215 | if (props != null) { |
| 216 | m_ensure.step(2); |
| 217 | if (!"testvalue".equals(props.get("testkey"))) { |
| 218 | Assert.fail("Could not find the configured property."); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 224 | static class ConfigurationConsumerCallbackInstanceWithComponentArg extends ConfigurationConsumerCallbackInstance { |
| 225 | |
| 226 | public ConfigurationConsumerCallbackInstanceWithComponentArg(Ensure e) { |
| 227 | super(e); |
| 228 | } |
| 229 | |
| 230 | public void updateConfigurationWithComponentArg(Component component, Dictionary props) throws Exception { |
| 231 | Assert.assertNotNull(component); |
| 232 | super.updateConfiguration(props); |
| 233 | } |
| 234 | } |
| 235 | |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 236 | static class ConfigurationConsumerWithCallbackInstance implements Runnable { |
| 237 | private final Ensure m_ensure; |
| 238 | |
| 239 | public ConfigurationConsumerWithCallbackInstance(Ensure e) { |
| 240 | m_ensure = e; |
| 241 | } |
| 242 | |
| 243 | public void run() { |
| 244 | m_ensure.step(4); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | static class ConfiguredServiceConsumer { |
| 249 | private final Ensure m_ensure; |
| 250 | private volatile Runnable m_runnable; |
| 251 | |
| 252 | public ConfiguredServiceConsumer(Ensure e) { |
| 253 | m_ensure = e; |
| 254 | } |
| 255 | |
| 256 | public void init() { |
| 257 | m_ensure.step(3); |
| 258 | m_runnable.run(); |
| 259 | } |
| 260 | } |
| 261 | } |