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.

ArtifactImageBugTestCase.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.accessibility.fo;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.net.URL;
  23. import java.util.MissingResourceException;
  24. import javax.xml.transform.Result;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerException;
  28. import javax.xml.transform.TransformerFactory;
  29. import javax.xml.transform.sax.SAXResult;
  30. import javax.xml.transform.stream.StreamSource;
  31. import org.junit.Test;
  32. import org.xml.sax.SAXException;
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.apps.Fop;
  35. import org.apache.fop.apps.FopFactory;
  36. import org.apache.fop.apps.MimeConstants;
  37. import org.apache.fop.render.intermediate.IFContext;
  38. import org.apache.fop.render.intermediate.IFDocumentHandler;
  39. import org.apache.fop.render.intermediate.IFSerializer;
  40. /**
  41. * Test for reproducing a NullPointerException occurring with activated PDF/UA and when an image
  42. * is marked as an artifact.
  43. * @see FOP-2646
  44. */
  45. public class ArtifactImageBugTestCase {
  46. @Test
  47. public void testMarkerStateTrackingBug() throws Exception {
  48. ByteArrayOutputStream out = new ByteArrayOutputStream();
  49. formatFO(needResource("artifact-image-npe.fo"), out, MimeConstants.MIME_PDF);
  50. //checkPDF(out);
  51. }
  52. private void formatFO(URL foFile, ByteArrayOutputStream out, String mimeFopIf)
  53. throws IOException, SAXException, TransformerException {
  54. FopFactory fopFactory = getFopFactory();
  55. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  56. if (mimeFopIf.equals(MimeConstants.MIME_FOP_IF)) {
  57. IFSerializer serializer = new IFSerializer(new IFContext(userAgent));
  58. IFDocumentHandler targetHandler
  59. = userAgent.getRendererFactory().createDocumentHandler(userAgent, MimeConstants.MIME_PDF);
  60. serializer.mimicDocumentHandler(targetHandler);
  61. userAgent.setDocumentHandlerOverride(serializer);
  62. }
  63. Fop fop = fopFactory.newFop(mimeFopIf, userAgent, out);
  64. Transformer transformer = TransformerFactory.newInstance().newTransformer();
  65. Source src = new StreamSource(foFile.openStream());
  66. Result res = new SAXResult(fop.getDefaultHandler());
  67. transformer.transform(src, res);
  68. }
  69. private FopFactory getFopFactory() throws IOException, SAXException {
  70. return FopFactory.newInstance(new File(".").toURI(),
  71. needResource("/org/apache/fop/pdf/PDFUA.xconf").openStream());
  72. }
  73. private URL needResource(String resourceName) {
  74. return needResource(getClass(), resourceName);
  75. }
  76. private URL needResource(Class contextClass, String resourceName) {
  77. URL url = contextClass.getResource(resourceName);
  78. if (url == null) {
  79. throw new MissingResourceException("Resource not found: " + resourceName,
  80. contextClass.getName(), resourceName);
  81. }
  82. return url;
  83. }
  84. }