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 | |
| 25 | import org.junit.Assert; |
| 26 | |
| 27 | import org.apache.felix.dm.Component; |
| 28 | import org.apache.felix.dm.DependencyManager; |
| 29 | import org.apache.felix.dm.itest.util.Ensure; |
| 30 | import org.apache.felix.dm.itest.util.TestBase; |
| 31 | import org.osgi.framework.BundleContext; |
| 32 | import org.osgi.service.cm.Configuration; |
| 33 | import org.osgi.service.cm.ConfigurationAdmin; |
| 34 | import org.osgi.service.metatype.AttributeDefinition; |
| 35 | import org.osgi.service.metatype.MetaTypeInformation; |
| 36 | import org.osgi.service.metatype.MetaTypeService; |
| 37 | import org.osgi.service.metatype.ObjectClassDefinition; |
| 38 | |
| 39 | /** |
| 40 | * Tests an Adapter which adapts A To B interface. |
| 41 | * And the Adapter also depends on a Configuration Dependency with MetaType support. |
| 42 | * |
| 43 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 44 | */ |
| 45 | public class AdapterWithConfigurationAndMetaType extends TestBase { |
| 46 | final static String PID = "AdapterWithConfigurationAndMetaType"; |
| 47 | final static String PID_HEADING = "English Dictionary"; |
| 48 | final static String PID_DESC = "Configuration for the EnglishDictionary Service"; |
| 49 | final static String WORDS_HEADING = "English words"; |
| 50 | final static String WORDS_DESC = "Declare here some valid English words"; |
| 51 | final static String WORDS_PROPERTY = "words"; |
| 52 | |
| 53 | public void testAdapterWithConfigurationDependencyAndMetaType() { |
| 54 | DependencyManager m = getDM(); |
| 55 | Ensure e = new Ensure(); |
| 56 | |
| 57 | m.add(m.createAdapterService(A.class, null) |
| 58 | .setInterface(B.class.getName(), null) |
| 59 | .setImplementation(new BImpl(e)) |
| 60 | .add(m.createConfigurationDependency() |
| 61 | .setPid(PID) |
| 62 | .setHeading(PID_HEADING) |
| 63 | .setDescription(PID_DESC) |
| 64 | .add(m.createPropertyMetaData() |
| 65 | .setCardinality(Integer.MAX_VALUE) |
| 66 | .setType(String.class) |
| 67 | .setHeading(WORDS_HEADING) |
| 68 | .setDescription(WORDS_DESC) |
| 69 | .setDefaults(new String[] {"hello", "world"}) |
| 70 | .setId(WORDS_PROPERTY)))); |
| 71 | |
| 72 | m.add(m.createComponent() |
| 73 | .setInterface(A.class.getName(), null) |
| 74 | .setImplementation(new AImpl())); |
| 75 | |
| 76 | Component configurator = m.createComponent() |
| 77 | .setImplementation(new Configurator(e)) |
| 78 | .add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true)) |
| 79 | .add(m.createServiceDependency().setService(MetaTypeService.class).setRequired(true)); |
| 80 | m.add(configurator); |
| 81 | |
| 82 | // Ensures that all components are started |
| 83 | e.waitForStep(4, 5000); |
| 84 | |
| 85 | // now stop configurator, and ensure that all components have been stopped |
| 86 | m.remove(configurator); |
| 87 | e.waitForStep(7, 5000); |
| 88 | m.clear(); |
| 89 | } |
| 90 | |
| 91 | public interface A { |
| 92 | } |
| 93 | |
| 94 | public interface B { |
| 95 | } |
| 96 | |
| 97 | public class AImpl implements A { |
| 98 | } |
| 99 | |
| 100 | public class Configurator { |
| 101 | volatile MetaTypeService m_metaType; |
| 102 | volatile ConfigurationAdmin m_cm; |
| 103 | volatile BundleContext m_ctx; |
| 104 | final Ensure m_ensure; |
| 105 | Configuration m_conf; |
| 106 | |
| 107 | Configurator(Ensure ensure) { |
| 108 | m_ensure = ensure; |
| 109 | } |
| 110 | |
| 111 | void start() { |
| 112 | m_ensure.step(1); |
| 113 | checkMetaTypeAndConfigure(); |
| 114 | } |
| 115 | |
| 116 | void stop() { |
| 117 | m_ensure.step(5); |
| 118 | if (m_conf != null) { |
| 119 | try { |
| 120 | m_ensure.step(6); |
| 121 | m_conf.delete(); |
| 122 | } |
| 123 | catch (IOException e) { |
| 124 | m_ensure.throwable(e); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void checkMetaTypeAndConfigure() { |
| 130 | MetaTypeInformation info = m_metaType.getMetaTypeInformation(m_ctx.getBundle()); |
| 131 | Assert.assertNotNull(info); |
| 132 | Assert.assertEquals(PID, info.getPids()[0]); |
| 133 | ObjectClassDefinition ocd = info.getObjectClassDefinition(PID, null); |
| 134 | Assert.assertNotNull(ocd); |
| 135 | Assert.assertEquals(PID_HEADING, ocd.getName()); |
| 136 | Assert.assertEquals(PID_DESC, ocd.getDescription()); |
| 137 | AttributeDefinition[] defs = ocd.getAttributeDefinitions(ObjectClassDefinition.ALL); |
| 138 | Assert.assertNotNull(defs); |
| 139 | Assert.assertEquals(1, defs.length); |
| 140 | Assert.assertEquals(WORDS_HEADING, defs[0].getName()); |
| 141 | Assert.assertEquals(WORDS_DESC, defs[0].getDescription()); |
| 142 | Assert.assertEquals(WORDS_PROPERTY, defs[0].getID()); |
| 143 | m_ensure.step(2); |
| 144 | |
| 145 | try { |
| 146 | m_conf = m_cm.getConfiguration(PID, null); |
| 147 | Hashtable<String, String> props = new Hashtable<>(); |
| 148 | props.put("foo", "bar"); |
| 149 | m_conf.update(props); |
| 150 | } catch (Throwable t) { |
| 151 | m_ensure.throwable(t); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | public class BImpl implements B, A { |
| 157 | final Ensure m_ensure; |
| 158 | volatile A m_a; |
| 159 | Dictionary<String, String> m_conf; |
| 160 | |
| 161 | public BImpl(Ensure e) { |
| 162 | m_ensure = e; |
| 163 | } |
| 164 | |
| 165 | public void updated(Dictionary<String, String> conf) { |
| 166 | if (conf != null) { |
| 167 | m_ensure.step(3); |
| 168 | m_conf = conf; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | public void start() { |
| 173 | Assert.assertNotNull(m_a); |
| 174 | Assert.assertNotNull(m_conf); |
| 175 | Assert.assertEquals("bar", m_conf.get("foo")); |
| 176 | m_ensure.step(4); |
| 177 | } |
| 178 | |
| 179 | public void stop() { |
| 180 | m_ensure.step(7); |
| 181 | } |
| 182 | } |
| 183 | } |