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.adapter; |
| 22 | import static org.apache.felix.dm.lambda.DependencyManagerActivator.component; |
| 23 | |
| 24 | import org.apache.felix.dm.Component; |
| 25 | import org.apache.felix.dm.DependencyManager; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 30 | */ |
| 31 | public class AdapterWithExtraDependenciesTest extends TestBase { |
| 32 | |
| 33 | public void testAdapterWithExtraDependenciesAndCallbacks() { |
| 34 | DependencyManager m = getDM(); |
| 35 | // helper class that ensures certain steps get executed in sequence |
| 36 | Ensure e = new Ensure(); |
| 37 | |
| 38 | // create a service adapter that adapts to services S1 and has an optional dependency on services S2 |
| 39 | Component sa = adapter(m, S1.class).impl(SA.class).withSrv(S2.class, s2 -> s2.cb("add", "remove")).build(); |
| 40 | m.add(sa); |
| 41 | |
| 42 | // create a service S1, which triggers the creation of the first adapter instance (A1) |
| 43 | Component s1 = component(m).provides(S1.class).impl(new S1Impl()).build(); |
| 44 | m.add(s1); |
| 45 | |
| 46 | // create a service S2, which will be added to A1 |
| 47 | Component s2 = component(m).provides(S2.class).impl(new S2Impl(e)).build(); |
| 48 | m.add(s2); |
| 49 | |
| 50 | // create a second service S1, which triggers the creation of the second adapter instance (A2) |
| 51 | Component s1b = component(m).provides(S1.class).impl(new S1Impl()).build(); |
| 52 | m.add(s1b); |
| 53 | |
| 54 | // observe that S2 is also added to A2 |
| 55 | e.waitForStep(2, 5000); |
| 56 | |
| 57 | // remove S2 again |
| 58 | m.remove(s2); |
| 59 | |
| 60 | // make sure both adapters have their "remove" callbacks invoked |
| 61 | e.waitForStep(4, 5000); |
| 62 | |
| 63 | m.remove(s1); |
| 64 | m.remove(sa); |
| 65 | m.clear(); |
| 66 | } |
| 67 | |
| 68 | public void testAdapterWithExtraDependenciesAndCallbacksRef() { |
| 69 | DependencyManager m = getDM(); |
| 70 | // helper class that ensures certain steps get executed in sequence |
| 71 | Ensure e = new Ensure(); |
| 72 | |
| 73 | // create a service adapter that adapts to services S1 and has an optional dependency on services S2 |
| 74 | Component sa = adapter(m, S1.class).impl(SA.class).withSrv(S2.class, s2 -> s2.cb(SA::add, SA::remove)).build(); |
| 75 | m.add(sa); |
| 76 | |
| 77 | // create a service S1, which triggers the creation of the first adapter instance (A1) |
| 78 | Component s1 = component(m).provides(S1.class).impl(new S1Impl()).build(); |
| 79 | m.add(s1); |
| 80 | |
| 81 | // create a service S2, which will be added to A1 |
| 82 | Component s2 = component(m).provides(S2.class).impl(new S2Impl(e)).build(); |
| 83 | m.add(s2); |
| 84 | |
| 85 | // create a second service S1, which triggers the creation of the second adapter instance (A2) |
| 86 | Component s1b = component(m).provides(S1.class).impl(new S1Impl()).build(); |
| 87 | m.add(s1b); |
| 88 | |
| 89 | // observe that S2 is also added to A2 |
| 90 | e.waitForStep(2, 5000); |
| 91 | |
| 92 | // remove S2 again |
| 93 | m.remove(s2); |
| 94 | |
| 95 | // make sure both adapters have their "remove" callbacks invoked |
| 96 | e.waitForStep(4, 5000); |
| 97 | |
| 98 | m.remove(s1); |
| 99 | m.remove(sa); |
| 100 | m.clear(); |
| 101 | } |
| 102 | |
| 103 | static interface S1 { |
| 104 | } |
| 105 | static interface S2 { |
| 106 | public void invoke(); |
| 107 | } |
| 108 | static class S1Impl implements S1 { |
| 109 | } |
| 110 | static class S2Impl implements S2 { |
| 111 | |
| 112 | private final Ensure m_e; |
| 113 | |
| 114 | public S2Impl(Ensure e) { |
| 115 | m_e = e; |
| 116 | } |
| 117 | |
| 118 | public void invoke() { |
| 119 | m_e.step(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public static class SA { |
| 124 | volatile S2 s2; |
| 125 | |
| 126 | public SA() { |
| 127 | System.out.println("Adapter created"); |
| 128 | } |
| 129 | public void init() { |
| 130 | System.out.println("Adapter init " + s2); |
| 131 | } |
| 132 | public void add(S2 s) { |
| 133 | System.out.println("adding " + s); |
| 134 | s.invoke(); |
| 135 | } |
| 136 | public void remove(S2 s) { |
| 137 | System.out.println("removing " + s); |
| 138 | s.invoke(); |
| 139 | } |
| 140 | } |
| 141 | } |