@Test
  public void testAttachedFragmentEar() throws Exception {
    ModelControllerClient client =
        FrameworkUtils.waitForService(context, ModelControllerClient.class);
    ServerDeploymentHelper server = new ServerDeploymentHelper(client);

    // Deploy the fragment
    InputStream input = deployer.getDeployment(GOOD_FRAGMENT_EAR);
    String earName = server.deploy(GOOD_FRAGMENT_EAR, input);

    // Find the deployed fragment
    Bundle fragment = packageAdmin.getBundles(GOOD_FRAGMENT, null)[0];
    Assert.assertEquals("Bundle RESOLVED", Bundle.RESOLVED, fragment.getState());

    // Find the deployed bundle
    Bundle host = packageAdmin.getBundles(GOOD_BUNDLE, null)[0];
    Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, host.getState());

    Class<?> clazz =
        host.loadClass("org.jboss.as.test.integration.osgi.deployment.bundle.AttachedType");
    Assert.assertNotNull("Class not null", clazz);
    Assert.assertSame(host, ((BundleReference) clazz.getClassLoader()).getBundle());

    server.undeploy(earName);
    Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, fragment.getState());
    Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, host.getState());
  }
  @Test
  public void testRedeployAfterUndeploy() throws Exception {
    ModelControllerClient client =
        FrameworkUtils.waitForService(context, ModelControllerClient.class);
    ServerDeploymentHelper server = new ServerDeploymentHelper(client);

    // Deploy the bundle
    InputStream input = deployer.getDeployment(GOOD_BUNDLE);
    String runtimeName = server.deploy("redeploy", input);

    // Find the deployed bundle
    Bundle bundle = packageAdmin.getBundles(GOOD_BUNDLE, null)[0];
    Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, bundle.getState());

    server.undeploy(runtimeName);
    Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, bundle.getState());

    // Redeploy the same bundle
    input = deployer.getDeployment(GOOD_BUNDLE);
    runtimeName = server.deploy("redeploy", input);

    // Find the deployed bundle
    bundle = packageAdmin.getBundles(GOOD_BUNDLE, null)[0];
    Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, bundle.getState());

    server.undeploy(runtimeName);
    Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, bundle.getState());
  }
  @Test
  public void testUnattachedFragment() throws Exception {
    ModelControllerClient client =
        FrameworkUtils.waitForService(context, ModelControllerClient.class);
    ServerDeploymentHelper server = new ServerDeploymentHelper(client);

    // Deploy the bundle
    InputStream input = deployer.getDeployment(GOOD_BUNDLE);
    String hostName = server.deploy("bundle-host", input);

    // Find the deployed bundle
    Bundle host = packageAdmin.getBundles(GOOD_BUNDLE, null)[0];
    Assert.assertEquals("Bundle ACTIVE", Bundle.ACTIVE, host.getState());

    // Deploy the fragment
    input = deployer.getDeployment(GOOD_FRAGMENT);
    String fragmentName = server.deploy("bundle-fragment", input);

    // Find the deployed bundle
    Bundle fragment = packageAdmin.getBundles(GOOD_FRAGMENT, null)[0];
    Assert.assertEquals("Bundle INSTALLED", Bundle.INSTALLED, fragment.getState());

    try {
      host.loadClass("org.jboss.as.test.integration.osgi.deployment.bundle.AttachedType");
      Assert.fail("ClassNotFoundException expected");
    } catch (ClassNotFoundException ex) {
      // expected
    }

    server.undeploy(fragmentName);
    Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, fragment.getState());

    server.undeploy(hostName);
    Assert.assertEquals("Bundle UNINSTALLED", Bundle.UNINSTALLED, host.getState());
  }
 @Test
 public void testBadBundleVersion() throws Exception {
   ModelControllerClient client =
       FrameworkUtils.waitForService(context, ModelControllerClient.class);
   ServerDeploymentHelper server = new ServerDeploymentHelper(client);
   InputStream input = deployer.getDeployment(BAD_BUNDLE_VERSION);
   try {
     server.deploy(BAD_BUNDLE_VERSION, input);
     Assert.fail("Deployment exception expected");
   } catch (Exception ex) {
     // expected
   }
 }