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.component; |
| 22 | |
| 23 | import org.apache.felix.dm.Component; |
| 24 | import org.apache.felix.dm.DependencyManager; |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 29 | */ |
| 30 | public class CompositionTest extends TestBase { |
| 31 | public void testComposition() { |
| 32 | DependencyManager m = getDM(); |
| 33 | // helper class that ensures certain steps get executed in sequence |
| 34 | Ensure e = new Ensure(); |
| 35 | // create a service provider and consumer |
| 36 | Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build(); |
| 37 | Component sc = component(m).impl(new ServiceConsumer(e)).composition("getComposition") |
| 38 | .withSrv(ServiceInterface.class, sb->sb.cb("add")).build(); |
| 39 | m.add(sp); |
| 40 | m.add(sc); |
| 41 | // ensure we executed all steps inside the component instance |
| 42 | e.step(6); |
| 43 | m.clear(); |
| 44 | } |
| 45 | |
| 46 | public void testCompositionRef() { |
| 47 | DependencyManager m = getDM(); |
| 48 | // helper class that ensures certain steps get executed in sequence |
| 49 | Ensure e = new Ensure(); |
| 50 | // create a service provider and consumer |
| 51 | Component sp = component(m).impl(new ServiceProvider(e)).provides(ServiceInterface.class).build(); |
| 52 | ServiceConsumer scimpl = new ServiceConsumer(e); |
| 53 | Component sc = component(m).impl(scimpl).composition(scimpl::getComposition) |
| 54 | .withSrv(ServiceInterface.class, sb->sb.cb("add")).build(); |
| 55 | m.add(sp); |
| 56 | m.add(sc); |
| 57 | // ensure we executed all steps inside the component instance |
| 58 | e.step(6); |
| 59 | m.clear(); |
| 60 | } |
| 61 | |
| 62 | static interface ServiceInterface { |
| 63 | public void invoke(); |
| 64 | } |
| 65 | |
| 66 | static class ServiceProvider implements ServiceInterface { |
| 67 | private final Ensure m_ensure; |
| 68 | public ServiceProvider(Ensure e) { |
| 69 | m_ensure = e; |
| 70 | } |
| 71 | public void invoke() { |
| 72 | m_ensure.step(4); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static class ServiceConsumer { |
| 77 | private final Ensure m_ensure; |
| 78 | private ServiceConsumerComposite m_composite; |
| 79 | @SuppressWarnings("unused") |
| 80 | private ServiceInterface m_service; |
| 81 | |
| 82 | public ServiceConsumer(Ensure e) { |
| 83 | m_ensure = e; |
| 84 | m_composite = new ServiceConsumerComposite(m_ensure); |
| 85 | } |
| 86 | |
| 87 | public Object[] getComposition() { |
| 88 | return new Object[] { this, m_composite }; |
| 89 | } |
| 90 | |
| 91 | void add(ServiceInterface service) { |
| 92 | m_ensure.step(1); |
| 93 | m_service = service; // This method seems to not being called anymore |
| 94 | } |
| 95 | |
| 96 | void start() { |
| 97 | m_composite.invoke(); |
| 98 | m_ensure.step(5); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static class ServiceConsumerComposite { |
| 103 | ServiceInterface m_service; |
| 104 | private Ensure m_ensure; |
| 105 | |
| 106 | ServiceConsumerComposite(Ensure ensure) |
| 107 | { |
| 108 | m_ensure = ensure; |
| 109 | } |
| 110 | |
| 111 | void add(ServiceInterface service) { |
| 112 | |
| 113 | m_ensure.step(2); |
| 114 | m_service = service; |
| 115 | } |
| 116 | |
| 117 | void invoke() |
| 118 | { |
| 119 | m_ensure.step(3); |
| 120 | m_service.invoke(); |
| 121 | } |
| 122 | } |
| 123 | } |