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.

BaseURIResolutionTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.apps.io;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import javax.xml.transform.Result;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerException;
  27. import javax.xml.transform.sax.SAXResult;
  28. import javax.xml.transform.sax.SAXTransformerFactory;
  29. import javax.xml.transform.stream.StreamSource;
  30. import org.xml.sax.SAXException;
  31. import static org.junit.Assert.assertTrue;
  32. import org.apache.commons.io.IOUtils;
  33. import org.apache.commons.io.output.ByteArrayOutputStream;
  34. import org.apache.xmlgraphics.io.ResourceResolver;
  35. import org.apache.fop.apps.FOPException;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.apps.Fop;
  38. import org.apache.fop.apps.FopConfParser;
  39. import org.apache.fop.apps.FopFactory;
  40. import org.apache.fop.apps.FopFactoryBuilder;
  41. import org.apache.fop.apps.MimeConstants;
  42. import static org.apache.fop.FOPTestUtils.getBaseDir;
  43. public abstract class BaseURIResolutionTest {
  44. private final FopFactory fopFactory;
  45. private SAXTransformerFactory tfactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
  46. private static final File BACKUP_DIR = new File(getBaseDir(), "build/test-results");
  47. public BaseURIResolutionTest(FopFactoryBuilder builder, File foFile) throws FOPException,
  48. TransformerException, IOException {
  49. fopFactory = builder.build();
  50. createDocument(foFile);
  51. }
  52. public BaseURIResolutionTest(InputStream confStream, ResourceResolver resolver, File foFile)
  53. throws FOPException, TransformerException, SAXException, IOException {
  54. this(new FopConfParser(confStream, getBaseDir().toURI(), resolver).getFopFactoryBuilder(),
  55. foFile);
  56. }
  57. private void createDocument(File foFile) throws TransformerException, FOPException,
  58. IOException {
  59. FOUserAgent ua = fopFactory.newFOUserAgent();
  60. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  61. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout);
  62. Transformer transformer = tfactory.newTransformer(); //Identity transf.
  63. Source src = new StreamSource(foFile);
  64. Result res = new SAXResult(fop.getDefaultHandler());
  65. transformer.transform(src, res);
  66. OutputStream out = new java.io.FileOutputStream(
  67. new File(BACKUP_DIR, foFile.getName() + ".pdf"));
  68. try {
  69. baout.writeTo(out);
  70. } finally {
  71. IOUtils.closeQuietly(out);
  72. }
  73. //Test using PDF as the area tree doesn't invoke Batik so we could check
  74. //if the resolver is actually passed to Batik by FOP
  75. assertTrue("Generated PDF has zero length", baout.size() > 0);
  76. }
  77. public abstract void testAssertions();
  78. static File getFODirectory() {
  79. return new File(getBaseDir(), "test/xml/uri-testing/");
  80. }
  81. }