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 | import org.osgi.framework.ServiceRegistration; |
| 31 | |
| 32 | /** |
| 33 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 34 | */ |
| 35 | public class AdapterWithCallbackInstanceTest extends TestBase { |
| 36 | |
| 37 | public void testServiceWithAdapterAndConsumer() { |
| 38 | DependencyManager m = getDM(); |
| 39 | // helper class that ensures certain steps get executed in sequence |
| 40 | Ensure e = new Ensure(); |
| 41 | |
| 42 | ServiceProvider serviceProvider = new ServiceProvider(e); |
| 43 | Component provider = component(m).provides(OriginalService.class).impl(serviceProvider).build(); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 44 | Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(AdaptedService.class).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 45 | |
| 46 | ServiceAdapterCallbackInstance callbackInstance = new ServiceAdapterCallbackInstance(e); |
| 47 | Component adapter = adapter(m, OriginalService.class) |
| 48 | .provides(AdaptedService.class).impl(new ServiceAdapter(e)).propagate(true) |
| 49 | .autoConfig("m_originalService") |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 50 | .callbackInstance(callbackInstance).add("set").change("changed").remove("unset") |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 51 | .build(); |
| 52 | |
| 53 | // add the provider and the adapter |
| 54 | m.add(provider); |
| 55 | m.add(adapter); |
| 56 | // Checks if the callbackInstances is called, and if the adapter start method is called |
| 57 | e.waitForStep(2, 5000); |
| 58 | |
| 59 | // add a consumer that will invoke the adapter |
| 60 | // which will in turn invoke the original provider |
| 61 | m.add(consumer); |
| 62 | // now validate that both have been invoked in the right order |
| 63 | e.waitForStep(4, 5000); |
| 64 | |
| 65 | // change the service properties of the provider, and check that the adapter callback instance is changed. |
| 66 | serviceProvider.changeServiceProperties(); |
| 67 | e.waitForStep(5, 5000); |
| 68 | |
| 69 | // remove the provider |
| 70 | m.remove(provider); |
| 71 | // ensure that the consumer is stopped, the adapter callback is called in its unset method, and the adapter is stopped. |
| 72 | e.waitForStep(8, 5000); |
| 73 | // remove adapter and consumer |
| 74 | m.remove(adapter); |
| 75 | m.remove(consumer); |
| 76 | } |
| 77 | |
| 78 | public void testServiceWithAdapterAndConsumerRef() { |
| 79 | DependencyManager m = getDM(); |
| 80 | // helper class that ensures certain steps get executed in sequence |
| 81 | Ensure e = new Ensure(); |
| 82 | |
| 83 | ServiceProvider serviceProvider = new ServiceProvider(e); |
| 84 | Component provider = component(m).provides(OriginalService.class).impl(serviceProvider).build(); |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 85 | Component consumer = component(m).impl(new ServiceConsumer(e)).withSvc(AdaptedService.class).build(); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 86 | |
| 87 | ServiceAdapterCallbackInstance callbackInstance = new ServiceAdapterCallbackInstance(e); |
| 88 | Component adapter = adapter(m, OriginalService.class, adp -> adp |
| 89 | .provides(AdaptedService.class).impl(new ServiceAdapter(e)) |
| 90 | .autoAdd(false).propagate(true) |
| 91 | .autoConfig("m_originalService") |
Pierre De Rop | 1152750 | 2016-02-18 21:07:16 +0000 | [diff] [blame] | 92 | .add(callbackInstance::set).change(callbackInstance::changed).remove(callbackInstance::unset)); |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 93 | |
| 94 | // add the provider and the adapter |
| 95 | m.add(provider); |
| 96 | m.add(adapter); |
| 97 | // Checks if the callbackInstances is called, and if the adapter start method is called |
| 98 | e.waitForStep(2, 5000); |
| 99 | |
| 100 | // add a consumer that will invoke the adapter |
| 101 | // which will in turn invoke the original provider |
| 102 | m.add(consumer); |
| 103 | // now validate that both have been invoked in the right order |
| 104 | e.waitForStep(4, 5000); |
| 105 | |
| 106 | // change the service properties of the provider, and check that the adapter callback instance is changed. |
| 107 | serviceProvider.changeServiceProperties(); |
| 108 | e.waitForStep(5, 5000); |
| 109 | |
| 110 | // remove the provider |
| 111 | m.remove(provider); |
| 112 | // ensure that the consumer is stopped, the adapter callback is called in its unset method, and the adapter is stopped. |
| 113 | e.waitForStep(8, 5000); |
| 114 | // remove adapter and consumer |
| 115 | m.remove(adapter); |
| 116 | m.remove(consumer); |
| 117 | } |
| 118 | |
| 119 | static interface OriginalService { |
| 120 | public void invoke(); |
| 121 | } |
| 122 | |
| 123 | static interface AdaptedService { |
| 124 | public void invoke(); |
| 125 | } |
| 126 | |
| 127 | static class ServiceProvider implements OriginalService { |
| 128 | private final Ensure m_ensure; |
Pierre De Rop | 6e8f921 | 2016-02-20 21:44:59 +0000 | [diff] [blame^] | 129 | private volatile ServiceRegistration<?> m_registration; // auto injected when started. |
Pierre De Rop | faca289 | 2016-01-31 23:27:05 +0000 | [diff] [blame] | 130 | public ServiceProvider(Ensure e) { |
| 131 | m_ensure = e; |
| 132 | } |
| 133 | public void changeServiceProperties() { |
| 134 | Hashtable<String, String> props = new Hashtable<>(); |
| 135 | props.put("foo", "bar"); |
| 136 | m_registration.setProperties(props); |
| 137 | } |
| 138 | public void invoke() { |
| 139 | m_ensure.step(4); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | public static class ServiceAdapter implements AdaptedService { |
| 144 | private volatile OriginalService m_originalService; |
| 145 | private final Ensure m_ensure; |
| 146 | |
| 147 | public ServiceAdapter(Ensure e) { |
| 148 | m_ensure = e; |
| 149 | } |
| 150 | |
| 151 | public void start() { m_ensure.step(2); } |
| 152 | public void stop() { m_ensure.step(7); } |
| 153 | public void invoke() { |
| 154 | m_originalService.invoke(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public static class ServiceAdapterCallbackInstance { |
| 159 | private final Ensure m_ensure; |
| 160 | public ServiceAdapterCallbackInstance(Ensure e) { |
| 161 | m_ensure = e; |
| 162 | } |
| 163 | |
| 164 | public void set(OriginalService m_originalService, Map<String, Object> props) { |
| 165 | m_ensure.step(1); |
| 166 | } |
| 167 | |
| 168 | public void changed(OriginalService m_originalService, Map<String, Object> props) { |
| 169 | Assert.assertEquals("bar", props.get("foo")); |
| 170 | m_ensure.step(5); |
| 171 | } |
| 172 | |
| 173 | public void unset(OriginalService m_originalService, Map<String, Object> props) { |
| 174 | m_ensure.step(8); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static class ServiceConsumer { |
| 179 | private volatile AdaptedService m_service; |
| 180 | private final Ensure m_ensure; |
| 181 | |
| 182 | public ServiceConsumer(Ensure e) { |
| 183 | m_ensure = e; |
| 184 | } |
| 185 | public void start() { |
| 186 | m_ensure.step(3); |
| 187 | m_service.invoke(); |
| 188 | } |
| 189 | public void stop() { |
| 190 | m_ensure.step(6); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |