Revision 3535
Added by barteau about 17 years ago
test/edu/ucsb/nceas/metacat/clientview/ClientViewHelperTest.java | ||
---|---|---|
1 |
/* |
|
2 |
* ClientViewHelperTest.java |
|
3 |
* JUnit based test |
|
4 |
* |
|
5 |
* Created on October 25, 2007, 5:11 PM |
|
6 |
*/ |
|
7 |
|
|
8 |
package edu.ucsb.nceas.metacat.clientview; |
|
9 |
|
|
10 |
import edu.ucsb.nceas.utilities.Options; |
|
11 |
import java.io.ByteArrayOutputStream; |
|
12 |
import java.io.File; |
|
13 |
import junit.framework.*; |
|
14 |
import com.oreilly.servlet.multipart.MultipartParser; |
|
15 |
import edu.ucsb.nceas.metacat.client.MetacatClient; |
|
16 |
import java.io.InputStream; |
|
17 |
import java.util.HashMap; |
|
18 |
import java.util.Stack; |
|
19 |
import java.util.TreeMap; |
|
20 |
import javax.servlet.http.HttpServletRequest; |
|
21 |
import javax.servlet.http.HttpServletResponse; |
|
22 |
import javax.servlet.http.HttpSession; |
|
23 |
import javax.xml.xpath.XPath; |
|
24 |
import org.w3c.dom.Document; |
|
25 |
import org.w3c.dom.Node; |
|
26 |
|
|
27 |
/** |
|
28 |
* |
|
29 |
* @author barteau |
|
30 |
*/ |
|
31 |
public class ClientViewHelperTest extends TestCase { |
|
32 |
ClientViewHelper instance; |
|
33 |
ClientView clientViewBean; |
|
34 |
|
|
35 |
final static String PATH_TO_PROPERTIES = "projects/metacat/build/war/WEB-INF/metacat.properties"; |
|
36 |
final static String USR = ""; |
|
37 |
final static String ORG = "NCEAS"; |
|
38 |
final static String PWD = ""; |
|
39 |
final static String DOWNLOAD_PACKAGE_DOCID = ""; |
|
40 |
final static String DOWNLOAD_DATA_DOCID = ""; |
|
41 |
|
|
42 |
public ClientViewHelperTest(String testName) { |
|
43 |
super(testName); |
|
44 |
} |
|
45 |
|
|
46 |
protected void setUp() throws Exception { |
|
47 |
if (PWD.equals("")) |
|
48 |
fail("Please set the class property PWD before running this test."); |
|
49 |
if (USR.equals("")) |
|
50 |
fail("Please set the class property USR before running this test."); |
|
51 |
if (DOWNLOAD_PACKAGE_DOCID.equals("")) |
|
52 |
fail("Please set the class property DOWNLOAD_PACKAGE_DOCID before running this test."); |
|
53 |
if (DOWNLOAD_DATA_DOCID.equals("")) |
|
54 |
fail("Please set the class property DOWNLOAD_DATA_DOCID before running this test."); |
|
55 |
|
|
56 |
Options.initialize(new File(PATH_TO_PROPERTIES)); |
|
57 |
|
|
58 |
clientViewBean = new ClientView(); |
|
59 |
clientViewBean.setUsername(USR); |
|
60 |
clientViewBean.setOrganization(ORG); |
|
61 |
clientViewBean.setPassword(PWD); |
|
62 |
|
|
63 |
instance = new ClientViewHelper("localhost:8084", "/sanparks", clientViewBean); |
|
64 |
} |
|
65 |
|
|
66 |
protected void tearDown() throws Exception { |
|
67 |
clientViewBean = null; |
|
68 |
instance = null; |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* Test of handleClientRequest method, of class edu.ucsb.nceas.metacat.clientview.ClientViewHelper. |
|
73 |
*/ |
|
74 |
public void testHandleClientRequest() { |
|
75 |
HashMap expResult, result; |
|
76 |
String expResultTxt, returnResultTxt; |
|
77 |
|
|
78 |
System.out.println("handleClientRequest"); |
|
79 |
|
|
80 |
System.out.print("...setLoggedIn"); |
|
81 |
clientViewBean.setAction("Login"); |
|
82 |
result = instance.handleClientRequest(null); |
|
83 |
returnResultTxt = (String) result.get("message"); |
|
84 |
assertTrue("Login: Failed to login", returnResultTxt.contains("Authentication successful for user:")); |
|
85 |
System.out.println(" ...success!"); |
|
86 |
|
|
87 |
System.out.print("...download (data file)"); |
|
88 |
clientViewBean.setAction("Download"); |
|
89 |
clientViewBean.setDocId("DOWNLOAD_DATA_DOCID"); |
|
90 |
clientViewBean.setMetaFileDocId("DOWNLOAD_PACKAGE_DOCID"); |
|
91 |
|
|
92 |
result = instance.handleClientRequest(null); |
|
93 |
returnResultTxt = (String) result.get("contentType"); |
|
94 |
assertEquals("Download: Content Type is incorrect for a data file download", returnResultTxt, "application/octet-stream"); |
|
95 |
returnResultTxt = (String) result.get("Content-Disposition"); |
|
96 |
assertNotNull("Download: Content-Disposition is not set", returnResultTxt); |
|
97 |
assertNotNull("Download: OutputStream is not set", result.get("outputStream")); |
|
98 |
assertTrue("Download: Returned an empty file", ((ByteArrayOutputStream) result.get("outputStream")).size() > 0); |
|
99 |
System.out.println(" ...success!"); |
|
100 |
|
|
101 |
System.out.print("...download (package zip file)"); |
|
102 |
clientViewBean.setDocId("DOWNLOAD_PACKAGE_DOCID"); |
|
103 |
clientViewBean.setMetaFileDocId("DOWNLOAD_PACKAGE_DOCID"); |
|
104 |
|
|
105 |
result = instance.handleClientRequest(null); |
|
106 |
returnResultTxt = (String) result.get("contentType"); |
|
107 |
assertEquals("Download: Content Type is incorrect for a data file download", returnResultTxt, "application/zip"); |
|
108 |
returnResultTxt = (String) result.get("Content-Disposition"); |
|
109 |
assertNotNull("Download: Content-Disposition is not set", returnResultTxt); |
|
110 |
assertNotNull("Download: OutputStream is not set", result.get("outputStream")); |
|
111 |
assertTrue("Download: Returned an empty file", ((ByteArrayOutputStream) result.get("outputStream")).size() > 0); |
|
112 |
System.out.println(" ...success!"); |
|
113 |
|
|
114 |
System.out.print("...logout"); |
|
115 |
clientViewBean.setAction("Logout"); |
|
116 |
result = instance.handleClientRequest(null); |
|
117 |
assertNull("Logout: Session ID still exists", clientViewBean.getSessionid()); |
|
118 |
System.out.println(" ...success!"); |
|
119 |
|
|
120 |
} |
|
121 |
} |
|
0 | 122 |
Also available in: Unified diff
JUnit tests for ClientViewHelper. Includes tests for logging in, logging out, downloading a single data file, and downloading a package zip file.