Пример #1
0
  /** {@link ScopeLink} functionality test. */
  @Test
  public void scopeLinksTest() {
    Manager.register(SampleService.class, SampleServiceFactory.class, LOCAL, SERVICE);
    SampleService instance1 = null;
    try {
      instance1 = Manager.get(SampleService.class, LOCAL, SERVICE);
      Assert.assertNotNull(instance1);
    } catch (final ManagerException e) {
      Assert.fail();
    }

    try {
      Manager.Features.disable(Feature.AUTO_IMPL_DISCOVERY);
      Manager.get(SampleService.class);
      Assert.fail("Exception shold be thrown before this step.");
    } catch (ManagerException e) {
      Assert.assertTrue(
          "Right exception instance should be there.", e instanceof ConfigurationNotFoundException);
    } finally {
      Manager.Features.enable(Feature.AUTO_IMPL_DISCOVERY);
    }

    Manager.link(SampleService.class, SERVICE);
    Manager.link(SampleService.class, SERVICE, CustomScope.get("SOMETHING"));
    Manager.link(
        SampleService.class, CustomScope.get("SOMETHING"), CompositeScope.get(LOCAL, SERVICE));
    SampleService instance2 = null;
    try {
      instance2 = Manager.get(SampleService.class);
      Assert.assertNotNull(instance2);
      Assert.assertSame(instance1, instance2);

      instance2 = Manager.get(SampleService.class, CustomScope.get("SOMETHING"));
      Assert.assertNotNull(instance2);
      Assert.assertSame(instance1, instance2);
    } catch (final ManagerException e) {
      Assert.fail();
    }
  }
Пример #2
0
  /** Service configuration module complex test. */
  @Test
  public void complexTest() {
    // checking clean configuration
    try {
      Manager.Features.disable(Feature.AUTO_IMPL_DISCOVERY);
      Manager.get(SampleService.class);
      Assert.fail("Exception shold be thrown before this step.");
    } catch (ManagerException e) {
      Assert.assertTrue(
          "Right exception instance should be there.", e instanceof ConfigurationNotFoundException);
    } finally {
      Manager.Features.enable(Feature.AUTO_IMPL_DISCOVERY);
    }

    // configuring local service
    Manager.register(SampleService.class, SampleServiceFactory.class, LOCAL, SERVICE);

    // defining additional custom scope for remote sample service
    CustomScope customScope = CustomScope.get("SAMPLE");

    // configuring remote service with obtaining trough service locator with custom configuration
    Configuration configuration = Configuration.create();
    configuration.set(SampleServiceRemoteFactory.PARAM_TEST, "configured parameter");
    Manager.register(
        SampleService.class,
        SampleServiceRemoteFactory.class,
        configuration,
        customScope,
        REMOTE,
        SERVICE);

    // checking local service
    try {
      SampleService localInstance = Manager.get(SampleService.class, SERVICE, LOCAL, null);
      Assert.assertTrue(localInstance instanceof SampleServiceImpl);
      Assert.assertEquals("CREATED", localInstance.getCreationMethod());
    } catch (ManagerException e) {
      Assert.fail("No exception should be on this step.");
    } catch (SampleServiceException e) {
      Assert.fail("No exception should be on this step.");
    }

    // checking remote service
    try {
      SampleService remoteInstance =
          Manager.get(SampleService.class, SERVICE, null, customScope, REMOTE);
      Assert.assertTrue(remoteInstance instanceof SampleServiceRemoteImpl);
      Assert.assertEquals(
          "LOCATED. PARAMETER: configured parameter", remoteInstance.getCreationMethod());
    } catch (ManagerException e) {
      Assert.fail("No exception should be on this step.");
    } catch (SampleServiceException e) {
      Assert.fail("No exception should be on this step.");
    }

    // resetting manager configuration
    Manager.tearDown();

    // checking clean configuration, default scope
    try {
      Manager.Features.disable(Feature.AUTO_IMPL_DISCOVERY);
      Manager.get(SampleService.class);
      Assert.fail("Exception shold be thrown before this step.");
    } catch (ManagerException e) {
      Assert.assertTrue(
          "Right exception instance should be there.", e instanceof ConfigurationNotFoundException);
    } finally {
      Manager.Features.enable(Feature.AUTO_IMPL_DISCOVERY);
    }

    // checking clean configuration, local scope
    try {
      Manager.Features.disable(Feature.AUTO_IMPL_DISCOVERY);
      Manager.get(SampleService.class, SERVICE, null, LOCAL);
      Assert.fail("Exception shold be thrown before this step.");
    } catch (ManagerException e) {
      Assert.assertTrue(
          "Right exception instance should be there.", e instanceof ConfigurationNotFoundException);
    } finally {
      Manager.Features.enable(Feature.AUTO_IMPL_DISCOVERY);
    }

    // checking clean configuration, remote scope
    try {
      Manager.Features.disable(Feature.AUTO_IMPL_DISCOVERY);
      Manager.get(SampleService.class, SERVICE, REMOTE, null, customScope);
      Assert.fail("Exception shold be thrown before this step.");
    } catch (ManagerException e) {
      Assert.assertTrue(
          "Right exception instance should be there.", e instanceof ConfigurationNotFoundException);
    } finally {
      Manager.Features.enable(Feature.AUTO_IMPL_DISCOVERY);
    }
  }