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.TestBase; |
| 26 | import org.osgi.framework.Bundle; |
| 27 | |
| 28 | /** |
| 29 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 30 | */ |
| 31 | public class BundleAdapterTest extends TestBase { |
| 32 | public void testBundleAdapter() { |
| 33 | DependencyManager m = getDM(); |
| 34 | // create a bundle adapter service (one is created for each bundle) |
| 35 | Component adapter = m.createBundleAdapterService(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE, null, false) |
| 36 | .setImplementation(BundleAdapter.class) |
| 37 | .setInterface(BundleAdapter.class.getName(), null); |
| 38 | |
| 39 | // create a service provider and consumer |
| 40 | Consumer c = new Consumer(); |
| 41 | Component consumer = m.createComponent().setImplementation(c) |
| 42 | .add(m.createServiceDependency().setService(BundleAdapter.class).setCallbacks("add", "remove")); |
| 43 | |
| 44 | // add the bundle adapter |
| 45 | m.add(adapter); |
| 46 | // add the service consumer |
| 47 | m.add(consumer); |
| 48 | // check if at least one bundle was found |
| 49 | c.check(); |
| 50 | // remove the consumer again |
| 51 | m.remove(consumer); |
| 52 | // check if all bundles were removed correctly |
| 53 | c.doubleCheck(); |
| 54 | // remove the bundle adapter |
| 55 | m.remove(adapter); |
| 56 | } |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 57 | |
| 58 | public void testBundleAdapterWithCallbackInstance() { |
| 59 | DependencyManager m = getDM(); |
| 60 | // create a bundle adapter service (one is created for each bundle) |
| 61 | BundleAdapterWithCallback baWithCb = new BundleAdapterWithCallback(); |
| 62 | BundleAdapterCallbackInstance cbInstance = new BundleAdapterCallbackInstance(baWithCb); |
| 63 | |
| 64 | Component adapter = m.createBundleAdapterService(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE, null, false, |
| 65 | cbInstance, "add", null, "remove") |
| 66 | .setImplementation(baWithCb) |
| 67 | .setInterface(BundleAdapter.class.getName(), null); |
| 68 | |
| 69 | // create a service provider and consumer |
| 70 | Consumer c = new Consumer(); |
| 71 | Component consumer = m.createComponent().setImplementation(c) |
| 72 | .add(m.createServiceDependency().setService(BundleAdapter.class).setCallbacks("add", "remove")); |
| 73 | |
| 74 | // add the bundle adapter |
| 75 | m.add(adapter); |
| 76 | // add the service consumer |
| 77 | m.add(consumer); |
| 78 | // check if at least one bundle was found |
| 79 | c.check(); |
| 80 | // remove the consumer again |
| 81 | m.remove(consumer); |
| 82 | // check if all bundles were removed correctly |
| 83 | c.doubleCheck(); |
| 84 | // remove the bundle adapter |
| 85 | m.remove(adapter); |
| 86 | } |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 87 | |
| 88 | public static class BundleAdapter { |
| 89 | volatile Bundle m_bundle; |
| 90 | |
| 91 | Bundle getBundle() { |
| 92 | return m_bundle; |
| 93 | } |
| 94 | } |
| 95 | |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 96 | public static class BundleAdapterWithCallback extends BundleAdapter { |
| 97 | void add(Bundle b) { |
| 98 | m_bundle = b; |
| 99 | } |
| 100 | |
| 101 | void remove(Bundle b) { |
| 102 | m_bundle = null; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public static class BundleAdapterCallbackInstance { |
| 107 | final BundleAdapterWithCallback m_ba; |
| 108 | |
| 109 | BundleAdapterCallbackInstance(BundleAdapterWithCallback ba) { |
| 110 | m_ba = ba; |
| 111 | } |
| 112 | |
| 113 | void add(Bundle b) { |
| 114 | m_ba.add(b); |
| 115 | } |
| 116 | |
| 117 | void remove(Bundle b) { |
| 118 | m_ba.remove(b); |
| 119 | } |
| 120 | } |
| 121 | |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 122 | static class Consumer { |
| 123 | private volatile int m_count = 0; |
| 124 | |
| 125 | public void add(BundleAdapter ba) { |
| 126 | Bundle b = ba.getBundle(); |
| 127 | System.out.println("Consumer.add(" + b.getSymbolicName() + ")"); |
| 128 | Assert.assertNotNull("bundle instance must not be null", b); |
| 129 | m_count++; |
| 130 | } |
| 131 | |
| 132 | public void check() { |
| 133 | Assert.assertTrue("we should have found at least one bundle", m_count > 0); |
| 134 | } |
| 135 | |
| 136 | public void remove(BundleAdapter ba) { |
| 137 | Bundle b = ba.getBundle(); |
| 138 | System.out.println("Consumer.remove(" + b.getSymbolicName() + ")"); |
| 139 | m_count--; |
| 140 | } |
| 141 | |
| 142 | public void doubleCheck() { |
| 143 | Assert.assertEquals("all bundles we found should have been removed again", 0, m_count); |
| 144 | } |
| 145 | } |
| 146 | } |