/** * 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 ); }
/** * 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( 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 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 ); }
@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 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" ) ); }
@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 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() ); }
@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 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 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() ); }
@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(); }
@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 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(); }
@Test public void givenSideEffectWithThisInConstructorWhenCreatingModelExpectNoException() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.values( Does.class ).withMixins( NoopMixin.class ).withSideEffects( DoesSideEffect.class ) ); Module module = singletonAssembler.application().findModule( "Layer 1", "Module 1" ); Does does = module.newValue( Does.class ); does.doSomething(); }
@Test public void givenConcernWithThisInConstructorWhenCreatingModelExpectNoException() throws ActivationException { SingletonAssembler singletonAssembler = new SingletonAssembler( module -> module.values( Does.class ).withMixins( NoopMixin.class ).withConcerns( DoesConcern.class ) ); Module module = singletonAssembler.application().findModule( "Layer 1", "Module 1" ); Does does = module.newValue( Does.class ); does.doSomething(); }