ZATS Download"
Line 15: | Line 15: | ||
</source> | </source> | ||
− | Actually, the downloaded mechanism is a process with two steps. When you invoke | + | Actually, the downloaded mechanism is a process with two steps. When you invoke <tt>save()</tt>, the <tt>Filedownload</tt> simply notifies ZK client engine of the downloaded URL. Then, the ZK client engine downloads such files according to the received URL. |
'''Notes''' | '''Notes''' | ||
Line 22: | Line 22: | ||
== Download files in a ZATS Mimic test case == | == Download files in a ZATS Mimic test case == | ||
− | In order to simulate same behavior as ZK client engine doing, ZATS Mimic introduces the | + | In order to simulate same behavior as ZK client engine doing, ZATS Mimic introduces the <tt>Resource</tt> interface. It represents a downloadable resource file at server. The typical steps for testing downloading are: |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
# perform some operations through operation agents | # perform some operations through operation agents | ||
# check is there a downloadable resource through desktop agent | # check is there a downloadable resource through desktop agent | ||
Line 67: | Line 44: | ||
Zats.end(); | Zats.end(); | ||
} | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | === More about downloadable resources === | ||
+ | |||
+ | Because ZATS Mimic handles the response from ZK application automatically, we can get the current downloadable resource file from the <tt>DesktopAgent.getDownloadable()</tt>. The method might return <tt>null</tt> when getting the downloadable resource, it indicates that there is no downloadable resource after the previous operation. | ||
+ | |||
+ | <source lang="java" high="2,3,5"> | ||
+ | void checkDownloadableResource(DesktopAgent desktopAgent) throws IOException { | ||
+ | Resource resource = desktopAgent.getDownloadable(); | ||
+ | if (resource != null) { | ||
+ | // There is a downloadable resource file. | ||
+ | } else { | ||
+ | // There is no downloadable resourece file currently. | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | Then, we can get more information from <tt>Resource</tt>, or just 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> | </source> |
Revision as of 06:34, 8 June 2012
Since 1.1.0
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(), 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. It represents a downloadable resource file at server. 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();
}
}
More about downloadable resources
Because ZATS Mimic handles the response from ZK application automatically, we can get the current downloadable resource file from the DesktopAgent.getDownloadable(). The method 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) {
// There is a downloadable resource file.
} else {
// There is no downloadable resourece file currently.
}
}
Then, we can get more information from Resource, or just 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();
}