/** * Test {@link Manager} warm up if there service with circular dependency in post-construct. * * @throws ManagerException */ @Test public void circularDependencyInWarmUpTest() throws ManagerException { Manager.register( ServiceWithCircularDependencyInWarmUp.class, ServiceWithCircularDependencyInWarmUpFactory.class); Manager.get(ServiceWithCircularDependencyInWarmUp.class); }
/** Error cases test. */ @Test public void errorCasesTest() { try { ServiceFactory<SampleService> factory = null; Manager.register(SampleService.class, factory, null, new Scope[] {}); Assert.fail("Exception shold be thrown before this step."); } catch (IllegalArgumentException e) { Assert.assertTrue( "Right exception should be there.", e.getMessage().startsWith("serviceFactory")); } try { Manager.register(null, new SampleServiceFactory(), null, new Scope[] {}); Assert.fail("Exception shold be thrown before this step."); } catch (IllegalArgumentException e) { Assert.assertTrue("Right exception should be there.", e.getMessage().startsWith("service")); } try { Manager.get(null); Assert.fail("Exception shold be thrown before this step."); } catch (IllegalArgumentException e) { Assert.assertTrue("Right exception should be there.", e.getMessage().startsWith("service")); } catch (ManagerException e) { Assert.fail("No exception should be on this step."); } }
/** * {@link PreDestroy} test. * * @throws ManagerException */ @Test public void preDestroyTest() throws ManagerException { // saving initial state final int currentValue = SampleServiceImpl.getPreDestroyedAcount(); Manager.register(SampleService.class, SampleServiceFactory.class); Manager.get(SampleService.class); // force initialization Manager.tearDown(); Assert.assertEquals(currentValue + 2, SampleServiceImpl.getPreDestroyedAcount()); }
@PostConstruct private void postConstruct() { try { System.out.println( this.getClass().getSimpleName() + " postConstruct[" + Manager.get(ServiceWithCircularDependencyInWarmUp.class).getMessage() + "] called"); } catch (final ManagerException e) { throw new RuntimeException(e); } }
/** {@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(); } }
/** 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); } }