blob: 87747ae22d370dfe3119177037f58432bbb553a2 [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.adapter;
22import static org.apache.felix.dm.lambda.DependencyManagerActivator.component;
23
24import org.apache.felix.dm.Component;
25import org.apache.felix.dm.DependencyManager;
26
27
28/**
29 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
30 */
31public class AdapterWithInstanceBoundDependencyTest extends TestBase {
32
33 public void testInstanceBoundDependency() {
34 DependencyManager m = getDM();
35 // helper class that ensures certain steps get executed in sequence
36 Ensure e = new Ensure();
37 // create a service provider and consumer
38
39 Component sp = component(m).provides(ServiceInterface.class).impl(new ServiceProvider(e)).build();
40 Component sp2 = component(m).provides(ServiceInterface2.class).impl(new ServiceProvider2(e)).build();
Pierre De Rop11527502016-02-18 21:07:16 +000041 Component sc = component(m).impl(new ServiceConsumer(e)).autoAdd(false).withSvc(ServiceInterface3.class).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +000042 Component sa = adapter(m, ServiceInterface.class).provides(ServiceInterface3.class).impl(new ServiceAdapter(e)).build();
43 m.add(sc);
44 m.add(sp);
45 m.add(sp2);
46 m.add(sa);
47 e.waitForStep(5, 15000);
48 // cleanup
49 m.remove(sa);
50 m.remove(sp2);
51 m.remove(sp);
52 m.remove(sc);
53 m.clear();
54 e.waitForStep(9, 5000); // make sure all components are stopped
55 }
56
57 static interface ServiceInterface {
58 public void invoke();
59 }
60
61 static interface ServiceInterface2 {
62 public void invoke();
63 }
64
65 static interface ServiceInterface3 {
66 public void invoke();
67 }
68
69 static class ServiceProvider2 implements ServiceInterface2 {
70 private final Ensure m_ensure;
71
72 public ServiceProvider2(Ensure ensure) {
73 m_ensure = ensure;
74 }
75
76 public void invoke() {
77 m_ensure.step(4);
78 }
79
80 public void stop() {
81 m_ensure.step();
82 }
83 }
84
85 static class ServiceProvider implements ServiceInterface {
86 private final Ensure m_ensure;
87 public ServiceProvider(Ensure e) {
88 m_ensure = e;
89 }
90 public void invoke() {
91 m_ensure.step(5);
92 }
93 public void stop() {
94 m_ensure.step();
95 }
96 }
97
98 static class ServiceAdapter implements ServiceInterface3 {
99 private Ensure m_ensure;
100 private volatile ServiceInterface m_originalService;
101 private volatile ServiceInterface2 m_injectedService;
102 private volatile Component m_component;
Pierre De Ropfaca2892016-01-31 23:27:05 +0000103
104 public ServiceAdapter(Ensure e) {
105 m_ensure = e;
106 }
107 public void init() {
108 m_ensure.step(1);
Pierre De Rop11527502016-02-18 21:07:16 +0000109 component(m_component, c->c.withSvc(ServiceInterface2.class));
Pierre De Ropfaca2892016-01-31 23:27:05 +0000110 }
111 public void start() {
112 m_ensure.step(2);
113 }
114 public void invoke() {
115 m_ensure.step(3);
116 m_injectedService.invoke();
117 m_originalService.invoke();
118 }
119
120 public void stop() {
121 m_ensure.step();
122 }
123 }
124
125 static class ServiceConsumer implements Runnable {
126 volatile ServiceInterface3 m_service;
127 final Ensure m_ensure;
128
129 ServiceConsumer(Ensure e) {
130 m_ensure = e;
131 }
132
133 public void init() {
134 Thread t = new Thread(this);
135 t.start();
136 }
137
138 public void run() {
139 m_service.invoke();
140 }
141 public void stop() {
142 m_ensure.step();
143 }
144 }
145}
146
147