blob: c6cc634e70aad5088aa00085cd749cd5de15b74c [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.junit.Assert;
26
27/**
28 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
29 */
30@SuppressWarnings({"unchecked", "rawtypes"})
31public class MultipleExtraDependenciesTest extends TestBase {
32 /**
33 * Check that list of extra dependencies (defined from init method) are handled properly.
34 * The extra dependencies are added using a List object (Component.add(List)).
35 * A component c1 will define two extra dependencies over *available* c4/c5 services.
36 */
37 public void testWithTwoAvailableExtraDependency() {
38 DependencyManager m = getDM();
39 // Helper class that ensures certain steps get executed in sequence
40 Ensure e = new Ensure();
Pierre De Rop11527502016-02-18 21:07:16 +000041 Component c1 = component(m).provides(Service1.class).impl(new MyComponent1(e)).withSvc(Service2.class, srv->srv.autoConfig("m_service2")).build();
42 Component c2 = component(m).impl(new MyComponent2(e)).withSvc(Service1.class, srv->srv.required(false).autoConfig(false).add("added")).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +000043 Component c3 = component(m).provides(Service2.class).impl(Service2Impl.class).build();
44 Component c4 = component(m).impl(Service3Impl1.class).provides(Service3.class, type -> "xx").build();
45 Component c5 = component(m).impl(Service3Impl2.class).provides(Service3.class, type -> "yy").build();
46
47 System.out.println("\n+++ Adding c2 / MyComponent2");
48 m.add(c2);
49 System.out.println("\n+++ Adding c3 / Service2");
50 m.add(c3);
51 System.out.println("\n+++ Adding c4 / Service3(xx)");
52 m.add(c4);
53 System.out.println("\n+++ Adding c5 / Service3(yy)");
54 m.add(c5);
55 System.out.println("\n+++ Adding c1 / MyComponent1");
56 // c1 have declared two extra dependency on Service3 (xx/yy).
57 // both extra dependencies are available, so the c1 component should be started immediately.
58 m.add(c1);
59 e.waitForStep(3, 3000);
60 m.clear();
61 }
62
63 /**
64 * Check that list of extra dependencies (defined from init method) are handled properly.
65 * The extra dependencies are added using a List object (Component.add(List)).
66 * A component c1 will define two extra dependencies over c4/c5. At the point c1.init()
67 * is adding the two extra dependencies from its init method, c4 is available, but not c5.
68 * So, c1 is not yet activated.
69 * Then c5 is added, and it triggers the c1 activation ...
70 */
71 public void testWithOneAvailableExtraDependency() {
72 DependencyManager m = getDM();
73 // Helper class that ensures certain steps get executed in sequence
74 Ensure e = new Ensure();
Pierre De Rop11527502016-02-18 21:07:16 +000075 Component c1 = component(m).provides(Service1.class).impl(new MyComponent1(e)).withSvc(Service2.class, srv->srv.autoConfig("m_service2")).build();
76 Component c2 = component(m).impl(new MyComponent2(e)).withSvc(Service1.class, srv->srv.required(false).autoConfig(false).add("added")).build();
Pierre De Ropfaca2892016-01-31 23:27:05 +000077 Component c3 = component(m).provides(Service2.class).impl(Service2Impl.class).build();
78 Component c4 = component(m).impl(Service3Impl1.class).provides(Service3.class, type -> "xx").build();
79 Component c5 = component(m).impl(Service3Impl2.class).provides(Service3.class, type -> "yy").build();
80
81 System.out.println("\n+++ Adding c2 / MyComponent2");
82 m.add(c2);
83 System.out.println("\n+++ Adding c3 / Service2");
84 m.add(c3);
85 System.out.println("\n+++ Adding c4 / Service3(xx)");
86 m.add(c4);
87 System.out.println("\n+++ Adding c1 / MyComponent1");
88 m.add(c1);
89
90 // c1 have declared two extra dependency on Service3 (xx/yy).
91 // So, because we have not yet added c5 (yy), c1 should not be started currently.
92 // But, now, we'll add c5 (Service3/yy) and c1 should then be started ...
93 System.out.println("\n+++ Adding c5 / Service3(yy)");
94 m.add(c5);
95 e.waitForStep(3, 3000);
96 m.clear();
97 }
98
99
100 public interface Service1 {}
101 public interface Service2 {}
102 public interface Service3 {}
103
104 public static class Service2Impl implements Service2 {}
105 public static class Service3Impl1 implements Service3 {}
106 public static class Service3Impl2 implements Service3 {}
107
108 public static class MyComponent1 implements Service1 {
109 Service2 m_service2;
110 Service3 m_service3_xx;
111 Service3 m_service3_yy;
112 Ensure m_ensure;
113
114 public MyComponent1(Ensure e) {
115 m_ensure = e;
116 }
117
118 void init(Component c) {
119 m_ensure.step(1);
120 // Service3/xx currently available
121 // Service3/yy not yet available
122
123 component(c, comp -> comp
Pierre De Rop11527502016-02-18 21:07:16 +0000124 .withSvc(Service3.class, srv->srv.filter("(type=xx)").autoConfig("m_service3_xx"))
125 .withSvc(Service3.class, srv->srv.filter("(type=yy)").autoConfig("m_service3_yy")));
Pierre De Ropfaca2892016-01-31 23:27:05 +0000126 }
127
128 void start() {
129 System.out.println("MyComponent1.start");
130 Assert.assertNotNull(m_service2);
131 Assert.assertNotNull(m_service3_xx);
132 Assert.assertNotNull(m_service3_yy);
133 m_ensure.step(2);
134 }
135 }
136
137 public static class MyComponent2 {
138 Ensure m_ensure;
139
140 public MyComponent2(Ensure e) {
141 m_ensure = e;
142 }
143
144 void added(Service1 s1) {
145 System.out.println("MyComponent2.bind(" + s1 + ")");
146 Assert.assertNotNull(s1);
147 m_ensure.step(3);
148 }
149
150 void start() {
151 System.out.println("MyComponent2.start");
152 }
153 }
154}