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.junit.Assert; |
| 22 | |
| 23 | import org.apache.felix.dm.Component; |
| 24 | import org.apache.felix.dm.DependencyManager; |
| 25 | import org.apache.felix.dm.itest.util.Ensure; |
| 26 | import org.apache.felix.dm.itest.util.TestBase; |
| 27 | |
| 28 | /** |
| 29 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 30 | */ |
| 31 | public class AbstractServiceDependencyTest extends TestBase { |
| 32 | public void testAbstractClassDependency() { |
| 33 | DependencyManager m = getDM(); |
| 34 | // helper class that ensures certain steps get executed in sequence |
| 35 | Ensure e = new Ensure(); |
| 36 | // create a service provider and consumer |
| 37 | Component sp = m.createComponent() |
| 38 | .setInterface(ServiceAbstract.class.getName(), null) |
| 39 | .setImplementation(new ServiceProvider(e)) |
| 40 | ; |
| 41 | Component sc = m.createComponent() |
| 42 | .setImplementation(new ServiceConsumer(e)) |
| 43 | .add(m.createServiceDependency() |
| 44 | .setService(ServiceAbstract.class) |
| 45 | .setRequired(true) |
| 46 | .setCallbacks("bind", "unbind") |
| 47 | ); |
| 48 | m.add(sp); |
| 49 | m.add(sc); |
| 50 | m.remove(sp); |
| 51 | // ensure we executed all steps inside the component instance |
| 52 | e.step(8); |
| 53 | m.clear(); |
| 54 | } |
| 55 | |
| 56 | static abstract class ServiceAbstract { |
| 57 | public abstract void invoke(); |
| 58 | } |
| 59 | |
| 60 | static class ServiceProvider extends ServiceAbstract { |
| 61 | private final Ensure m_ensure; |
| 62 | public ServiceProvider(Ensure e) { |
| 63 | m_ensure = e; |
| 64 | } |
| 65 | |
| 66 | public void start() { |
| 67 | m_ensure.step(1); |
| 68 | } |
| 69 | |
| 70 | public void invoke() { |
| 71 | m_ensure.step(4); |
| 72 | } |
| 73 | |
| 74 | public void stop() { |
| 75 | m_ensure.step(7); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static class ServiceConsumer { |
| 80 | private volatile ServiceAbstract m_service; |
| 81 | private final Ensure m_ensure; |
| 82 | |
| 83 | public ServiceConsumer(Ensure e) { |
| 84 | m_ensure = e; |
| 85 | } |
| 86 | |
| 87 | public void bind(ServiceAbstract service) { |
| 88 | m_ensure.step(2); |
| 89 | m_service = service; |
| 90 | } |
| 91 | |
| 92 | public void start() { |
| 93 | m_ensure.step(3); |
| 94 | m_service.invoke(); |
| 95 | } |
| 96 | |
| 97 | public void stop() { |
| 98 | m_ensure.step(5); |
| 99 | } |
| 100 | |
| 101 | public void unbind(ServiceAbstract service) { |
| 102 | System.out.println("UNBINDDDDDDDDDDDDDDDDDDDDDDDDDDD"); |
| 103 | Assert.assertEquals(m_service, service); |
| 104 | m_ensure.step(6); |
| 105 | } |
| 106 | } |
| 107 | } |