ZATS Download"
(Created page with "In a ZK application, developers usually perform a download through the <javadoc>org.zkoss.zul.Filedownload</javadoc> when some events occurred.<ref>for more detail, please refer ...") |
|||
Line 1: | Line 1: | ||
− | + | '''Since 1.1.0''' | |
+ | ---- | ||
+ | Besides providing direct links for downloading files, we can also perform the file downloading from server side in the ZK web application. | ||
+ | We usually perform the file downloading through the <javadoc>org.zkoss.zul.Filedownload</javadoc> when some events occurred.<ref>for more detail, please refer to [[ZK_Component_Reference/Essential_Components/Filedownload]]</ref>. | ||
The following is a typical example about downloading a file: | The following is a typical example about downloading a file: | ||
Line 14: | Line 17: | ||
</source> | </source> | ||
− | Actually, the | + | Actually, the downloaded mechanism is a process with two steps. When you invoke '''save''' method, the '''Filedownload''' simply notifies ZK client engine of the downloaded URL. Then, the ZK client engine downloads such files according to the received URL. |
'''Notes''' | '''Notes''' | ||
<references/> | <references/> | ||
+ | == Download files in a ZATS Mimic test case == | ||
− | + | In order to simulate same behavior as ZK client engine doing, ZATS Mimic introduces the '''Resource''' interface. The '''Resource''' represents a downloadable resource file at server. We can get some information from a '''Resource''' object, or fetch the content of resource file in binary through the input stream. | |
− | + | <source lang="java" high="2,3"> | |
− | + | void doSomething(Resource resource) throws IOException { | |
+ | String name = resource.getName(); | ||
+ | InputStream inputStrem = resource.getInputStream(); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | Because of ZATS Mimic handle the reply from ZK application automatically, we can get the current downloadable resource file from <javadoc>org.zkoss.zats.mimic.DesktopAgent</javadoc>. The '''DesktopAgent''' might return '''null''' when getting the downloadable resource, it indicates that there is no downloadable resource after the previous operation. | ||
+ | |||
+ | <source lang="java" high="2,3,6"> | ||
+ | void checkDownloadableResource(DesktopAgent desktopAgent) throws IOException { | ||
+ | Resource resource = desktopAgent.getDownloadable(); | ||
+ | if (resource != null) { | ||
+ | System.out.println("We can download " + resource.getName()); | ||
+ | InputStream inputStream = resource.getInputStream(); | ||
+ | } else { | ||
+ | System.out.println("There is no downloadable resourece file currently"); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | As described above, the typical steps for testing downloading are: | ||
+ | # perform some operations through operation agents | ||
+ | # check is there a downloadable resource through desktop agent | ||
+ | # fetch and verify the information or content of the resource | ||
+ | |||
+ | <source lang="java" high="8,9,10,11,12,13"> | ||
+ | @Test | ||
+ | public void test() throws Exception { | ||
+ | Zats.init("."); | ||
+ | try { | ||
+ | DesktopAgent desktop = Zats.newClient().connect("/foo.zul"); | ||
+ | Assert.assertNull(desktop.getDownloadable()); | ||
+ | |||
+ | desktop.query("#btn").click(); | ||
+ | Resource resource = desktop.getDownloadable(); | ||
+ | Assert.assertNotNull(resource); | ||
+ | Assert.assertEquals("hello.txt", resource.getName()); | ||
+ | String content = fetchContent(resource.getInputStream()); | ||
+ | Assert.assertEquals("Hello world!", content); | ||
+ | } finally { | ||
+ | Zats.end(); | ||
+ | } | ||
+ | } | ||
+ | </source> |
Revision as of 04:29, 8 June 2012
Since 1.1.0
Besides providing direct links for downloading files, we can also perform the file downloading from server side in the ZK web application. We usually perform the file downloading through the Filedownload when some events occurred.[1].
The following is a typical example about downloading a file:
<zk>
<button id="btn" label="download">
<attribute name="onClick"><![CDATA[
org.zkoss.zul.Filedownload.save("foo.txt", "application/octet-stream");
]]>
</attribute>
</button>
</zk>
Actually, the downloaded mechanism is a process with two steps. When you invoke save method, the Filedownload simply notifies ZK client engine of the downloaded URL. Then, the ZK client engine downloads such files according to the received URL.
Notes
- ↑ for more detail, please refer to ZK_Component_Reference/Essential_Components/Filedownload
Download files in a ZATS Mimic test case
In order to simulate same behavior as ZK client engine doing, ZATS Mimic introduces the Resource interface. The Resource represents a downloadable resource file at server. We can get some information from a Resource object, or fetch the content of resource file in binary through the input stream.
void doSomething(Resource resource) throws IOException {
String name = resource.getName();
InputStream inputStrem = resource.getInputStream();
}
Because of ZATS Mimic handle the reply from ZK application automatically, we can get the current downloadable resource file from DesktopAgent. The DesktopAgent might return null when getting the downloadable resource, it indicates that there is no downloadable resource after the previous operation.
void checkDownloadableResource(DesktopAgent desktopAgent) throws IOException {
Resource resource = desktopAgent.getDownloadable();
if (resource != null) {
System.out.println("We can download " + resource.getName());
InputStream inputStream = resource.getInputStream();
} else {
System.out.println("There is no downloadable resourece file currently");
}
}
As described above, the typical steps for testing downloading are:
- perform some operations through operation agents
- check is there a downloadable resource through desktop agent
- fetch and verify the information or content of the resource
@Test
public void test() throws Exception {
Zats.init(".");
try {
DesktopAgent desktop = Zats.newClient().connect("/foo.zul");
Assert.assertNull(desktop.getDownloadable());
desktop.query("#btn").click();
Resource resource = desktop.getDownloadable();
Assert.assertNotNull(resource);
Assert.assertEquals("hello.txt", resource.getName());
String content = fetchContent(resource.getInputStream());
Assert.assertEquals("Hello world!", content);
} finally {
Zats.end();
}
}