-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robert Wenzel
committed
Nov 28, 2017
1 parent
6c0fdf8
commit 6a385ee
Showing
8 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
zats-mimic/src/main/java/org/zkoss/zats/junit/AutoClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.</br> | ||
* 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; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
zats-mimic/src/main/java/org/zkoss/zats/junit/AutoEnvironment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}.</br> | ||
* Used with {@link org.junit.ClassRule} it provides a pluggable alternative to separate static methods annotated with {@link BeforeClass} and {@link AfterClass}</br> | ||
* </br> | ||
* Also see: <a href="https://dzone.com/articles/junit-49-class-and-suite-level-rules" target="_blank">dzone.com/articles/junit-49-class-and-suite-level-rules</a> | ||
*/ | ||
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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
zats-mimic/src/test/java/org/zkoss/zats/junit/AutoEnvClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
zats-mimic/src/test/java/org/zkoss/zats/junit/AutoEnvClientWebInfTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<zk> | ||
<button id="testButton" label="Test Button" onClick='testLabel.setValue("updated")'/> | ||
<label id="testLabel" value="initial"/> | ||
</zk> |
4 changes: 4 additions & 0 deletions
4
zats-mimic/src/test/resources/web/junit/testLibraryProperty.zul
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?> | ||
<zk> | ||
<label id="libProp" value="${c:property('zats.hello')}"/> | ||
</zk> |