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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop;
  19. import static org.junit.Assert.fail;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.StringReader;
  22. import java.security.DigestOutputStream;
  23. import java.security.MessageDigest;
  24. import java.util.Date;
  25. import javax.xml.parsers.SAXParserFactory;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import org.xml.sax.InputSource;
  29. import org.apache.fop.apps.FOUserAgent;
  30. import org.apache.fop.apps.Fop;
  31. import org.apache.fop.apps.FopFactory;
  32. import org.apache.fop.apps.MimeConstants;
  33. import org.apache.fop.util.DigestFilter;
  34. /**
  35. * Framework for simple regression testing.
  36. * The testcase reads a control XML file which specifies a FO source,
  37. * a MD5 for the source to help diferentiating failures caused by causal
  38. * source modification from failures caused by regression, a renderer (only
  39. * PDF currently supported) and a MD5 for the result.
  40. *
  41. */
  42. public final class GenericFOPTestCase {
  43. // configure fopFactory as desired
  44. private FopFactory fopFactory = FopFactory.newInstance();
  45. private SAXParserFactory parserFactory;
  46. @Before
  47. public void setUp() throws Exception {
  48. parserFactory = SAXParserFactory.newInstance();
  49. parserFactory.setNamespaceAware(true);
  50. }
  51. @org.junit.Test
  52. public void testSimple() throws Exception {
  53. final String digestIn = "17bf13298796065f7775db8707133aeb";
  54. final String digestOut = "e2761f51152f6663911e567901596707";
  55. final String fo
  56. = "<fo:root xmlns:fo='http://www.w3.org/1999/XSL/Format'>"
  57. + " <fo:layout-master-set>"
  58. + " <fo:simple-page-master master-name='simple'"
  59. + " page-height='25cm' page-width='20cm'>"
  60. + " <fo:region-body/>"
  61. + " </fo:simple-page-master>"
  62. + " </fo:layout-master-set>"
  63. + " <fo:page-sequence master-reference='simple'>"
  64. + " <fo:flow flow-name='xsl-region-body'>"
  65. + " <fo:block>This is a blind text.</fo:block>"
  66. + " </fo:flow>"
  67. + " </fo:page-sequence>"
  68. + "</fo:root>";
  69. renderPDF(fo, digestIn, digestOut);
  70. }
  71. private String digestToString(byte[] value) {
  72. StringBuffer buffer = new StringBuffer(2 * value.length);
  73. for (int i = 0; i < value.length; i++) {
  74. int val = value[i];
  75. int hi = (val >> 4) & 0xF;
  76. int lo = val & 0xF;
  77. if (hi < 10) {
  78. buffer.append((char) (hi + 0x30));
  79. } else {
  80. buffer.append((char) (hi + 0x61 - 10));
  81. }
  82. if (lo < 10) {
  83. buffer.append((char) (lo + 0x30));
  84. } else {
  85. buffer.append((char) (lo + 0x61 - 10));
  86. }
  87. }
  88. return buffer.toString();
  89. }
  90. private void renderPDF(String fo, String digestIn, String digestOut)
  91. throws Exception {
  92. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  93. foUserAgent.setCreationDate(new Date(10000));
  94. MessageDigest outDigest = MessageDigest.getInstance("MD5");
  95. DigestOutputStream out = new DigestOutputStream(new ByteArrayOutputStream(), outDigest);
  96. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  97. InputSource source = new InputSource(new StringReader(fo));
  98. DigestFilter filter = new DigestFilter("MD5");
  99. filter.setParent(parserFactory.newSAXParser().getXMLReader());
  100. filter.setContentHandler(fop.getDefaultHandler());
  101. filter.parse(source);
  102. String digestInActual = digestToString(filter.getDigestValue());
  103. if (!digestIn.equals(digestInActual)) {
  104. fail("input MD5: was " + digestInActual + ", expected " + digestIn);
  105. }
  106. String digestOutActual = digestToString(outDigest.digest());
  107. if (!digestOut.equals(digestOutActual)) {
  108. fail(
  109. "output MD5: was "
  110. + digestOutActual
  111. + ", expected "
  112. + digestOut);
  113. }
  114. }
  115. }