diff --git a/zats-mimic/pom.xml b/zats-mimic/pom.xml index 4c7e254c..7fdb7d4c 100644 --- a/zats-mimic/pom.xml +++ b/zats-mimic/pom.xml @@ -44,7 +44,7 @@ UTF-8 5.0.10 - 9.4.6.v20170531 + 9.4.7.v20170914 @@ -61,7 +61,7 @@ junit junit 4.12 - test + provided org.eclipse.jetty diff --git a/zats-mimic/src/main/java/org/zkoss/zats/junit/AutoClient.java b/zats-mimic/src/main/java/org/zkoss/zats/junit/AutoClient.java new file mode 100644 index 00000000..6889327a --- /dev/null +++ b/zats-mimic/src/main/java/org/zkoss/zats/junit/AutoClient.java @@ -0,0 +1,50 @@ +package org.zkoss.zats.junit; + +import org.junit.After; +import org.junit.Before; +import org.junit.rules.ExternalResource; +import org.junit.rules.TestRule; +import org.zkoss.zats.mimic.Client; +import org.zkoss.zats.mimic.DesktopAgent; + +/** + * A {@link TestRule} implementing {@link ExternalResource} automatically creating and destroying a new Zats {@link Client} instance.
+ * Used with {@link org.junit.Rule} this will provide a pluggable alternative to separate methods annotated with {@link Before} and {@link After} + */ +public class AutoClient extends ExternalResource { + private AutoEnvironment autoEnv; + private Client client; + + public AutoClient(AutoEnvironment autoEnv) { + this.autoEnv = autoEnv; + } + + @Override + protected void before() throws Throwable { + client = autoEnv.getZatsEnvironment().newClient(); + } + + @Override + protected void after() { + client.destroy(); + } + + /** + * convenience method to load a zul page directly (calls: {@link Client#connect(String)}) + * + * @param zulPath + * @return {@link DesktopAgent} + */ + public DesktopAgent connect(String zulPath) { + return client.connect(zulPath); + } + + /** + * allows access to the automatically created Zats {@link Client} instance. To access all functionality. + * + * @return the current Zats {@link Client} + */ + public Client getClient() { + return client; + } +} diff --git a/zats-mimic/src/main/java/org/zkoss/zats/junit/AutoEnvironment.java b/zats-mimic/src/main/java/org/zkoss/zats/junit/AutoEnvironment.java new file mode 100644 index 00000000..1b61aa7a --- /dev/null +++ b/zats-mimic/src/main/java/org/zkoss/zats/junit/AutoEnvironment.java @@ -0,0 +1,68 @@ +package org.zkoss.zats.junit; + +import org.junit.*; +import org.junit.rules.ExternalResource; +import org.junit.rules.TestRule; +import org.zkoss.zats.mimic.DefaultZatsEnvironment; +import org.zkoss.zats.mimic.ZatsEnvironment; + +/** + * A {@link TestRule} implementing {@link ExternalResource} creating and destroying a {@link ZatsEnvironment}.
+ * Used with {@link org.junit.ClassRule} it provides a pluggable alternative to separate static methods annotated with {@link BeforeClass} and {@link AfterClass}
+ *
+ * Also see: dzone.com/articles/junit-49-class-and-suite-level-rules + */ +public class AutoEnvironment extends ExternalResource { + private ZatsEnvironment zatsEnvironment; + + private String resourceRoot; + private String webInfPath; + + /** + * Creates a default {@link DefaultZatsEnvironment#DefaultZatsEnvironment()} + * + * @param resourceRoot - will be passed to {@link ZatsEnvironment#init(String)} + */ + public AutoEnvironment(String resourceRoot) { + this.resourceRoot = resourceRoot; + } + + /** + * Creates a {@link DefaultZatsEnvironment#DefaultZatsEnvironment()} with a specified WEB-INF folder + * + * @param webInfPath - will be used as the constructor argument in {@link DefaultZatsEnvironment#DefaultZatsEnvironment(String)} + * @param resourceRoot - will be passed to {@link ZatsEnvironment#init(String)} + */ + public AutoEnvironment(String webInfPath, String resourceRoot) { + this.webInfPath = webInfPath; + this.resourceRoot = resourceRoot; + } + + @Override + protected void before() throws Throwable { + if (webInfPath != null) { + zatsEnvironment = new DefaultZatsEnvironment(webInfPath); + } else { + zatsEnvironment = new DefaultZatsEnvironment(); + } + zatsEnvironment.init(resourceRoot); + } + + @Override + protected void after() { + zatsEnvironment.destroy(); + } + + /** + * creates an {@link AutoClient} rule from the current {@link AutoEnvironment} + * + * @return an {@link AutoClient} rule + */ + public AutoClient autoClient() { + return new AutoClient(this); + } + + public ZatsEnvironment getZatsEnvironment() { + return zatsEnvironment; + } +} diff --git a/zats-mimic/src/test/java/org/zkoss/zats/junit/AutoEnvClientTest.java b/zats-mimic/src/test/java/org/zkoss/zats/junit/AutoEnvClientTest.java new file mode 100644 index 00000000..47518fb8 --- /dev/null +++ b/zats-mimic/src/test/java/org/zkoss/zats/junit/AutoEnvClientTest.java @@ -0,0 +1,33 @@ +package org.zkoss.zats.junit; + +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.zkoss.zats.mimic.DesktopAgent; +import org.zkoss.zul.Label; + + +public class AutoEnvClientTest { + @ClassRule + public static AutoEnvironment autoEnv = new AutoEnvironment("src/test/resources/web/junit"); + + @Rule + public AutoClient autoClient = autoEnv.autoClient(); + + @Test + public void testAutoClient() { + DesktopAgent desktopAgent = autoClient.connect("/test.zul"); + Label testLabel = desktopAgent.query("#testLabel").as(Label.class); + Assert.assertEquals("initial label: ", "initial", testLabel.getValue()); + desktopAgent.query("#testButton").click(); + Assert.assertEquals("updated label: ", "updated", testLabel.getValue()); + } + + @Test + public void testLibraryProperty() { + DesktopAgent desktopAgent = autoClient.connect("/testLibraryProperty.zul"); + Label testLabel = desktopAgent.query("#libProp").as(Label.class); + Assert.assertEquals("libproperty ('zats.hello') should not exist in default config: ", "", testLabel.getValue()); + } +} diff --git a/zats-mimic/src/test/java/org/zkoss/zats/junit/AutoEnvClientWebInfTest.java b/zats-mimic/src/test/java/org/zkoss/zats/junit/AutoEnvClientWebInfTest.java new file mode 100644 index 00000000..63283b1f --- /dev/null +++ b/zats-mimic/src/test/java/org/zkoss/zats/junit/AutoEnvClientWebInfTest.java @@ -0,0 +1,26 @@ +package org.zkoss.zats.junit; + +import org.junit.Assert; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.zkoss.lang.Library; +import org.zkoss.zats.mimic.DesktopAgent; +import org.zkoss.zul.Label; + +public class AutoEnvClientWebInfTest { + @ClassRule + public static AutoEnvironment autoEnv = new AutoEnvironment("src/test/resources/web/WEB-INF/custom", "src/test/resources/web/junit"); + + @Rule + public AutoClient autoClient = autoEnv.autoClient(); + + @Test + public void testAutoClient() { + DesktopAgent desktopAgent = autoClient.connect("/testLibraryProperty.zul"); + Label testLabel = desktopAgent.query("#libProp").as(Label.class); + Assert.assertEquals("libproperty ('zats.hello') from web/WEB-INF/custom/zk.xml: ", "hello zats", testLabel.getValue()); + Library.setProperty("zats.hello", null); + } + +} diff --git a/zats-mimic/src/test/java/org/zkoss/zats/testcase/EnvironmentTest.java b/zats-mimic/src/test/java/org/zkoss/zats/testcase/EnvironmentTest.java index 8ea0bd60..4ec7b06a 100644 --- a/zats-mimic/src/test/java/org/zkoss/zats/testcase/EnvironmentTest.java +++ b/zats-mimic/src/test/java/org/zkoss/zats/testcase/EnvironmentTest.java @@ -28,6 +28,7 @@ import junit.framework.Assert; import org.junit.Test; +import org.zkoss.lang.Library; import org.zkoss.zats.mimic.Client; import org.zkoss.zats.mimic.ComponentAgent; import org.zkoss.zats.mimic.DefaultZatsEnvironment; @@ -107,6 +108,7 @@ public void testCustomConfig() { //custom config assertEquals("hello zats", desktop.query("#msg").as(Label.class).getValue()); }finally{ + Library.setProperty("zats.hello", null); ctx.destroy(); } } diff --git a/zats-mimic/src/test/resources/web/junit/test.zul b/zats-mimic/src/test/resources/web/junit/test.zul new file mode 100644 index 00000000..7a79f5e7 --- /dev/null +++ b/zats-mimic/src/test/resources/web/junit/test.zul @@ -0,0 +1,4 @@ + +