KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > jsptests > Displ107Test


1 package org.displaytag.jsptests;
2
3 import java.io.InputStream JavaDoc;
4
5 import org.displaytag.properties.MediaTypeEnum;
6 import org.displaytag.tags.TableTagParameters;
7 import org.displaytag.test.DisplaytagCase;
8 import org.displaytag.util.ParamEncoder;
9
10 import com.meterware.httpunit.GetMethodWebRequest;
11 import com.meterware.httpunit.WebRequest;
12 import com.meterware.httpunit.WebResponse;
13
14
15 /**
16  * Tests for DISPL-107: Excel and Text exports use Windows Latin-1 encoding.
17  * @author Fabrizio Giustina
18  * @version $Revision: 1006 $ ($Author: fgiust $)
19  */

20 public class Displ107Test extends DisplaytagCase
21 {
22
23     /**
24      * @see org.displaytag.test.DisplaytagCase#getJspName()
25      */

26     public String JavaDoc getJspName()
27     {
28         return "DISPL-107.jsp";
29     }
30
31     /**
32      * Encoding should be utf8.
33      * @param jspName jsp name, with full path
34      * @throws Exception any axception thrown during test.
35      */

36     public void doTest(String JavaDoc jspName) throws Exception JavaDoc
37     {
38         WebRequest request = new GetMethodWebRequest(jspName);
39
40         WebResponse response = runner.getResponse(request);
41         assertEquals("Wrong encoding", "UTF8", response.getCharacterSet());
42
43         ParamEncoder encoder = new ParamEncoder("table");
44         String JavaDoc mediaParameter = encoder.encodeParameterName(TableTagParameters.PARAMETER_EXPORTTYPE);
45         request = new GetMethodWebRequest(jspName);
46         request.setParameter(mediaParameter, Integer.toString(MediaTypeEnum.CSV.getCode()));
47
48         response = runner.getResponse(request);
49         checkContent(response);
50
51         // enabled filter
52
request.setParameter(TableTagParameters.PARAMETER_EXPORTING, "1");
53         response = runner.getResponse(request);
54         checkContent(response);
55
56     }
57
58     /**
59      * Actually check exported bytes
60      * @param response WebResponse
61      * @throws Exception any axception thrown during test.
62      */

63     private void checkContent(WebResponse response) throws Exception JavaDoc
64     {
65         // we are really testing an xml output?
66
assertEquals("Expected a different content type.", "text/csv", response.getContentType());
67         assertEquals("Wrong encoding", "UTF8", response.getCharacterSet());
68
69         InputStream JavaDoc stream = response.getInputStream();
70         byte[] result = new byte[11];
71         stream.read(result);
72
73         byte[] expected = "ant,àèì\n".getBytes("utf-8");
74         if (log.isDebugEnabled())
75         {
76             log.debug("expected: [" + new String JavaDoc(expected, "utf-8") + "]");
77             log.debug("result: [" + new String JavaDoc(result, "utf-8") + "]");
78         }
79         assertEquals("Wrong length", expected.length, result.length);
80
81         for (int j = 0; j < result.length; j++)
82         {
83             assertEquals(
84                 "Wrong byte at position " + j + ", output=" + new String JavaDoc(result, "utf-8"),
85                 expected[j],
86                 result[j]);
87
88         }
89     }
90
91 }
Popular Tags