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 5.1KB

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