KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.displaytag.jsptests;
2
3 import java.io.InputStream JavaDoc;
4
5 import org.displaytag.export.ExportViewFactory;
6 import org.displaytag.properties.MediaTypeEnum;
7 import org.displaytag.tags.TableTagParameters;
8 import org.displaytag.test.DisplaytagCase;
9 import org.displaytag.util.ParamEncoder;
10
11 import com.lowagie.text.pdf.PdfReader;
12 import com.meterware.httpunit.GetMethodWebRequest;
13 import com.meterware.httpunit.WebRequest;
14 import com.meterware.httpunit.WebResponse;
15
16
17 /**
18  * Tests for pdf export.
19  * @author Fabrizio Giustina
20  * @version $Revision: 707 $ ($Author: fgiust $)
21  */

22 public class ExportPdfTest extends DisplaytagCase
23 {
24
25     /**
26      * @see org.displaytag.test.DisplaytagCase#getJspName()
27      */

28     public String JavaDoc getJspName()
29     {
30         return "exportfull.jsp";
31     }
32
33     /**
34      * Test for content disposition and filename.
35      * @param jspName jsp name, with full path
36      * @throws Exception any axception thrown during test.
37      */

38     public void doTest(String JavaDoc jspName) throws Exception JavaDoc
39     {
40
41         ParamEncoder encoder = new ParamEncoder("table");
42         String JavaDoc mediaParameter = encoder.encodeParameterName(TableTagParameters.PARAMETER_EXPORTTYPE);
43         WebRequest request = new GetMethodWebRequest(jspName);
44
45         // this will force media type initialization
46
ExportViewFactory.getInstance();
47         MediaTypeEnum pdfMedia = MediaTypeEnum.fromName("pdf");
48         assertNotNull("Pdf export view not correctly registered.", pdfMedia);
49         request.setParameter(mediaParameter, Integer.toString(pdfMedia.getCode()));
50
51         WebResponse response = runner.getResponse(request);
52
53         // we are really testing an xml output?
54
assertEquals("Expected a different content type.", "application/pdf", response.getContentType());
55
56         InputStream JavaDoc stream = response.getInputStream();
57         byte[] result = new byte[3000];
58         stream.read(result);
59
60         PdfReader reader = new PdfReader(result);
61         assertEquals("Expected a valid pdf file with a single page", 1, reader.getNumberOfPages());
62
63     }
64
65 }
Popular Tags