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 java.util.Hashtable; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import org.apache.felix.dm.Component; |
| 28 | import org.apache.felix.dm.DependencyManager; |
| 29 | import org.junit.Assert; |
| 30 | |
| 31 | /** |
| 32 | * Checks if a service adapter propagates its service properties, if |
| 33 | * the adapted service properties are changed: |
| 34 | * |
| 35 | * S1Impl provides S |
| 36 | * S1Adapter adapts S1Impl(S) to S2 |
| 37 | * S3 depends on S2 |
| 38 | * |
| 39 | * So, when S1Impl service properties are changed, S1Adapter shall propagate the changed properties to S3. |
| 40 | * |
| 41 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 42 | */ |
| 43 | @SuppressWarnings({"unchecked", "rawtypes"}) |
| 44 | public class AdapterWithPropagationTest extends TestBase { |
| 45 | public static interface S1 {} |
| 46 | |
| 47 | static class S1Impl implements S1 { |
| 48 | private Ensure m_ensure; |
| 49 | public S1Impl(Ensure e) { |
| 50 | m_ensure = e; |
| 51 | } |
| 52 | |
| 53 | public void start() { |
| 54 | m_ensure.step(1); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public static interface S2 {} |
| 59 | |
| 60 | static class S1Adapter implements S2 { |
| 61 | private Ensure m_ensure; |
| 62 | public S1Adapter(Ensure e) { |
| 63 | m_ensure = e; |
| 64 | } |
| 65 | |
| 66 | public void add(Map properties, S1 s1) { |
| 67 | Assert.assertTrue("v1".equals(properties.get("p1"))); |
| 68 | Assert.assertTrue("v2overriden".equals(properties.get("p2"))); |
| 69 | m_ensure.step(2); |
| 70 | } |
| 71 | |
| 72 | public void change(Map properties, S1 s1) { |
| 73 | Assert.assertTrue("v1modified".equals(properties.get("p1"))); |
| 74 | Assert.assertTrue("v2overriden".equals(properties.get("p2"))); |
| 75 | m_ensure.step(4); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static class S3 { |
| 80 | private final Ensure m_ensure; |
| 81 | |
| 82 | public S3(Ensure e) { |
| 83 | m_ensure = e; |
| 84 | } |
| 85 | |
| 86 | public void add(Map properties, S2 s2) { |
| 87 | Assert.assertTrue("v1".equals(properties.get("p1"))); |
| 88 | Assert.assertTrue("v2".equals(properties.get("p2"))); // s1 should not override adapter service properties |
| 89 | m_ensure.step(3); |
| 90 | } |
| 91 | |
| 92 | public void change(Map properties, S2 s2) { |
| 93 | Assert.assertTrue("v1modified".equals(properties.get("p1"))); |
| 94 | Assert.assertTrue("v2".equals(properties.get("p2"))); // s1 should not override adapter service properties |
| 95 | m_ensure.step(5); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public void testAdapterWithPropagation() { |
| 100 | DependencyManager m = getDM(); |
| 101 | // helper class that ensures certain steps get executed in sequence |
| 102 | Ensure e = new Ensure(); |
| 103 | |
| 104 | Component s1 = component(m).impl(new S1Impl(e)).provides(S1.class).properties(p1 -> "v1", p2 -> "v2overriden").build(); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 105 | Component s1Adapter = adapter(m, S1.class).add("add").change("change").impl(new S1Adapter(e)).provides(S2.class).properties(p2 -> "v2").build(); |
| 106 | Component s3 = component(m).impl(new S3(e)).withSvc(S2.class, s -> s.add("add").change("change")).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 107 | |
| 108 | m.add(s1); |
| 109 | m.add(s1Adapter); |
| 110 | m.add(s3); |
| 111 | |
| 112 | e.waitForStep(3, 5000); |
| 113 | |
| 114 | Hashtable s1Properties = new Hashtable(); |
| 115 | s1Properties.put("p1", "v1modified"); |
| 116 | s1Properties.put("p2", "v2overriden"); |
| 117 | s1.setServiceProperties(s1Properties); |
| 118 | |
| 119 | e.waitForStep(5, 5000); |
| 120 | |
| 121 | m.clear(); |
| 122 | } |
| 123 | } |