You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericFOPTestCase.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 1999-2005 The Apache Software Foundation.
  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. /* $Id$ */
  17. package org.apache.fop;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.StringReader;
  20. import java.security.DigestOutputStream;
  21. import java.security.MessageDigest;
  22. import java.util.Date;
  23. import javax.xml.parsers.SAXParserFactory;
  24. import junit.framework.Test;
  25. import junit.framework.TestCase;
  26. import junit.framework.TestSuite;
  27. import org.apache.fop.apps.Fop;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.apps.MimeConstants;
  30. import org.apache.fop.util.DigestFilter;
  31. import org.xml.sax.InputSource;
  32. /**
  33. * Framework for simple regression testing.
  34. * The testcase reads a control XML file which specifies a FO source,
  35. * a MD5 for the source to help diferentiating failures caused by causal
  36. * source modification from failures caused by regression, a renderer (only
  37. * PDF currently supported) and a MD5 for the result.
  38. *
  39. */
  40. public final class GenericFOPTestCase extends TestCase {
  41. protected SAXParserFactory parserFactory;
  42. public static Test suite() {
  43. TestSuite suite = new TestSuite(GenericFOPTestCase.class);
  44. suite.setName("Fop regression tests");
  45. return suite;
  46. }
  47. /**
  48. * Constructor for FopTest.
  49. * @param name the name of the test suite
  50. */
  51. public GenericFOPTestCase(String name) {
  52. super(name);
  53. }
  54. /** @see junit.framework.TestCase#setUp() */
  55. protected void setUp() throws Exception {
  56. parserFactory = SAXParserFactory.newInstance();
  57. parserFactory.setNamespaceAware(true);
  58. }
  59. public void testSimple() throws Exception {
  60. final String digestIn = "17bf13298796065f7775db8707133aeb";
  61. final String digestOut = "e2761f51152f6663911e567901596707";
  62. final String fo =
  63. "<fo:root xmlns:fo='http://www.w3.org/1999/XSL/Format'>"
  64. + " <fo:layout-master-set>"
  65. + " <fo:simple-page-master master-name='simple'"
  66. + " page-height='25cm' page-width='20cm'>"
  67. + " <fo:region-body/>"
  68. + " </fo:simple-page-master>"
  69. + " </fo:layout-master-set>"
  70. + " <fo:page-sequence master-reference='simple'>"
  71. + " <fo:flow flow-name='xsl-region-body'>"
  72. + " <fo:block>This is a blind text.</fo:block>"
  73. + " </fo:flow>"
  74. + " </fo:page-sequence>"
  75. + "</fo:root>";
  76. renderPDF(fo, digestIn, digestOut);
  77. }
  78. private String digestToString(byte[] value) {
  79. StringBuffer buffer = new StringBuffer(2 * value.length);
  80. for (int i = 0; i < value.length; i++) {
  81. int val = value[i];
  82. int hi = (val >> 4) & 0xF;
  83. int lo = val & 0xF;
  84. if (hi < 10) {
  85. buffer.append((char) (hi + 0x30));
  86. } else {
  87. buffer.append((char) (hi + 0x61 - 10));
  88. }
  89. if (lo < 10) {
  90. buffer.append((char) (lo + 0x30));
  91. } else {
  92. buffer.append((char) (lo + 0x61 - 10));
  93. }
  94. }
  95. return buffer.toString();
  96. }
  97. private void renderPDF(String fo, String digestIn, String digestOut)
  98. throws Exception {
  99. FOUserAgent foUserAgent = new FOUserAgent();
  100. foUserAgent.setCreationDate(new Date(10000));
  101. MessageDigest outDigest = MessageDigest.getInstance("MD5");
  102. DigestOutputStream out =
  103. new DigestOutputStream(new ByteArrayOutputStream(), outDigest);
  104. Fop fop = new Fop(MimeConstants.MIME_PDF, foUserAgent);
  105. fop.setOutputStream(out);
  106. InputSource source = new InputSource(new StringReader(fo));
  107. DigestFilter filter = new DigestFilter("MD5");
  108. filter.setParent(parserFactory.newSAXParser().getXMLReader());
  109. filter.setContentHandler(fop.getDefaultHandler());
  110. filter.parse(source);
  111. String digestInActual = digestToString(filter.getDigestValue());
  112. if (!digestIn.equals(digestInActual)) {
  113. fail("input MD5: was " + digestInActual + ", expected " + digestIn);
  114. }
  115. String digestOutActual = digestToString(outDigest.digest());
  116. if (!digestOut.equals(digestOutActual)) {
  117. fail(
  118. "output MD5: was "
  119. + digestOutActual
  120. + ", expected "
  121. + digestOut);
  122. }
  123. }
  124. }