Download"
From Documentation
(Created page with "{{Template:UnderConstruction}} {{ZATSEssentialsPageHeader}} Since 1.1.0 We usually perform the file downloading through the <javadoc>org.zkoss.zul.Filedownload</javadoc> when ...") |
m (correct highlight (via JWB)) |
||
(15 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
{{ZATSEssentialsPageHeader}} | {{ZATSEssentialsPageHeader}} | ||
Since 1.1.0 | Since 1.1.0 | ||
− | We usually | + | We usually implement file downloading through <javadoc>org.zkoss.zul.Filedownload</javadoc> when some events are triggered <ref>For more details, please refer to [[ZK_Component_Reference/Essential_Components/Filedownload]]</ref>. Following is a simple implementation of downloading a file: |
− | <source lang=" | + | '''download.zul''' |
+ | <source lang="xml"> | ||
<zk> | <zk> | ||
− | < | + | <div apply="DownloadComposer"> |
− | < | + | <button id="btn" label="download" /> |
− | + | </div> | |
− | |||
− | |||
− | </ | ||
</zk> | </zk> | ||
</source> | </source> | ||
− | + | '''DownloadComposer.java''' | |
+ | <source lang="java" start="10" highlight="13"> | ||
+ | public class DownloadComposer extends GenericForwardComposer { | ||
+ | @Listen("onClick=#btn") | ||
+ | public void download() throws IOException { | ||
+ | Filedownload.save("/hello.txt", "application/octet-stream"); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | The download mechanism is a process involving two steps. When you invoke <code>save()</code>, the <code>Filedownload</code> simply notifies ZK client engine of the download URL. Then, ZK client engine downloads such file according to the referred URL. | ||
'''Notes''' | '''Notes''' | ||
<references/> | <references/> | ||
− | |||
− | In order to simulate | + | = Download Files in a Test Case = |
− | # perform some operations | + | |
− | # check | + | In order to simulate the behavior of the ZK client engine, ZATS Mimic introduces the <code>Resource</code> interface which represents a downloadable resource file saved on the server. General steps for testing a download function are as follows: |
+ | |||
+ | # perform some operations to trigger the download function, e.g. click a button | ||
+ | # check the presence of a downloadable resource through a desktop agent | ||
# fetch and verify the information or content of the resource | # fetch and verify the information or content of the resource | ||
− | <source lang="java" | + | <source lang="java" start="10" highlight="14,15,16,17,18,19"> |
@Test | @Test | ||
public void test() throws Exception { | public void test() throws Exception { | ||
− | + | DesktopAgent desktop = Zats.newClient().connect("/essentials/download.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); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
</source> | </source> | ||
+ | * '''Line 14''': Click the button to trigger downloading. | ||
+ | * '''Line 15-16''': Since ZATS Mimic handles the response from ZK application automatically, we can retrieve current downloadable resource files from <code>DesktopAgent.getDownloadable()</code>. If the method returns a <code>null</code> when attempting to retrieve downloadable resources, it indicates that there are no downloadable resources after the previous operation. | ||
+ | * '''Line 17-19''': We can get more information from <code>Resource</code>, or fetch the content of resource files in binary through the input stream to verify the result. | ||
− | |||
− | |||
+ | {{ZATSEssentialsPageHeader}} | ||
{{ZATSEssentialsPageFooter}} | {{ZATSEssentialsPageFooter}} |
Latest revision as of 02:55, 18 January 2022
Since 1.1.0
We usually implement file downloading through Filedownload when some events are triggered [1]. Following is a simple implementation of downloading a file:
download.zul
<zk>
<div apply="DownloadComposer">
<button id="btn" label="download" />
</div>
</zk>
DownloadComposer.java
public class DownloadComposer extends GenericForwardComposer {
@Listen("onClick=#btn")
public void download() throws IOException {
Filedownload.save("/hello.txt", "application/octet-stream");
}
}
The download mechanism is a process involving two steps. When you invoke save()
, the Filedownload
simply notifies ZK client engine of the download URL. Then, ZK client engine downloads such file according to the referred URL.
Notes
- ↑ For more details, please refer to ZK_Component_Reference/Essential_Components/Filedownload
Download Files in a Test Case
In order to simulate the behavior of the ZK client engine, ZATS Mimic introduces the Resource
interface which represents a downloadable resource file saved on the server. General steps for testing a download function are as follows:
- perform some operations to trigger the download function, e.g. click a button
- check the presence of a downloadable resource through a desktop agent
- fetch and verify the information or content of the resource
@Test
public void test() throws Exception {
DesktopAgent desktop = Zats.newClient().connect("/essentials/download.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);
}
- Line 14: Click the button to trigger downloading.
- Line 15-16: Since ZATS Mimic handles the response from ZK application automatically, we can retrieve current downloadable resource files from
DesktopAgent.getDownloadable()
. If the method returns anull
when attempting to retrieve downloadable resources, it indicates that there are no downloadable resources after the previous operation. - Line 17-19: We can get more information from
Resource
, or fetch the content of resource files in binary through the input stream to verify the result.