blob: 0d55e239c76b30dcc6028c6f908303fbbf306e27 [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;
25import org.osgi.framework.Constants;
26
27/**
28 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
29 */
Pierre De Ropfaca2892016-01-31 23:27:05 +000030public class MultipleServiceDependencyTest extends TestBase {
31 public void testMultipleServiceRegistrationAndConsumption() {
32 DependencyManager m = getDM();
33 // helper class that ensures certain steps get executed in sequence
34 Ensure e = new Ensure();
35 // create a service provider and consumer
36 Component provider = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build();
37 Component providerWithHighRank = component(m).impl(new ServiceProvider2(e)).provides(ServiceInterface.class.getName(), Constants.SERVICE_RANKING, Integer.valueOf(5)).build();
Pierre De Rop11527502016-02-18 21:07:16 +000038 Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +000039 m.add(provider);
40 m.add(providerWithHighRank);
41 m.add(consumer);
42 e.waitForStep(3, 5000);
43 m.remove(providerWithHighRank);
44 e.step(4);
45 e.waitForStep(5, 5000);
46 m.remove(provider);
47 m.remove(consumer);
48 e.waitForStep(6, 5000);
49 }
50
51 public void testReplacementAutoConfig() {
52 DependencyManager m = getDM();
53 // helper class that ensures certain steps get executed in sequence
54 Ensure e = new Ensure();
55 // create a service provider and consumer
56 Component provider = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build();
57 Component provider2 = component(m).impl(new ServiceProvider2(e)).provides(ServiceInterface.class.getName()).build();
Pierre De Rop11527502016-02-18 21:07:16 +000058 Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +000059 m.add(provider2);
60 m.add(consumer);
61 e.waitForStep(3, 5000);
62 m.add(provider);
63 m.remove(provider2);
64 e.step(4);
65 e.waitForStep(5, 5000);
66 m.remove(provider);
67 m.remove(consumer);
68 e.waitForStep(6, 5000);
69 }
70
71 public void testReplacementCallbacks() {
72 DependencyManager m = getDM();
73 // helper class that ensures certain steps get executed in sequence
74 Ensure e = new Ensure();
75 // create a service provider and consumer
76 Component provider = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class.getName()).build();
77 Component provider2 = component(m).impl(new ServiceProvider2(e)).provides(ServiceInterface.class.getName()).build();
Pierre De Rop11527502016-02-18 21:07:16 +000078 Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(ServiceInterface.class, srv->srv.add("add").remove("remove")).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +000079 m.add(provider2);
80 m.add(consumer);
81 e.waitForStep(3, 15000);
82 m.add(provider);
83 m.remove(provider2);
84 e.step(4);
85 e.waitForStep(5, 15000);
86 m.remove(provider);
87 m.remove(consumer);
88 e.waitForStep(6, 15000);
89 }
90
91 static interface ServiceInterface {
92 public void invoke();
93 }
94
95 static class ServiceProvider implements ServiceInterface {
96 private final Ensure m_ensure;
97 public ServiceProvider(Ensure e) {
98 m_ensure = e;
99 }
100 public void invoke() {
101 m_ensure.step(5);
102 }
103 }
104
105 static class ServiceProvider2 implements ServiceInterface {
106 private final Ensure m_ensure;
107 public ServiceProvider2(Ensure e) {
108 m_ensure = e;
109 }
110 public void invoke() {
111 m_ensure.step(2);
112 }
113 }
114
115 static class ServiceConsumer implements Runnable {
116 private volatile ServiceInterface m_service;
117 private final Ensure m_ensure;
118
119 @SuppressWarnings("unused")
120 private void add(ServiceInterface service) { m_service = service; }
121
122 @SuppressWarnings("unused")
123 private void remove(ServiceInterface service) { if (m_service == service) { m_service = null; }}
124 public ServiceConsumer(Ensure e) { m_ensure = e; }
125
126 public void start() {
127 Thread t = new Thread(this);
128 t.start();
129 }
130
131 public void run() {
132 m_ensure.step(1);
133 m_service.invoke();
134 m_ensure.step(3);
135 m_ensure.waitForStep(4, 15000);
136 m_service.invoke();
137 }
138
139 public void stop() {
140 m_ensure.step(6);
141 }
142 }
143}