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.Dictionary; |
| 25 | import java.util.Hashtable; |
| 26 | |
| 27 | import org.apache.felix.dm.Component; |
| 28 | import org.apache.felix.dm.DependencyManager; |
| 29 | import org.junit.Assert; |
| 30 | import org.osgi.framework.ServiceRegistration; |
| 31 | |
| 32 | /** |
| 33 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 34 | */ |
| 35 | public class AdapterWithoutPropagationTest extends TestBase { |
| 36 | |
| 37 | public void testAdapterNoPropagate() { |
| 38 | DependencyManager m = getDM(); |
| 39 | // helper class that ensures certain steps get executed in sequence |
| 40 | Ensure e = new Ensure(); |
| 41 | |
| 42 | // The provider has a "foo=bar" property |
| 43 | ServiceProvider serviceProvider = new ServiceProvider(e); |
| 44 | Component provider = component(m).provides(OriginalService.class).properties(foo -> "bar").impl(serviceProvider).build(); |
| 45 | |
| 46 | // The Adapter will see the "foo=bar" property from the adaptee |
| 47 | Component adapter = adapter(m, OriginalService.class) |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 48 | .propagate(false).add("set").change("change").provides(AdaptedService.class).impl(new ServiceAdapter(e)).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 49 | |
| 50 | // The consumer depends on the AdaptedService, but won't see foo=bar property from the adaptee |
| 51 | Component consumer = component(m) |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 52 | .impl(new ServiceConsumer(e)).withSvc(AdaptedService.class, b -> b.add("set").change("change")).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 53 | |
| 54 | // add the provider and the adapter |
| 55 | m.add(provider); |
| 56 | m.add(adapter); |
| 57 | // Checks if the adapter has been started and has seen the adaptee properties |
| 58 | e.waitForStep(1, 5000); |
| 59 | |
| 60 | // add a consumer that must not see the adaptee service properties |
| 61 | m.add(consumer); |
| 62 | e.waitForStep(2, 5000); |
| 63 | |
| 64 | // change the service properties of the provider, and check that the adapter callback instance is caled. |
| 65 | serviceProvider.changeServiceProperties(); |
| 66 | e.waitForStep(3, 5000); |
| 67 | |
| 68 | // cleanup |
| 69 | m.clear(); |
| 70 | } |
| 71 | |
| 72 | public void testAdapterNoPropagateRef() { |
| 73 | DependencyManager m = getDM(); |
| 74 | // helper class that ensures certain steps get executed in sequence |
| 75 | Ensure e = new Ensure(); |
| 76 | |
| 77 | // The provider has a "foo=bar" property |
| 78 | ServiceProvider serviceProvider = new ServiceProvider(e); |
| 79 | Component provider = component(m).provides(OriginalService.class).properties(foo -> "bar").impl(serviceProvider).build(); |
| 80 | |
| 81 | // The Adapter will see the "foo=bar" property from the adaptee |
| 82 | ServiceAdapter saimpl = new ServiceAdapter(e); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 83 | Component adapter = adapter(m, OriginalService.class).propagate(false).impl(saimpl).provides(AdaptedService.class).add(saimpl::set).change(saimpl::change).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 84 | |
| 85 | // The consumer depends on the AdaptedService, but won't see foo=bar property from the adaptee |
| 86 | ServiceConsumer scimpl = new ServiceConsumer(e); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame^] | 87 | Component consumer = component(m).impl(scimpl).withSvc(AdaptedService.class, s -> s.add(scimpl::set).change(scimpl::change)).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 88 | |
| 89 | // add the provider and the adapter |
| 90 | m.add(provider); |
| 91 | m.add(adapter); |
| 92 | // Checks if the adapter has been started and has seen the adaptee properties |
| 93 | e.waitForStep(1, 5000); |
| 94 | |
| 95 | // add a consumer that must not see the adaptee service properties |
| 96 | m.add(consumer); |
| 97 | e.waitForStep(2, 5000); |
| 98 | |
| 99 | // change the service properties of the provider, and check that the adapter callback instance is caled. |
| 100 | serviceProvider.changeServiceProperties(); |
| 101 | e.waitForStep(3, 5000); |
| 102 | |
| 103 | // cleanup |
| 104 | m.clear(); |
| 105 | } |
| 106 | |
| 107 | static interface OriginalService { |
| 108 | } |
| 109 | |
| 110 | static interface AdaptedService { |
| 111 | } |
| 112 | |
| 113 | static class ServiceProvider implements OriginalService { |
| 114 | private volatile ServiceRegistration m_registration; // auto injected when started. |
| 115 | public ServiceProvider(Ensure e) { |
| 116 | } |
| 117 | public void changeServiceProperties() { |
| 118 | Hashtable<String, String> props = new Hashtable<>(); |
| 119 | props.put("foo", "bar2"); |
| 120 | m_registration.setProperties(props); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public static class ServiceAdapter implements AdaptedService { |
| 125 | private final Ensure m_ensure; |
| 126 | |
| 127 | public ServiceAdapter(Ensure e) { |
| 128 | m_ensure = e; |
| 129 | } |
| 130 | |
| 131 | public void set(OriginalService adaptee, Dictionary<String, Object> props) { |
| 132 | Assert.assertEquals("bar", props.get("foo")); |
| 133 | m_ensure.step(1); |
| 134 | } |
| 135 | |
| 136 | void change(OriginalService adapted, Dictionary<String, Object> props) { |
| 137 | Assert.assertEquals("bar2", props.get("foo")); |
| 138 | m_ensure.step(3); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static class ServiceConsumer { |
| 143 | @SuppressWarnings("unused") |
| 144 | private volatile AdaptedService m_service; |
| 145 | private final Ensure m_ensure; |
| 146 | |
| 147 | public ServiceConsumer(Ensure e) { |
| 148 | m_ensure = e; |
| 149 | } |
| 150 | |
| 151 | void set(AdaptedService adapted, Dictionary<String, Object> props) { |
| 152 | Assert.assertNull(props.get("foo")); |
| 153 | m_ensure.step(2); |
| 154 | } |
| 155 | |
| 156 | void change(AdaptedService adapted, Dictionary<String, Object> props) { |
| 157 | Assert.assertNull(props.get("foo")); |
| 158 | Assert.fail("Change callback should not be called"); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |