Skip to content

Commit 6a385ee

Browse files
author
Robert Wenzel
committed
added junit test rules
1 parent 6c0fdf8 commit 6a385ee

File tree

8 files changed

+189
-2
lines changed

8 files changed

+189
-2
lines changed

zats-mimic/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<properties>
4545
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4646
<zk.version>5.0.10</zk.version>
47-
<jetty.version>9.4.6.v20170531</jetty.version>
47+
<jetty.version>9.4.7.v20170914</jetty.version>
4848
</properties>
4949
<repositories>
5050
<repository>
@@ -61,7 +61,7 @@
6161
<groupId>junit</groupId>
6262
<artifactId>junit</artifactId>
6363
<version>4.12</version>
64-
<scope>test</scope>
64+
<scope>provided</scope>
6565
</dependency>
6666
<dependency>
6767
<groupId>org.eclipse.jetty</groupId>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.zkoss.zats.junit;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.rules.ExternalResource;
6+
import org.junit.rules.TestRule;
7+
import org.zkoss.zats.mimic.Client;
8+
import org.zkoss.zats.mimic.DesktopAgent;
9+
10+
/**
11+
* A {@link TestRule} implementing {@link ExternalResource} automatically creating and destroying a new Zats {@link Client} instance.</br>
12+
* Used with {@link org.junit.Rule} this will provide a pluggable alternative to separate methods annotated with {@link Before} and {@link After}
13+
*/
14+
public class AutoClient extends ExternalResource {
15+
private AutoEnvironment autoEnv;
16+
private Client client;
17+
18+
public AutoClient(AutoEnvironment autoEnv) {
19+
this.autoEnv = autoEnv;
20+
}
21+
22+
@Override
23+
protected void before() throws Throwable {
24+
client = autoEnv.getZatsEnvironment().newClient();
25+
}
26+
27+
@Override
28+
protected void after() {
29+
client.destroy();
30+
}
31+
32+
/**
33+
* convenience method to load a zul page directly (calls: {@link Client#connect(String)})
34+
*
35+
* @param zulPath
36+
* @return {@link DesktopAgent}
37+
*/
38+
public DesktopAgent connect(String zulPath) {
39+
return client.connect(zulPath);
40+
}
41+
42+
/**
43+
* allows access to the automatically created Zats {@link Client} instance. To access all functionality.
44+
*
45+
* @return the current Zats {@link Client}
46+
*/
47+
public Client getClient() {
48+
return client;
49+
}
50+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.zkoss.zats.junit;
2+
3+
import org.junit.*;
4+
import org.junit.rules.ExternalResource;
5+
import org.junit.rules.TestRule;
6+
import org.zkoss.zats.mimic.DefaultZatsEnvironment;
7+
import org.zkoss.zats.mimic.ZatsEnvironment;
8+
9+
/**
10+
* A {@link TestRule} implementing {@link ExternalResource} creating and destroying a {@link ZatsEnvironment}.</br>
11+
* Used with {@link org.junit.ClassRule} it provides a pluggable alternative to separate static methods annotated with {@link BeforeClass} and {@link AfterClass}</br>
12+
* </br>
13+
* 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>
14+
*/
15+
public class AutoEnvironment extends ExternalResource {
16+
private ZatsEnvironment zatsEnvironment;
17+
18+
private String resourceRoot;
19+
private String webInfPath;
20+
21+
/**
22+
* Creates a default {@link DefaultZatsEnvironment#DefaultZatsEnvironment()}
23+
*
24+
* @param resourceRoot - will be passed to {@link ZatsEnvironment#init(String)}
25+
*/
26+
public AutoEnvironment(String resourceRoot) {
27+
this.resourceRoot = resourceRoot;
28+
}
29+
30+
/**
31+
* Creates a {@link DefaultZatsEnvironment#DefaultZatsEnvironment()} with a specified WEB-INF folder
32+
*
33+
* @param webInfPath - will be used as the constructor argument in {@link DefaultZatsEnvironment#DefaultZatsEnvironment(String)}
34+
* @param resourceRoot - will be passed to {@link ZatsEnvironment#init(String)}
35+
*/
36+
public AutoEnvironment(String webInfPath, String resourceRoot) {
37+
this.webInfPath = webInfPath;
38+
this.resourceRoot = resourceRoot;
39+
}
40+
41+
@Override
42+
protected void before() throws Throwable {
43+
if (webInfPath != null) {
44+
zatsEnvironment = new DefaultZatsEnvironment(webInfPath);
45+
} else {
46+
zatsEnvironment = new DefaultZatsEnvironment();
47+
}
48+
zatsEnvironment.init(resourceRoot);
49+
}
50+
51+
@Override
52+
protected void after() {
53+
zatsEnvironment.destroy();
54+
}
55+
56+
/**
57+
* creates an {@link AutoClient} rule from the current {@link AutoEnvironment}
58+
*
59+
* @return an {@link AutoClient} rule
60+
*/
61+
public AutoClient autoClient() {
62+
return new AutoClient(this);
63+
}
64+
65+
public ZatsEnvironment getZatsEnvironment() {
66+
return zatsEnvironment;
67+
}
68+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.zkoss.zats.junit;
2+
3+
import org.junit.Assert;
4+
import org.junit.ClassRule;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.zkoss.zats.mimic.DesktopAgent;
8+
import org.zkoss.zul.Label;
9+
10+
11+
public class AutoEnvClientTest {
12+
@ClassRule
13+
public static AutoEnvironment autoEnv = new AutoEnvironment("src/test/resources/web/junit");
14+
15+
@Rule
16+
public AutoClient autoClient = autoEnv.autoClient();
17+
18+
@Test
19+
public void testAutoClient() {
20+
DesktopAgent desktopAgent = autoClient.connect("/test.zul");
21+
Label testLabel = desktopAgent.query("#testLabel").as(Label.class);
22+
Assert.assertEquals("initial label: ", "initial", testLabel.getValue());
23+
desktopAgent.query("#testButton").click();
24+
Assert.assertEquals("updated label: ", "updated", testLabel.getValue());
25+
}
26+
27+
@Test
28+
public void testLibraryProperty() {
29+
DesktopAgent desktopAgent = autoClient.connect("/testLibraryProperty.zul");
30+
Label testLabel = desktopAgent.query("#libProp").as(Label.class);
31+
Assert.assertEquals("libproperty ('zats.hello') should not exist in default config: ", "", testLabel.getValue());
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.zkoss.zats.junit;
2+
3+
import org.junit.Assert;
4+
import org.junit.ClassRule;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.zkoss.lang.Library;
8+
import org.zkoss.zats.mimic.DesktopAgent;
9+
import org.zkoss.zul.Label;
10+
11+
public class AutoEnvClientWebInfTest {
12+
@ClassRule
13+
public static AutoEnvironment autoEnv = new AutoEnvironment("src/test/resources/web/WEB-INF/custom", "src/test/resources/web/junit");
14+
15+
@Rule
16+
public AutoClient autoClient = autoEnv.autoClient();
17+
18+
@Test
19+
public void testAutoClient() {
20+
DesktopAgent desktopAgent = autoClient.connect("/testLibraryProperty.zul");
21+
Label testLabel = desktopAgent.query("#libProp").as(Label.class);
22+
Assert.assertEquals("libproperty ('zats.hello') from web/WEB-INF/custom/zk.xml: ", "hello zats", testLabel.getValue());
23+
Library.setProperty("zats.hello", null);
24+
}
25+
26+
}

zats-mimic/src/test/java/org/zkoss/zats/testcase/EnvironmentTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import junit.framework.Assert;
2929

3030
import org.junit.Test;
31+
import org.zkoss.lang.Library;
3132
import org.zkoss.zats.mimic.Client;
3233
import org.zkoss.zats.mimic.ComponentAgent;
3334
import org.zkoss.zats.mimic.DefaultZatsEnvironment;
@@ -107,6 +108,7 @@ public void testCustomConfig() {
107108
//custom config
108109
assertEquals("hello zats", desktop.query("#msg").as(Label.class).getValue());
109110
}finally{
111+
Library.setProperty("zats.hello", null);
110112
ctx.destroy();
111113
}
112114
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<zk>
2+
<button id="testButton" label="Test Button" onClick='testLabel.setValue("updated")'/>
3+
<label id="testLabel" value="initial"/>
4+
</zk>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
2+
<zk>
3+
<label id="libProp" value="${c:property('zats.hello')}"/>
4+
</zk>

0 commit comments

Comments
 (0)