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.

IFMimickingTestCase.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.intermediate;
  19. import java.io.File;
  20. import javax.xml.transform.ErrorListener;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerException;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.dom.DOMResult;
  25. import javax.xml.transform.sax.SAXResult;
  26. import javax.xml.transform.stream.StreamSource;
  27. import junit.framework.TestCase;
  28. import org.apache.fop.apps.FOPException;
  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.events.Event;
  34. import org.apache.fop.events.EventFormatter;
  35. import org.apache.fop.events.EventListener;
  36. import org.apache.fop.render.intermediate.IFContext;
  37. import org.apache.fop.render.intermediate.IFDocumentHandler;
  38. import org.apache.fop.render.intermediate.IFException;
  39. import org.apache.fop.render.intermediate.IFSerializer;
  40. /**
  41. * This test checks the correct mimicking of a different output format.
  42. */
  43. public class IFMimickingTestCase extends TestCase {
  44. private FopFactory fopFactory;
  45. /** {@inheritDoc} */
  46. protected void setUp() throws Exception {
  47. super.setUp();
  48. fopFactory = FopFactory.newInstance();
  49. File configFile = new File("test/test-no-xml-metrics.xconf");
  50. fopFactory.setUserConfig(configFile);
  51. }
  52. /**
  53. * Tests IF document handler mimicking with PDF output.
  54. * @throws Exception if an error occurs
  55. */
  56. public void testMimickingPDF() throws Exception {
  57. doTestMimicking(MimeConstants.MIME_PDF);
  58. }
  59. /**
  60. * Tests IF document handler mimicking with PostScript output.
  61. * @throws Exception if an error occurs
  62. */
  63. public void testMimickingPS() throws Exception {
  64. doTestMimicking(MimeConstants.MIME_POSTSCRIPT);
  65. }
  66. /**
  67. * Tests IF document handler mimicking with TIFF output.
  68. * @throws Exception if an error occurs
  69. */
  70. public void testMimickingTIFF() throws Exception {
  71. doTestMimicking(MimeConstants.MIME_TIFF);
  72. }
  73. private void doTestMimicking(String mime) throws FOPException, IFException,
  74. TransformerException {
  75. //Set up XMLRenderer to render to a DOM
  76. DOMResult domResult = new DOMResult();
  77. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  78. userAgent.getEventBroadcaster().addEventListener(new EventListener() {
  79. public void processEvent(Event event) {
  80. if (event.getEventGroupID().equals("org.apache.fop.fonts.FontEventAdapter")) {
  81. fail("There must be no font-related event! Got: "
  82. + EventFormatter.format(event));
  83. }
  84. }
  85. });
  86. //Create an instance of the target renderer so the XMLRenderer can use its font setup
  87. IFDocumentHandler targetHandler = userAgent.getRendererFactory().createDocumentHandler(
  88. userAgent, mime);
  89. //Setup painter
  90. IFSerializer serializer = new IFSerializer();
  91. serializer.setContext(new IFContext(userAgent));
  92. serializer.mimicDocumentHandler(targetHandler);
  93. serializer.setResult(domResult);
  94. userAgent.setDocumentHandlerOverride(serializer);
  95. Fop fop = fopFactory.newFop(userAgent);
  96. //minimal-pdf-a.fo uses the Gladiator font so is an ideal FO file for this test:
  97. StreamSource src = new StreamSource(new File("test/xml/pdf-a/minimal-pdf-a.fo"));
  98. TransformerFactory tFactory = TransformerFactory.newInstance();
  99. Transformer transformer = tFactory.newTransformer();
  100. setErrorListener(transformer);
  101. transformer.transform(src, new SAXResult(fop.getDefaultHandler()));
  102. }
  103. /**
  104. * Sets an error listener which doesn't swallow errors like Xalan's default one.
  105. * @param transformer the transformer to set the error listener on
  106. */
  107. protected void setErrorListener(Transformer transformer) {
  108. transformer.setErrorListener(new ErrorListener() {
  109. public void error(TransformerException exception) throws TransformerException {
  110. throw exception;
  111. }
  112. public void fatalError(TransformerException exception) throws TransformerException {
  113. throw exception;
  114. }
  115. public void warning(TransformerException exception) throws TransformerException {
  116. //ignore
  117. }
  118. });
  119. }
  120. }