ZATS Download"

From Documentation
(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 ...")
 
m (correct highlight (via JWB))
 
(15 intermediate revisions by one other user not shown)
Line 1: Line 1:
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 to [[ZK_Component_Reference/Essential_Components/Filedownload]]</ref>.
+
{{Template:UnderConstruction}}
 +
{{ZATSEssentialsPageHeader}}
 +
Since 1.1.0
  
The following is a typical example about downloading a file:
 
  
<source lang="java" high="4">
+
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:
 +
 
 +
'''download.zul'''
 +
<source lang="xml">
 
<zk>
 
<zk>
<button id="btn" label="download">
+
<div apply="DownloadComposer">
<attribute name="onClick"><![CDATA[
+
<button id="btn" label="download" />
org.zkoss.zul.Filedownload.save("foo.txt", "application/octet-stream");
+
</div>
]]>
 
</attribute>
 
</button>
 
 
</zk>
 
</zk>
 
</source>
 
</source>
  
Actually, the download mechanism is a two steps process. When you invoke '''save''' method, the <javadoc>org.zkoss.zul.Filedownload</javadoc> simply notifies ZK client engine of the download URL. Then, the client engine download such file according to the received URL.
+
'''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>
 +
 
 +
Actually, the downloaded mechanism is a process with two steps. When you invoke <code>save()</code>, the <code>Filedownload</code> 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 ==
  
== Download file in ZATS mimic test case ==
+
In order to simulate same behavior as ZK client engine doing, ZATS Mimic introduces the <code>Resource</code> 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
 +
 
 +
<source lang="java" start="10" highlight="14,15,16,17,18,19">
 +
@Test
 +
public void test() throws Exception {
 +
DesktopAgent desktop = Zats.newClient().connect("/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>
 +
* '''Line 14-16''': Because ZATS Mimic handles the response from ZK application automatically, we can get the current downloadable resource file from the <code>DesktopAgent.getDownloadable()</code>. The method might return <code>null</code> when getting the downloadable resource, it indicates that there is no downloadable resource after the previous operation.
 +
* '''Line 17-19''': We can get more information from <code>Resource</code>, or fetch the content of resource file in binary through the input stream.
  
In order to archive same behavior as ZK client engine, ZATS mimic introduces '''Resource'''.
+
{{ZATSEssentialsPageFooter}}
you can get the current downloadable resource from the method of desktop.
 

Latest revision as of 02:59, 20 January 2022

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

aowang



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:

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");
	}
}

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

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:

  1. perform some operations through operation agents
  2. check is there a downloadable resource through desktop agent
  3. fetch and verify the information or content of the resource
@Test
public void test() throws Exception {
	DesktopAgent desktop = Zats.newClient().connect("/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-16: 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.
  • Line 17-19: We can get more information from Resource, or fetch the content of resource file in binary through the input stream.



Last Update : 2022/01/20

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.