1 17 18 19 20 package org.apache.fop; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.StringReader ; 24 import java.security.DigestOutputStream ; 25 import java.security.MessageDigest ; 26 import java.util.Date ; 27 28 import javax.xml.parsers.SAXParserFactory ; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 import org.apache.fop.apps.Fop; 35 import org.apache.fop.apps.FOUserAgent; 36 import org.apache.fop.apps.FopFactory; 37 import org.apache.fop.apps.MimeConstants; 38 import org.apache.fop.util.DigestFilter; 39 import org.xml.sax.InputSource ; 40 41 49 public final class GenericFOPTestCase extends TestCase { 50 51 private FopFactory fopFactory = FopFactory.newInstance(); 53 54 protected SAXParserFactory parserFactory; 55 56 public static Test suite() { 57 TestSuite suite = new TestSuite(GenericFOPTestCase.class); 58 suite.setName("Fop regression tests"); 59 return suite; 60 } 61 62 66 public GenericFOPTestCase(String name) { 67 super(name); 68 } 69 70 71 protected void setUp() throws Exception { 72 parserFactory = SAXParserFactory.newInstance(); 73 parserFactory.setNamespaceAware(true); 74 } 75 76 public void testSimple() throws Exception { 77 final String digestIn = "17bf13298796065f7775db8707133aeb"; 78 final String digestOut = "e2761f51152f6663911e567901596707"; 79 final String fo = 80 "<fo:root xmlns:fo='http://www.w3.org/1999/XSL/Format'>" 81 + " <fo:layout-master-set>" 82 + " <fo:simple-page-master master-name='simple'" 83 + " page-height='25cm' page-width='20cm'>" 84 + " <fo:region-body/>" 85 + " </fo:simple-page-master>" 86 + " </fo:layout-master-set>" 87 + " <fo:page-sequence master-reference='simple'>" 88 + " <fo:flow flow-name='xsl-region-body'>" 89 + " <fo:block>This is a blind text.</fo:block>" 90 + " </fo:flow>" 91 + " </fo:page-sequence>" 92 + "</fo:root>"; 93 renderPDF(fo, digestIn, digestOut); 94 } 95 96 private String digestToString(byte[] value) { 97 StringBuffer buffer = new StringBuffer (2 * value.length); 98 for (int i = 0; i < value.length; i++) { 99 int val = value[i]; 100 int hi = (val >> 4) & 0xF; 101 int lo = val & 0xF; 102 if (hi < 10) { 103 buffer.append((char) (hi + 0x30)); 104 } else { 105 buffer.append((char) (hi + 0x61 - 10)); 106 } 107 if (lo < 10) { 108 buffer.append((char) (lo + 0x30)); 109 } else { 110 buffer.append((char) (lo + 0x61 - 10)); 111 } 112 } 113 return buffer.toString(); 114 } 115 116 private void renderPDF(String fo, String digestIn, String digestOut) 117 throws Exception { 118 FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 119 foUserAgent.setCreationDate(new Date (10000)); 120 MessageDigest outDigest = MessageDigest.getInstance("MD5"); 121 DigestOutputStream out = 122 new DigestOutputStream (new ByteArrayOutputStream (), outDigest); 123 Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); 124 InputSource source = new InputSource (new StringReader (fo)); 125 DigestFilter filter = new DigestFilter("MD5"); 126 filter.setParent(parserFactory.newSAXParser().getXMLReader()); 127 filter.setContentHandler(fop.getDefaultHandler()); 128 filter.parse(source); 129 String digestInActual = digestToString(filter.getDigestValue()); 130 if (!digestIn.equals(digestInActual)) { 131 fail("input MD5: was " + digestInActual + ", expected " + digestIn); 132 } 133 String digestOutActual = digestToString(outDigest.digest()); 134 if (!digestOut.equals(digestOutActual)) { 135 fail( 136 "output MD5: was " 137 + digestOutActual 138 + ", expected " 139 + digestOut); 140 } 141 } 142 143 } 144 | Popular Tags |