blob: 58f64ec4abc7286ae99bfff5ce3867e9d2f073df [file] [log] [blame]
Pierre De Rop914616e2015-06-04 06:57:52 +00001/*
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 */
19package org.apache.felix.dm.itest.api;
20
21import org.apache.felix.dm.Component;
22import org.apache.felix.dm.Dependency;
23import org.apache.felix.dm.DependencyManager;
24import org.apache.felix.dm.itest.util.Ensure;
25import org.apache.felix.dm.itest.util.TestBase;
26import org.osgi.framework.BundleContext;
27import 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 optionally depends on BFactory.
35 * component A optionally depends on B
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 A.bind(B) should be called back.
39 *
40 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
41 */
42public class FELIX4913_OptionalCallbackInvokedTwiceTest extends TestBase {
43
44 public void test_A_Defines_BDependency_FromInitMethod() throws Throwable {
45 final DependencyManager m = getDM();
46 Ensure e = new Ensure();
47
48 Component bFactory = m.createComponent().setImplementation(new BFactory()).setInterface(BFactory.class.getName(), null);
49 Dependency dep = m.createServiceDependency().setService(B.class).setRequired(false).setCallbacks("bind", "unbind");
50 Component a = m.createComponent()
51 .setImplementation(new A(e, dep))
52 .add(m.createServiceDependency().setService(BFactory.class).setRequired(false).setCallbacks("bind", "unbind"));
53
54 // Enable first bFactory.
55 m.add(bFactory);
56
57 // Then Enable A.
58 m.add(a);
59
60 // A should get BFactory, then it should instantiate B, and B should then be bound to A.
61 e.waitForStep(4, 5000);
62
63 // Now, remove BFactory. The AComponent should then call bFactory.removeB(), abd A.unbind(B) should be called.
64 m.remove(bFactory);
65 e.waitForStep(6, 5000);
66 e.ensure();
67 clearComponents();
68 }
69
70 public void test_A_Defines_BDependency_BeforeActivation() throws Throwable {
71 final DependencyManager m = getDM();
72 Ensure e = new Ensure();
73
74 Component bFactory = m.createComponent()
75 .setImplementation(new BFactory()).setInterface(BFactory.class.getName(), null);
76 Component a = m.createComponent()
77 .setImplementation(new A(e, null))
78 .add(m.createServiceDependency().setService(B.class).setRequired(false).setCallbacks("bind", "unbind"))
79 .add(m.createServiceDependency().setService(BFactory.class).setRequired(false).setCallbacks("bind", "unbind"));
80
81 // Enable first bFactory.
82 m.add(bFactory);
83
84 // Then Enable A.
85 m.add(a);
86
87 // A should get BFactory, then it should instantiate B, and B should then be bound to A.
88 e.waitForStep(4, 5000);
89
90 // Now, remove BFactory. The AComponent should then call bFactory.removeB(), abd A.unbind(B) should be called.
91 m.remove(bFactory);
92 e.waitForStep(6, 5000);
93 e.ensure();
94 clearComponents();
95 }
96
97 public static class A {
98 final Ensure m_e;
99 final Dependency m_BDependency;
100
101 public A(Ensure e, Dependency bfactoryDependency) {
102 m_e = e;
103 m_BDependency = bfactoryDependency;
104 }
105
106 void init(Component component) {
107 m_e.step(1);
108 if (m_BDependency != null) {
109 component.add(m_BDependency);
110 }
111 }
112
113 void start() {
114 m_e.step(2);
115 }
116
117 void bind(BFactory bFactory) {
118 m_e.step(3);
119 bFactory.createB();
120 }
121
122 void unbind(BFactory bFactory) {
123 m_e.step(5);
124 bFactory.deleteB();
125 }
126
127 void bind(B b) {
128 m_e.step(4);
129 }
130
131 void unbind(B b) {
132 m_e.step(6);
133 }
134 }
135
136 public static class BFactory {
137 volatile BundleContext m_bctx;
138 ServiceRegistration m_registraiton;
139
140 void createB() {
141 m_registraiton = m_bctx.registerService(B.class.getName(), new B(), null);
142 }
143
144 void deleteB() {
145 m_registraiton.unregister();
146 }
147 }
148
149 public static class B {
150 }
151
152}