/** * Tests that an object builder can be created for an registered object. */ @Test public void newBuilderForRegisteredComposite() throws ActivationException, AssemblyException { SingletonAssembler assembler = new SingletonAssembler() { public void assemble( ModuleAssembly module ) throws AssemblyException { module.transients( AnyComposite.class ); } }; assembler.module().newTransientBuilder( AnyComposite.class ); }
@Test public void testClassScannerFiles() throws ActivationException, AssemblyException { SingletonAssembler singleton = new SingletonAssembler( module -> { // Find all classes starting from TestValue, but include only the ones that are named *Value findClasses( TestValue.class ).filter( matches( ".*Value" ) ) .forEach( module::values ); } ); singleton.module().newValueBuilder( TestValue.class ); singleton.module().newValueBuilder( Test2Value.class ); }
/** * Tests that an object builder cannot be created for a 'null' type. * * @throws Exception expected */ @Test( expected = NullPointerException.class ) public void newBuilderForNullType() throws Exception { SingletonAssembler assembler = new SingletonAssembler( module -> {} ); assembler.module().newObject( null ); }
/** * Tests that an object builder cannot be created for an unregistered object. * * @throws Exception expected */ @Test( expected = NoSuchObjectTypeException.class ) public void newBuilderForUnregisteredObject() throws Exception { SingletonAssembler assembler = new SingletonAssembler( module -> {} ); assembler.module().newObject( AnyObject.class ); }
@Test public void givenManyConstructorsWhenInstantiateThenChooseCorrectConstructor() throws ActivationException { SingletonAssembler assembler = new SingletonAssembler( module -> module.objects( ManyConstructorObject.class ) ); ManyConstructorObject object = assembler.module().newObject( ManyConstructorObject.class ); Assert.assertThat( "ref is not null", object.anyObject, notNullValue() ); object = assembler.module() .newObject( ManyConstructorObject.class, new AnyObject() ); Assert.assertThat( "ref is not null", object.anyObject, notNullValue() ); }
/** * Tests that an object builder cannot be created for a 'null' type. * * @throws Exception expected */ @Test( expected = NullPointerException.class ) public void newObjectInstanceForNullType() throws Exception { SingletonAssembler assembler = new SingletonAssembler( module -> {} ); assembler.module().newObject( null ); }
@Test public final void givenImportedSpringServicesWhenServiceIsInjectedThenUseSpringService() throws ActivationException, AssemblyException { SingletonAssembler assembler = new SingletonAssembler() { public void assemble( ModuleAssembly module ) throws AssemblyException { module.objects( PolygeneImportServiceTest.class ); // START SNIPPET: import new SpringImporterAssembler( appContext ).assemble( module ); // END SNIPPET: import } }; assembler.module().injectTo( this ); assertThat( "service can be called", service.comment( "beer" ), equalTo( "beer is good." ) ); }
@Test public void givenAvailableServiceWhenCheckServiceReferenceThenReturnTrue() throws ActivationException, AssemblyException { SingletonAssembler assembler = new SingletonAssembler() { public void assemble( ModuleAssembly module ) throws AssemblyException { module.services( TestServiceComposite1.class ); } }; ServiceReference<TestServiceComposite1> serviceRef = assembler.module() .findService( TestServiceComposite1.class ); assertThat( "service is available", serviceRef.isAvailable(), equalTo( true ) ); }
@Test( expected = ConstructionException.class ) public void testMissingServiceDependency() throws ActivationException { // No service fulfils the dependency injection -> fail to create application new SingletonAssembler( module -> module.objects( ServiceUser.class ) ) .module().newObject( ServiceUser.class ); }
/** * Tests that an object can be created for an registered object class. */ @Test public void newInstanceForRegisteredObject() throws ActivationException { SingletonAssembler assembler = new SingletonAssembler( module -> module.objects( AnyObject.class ) ); assembler.module().newObject( AnyObject.class ); }
@Test public void givenClassWithInnerClassesWhenInstantiateThenInstantiateInnerClass() throws ActivationException { SingletonAssembler assembler = new SingletonAssembler( module -> module.objects( OuterClass.class ) ); Assert.assertThat( "inner class has been injected", assembler.module() .newObject( OuterClass.class ) .name(), equalTo( "Module 1" ) ); }
@Test public void testConcernOnObject() throws ActivationException, AssemblyException { SingletonAssembler assembler = new SingletonAssembler( module -> module.objects( TestObject.class ) ); TestObject object = assembler.module().newObject( TestObject.class ); object.doStuff(); }
@Test public void testGroovyClassMixin() throws Exception { // START SNIPPET: direct SingletonAssembler assembler = new SingletonAssembler( assembly -> assembly.transients( HelloSpeaker.class ) .withMixins( HelloSpeakerMixin.class ) ); HelloSpeaker speaker = assembler.module().newTransient( HelloSpeaker.class ); assertThat( speaker.sayHello(), equalTo("Hello there, Groovy") ); // END SNIPPET: direct } }
@Test public void testGroovyScriptResourceMixin() throws Exception { // START SNIPPET: script SingletonAssembler assembler = new SingletonAssembler( assembly -> assembly.values( HelloSpeaker.class ) .setMetaInfo( Scripting.GROOVY ) .withMixins( ScriptMixin.class ) ); HelloSpeaker speaker = assembler.module().newValue( HelloSpeaker.class ); assertThat( speaker.sayHello(), equalTo("Hello, Groovy") ); // END SNIPPET: script }
@Test public void whenInvocationInjectionWithMethodWhenInjectedThenInjectMethod() throws Exception { SingletonAssembler assembly = new SingletonAssembler( module -> module.transients( MyComposite.class ) ); MyComposite composite = assembly.module().newTransient( MyComposite.class ); composite.doStuff(); composite.doStuff(); composite.doStuff2(); composite.doStuff3(); }
void singleton() throws ActivationException, AssemblyException { // START SNIPPET: singleton SingletonAssembler assembler = new SingletonAssembler( module -> { module.services( MyService.class ).identifiedBy( "Foo" ); module.services( MyService.class ).identifiedBy( "Bar" ); module.objects( Stuff.class ); } ); Module module = assembler.module(); Stuff stuff = module.newObject( Stuff.class ); // END SNIPPET: singleton } }
@BeforeClass public static void setup() throws Exception { assembler = new SingletonAssembler( module -> { module.entities( CheckingAccountRolemap.class, SavingsAccountRolemap.class, CreditorRolemap.class ); new EntityTestAssembler().assemble( module ); } ); uowf = assembler.module().unitOfWorkFactory(); bootstrapData( assembler ); }
private void testInjection( SingletonAssembler assembly ) { ObjectFactory factory = assembly.module(); ServiceUser user = factory.newObject( ServiceUser.class ); assertEquals( "X", user.testSingle() ); assertThat( user.testIdentity(), equalTo( StringIdentity.identityOf( "Foo" ) ) ); assertEquals( "FooX", user.testServiceReference() ); assertEquals( "Bar", user.testQualifier() ); assertEquals( "A", user.testStringIterable() ); assertEquals( new Long( 1L ), user.testLongIterable() ); assertEquals( "FooXBarX", user.testIterableServiceReferences() ); assertEquals( "XX", user.testIterable() ); }
@Test public void givenConstrainedConfigurationWhenCorrectValueExpectNoFailure() throws Exception { SingletonAssembler underTest = new SingletonAssembler( module -> { module.defaultServices(); module.services( MemoryEntityStoreService.class ); module.services( TestService.class ).identifiedBy( "TestService1" ); module.configurations( TestConfiguration.class ); } ); ServiceReference<TestService> service = underTest.module().findService( TestService.class ); service.get().test(); }
@Test( expected = ConstraintViolationException.class ) public void givenConstrainedConfigurationWhenIncorrectValueExpectConstraintViolationFailure() throws Exception { SingletonAssembler underTest = new SingletonAssembler( module -> { module.defaultServices(); module.services( MemoryEntityStoreService.class ); module.services( TestService.class ).identifiedBy( "TestService2" ); module.configurations( TestConfiguration.class ); } ); ServiceReference<TestService> service = underTest.module().findService( TestService.class ); service.get().test(); fail( "Expected failure from constraint violation." ); }