Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +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.itest.api; |
| 20 | |
| 21 | import org.apache.felix.dm.Component; |
| 22 | import org.apache.felix.dm.DependencyManager; |
| 23 | import org.apache.felix.dm.itest.util.Ensure; |
| 24 | import org.apache.felix.dm.itest.util.TestBase; |
| 25 | |
| 26 | /** |
| 27 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 28 | */ |
| 29 | public class AdapterAndConsumerTest extends TestBase { |
| 30 | |
| 31 | public void testServiceWithAdapterAndConsumer() { |
| 32 | DependencyManager m = getDM(); |
| 33 | // helper class that ensures certain steps get executed in sequence |
| 34 | Ensure e = new Ensure(); |
| 35 | |
| 36 | Component provider = m.createComponent() |
| 37 | .setInterface(OriginalService.class.getName(), null) |
| 38 | .setImplementation(new ServiceProvider(e)); |
| 39 | |
| 40 | Component consumer = m.createComponent() |
| 41 | .setImplementation(new ServiceConsumer(e)) |
| 42 | .add(m.createServiceDependency() |
| 43 | .setService(AdaptedService.class) |
| 44 | .setRequired(true) |
| 45 | ); |
| 46 | |
| 47 | Component adapter = m.createAdapterService(OriginalService.class, null) |
| 48 | .setInterface(AdaptedService.class.getName(), null) |
| 49 | .setImplementation(ServiceAdapter.class); |
| 50 | |
| 51 | // add the provider and the adapter |
| 52 | m.add(provider); |
| 53 | m.add(adapter); |
| 54 | // add a consumer that will invoke the adapter |
| 55 | // which will in turn invoke the original provider |
| 56 | m.add(consumer); |
| 57 | // now validate that both have been invoked in the right order |
| 58 | e.waitForStep(2, 5000); |
| 59 | // remove the provider again |
| 60 | m.remove(provider); |
| 61 | // ensure that the consumer is stopped |
| 62 | e.waitForStep(3, 5000); |
| 63 | // remove adapter and consumer |
| 64 | m.remove(adapter); |
| 65 | m.remove(consumer); |
| 66 | } |
| 67 | |
| 68 | static interface OriginalService { |
| 69 | public void invoke(); |
| 70 | } |
| 71 | |
| 72 | static interface AdaptedService { |
| 73 | public void invoke(); |
| 74 | } |
| 75 | |
| 76 | static class ServiceProvider implements OriginalService { |
| 77 | private final Ensure m_ensure; |
| 78 | public ServiceProvider(Ensure e) { |
| 79 | m_ensure = e; |
| 80 | } |
| 81 | public void invoke() { |
| 82 | m_ensure.step(2); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public static class ServiceAdapter implements AdaptedService { |
| 87 | private volatile OriginalService m_originalService; |
| 88 | |
| 89 | public void start() { System.out.println("start"); } |
| 90 | public void stop() { System.out.println("stop"); } |
| 91 | public void invoke() { |
| 92 | m_originalService.invoke(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static class ServiceConsumer { |
| 97 | private volatile AdaptedService m_service; |
| 98 | private final Ensure m_ensure; |
| 99 | |
| 100 | public ServiceConsumer(Ensure e) { |
| 101 | m_ensure = e; |
| 102 | } |
| 103 | public void start() { |
| 104 | m_ensure.step(1); |
| 105 | m_service.invoke(); |
| 106 | } |
| 107 | public void stop() { |
| 108 | m_ensure.step(3); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |