/* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.test.osgi.example.ds; import java.io.InputStream; import java.util.Comparator; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.jboss.arquillian.container.test.api.Deployer; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.osgi.metadata.OSGiManifestBuilder; import org.jboss.osgi.provision.XResourceProvisioner; import org.jboss.osgi.repository.XRepository; import org.jboss.osgi.resolver.XResource; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.Asset; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.osgi.resource.Resource; import org.osgi.service.repository.Repository; import org.osgi.util.tracker.ServiceTracker; /** * Example for Declarative Services * * @author thomas.diesler@jboss.com * @since 06-Jul-2011 */ @RunWith(Arquillian.class) public class DeclarativeServicesTestCase { static final String DS_BUNDLE = "ds-bundle"; @ArquillianResource Deployer deployer; @ArquillianResource BundleContext context; @Deployment public static JavaArchive dsProvider() { final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "declarative-services-tests"); archive.addClasses(SampleComparator.class); archive.setManifest(new Asset() { @Override public InputStream openStream() { OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance(); builder.addBundleSymbolicName(archive.getName()); builder.addBundleManifestVersion(2); builder.addImportPackages(ServiceTracker.class); builder.addExportPackages(SampleComparator.class); return builder.openStream(); } }); return archive; } @Test @SuppressWarnings({ "rawtypes" }) public void testImmediateService() throws Exception { InputStream input = deployer.getDeployment(DS_BUNDLE); Bundle bundle = context.installBundle(DS_BUNDLE, input); try { bundle.start(); // Track the service provided by the test bundle final CountDownLatch latch = new CountDownLatch(1); ServiceTracker<Comparator, Comparator> tracker = new ServiceTracker<Comparator, Comparator>(context, Comparator.class.getName(), null) { @Override public Comparator addingService(ServiceReference<Comparator> reference) { Comparator<?> service = super.addingService(reference); latch.countDown(); return service; } }; tracker.open(); // Wait for the service to become available if (latch.await(2, TimeUnit.SECONDS) == false) throw new TimeoutException("Timeout tracking Comparator service"); } finally { bundle.uninstall(); } } @Deployment(name = DS_BUNDLE, managed = false, testable = false) public static JavaArchive testBundle() { final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, DS_BUNDLE); archive.addAsResource("ds/OSGI-INF/sample.xml", "OSGI-INF/sample.xml"); archive.setManifest(new Asset() { @Override public InputStream openStream() { OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance(); builder.addBundleSymbolicName(archive.getName()); builder.addBundleManifestVersion(2); builder.addManifestHeader("Service-Component", "OSGI-INF/sample.xml"); builder.addImportPackages(SampleComparator.class); return builder.openStream(); } }); return archive; } }