Pierre De Rop | 863fbe2 | 2015-09-18 22:04:50 +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 org.apache.felix.dm.Component; |
| 22 | import org.apache.felix.dm.Dependency; |
| 23 | import org.apache.felix.dm.DependencyManager; |
| 24 | import org.apache.felix.dm.itest.util.Ensure; |
| 25 | import org.apache.felix.dm.itest.util.TestBase; |
| 26 | import org.osgi.framework.BundleContext; |
| 27 | import org.osgi.framework.ServiceRegistration; |
| 28 | |
| 29 | /** |
| 30 | * This test validates a corner case: |
| 31 | * |
| 32 | * We have the following component players: A, BFactory, B. |
| 33 | * |
| 34 | * component A defines from A.init() a required dependency on BFactory, and an optional dependency on B. |
| 35 | * component A has a "start" lifecycle callback. |
| 36 | * |
| 37 | * when A.bind(BFactory factory) is called, the factory.create() method is then invoked, which triggers a registration of the B Service. |
| 38 | * At this point B is available, then A.start() should be called before A.bind(B). |
| 39 | * |
| 40 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 41 | */ |
| 42 | public class FELIX5045_OptionalDependencyCBCalledBeforeStartTest extends TestBase { |
| 43 | public void test_A_DependsOnBFactoryFromInit() throws Throwable { |
| 44 | final DependencyManager m = getDM(); |
| 45 | Ensure e = new Ensure(); |
| 46 | |
| 47 | Component bFactory = m.createComponent().setImplementation(new BFactory()).setInterface(BFactory.class.getName(), null); |
| 48 | Component a = m.createComponent().setImplementation(new A(e)); |
| 49 | |
| 50 | // Enable first bFactory. |
| 51 | m.add(bFactory); |
| 52 | |
| 53 | // Then enable A. |
| 54 | m.add(a); |
| 55 | |
| 56 | // A should get BFactory, then it should instantiate B, then A.start() should be called, then A.bind(B) should be called. |
| 57 | e.waitForStep(4, 5000); |
| 58 | |
| 59 | // Now, remove BFactory. A.unbind(B b) should be called, then |
| 60 | m.remove(bFactory); |
| 61 | e.waitForStep(6, 5000); |
| 62 | e.ensure(); |
| 63 | } |
| 64 | |
| 65 | public static class A { |
| 66 | final Ensure m_e; |
| 67 | |
| 68 | public A(Ensure e) { |
| 69 | m_e = e; |
| 70 | } |
| 71 | |
| 72 | void init(Component component) { |
| 73 | m_e.step(1); |
| 74 | DependencyManager dm = component.getDependencyManager(); |
| 75 | Dependency depBFactory = dm.createServiceDependency().setService(BFactory.class).setRequired(true).setCallbacks("bind", "unbind"); |
| 76 | Dependency depB = dm.createServiceDependency().setService(B.class).setRequired(false).setCallbacks("bind", "unbind"); |
| 77 | component.add(depBFactory, depB); |
| 78 | } |
| 79 | |
| 80 | void bind(BFactory bFactory) { |
| 81 | m_e.step(2); |
| 82 | bFactory.createB(); |
| 83 | } |
| 84 | |
| 85 | void start() { |
| 86 | m_e.step(3); |
| 87 | } |
| 88 | |
| 89 | void bind(B b) { |
| 90 | m_e.step(4); |
| 91 | } |
| 92 | |
| 93 | void unbind(B b) { |
| 94 | m_e.step(5); |
| 95 | } |
| 96 | |
| 97 | void unbind(BFactory bFactory) { |
| 98 | m_e.step(6); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public static class BFactory { |
| 103 | volatile BundleContext m_bctx; |
| 104 | ServiceRegistration m_registraiton; |
| 105 | |
| 106 | void createB() { |
| 107 | m_registraiton = m_bctx.registerService(B.class.getName(), new B(), null); |
| 108 | } |
| 109 | |
| 110 | void deleteB() { |
| 111 | m_registraiton.unregister(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public static class B { |
| 116 | } |
| 117 | } |