blob: 6f4e35a9895ddf1664f95f52d818d4c720f9a6b7 [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 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
28 */
29public class ServiceDependencyTest extends TestBase {
30 public void testServiceRegistrationAndConsumption() {
31 DependencyManager m = getDM();
32 // helper class that ensures certain steps get executed in sequence
33 Ensure e = new Ensure();
34 // create a service provider and consumer
35 Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build();
36 Component sc = component(m).impl(new ServiceConsumer(e)).withSrv(ServiceInterface.class).build();
37
38 Component sc2 = component(m).impl(new ServiceConsumerCallbacks(e))
39 .withSrv(ServiceInterface.class, srv -> srv.required(false).cb(ServiceConsumerCallbacks::add, ServiceConsumerCallbacks::remove))
40 .build();
41
42 m.add(sp);
43 m.add(sc);
44 m.remove(sc);
45 m.add(sc2);
46 m.remove(sp);
47 m.remove(sc2);
48 // ensure we executed all steps inside the component instance
49 e.step(6);
50 }
51
52 static interface ServiceInterface {
53 public void invoke();
54 }
55
56 static class ServiceProvider implements ServiceInterface {
57 private final Ensure m_ensure;
58 public ServiceProvider(Ensure e) {
59 m_ensure = e;
60 }
61 public void invoke() {
62 m_ensure.step(2);
63 }
64 }
65
66 static class ServiceConsumer {
67 private volatile ServiceInterface m_service;
68 private final Ensure m_ensure;
69
70 public ServiceConsumer(Ensure e) {
71 m_ensure = e;
72 }
73
74 public void init() {
75 m_ensure.step(1);
76 m_service.invoke();
77 }
78
79 public void destroy() {
80 m_ensure.step(3);
81 }
82 }
83
84 static class ServiceConsumerCallbacks {
85 private final Ensure m_ensure;
86
87 public ServiceConsumerCallbacks(Ensure e) {
88 m_ensure = e;
89 }
90
91 public void add(ServiceInterface service) {
92 m_ensure.step(4);
93 }
94 public void remove(ServiceInterface service) {
95 m_ensure.step(5);
96 }
97 }
98}