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.

BasicDriverTestCase.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 java.io.File;
  20. import javax.xml.transform.Result;
  21. import javax.xml.transform.Source;
  22. import javax.xml.transform.Transformer;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.sax.SAXResult;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.junit.Test;
  27. import static org.junit.Assert.assertTrue;
  28. import org.apache.commons.io.output.ByteArrayOutputStream;
  29. import org.apache.fop.apps.FOPException;
  30. import org.apache.fop.apps.FOUserAgent;
  31. import org.apache.fop.apps.Fop;
  32. import org.apache.fop.apps.FopFactory;
  33. import org.apache.fop.apps.MimeConstants;
  34. import org.apache.fop.cli.InputHandler;
  35. import static org.apache.fop.FOPTestUtils.getBaseDir;
  36. /**
  37. * Basic runtime test for the old Fop class. It is used to verify that
  38. * nothing obvious is broken after compiling.
  39. */
  40. public class BasicDriverTestCase {
  41. private FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  42. /**
  43. * Tests Fop with JAXP and OutputStream generating PDF.
  44. * @throws Exception if anything fails
  45. */
  46. @Test
  47. public void testFO2PDFWithJAXP() throws Exception {
  48. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  49. File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo");
  50. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  51. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, baout);
  52. TransformerFactory factory = TransformerFactory.newInstance();
  53. Transformer transformer = factory.newTransformer(); //Identity transf.
  54. Source src = new StreamSource(foFile);
  55. Result res = new SAXResult(fop.getDefaultHandler());
  56. transformer.transform(src, res);
  57. assertTrue("Generated PDF has zero length", baout.size() > 0);
  58. }
  59. /**
  60. * Tests Fop with JAXP and OutputStream generating PostScript.
  61. * @throws Exception if anything fails
  62. */
  63. @Test
  64. public void testFO2PSWithJAXP() throws Exception {
  65. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  66. File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo");
  67. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  68. Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, foUserAgent, baout);
  69. TransformerFactory factory = TransformerFactory.newInstance();
  70. Transformer transformer = factory.newTransformer(); //Identity transf.
  71. Source src = new StreamSource(foFile);
  72. Result res = new SAXResult(fop.getDefaultHandler());
  73. transformer.transform(src, res);
  74. assertTrue("Generated PostScript has zero length", baout.size() > 0);
  75. }
  76. /**
  77. * Tests Fop with JAXP and OutputStream generating RTF.
  78. * @throws Exception if anything fails
  79. */
  80. @Test
  81. public void testFO2RTFWithJAXP() throws Exception {
  82. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  83. File foFile = new File(getBaseDir(), "test/xml/bugtests/block.fo");
  84. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  85. Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, foUserAgent, baout);
  86. TransformerFactory factory = TransformerFactory.newInstance();
  87. Transformer transformer = factory.newTransformer(); //Identity transf.
  88. Source src = new StreamSource(foFile);
  89. Result res = new SAXResult(fop.getDefaultHandler());
  90. transformer.transform(src, res);
  91. assertTrue("Generated RTF has zero length", baout.size() > 0);
  92. }
  93. /**
  94. * Tests Fop with XsltInputHandler and OutputStream.
  95. * @throws Exception if anything fails
  96. */
  97. @Test
  98. public void testFO2PDFWithXSLTInputHandler() throws Exception {
  99. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  100. File xmlFile = new File(getBaseDir(), "test/xml/1.xml");
  101. File xsltFile = new File(getBaseDir(), "test/xsl/doc.xsl");
  102. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  103. InputHandler handler = new InputHandler(xmlFile, xsltFile, null);
  104. handler.renderTo(foUserAgent, MimeConstants.MIME_PDF, baout);
  105. assertTrue("Generated PDF has zero length", baout.size() > 0);
  106. }
  107. @Test
  108. public void testXSLILoop() throws Exception {
  109. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  110. File xmlFile = new File(getBaseDir(), "test/xml/1.xml");
  111. File xsltFile = new File(getBaseDir(), "test/xsl/iloop.xsl");
  112. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  113. InputHandler handler = new InputHandler(xmlFile, xsltFile, null);
  114. try {
  115. handler.renderTo(foUserAgent, MimeConstants.MIME_PDF, baout);
  116. } catch (FOPException e) { /* NOP */ }
  117. }
  118. }