PageRenderTime 64ms CodeModel.GetById 25ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/osgi/deployment/BundleDeploymentCaseTwoTestCase.java

#
Java | 129 lines | 85 code | 19 blank | 25 comment | 4 complexity | 5037253c1680c14811bc10d731a6382a MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2009, Red Hat Middleware LLC, and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * Licensed under the Apache License, Version 2.0 (the "License");
  8 * you may not use this file except in compliance with the License.
  9 * You may obtain a copy of the License at
 10 * http://www.apache.org/licenses/LICENSE-2.0
 11 * Unless required by applicable law or agreed to in writing, software
 12 * distributed under the License is distributed on an "AS IS" BASIS,
 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14 * See the License for the specific language governing permissions and
 15 * limitations under the License.
 16 */
 17package org.jboss.as.test.integration.osgi.deployment;
 18
 19import static org.junit.Assert.assertEquals;
 20import static org.junit.Assert.fail;
 21
 22import java.io.InputStream;
 23import java.util.concurrent.CountDownLatch;
 24import java.util.concurrent.TimeUnit;
 25
 26import javax.inject.Inject;
 27
 28import org.jboss.arquillian.container.test.api.Deployer;
 29import org.jboss.arquillian.container.test.api.Deployment;
 30import org.jboss.arquillian.junit.Arquillian;
 31import org.jboss.arquillian.test.api.ArquillianResource;
 32import org.jboss.as.test.integration.osgi.xservice.bundle.SimpleActivator;
 33import org.jboss.as.test.integration.osgi.xservice.bundle.SimpleService;
 34import org.jboss.as.test.osgi.OSGiTestSupport;
 35import org.jboss.osgi.spi.OSGiManifestBuilder;
 36import org.jboss.shrinkwrap.api.ShrinkWrap;
 37import org.jboss.shrinkwrap.api.asset.Asset;
 38import org.jboss.shrinkwrap.api.spec.JavaArchive;
 39import org.junit.Test;
 40import org.junit.runner.RunWith;
 41import org.osgi.framework.Bundle;
 42import org.osgi.framework.BundleActivator;
 43import org.osgi.framework.BundleContext;
 44import org.osgi.framework.BundleEvent;
 45import org.osgi.framework.BundleListener;
 46import org.osgi.service.packageadmin.PackageAdmin;
 47
 48/**
 49 * Bundle gets installed/uninstalled through the deployment API.
 50 *
 51 * @author thomas.diesler@jboss.com
 52 * @since 12-Apr-2011
 53 */
 54@RunWith(Arquillian.class)
 55public class BundleDeploymentCaseTwoTestCase {
 56
 57    static final String BUNDLE_DEPLOYMENT_NAME = "test-bundle-two";
 58
 59    @ArquillianResource
 60    public Deployer deployer;
 61
 62    @Inject
 63    public BundleContext context;
 64
 65    @Deployment
 66    public static JavaArchive createdeployment() {
 67        final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "bundle-deployment-casetwo");
 68        archive.addClass(OSGiTestSupport.class);
 69        archive.setManifest(new Asset() {
 70            public InputStream openStream() {
 71                OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
 72                builder.addBundleSymbolicName(archive.getName());
 73                builder.addBundleManifestVersion(2);
 74                builder.addImportPackages(PackageAdmin.class);
 75                return builder.openStream();
 76            }
 77        });
 78        return archive;
 79    }
 80
 81    @Test
 82    public void testBundleDeployment() throws Exception {
 83
 84        deployer.deploy(BUNDLE_DEPLOYMENT_NAME);
 85
 86        // Find the deployed bundle
 87        Bundle bundle = OSGiTestSupport.getDeployedBundle(context, BUNDLE_DEPLOYMENT_NAME, null);
 88
 89        // Start the bundle. Note, it may have started already
 90        bundle.start();
 91        assertEquals(Bundle.ACTIVE, bundle.getState());
 92
 93        // Stop the bundle
 94        bundle.stop();
 95        assertEquals(Bundle.RESOLVED, bundle.getState());
 96
 97        final CountDownLatch uninstallLatch = new CountDownLatch(1);
 98        context.addBundleListener(new BundleListener() {
 99            public void bundleChanged(BundleEvent event) {
100                if (event.getType() == BundleEvent.UNINSTALLED)
101                    uninstallLatch.countDown();
102            }
103        });
104
105        deployer.undeploy(BUNDLE_DEPLOYMENT_NAME);
106
107        if (uninstallLatch.await(1000, TimeUnit.MILLISECONDS) == false)
108            fail("UNINSTALLED event not received");
109
110        assertEquals(Bundle.UNINSTALLED, bundle.getState());
111    }
112
113    @Deployment(name = BUNDLE_DEPLOYMENT_NAME, managed = false, testable = false)
114    public static JavaArchive getTestArchive() {
115        final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, BUNDLE_DEPLOYMENT_NAME);
116        archive.addClasses(SimpleActivator.class, SimpleService.class);
117        archive.setManifest(new Asset() {
118            public InputStream openStream() {
119                OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
120                builder.addBundleSymbolicName(archive.getName());
121                builder.addBundleManifestVersion(2);
122                builder.addBundleActivator(SimpleActivator.class);
123                builder.addImportPackages(BundleActivator.class);
124                return builder.openStream();
125            }
126        });
127        return archive;
128    }
129}