Pierre De Rop | 6e8f921 | 2016-02-20 21:44:59 +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 | */ |
Pierre De Rop | 0e62afd | 2016-01-04 11:10:49 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm.itest.api; |
| 20 | import org.apache.felix.dm.Component; |
| 21 | import org.apache.felix.dm.DependencyManager; |
| 22 | import org.apache.felix.dm.itest.util.Ensure; |
| 23 | import org.apache.felix.dm.itest.util.TestBase; |
| 24 | import org.junit.Assert; |
| 25 | |
| 26 | public class FELI5155_AdapterCallbackInstanceCalledTwice extends TestBase { |
| 27 | static Ensure m_e; |
| 28 | |
| 29 | public void testAdapterCallbackInstanceCalledTwice() { |
| 30 | DependencyManager m = getDM(); |
| 31 | m_e = new Ensure(); |
| 32 | |
| 33 | S1AdapterImpl adapterImpl = new S1AdapterImpl(); |
| 34 | S2DependencyCallbackInstance cb = new S2DependencyCallbackInstance(adapterImpl); |
| 35 | |
| 36 | Component s1 = m.createComponent().setImplementation(S1Impl.class).setInterface(S1.class.getName(), null); |
| 37 | Component s2 = m.createComponent().setImplementation(S2Impl.class).setInterface(S2.class.getName(), null); |
| 38 | |
| 39 | Component s1Adapter = m.createAdapterService(S1.class, null, "setS1", null, null, null) |
| 40 | .setImplementation(adapterImpl) |
| 41 | .add(m.createServiceDependency().setService(S2.class).setRequired(true).setCallbacks(cb, "setS2", null, null, null)); |
| 42 | |
| 43 | m.add(s1); |
| 44 | m.add(s1Adapter); |
| 45 | m.add(s2); |
| 46 | |
| 47 | m_e.waitForStep(2, 5000); |
| 48 | clearComponents(); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | public interface S1 { |
| 53 | } |
| 54 | |
| 55 | public static class S1Impl implements S1 { |
| 56 | } |
| 57 | |
| 58 | public interface S2 { |
| 59 | } |
| 60 | |
| 61 | public static class S2Impl implements S2 { |
| 62 | } |
| 63 | |
| 64 | public static class S1AdapterImpl { |
| 65 | volatile S1 m_s1; |
| 66 | volatile S2 m_s2; |
| 67 | |
| 68 | void setS1(S1 s1) { |
| 69 | m_s1 = s1; |
| 70 | } |
| 71 | |
| 72 | void setS2(S2 s2) { |
| 73 | Assert.assertNull("service already injected: ", m_s2); |
| 74 | m_s2 = s2; |
| 75 | m_e.step(1); |
| 76 | } |
| 77 | |
| 78 | void start() { |
| 79 | Assert.assertNotNull("service s1 not injected", m_s1); |
| 80 | m_e.step(2); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public static class S2DependencyCallbackInstance { |
| 85 | final S1AdapterImpl m_adapter; |
| 86 | |
| 87 | S2DependencyCallbackInstance(S1AdapterImpl adapter) { |
| 88 | m_adapter = adapter; |
| 89 | } |
| 90 | |
| 91 | void setS2(S2 s2) { |
| 92 | m_adapter.setS2(s2); |
| 93 | } |
| 94 | } |
| 95 | } |