Skip to content

Commit

Permalink
added junit test rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Wenzel committed Nov 28, 2017
1 parent 6c0fdf8 commit 6a385ee
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 2 deletions.
4 changes: 2 additions & 2 deletions zats-mimic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<zk.version>5.0.10</zk.version>
<jetty.version>9.4.6.v20170531</jetty.version>
<jetty.version>9.4.7.v20170914</jetty.version>
</properties>
<repositories>
<repository>
Expand All @@ -61,7 +61,7 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
Expand Down
50 changes: 50 additions & 0 deletions zats-mimic/src/main/java/org/zkoss/zats/junit/AutoClient.java
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 zats-mimic/src/main/java/org/zkoss/zats/junit/AutoEnvironment.java
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;
}
}
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());
}
}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down
4 changes: 4 additions & 0 deletions zats-mimic/src/test/resources/web/junit/test.zul
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>
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>

0 comments on commit 6a385ee

Please sign in to comment.