PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms 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. */
  17. package org.jboss.as.test.integration.osgi.deployment;
  18. import static org.junit.Assert.assertEquals;
  19. import static org.junit.Assert.fail;
  20. import java.io.InputStream;
  21. import java.util.concurrent.CountDownLatch;
  22. import java.util.concurrent.TimeUnit;
  23. import javax.inject.Inject;
  24. import org.jboss.arquillian.container.test.api.Deployer;
  25. import org.jboss.arquillian.container.test.api.Deployment;
  26. import org.jboss.arquillian.junit.Arquillian;
  27. import org.jboss.arquillian.test.api.ArquillianResource;
  28. import org.jboss.as.test.integration.osgi.xservice.bundle.SimpleActivator;
  29. import org.jboss.as.test.integration.osgi.xservice.bundle.SimpleService;
  30. import org.jboss.as.test.osgi.OSGiTestSupport;
  31. import org.jboss.osgi.spi.OSGiManifestBuilder;
  32. import org.jboss.shrinkwrap.api.ShrinkWrap;
  33. import org.jboss.shrinkwrap.api.asset.Asset;
  34. import org.jboss.shrinkwrap.api.spec.JavaArchive;
  35. import org.junit.Test;
  36. import org.junit.runner.RunWith;
  37. import org.osgi.framework.Bundle;
  38. import org.osgi.framework.BundleActivator;
  39. import org.osgi.framework.BundleContext;
  40. import org.osgi.framework.BundleEvent;
  41. import org.osgi.framework.BundleListener;
  42. import org.osgi.service.packageadmin.PackageAdmin;
  43. /**
  44. * Bundle gets installed/uninstalled through the deployment API.
  45. *
  46. * @author thomas.diesler@jboss.com
  47. * @since 12-Apr-2011
  48. */
  49. @RunWith(Arquillian.class)
  50. public class BundleDeploymentCaseTwoTestCase {
  51. static final String BUNDLE_DEPLOYMENT_NAME = "test-bundle-two";
  52. @ArquillianResource
  53. public Deployer deployer;
  54. @Inject
  55. public BundleContext context;
  56. @Deployment
  57. public static JavaArchive createdeployment() {
  58. final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "bundle-deployment-casetwo");
  59. archive.addClass(OSGiTestSupport.class);
  60. archive.setManifest(new Asset() {
  61. public InputStream openStream() {
  62. OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
  63. builder.addBundleSymbolicName(archive.getName());
  64. builder.addBundleManifestVersion(2);
  65. builder.addImportPackages(PackageAdmin.class);
  66. return builder.openStream();
  67. }
  68. });
  69. return archive;
  70. }
  71. @Test
  72. public void testBundleDeployment() throws Exception {
  73. deployer.deploy(BUNDLE_DEPLOYMENT_NAME);
  74. // Find the deployed bundle
  75. Bundle bundle = OSGiTestSupport.getDeployedBundle(context, BUNDLE_DEPLOYMENT_NAME, null);
  76. // Start the bundle. Note, it may have started already
  77. bundle.start();
  78. assertEquals(Bundle.ACTIVE, bundle.getState());
  79. // Stop the bundle
  80. bundle.stop();
  81. assertEquals(Bundle.RESOLVED, bundle.getState());
  82. final CountDownLatch uninstallLatch = new CountDownLatch(1);
  83. context.addBundleListener(new BundleListener() {
  84. public void bundleChanged(BundleEvent event) {
  85. if (event.getType() == BundleEvent.UNINSTALLED)
  86. uninstallLatch.countDown();
  87. }
  88. });
  89. deployer.undeploy(BUNDLE_DEPLOYMENT_NAME);
  90. if (uninstallLatch.await(1000, TimeUnit.MILLISECONDS) == false)
  91. fail("UNINSTALLED event not received");
  92. assertEquals(Bundle.UNINSTALLED, bundle.getState());
  93. }
  94. @Deployment(name = BUNDLE_DEPLOYMENT_NAME, managed = false, testable = false)
  95. public static JavaArchive getTestArchive() {
  96. final JavaArchive archive = ShrinkWrap.create(JavaArchive.class, BUNDLE_DEPLOYMENT_NAME);
  97. archive.addClasses(SimpleActivator.class, SimpleService.class);
  98. archive.setManifest(new Asset() {
  99. public InputStream openStream() {
  100. OSGiManifestBuilder builder = OSGiManifestBuilder.newInstance();
  101. builder.addBundleSymbolicName(archive.getName());
  102. builder.addBundleManifestVersion(2);
  103. builder.addBundleActivator(SimpleActivator.class);
  104. builder.addImportPackages(BundleActivator.class);
  105. return builder.openStream();
  106. }
  107. });
  108. return archive;
  109. }
  110. }