Tighten private mixins and @This injection tests
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/3c6c11f4 Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/3c6c11f4 Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/3c6c11f4 Branch: refs/heads/develop Commit: 3c6c11f4d3cc7d2fb3421e1bb2671560ecfb649c Parents: f871f7a Author: Paul Merlin <[email protected]> Authored: Mon Nov 16 16:23:51 2015 +0100 Committer: Paul Merlin <[email protected]> Committed: Mon Nov 16 16:23:51 2015 +0100 ---------------------------------------------------------------------- .../ConstructorInjectionOfThisTest.java | 72 +++++++++++++++++--- .../zest/runtime/mixin/PrivateMixinTest.java | 38 +++++++++-- 2 files changed, 92 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/3c6c11f4/core/runtime/src/test/java/org/apache/zest/runtime/injection/ConstructorInjectionOfThisTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/injection/ConstructorInjectionOfThisTest.java b/core/runtime/src/test/java/org/apache/zest/runtime/injection/ConstructorInjectionOfThisTest.java index 308e506..43ae9cb 100644 --- a/core/runtime/src/test/java/org/apache/zest/runtime/injection/ConstructorInjectionOfThisTest.java +++ b/core/runtime/src/test/java/org/apache/zest/runtime/injection/ConstructorInjectionOfThisTest.java @@ -4,7 +4,7 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * - * You may obtain a copy of the License at + * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software @@ -13,20 +13,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.zest.runtime.injection; import org.junit.Test; import org.apache.zest.api.activation.ActivationException; +import org.apache.zest.api.common.UseDefaults; import org.apache.zest.api.concern.ConcernOf; import org.apache.zest.api.injection.scope.This; -import org.apache.zest.api.mixin.Mixins; import org.apache.zest.api.mixin.NoopMixin; +import org.apache.zest.api.property.Property; import org.apache.zest.api.sideeffect.SideEffectOf; import org.apache.zest.api.structure.Module; import org.apache.zest.bootstrap.AssemblyException; import org.apache.zest.bootstrap.ModuleAssembly; import org.apache.zest.bootstrap.SingletonAssembler; +import org.junit.Ignore; + +import static org.junit.Assert.assertFalse; /** * This test is created in response to QI-359 @@ -35,7 +38,7 @@ public class ConstructorInjectionOfThisTest { @Test - public void givenConcernWithThisInConstructorWhenCreatingModelExpectException() + public void givenMixinWithThisInConstructorWhenCreatingModelExpectNoException() throws ActivationException, AssemblyException { SingletonAssembler singletonAssembler = new SingletonAssembler() @@ -45,7 +48,7 @@ public class ConstructorInjectionOfThisTest public void assemble( ModuleAssembly module ) throws AssemblyException { - module.values( Does.class ).withConcerns( DoesConcern.class ); + module.values( Does.class ).withMixins( DoesMixin.class ); } }; Module module = singletonAssembler.application().findModule( "Layer 1", "Module 1" ); @@ -53,8 +56,27 @@ public class ConstructorInjectionOfThisTest does.doSomething(); } - @Test - public void givenSideEffectWithThisInConstructorWhenCreatingModelExpectException() + @Test @Ignore + public void givenConcernWithThisInConstructorWhenCreatingModelExpectNoException() + throws ActivationException, AssemblyException + { + SingletonAssembler singletonAssembler = new SingletonAssembler() + { + + @Override + public void assemble( ModuleAssembly module ) + throws AssemblyException + { + 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(); + } + + @Test @Ignore + public void givenSideEffectWithThisInConstructorWhenCreatingModelExpectNoException() throws ActivationException, AssemblyException { SingletonAssembler singletonAssembler = new SingletonAssembler() @@ -64,7 +86,7 @@ public class ConstructorInjectionOfThisTest public void assemble( ModuleAssembly module ) throws AssemblyException { - module.values( Does.class ).withSideEffects( DoesSideEffect.class ); + module.values( Does.class ).withMixins( NoopMixin.class ).withSideEffects( DoesSideEffect.class ); } }; Module module = singletonAssembler.application().findModule( "Layer 1", "Module 1" ); @@ -72,7 +94,29 @@ public class ConstructorInjectionOfThisTest does.doSomething(); } - public static class DoesConcern extends ConcernOf<Does> + public static class DoesMixin + implements Does + { + private DoesPrivateFragment doesPrivateFragment; + + public DoesMixin( @This DoesPrivateFragment doesPrivateFragment ) + { + if( doesPrivateFragment == null ) + { + throw new NullPointerException(); + } + this.doesPrivateFragment = doesPrivateFragment; + } + + @Override + public void doSomething() + { + assertFalse( doesPrivateFragment.someState().get() ); + } + } + + public static class DoesConcern + extends ConcernOf<Does> implements Does { @@ -92,7 +136,8 @@ public class ConstructorInjectionOfThisTest } } - public static class DoesSideEffect extends SideEffectOf<Does> + public static class DoesSideEffect + extends SideEffectOf<Does> implements Does { @@ -112,7 +157,12 @@ public class ConstructorInjectionOfThisTest } } - @Mixins( NoopMixin.class ) + public interface DoesPrivateFragment + { + @UseDefaults + Property<Boolean> someState(); + } + public interface Does { void doSomething(); http://git-wip-us.apache.org/repos/asf/zest-java/blob/3c6c11f4/core/runtime/src/test/java/org/apache/zest/runtime/mixin/PrivateMixinTest.java ---------------------------------------------------------------------- diff --git a/core/runtime/src/test/java/org/apache/zest/runtime/mixin/PrivateMixinTest.java b/core/runtime/src/test/java/org/apache/zest/runtime/mixin/PrivateMixinTest.java index 9ed5207..98a24d5 100644 --- a/core/runtime/src/test/java/org/apache/zest/runtime/mixin/PrivateMixinTest.java +++ b/core/runtime/src/test/java/org/apache/zest/runtime/mixin/PrivateMixinTest.java @@ -12,7 +12,6 @@ * limitations under the License. * */ - package org.apache.zest.runtime.mixin; import org.junit.Test; @@ -33,6 +32,7 @@ import static org.junit.Assert.assertThat; public class PrivateMixinTest extends AbstractZestTest { + @Override public void assemble( ModuleAssembly module ) throws AssemblyException { @@ -43,10 +43,10 @@ public class PrivateMixinTest * Tests that private mixins are injected correctly. */ @Test - public void privateMixinInjection() + public void privateMixinFieldAndConstructorInjection() { SpeakComposite test = module.newTransient( SpeakComposite.class ); - assertThat( "Speak", test.speak(), is( equalTo( "I say it works" ) ) ); + assertThat( "Speak", test.speak(), is( equalTo( "I say it works!" ) ) ); } @Mixins( SpeakMixin.class ) @@ -58,21 +58,27 @@ public class PrivateMixinTest public static class SpeakMixin implements Speak { - @This - Word word; + private final Word word; + @This Punctuation punctuation; + + public SpeakMixin( @This Word word ) + { + this.word = word; + } + @Override public String speak() { - return "I say " + word.get(); + return "I say " + word.get() + punctuation.punctuate(); } } - @Mixins( { WordMixin.class } ) public interface SpeakComposite extends Speak, TransientComposite { } + @Mixins( WordMixin.class ) public interface Word { String get(); @@ -81,9 +87,27 @@ public class PrivateMixinTest public static class WordMixin implements Word { + @Override public String get() { return "it works"; } } + + @Mixins( PunctuationMixin.class ) + public interface Punctuation + { + String punctuate(); + } + + public static class PunctuationMixin + implements Punctuation + { + @Override + public String punctuate() + { + return "!"; + } + + } }
