KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > document > PdfViewTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.web.servlet.view.document;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import com.lowagie.text.Document;
26 import com.lowagie.text.PageSize;
27 import com.lowagie.text.Paragraph;
28 import com.lowagie.text.pdf.PdfWriter;
29 import junit.framework.TestCase;
30
31 import org.springframework.mock.web.MockHttpServletRequest;
32 import org.springframework.mock.web.MockHttpServletResponse;
33
34 /**
35  * @author Alef Arendsen
36  * @author Juergen Hoeller
37  */

38 public class PdfViewTests extends TestCase {
39
40     public void testPdf() throws Exception JavaDoc {
41         final String JavaDoc text = "this should be in the PDF";
42
43         MockHttpServletRequest request = new MockHttpServletRequest();
44         MockHttpServletResponse response = new MockHttpServletResponse();
45
46         AbstractPdfView pdfView = new AbstractPdfView() {
47             protected void buildPdfDocument(Map JavaDoc model, Document document, PdfWriter writer,
48                     HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
49                 document.add(new Paragraph(text));
50             }
51         };
52
53         pdfView.render(new HashMap JavaDoc(), request, response);
54         byte[] pdfContent = response.getContentAsByteArray();
55
56         assertEquals("correct response content type", "application/pdf", response.getContentType());
57         assertEquals("correct response content length", pdfContent.length, response.getContentLength());
58
59         // rebuild iText document for comparison
60
Document document = new Document(PageSize.A4);
61         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
62         PdfWriter writer = PdfWriter.getInstance(document, baos);
63         writer.setViewerPreferences(PdfWriter.AllowPrinting | PdfWriter.PageLayoutSinglePage);
64         document.open();
65         document.add(new Paragraph(text));
66         document.close();
67         byte[] baosContent = baos.toByteArray();
68
69         assertEquals("correct size", pdfContent.length, baosContent.length);
70
71         int diffCount = 0;
72         for (int i = 0; i < pdfContent.length; i++) {
73             if (pdfContent[i] != baosContent[i]) {
74                 diffCount++;
75             }
76         }
77
78         assertTrue("difference only in encryption", diffCount < 70);
79     }
80
81 }
82
Popular Tags