blob: fa581262e9ed0956bb5696d853277a1bf2597c31 [file] [log] [blame]
Pierre De Ropfaca2892016-01-31 23:27:05 +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.lambda.itest;
20
21import static org.apache.felix.dm.lambda.DependencyManagerActivator.component;
22
23import org.apache.felix.dm.Component;
24import org.apache.felix.dm.DependencyManager;
25
26/**
27 * Test which validates multi-dependencies combination.
28 *
29 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
30 */
31@SuppressWarnings({"unchecked", "rawtypes", "serial"})
32public class MultipleExtraDependencyTest extends TestBase {
33 public void testMultipleExtraDependencies()
34 {
35 DependencyManager m = getDM();
36 Ensure e = new Ensure();
37
38 Component sp2 = component(m)
39 .impl(ServiceProvider2.class).provides(ServiceProvider2.class)
Pierre De Rop11527502016-02-18 21:07:16 +000040 .withSvc(Runnable.class, srv->srv.filter("(foo=bar)").required(false).autoConfig("m_runnable"))
41 .withSvc(Sequencer.class, srv->srv.add("bind"))
Pierre De Ropfaca2892016-01-31 23:27:05 +000042 .composition("getComposition")
43 .build();
44
45 Component sp = component(m)
46 .impl(ServiceProvider.class)
47 .provides(ServiceInterface.class, foo -> "bar")
48 .start("start").stop("stop")
Pierre De Rop11527502016-02-18 21:07:16 +000049 .withSvc(Sequencer.class, srv->srv.autoConfig("m_sequencer"))
50 .withSvc(ServiceProvider2.class, srv->srv.add("bind").remove("unbind"))
Pierre De Ropfaca2892016-01-31 23:27:05 +000051 .build();
52
53 Component sc = component(m)
54 .impl(ServiceConsumer.class)
55 .start("start").stop("stop")
Pierre De Rop11527502016-02-18 21:07:16 +000056 .withSvc(Sequencer.class, srv->srv.autoConfig("m_sequencer"))
57 .withSvc(ServiceInterface.class, srv->srv.filter("(foo=bar)").autoConfig("m_service"))
Pierre De Ropfaca2892016-01-31 23:27:05 +000058 .build();
59
60 Component sequencer = component(m)
61 .impl(new SequencerImpl(e))
62 .provides(Sequencer.class.getName())
63 .build();
64
65 m.add(sp2);
66 m.add(sp);
67 m.add(sc);
68 m.add(sequencer);
69
70 // Check if ServiceProvider component have been initialized orderly
71 e.waitForStep(7, 5000);
72
73 // Stop the test.annotation bundle
74 m.remove(sequencer);
75 m.remove(sp);
76 m.remove(sp2);
77 m.remove(sc);
78
79 // And check if ServiceProvider2 has been deactivated orderly
80 e.waitForStep(11, 5000);
81 }
82
83 public interface Sequencer
84 {
85 void step();
86 void step(int step);
87 void waitForStep(int step, int timeout);
88 }
89
90 public static class SequencerImpl implements Sequencer {
91 Ensure m_ensure;
92
93 public SequencerImpl(Ensure e)
94 {
95 m_ensure = e;
96 }
97
98 public void step()
99 {
100 m_ensure.step();
101 }
102
103 public void step(int step)
104 {
105 m_ensure.step(step);
106 }
107
108 public void waitForStep(int step, int timeout)
109 {
110 m_ensure.waitForStep(step, timeout);
111 }
112 }
113
114 public interface ServiceInterface
115 {
116 public void doService();
117 }
118
119 public static class ServiceConsumer
120 {
121 volatile Sequencer m_sequencer;
122 volatile ServiceInterface m_service;
123
124 void start()
125 {
126 m_sequencer.step(6);
127 m_service.doService();
128 }
129
130 void stop()
131 {
132 m_sequencer.step(8);
133 }
134 }
135
136 public static class ServiceProvider implements ServiceInterface
137 {
138 Sequencer m_sequencer;
139 ServiceProvider2 m_serviceProvider2;
140
141 void bind(ServiceProvider2 provider2)
142 {
143 m_serviceProvider2 = provider2;
144 }
145
146 void start()
147 {
148 m_serviceProvider2.step(4);
149 m_sequencer.step(5);
150 }
151
152 void stop()
153 {
154 m_sequencer.step(9);
155 }
156
157 void unbind(ServiceProvider2 provider2)
158 {
159 m_sequencer.step(10);
160 }
161
162 public void doService()
163 {
164 m_sequencer.step(7);
165 }
166 }
167
168 public static class ServiceProvider2
169 {
170 Composite m_composite = new Composite();
171 Sequencer m_sequencer;
172 Runnable m_runnable;
173
174 void bind(Sequencer seq)
175 {
176 m_sequencer = seq;
177 m_sequencer.step(1);
178 }
179
180 void start()
181 {
182 m_sequencer.step(3);
183 m_runnable.run(); // NullObject
184 }
185
186 public void step(int step) // called by ServiceProvider.start() method
187 {
188 m_sequencer.step(step);
189 }
190
191 void stop()
192 {
193 m_sequencer.step(11);
194 }
195
196 Object[] getComposition()
197 {
198 return new Object[] { this, m_composite };
199 }
200 }
201
202 public static class Composite
203 {
204 void bind(Sequencer seq)
205 {
206 seq.step(2);
207 }
208 }
209}