/** * 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 ); }
/** * Creates a Polygene Runtime instance containing one Layer with one Module. * The Layer will be named "Layer 1" and the Module will be named "Module 1". It is possible to add * additional layers and modules via the Assembler interface that must be implemented in the subclass of this * class. * * @throws AssemblyException Either if the model can not be created from the disk, or some inconsistency in * the programming model makes it impossible to create it. * @throws ActivationException If the automatic {@code activate()} method is throwing this Exception.. */ public SingletonAssembler() throws AssemblyException, ActivationException { // START SNIPPET: actual polygene = new Energy4Java(); applicationInstance = createApplicationInstance(); activateApplication(); // END SNIPPET: actual moduleInstance = applicationInstance.findModule( layerName(), moduleName() ); }
private void activateApplication() throws ActivationException { try { beforeActivation( applicationInstance ); applicationInstance.activate(); } catch( Exception e ) { if( e instanceof ActivationException ) { throw ( (ActivationException) e ); } throw new ActivationException( "Could not activate application", e ); } } // END SNIPPET: actual
@Test public final void givenImportedSpringServicesWhenServicesAreInjectedThenCanIdentifyByName() throws ActivationException, AssemblyException { SingletonAssembler assembler = new SingletonAssembler() { public void assemble( ModuleAssembly module ) throws AssemblyException { module.objects( PolygeneImportServiceTest.class ); new SpringImporterAssembler( appContext ).assemble( module ); } }; assembler.module().injectTo(this); CommentService service = StreamSupport.stream( services.spliterator(), false ) .filter( withId( "commentService2" ) ) .findFirst().map( ServiceReference::get ).orElse( null ); assertThat( "service with correct id has been selected", service.comment( "pizza" ), equalTo( "pizza is good." ) ); }
@Override public void evaluate() throws Throwable { String name = indexName( description.getClassName(), description.getMethodName() ); SingletonAssembler assembler = activateEmbeddedElasticsearch( name ); Application application = assembler.application(); client = findClient( assembler.module() ); try { base.evaluate(); } finally { application.passivate(); client.close(); client = null; } } };
@Test public void testModulesActivators() throws Exception { SingletonAssembler assembly = new SingletonAssembler( module -> module.withActivators( TestedActivator.class ) ); // Activate Application application = assembly.application(); // Assert activated Assert.assertEquals( "Activation Level", 2, activationLevel ); // Passivate application.passivate(); // Assert passivated Assert.assertEquals( "Passivation Level", 2, passivationLevel ); }
@Test public void testInjectService() throws Exception { SingletonAssembler assembly = new SingletonAssembler( module -> { module.services( MyServiceComposite.class ) .identifiedBy( "Foo" ) .setMetaInfo( new ServiceName( "Foo" ) ); module.services( MyServiceComposite2.class ) .identifiedBy( "Bar" ) .setMetaInfo( new ServiceName( "Bar" ) ); module.services( StringService.class, LongService.class ); module.objects( ServiceUser.class ); } ); testInjection( assembly ); }
@Test public void givenTransientWithRuntimeMixinsWhenAssembledExpectCorrectComposition() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.transients( SayWhat.class ).withMixins( SayThisMixin.class, SayThatMixin.class ) ); SayWhat value = singletonAssembler.transientBuilderFactory().newTransient( SayWhat.class ); assertThat( value.sayThis(), equalTo( "this" ) ); assertThat( value.sayThat(), equalTo( "that" ) ); }
@Test public void givenServiceWithRuntimeMixinsWhenAssembledExpectCorrectComposition() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.services( SayWhat.class ).withMixins( SayThisMixin.class, SayThatMixin.class ) ); SayWhat value = singletonAssembler.serviceFinder().findService( SayWhat.class ).get(); assertThat( value.sayThis(), equalTo( "this" ) ); assertThat( value.sayThat(), equalTo( "that" ) ); }
@Test public void givenValueWithRuntimeMixinsWhenAssembledExpectCorrectComposition() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.values( SayWhat.class ).withMixins( SayThisMixin.class, SayThatMixin.class ) ); SayWhat value = singletonAssembler.valueBuilderFactory().newValue( SayWhat.class ); assertThat( value.sayThis(), equalTo( "this" ) ); assertThat( value.sayThat(), equalTo( "that" ) ); }
/** * 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 testActivatable() throws Exception { SingletonAssembler assembly = new SingletonAssembler() { public void assemble( ModuleAssembly module ) throws AssemblyException { module.objects( ActivatableServiceTest.class ); module.services( ActivatableComposite.class ).withActivators( TestActivator.class).instantiateOnStartup(); } }; assertTrue( isActive ); assembly.module().injectTo( this ); assertTrue( isActive ); service.get(); assertTrue( isActive ); assembly.application().passivate(); assertFalse( isActive ); }
@Test public void givenMixinWithThisInConstructorWhenCreatingModelExpectNoException() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.values( Does.class ).withMixins( DoesMixin.class ) ); Module module = singletonAssembler.application().findModule( "Layer 1", "Module 1" ); Does does = module.newValue( Does.class ); does.doSomething(); }
SingletonAssembler polygene = new SingletonAssembler( module -> module.values( MyStuffValueComposite.class ) );
@Test public void givenTransientWithRuntimeMixinOverrideWhenAssembledExpectCorrectComposition() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.transients( SayWhere.class ).withMixins( SayHereMixin.class ) ); SayWhere value = singletonAssembler.transientBuilderFactory().newTransient( SayWhere.class ); assertThat( value.sayHere(), equalTo( "here" ) ); assertThat( value.sayThere(), nullValue() ); }
@Test public void givenServiceWithRuntimeMixinOverrideWhenAssembledExpectCorrectComposition() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.services( SayWhere.class ).withMixins( SayHereMixin.class ) ); SayWhere value = singletonAssembler.serviceFinder().findService( SayWhere.class ).get(); assertThat( value.sayHere(), equalTo( "here" ) ); assertThat( value.sayThere(), nullValue() ); }
@Test public void givenValueWithRuntimeMixinOverrideWhenAssembledExpectCorrectComposition() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.values( SayWhere.class ).withMixins( SayHereMixin.class ) ); SayWhere value = singletonAssembler.valueBuilderFactory().newValue( SayWhere.class ); assertThat( value.sayHere(), equalTo( "here" ) ); assertThat( value.sayThere(), nullValue() ); }
/** * 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 can be created for an registered object class. */ @Test public void newInstanceForRegisteredComposite() 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 givenSuccessPassivationWhenPassivatingExpectNoExceptions() throws Throwable { SingletonAssembler assembly = new SingletonAssembler() { public void assemble( ModuleAssembly module ) throws AssemblyException { module.addServices( DataAccessService.class ).withActivators( PassivationSuccessActivator.class ); module.addServices( DataAccessService.class ).withActivators( PassivationSuccessActivator.class ); } }; assembly.module().findServices( DataAccess.class ).forEach( service -> { assertTrue( "Service should not be Active before accessed", !service.isActive() ); assertTrue( service.get().data().activated ); assertTrue( "Service should be Active after access.", service.isActive() ); } ); assembly.application().passivate(); }