polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nic...@apache.org
Subject [01/25] polygene-java git commit: Starting to support JUnit 5, as the extension model is much easier to deal with than JUnit4.
Date Thu, 19 Apr 2018 02:47:42 GMT
Repository: polygene-java
Updated Branches:
  refs/heads/develop 1adfa5732 -> ecee06b46


Starting to support JUnit 5, as the extension model is much easier to deal with than JUnit4.


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/e647b599
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/e647b599
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/e647b599

Branch: refs/heads/develop
Commit: e647b599642b353e3b8275a284dd17e9fe1aa5d7
Parents: c76a0bf
Author: niclas <niclas@hedhman.org>
Authored: Fri Apr 13 08:40:16 2018 +0800
Committer: niclas <niclas@hedhman.org>
Committed: Fri Apr 13 08:40:16 2018 +0800

----------------------------------------------------------------------
 .../polygene/test/PolygeneUnitExtension.java    | 73 ++++++++++++++++++++
 .../apache/polygene/test/junit5/Junit5Test.java | 40 +++++++++++
 2 files changed, 113 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e647b599/core/testsupport/src/main/java/org/apache/polygene/test/PolygeneUnitExtension.java
----------------------------------------------------------------------
diff --git a/core/testsupport/src/main/java/org/apache/polygene/test/PolygeneUnitExtension.java
b/core/testsupport/src/main/java/org/apache/polygene/test/PolygeneUnitExtension.java
new file mode 100644
index 0000000..d3eb8fe
--- /dev/null
+++ b/core/testsupport/src/main/java/org/apache/polygene/test/PolygeneUnitExtension.java
@@ -0,0 +1,73 @@
+package org.apache.polygene.test;
+
+import org.apache.polygene.api.structure.Application;
+import org.apache.polygene.bootstrap.Assembler;
+import org.apache.polygene.bootstrap.ModuleAssembly;
+import org.apache.polygene.bootstrap.SingletonAssembler;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+/** JUNIT 5 Extension for running Polygene unit tests.
+ *
+ * This will create a Singleton Application only, i.e. one layer with one module.
+ */
+public class PolygeneUnitExtension
+    implements Extension, BeforeTestExecutionCallback, AfterTestExecutionCallback
+{
+    private final Assembler assembler;
+    private Application application;
+
+    public static PolygeneUnitExtensionBuilder forModule( Assembler assembler )
+    {
+        return new PolygeneUnitExtensionBuilder(assembler);
+    }
+
+    private PolygeneUnitExtension( Assembler assembler )
+    {
+
+        this.assembler = assembler;
+    }
+
+    @Override
+    public void beforeTestExecution( ExtensionContext context )
+        throws Exception
+    {
+        SingletonAssembler app = new SingletonAssembler( assembler )
+        {
+            @Override
+            public void assemble( ModuleAssembly module )
+            {
+                super.assemble( module );
+                module.objects( context.getRequiredTestClass() );
+            }
+        };
+        app.module().objectFactory().injectTo( context.getRequiredTestInstance() );
+        application = app.application();
+    }
+
+    @Override
+    public void afterTestExecution( ExtensionContext context )
+        throws Exception
+    {
+        application.passivate();
+    }
+
+    public static class PolygeneUnitExtensionBuilder
+    {
+        private final Assembler assembler;
+
+        public PolygeneUnitExtensionBuilder( Assembler assembler )
+        {
+            this.assembler = assembler;
+        }
+
+        public PolygeneUnitExtension build()
+        {
+            return new PolygeneUnitExtension( assembler );
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/e647b599/core/testsupport/src/test/java/org/apache/polygene/test/junit5/Junit5Test.java
----------------------------------------------------------------------
diff --git a/core/testsupport/src/test/java/org/apache/polygene/test/junit5/Junit5Test.java
b/core/testsupport/src/test/java/org/apache/polygene/test/junit5/Junit5Test.java
new file mode 100644
index 0000000..9d6d05c
--- /dev/null
+++ b/core/testsupport/src/test/java/org/apache/polygene/test/junit5/Junit5Test.java
@@ -0,0 +1,40 @@
+package org.apache.polygene.test.junit5;
+
+import org.apache.polygene.api.injection.scope.Structure;
+import org.apache.polygene.api.value.ValueBuilder;
+import org.apache.polygene.api.value.ValueBuilderFactory;
+import org.apache.polygene.test.PolygeneUnitExtension;
+import org.apache.polygene.test.model.Cat;
+import org.junit.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.runner.RunWith;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.hamcrest.core.IsNull.notNullValue;
+
+/**
+ * This test is to help develop the JUnit 5 Extension.
+ */
+public class Junit5Test
+{
+
+    @RegisterExtension
+    PolygeneUnitExtension polygene = PolygeneUnitExtension.forModule( module -> {
+        module.values(Cat.class );
+    } ).build();
+
+    @Structure
+    private ValueBuilderFactory vbf;
+
+    @Test
+    public void givenPolygeneWhenInstantiatingCatExpectCatInstantiation()
+    {
+        ValueBuilder<Cat> builder = vbf.newValueBuilder( Cat.class );
+        builder.prototype().name().set( "Kim" );
+        Cat cat = builder.newInstance();
+
+        assertThat( cat, notNullValue() );
+        assertThat( cat.name().get(), equalTo("Kim") );
+    }
+}


Mime
View raw message