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