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.

EventProcessingTestCase.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.events;
  19. import java.io.File;
  20. import java.io.InputStream;
  21. import java.net.URI;
  22. import java.util.Collections;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import javax.xml.transform.Result;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Transformer;
  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.apache.commons.io.output.NullOutputStream;
  33. import org.apache.xmlgraphics.util.MimeConstants;
  34. import org.apache.fop.ResourceEventProducer;
  35. import org.apache.fop.apps.FOUserAgent;
  36. import org.apache.fop.apps.Fop;
  37. import org.apache.fop.apps.FopFactory;
  38. import org.apache.fop.area.AreaEventProducer;
  39. import org.apache.fop.fo.FOValidationEventProducer;
  40. import org.apache.fop.fo.flow.table.TableEventProducer;
  41. import org.apache.fop.layoutmgr.BlockLevelEventProducer;
  42. import org.apache.fop.layoutmgr.inline.InlineLevelEventProducer;
  43. /**
  44. * Tests that the event notification system runs smoothly.
  45. */
  46. public class EventProcessingTestCase {
  47. private final TransformerFactory tFactory = TransformerFactory.newInstance();
  48. private static final URI BASE_DIR;
  49. public static final URI CONFIG_BASE_DIR;
  50. static {
  51. URI base = (new File(".")).toURI();
  52. BASE_DIR = base.resolve("test/events/");
  53. /** The base directory of configuration files */
  54. CONFIG_BASE_DIR = base.resolve("test/config/");
  55. }
  56. public void doTest(InputStream inStream, URI fopConf, String expectedEventID, String mimeType,
  57. Map<String, Object> expectedParams) throws Exception {
  58. EventChecker eventChecker = new EventChecker(expectedEventID, expectedParams);
  59. FopFactory fopFactory;
  60. if (fopConf != null) {
  61. fopFactory = FopFactory.newInstance(new File(fopConf));
  62. } else {
  63. fopFactory = FopFactory.newInstance(BASE_DIR);
  64. }
  65. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  66. userAgent.getEventBroadcaster().addEventListener(eventChecker);
  67. Fop fop = fopFactory.newFop(mimeType, userAgent, new NullOutputStream());
  68. Transformer transformer = tFactory.newTransformer();
  69. Source src = new StreamSource(inStream);
  70. Result res = new SAXResult(fop.getDefaultHandler());
  71. transformer.transform(src, res);
  72. eventChecker.end();
  73. }
  74. public void doTest(InputStream inStream, URI fopConf, String expectedEventID, String mimeType)
  75. throws Exception {
  76. Map<String, Object> noParams = Collections.emptyMap();
  77. doTest(inStream, fopConf, expectedEventID, mimeType, noParams);
  78. }
  79. public void doTest(String filename, String expectedEventID, Map<String, Object> expectedParams)
  80. throws Exception {
  81. doTest(BASE_DIR.resolve(filename).toURL().openStream(), null, expectedEventID,
  82. MimeConstants.MIME_PDF, expectedParams);
  83. }
  84. public void doTest(String filename, String expectedEventID) throws Exception {
  85. doTest(BASE_DIR.resolve(filename).toURL().openStream(), null, expectedEventID,
  86. MimeConstants.MIME_PDF);
  87. }
  88. @Test
  89. public void testArea() throws Exception {
  90. doTest("area.fo",
  91. AreaEventProducer.class.getName() + ".unresolvedIDReferenceOnPage");
  92. }
  93. @Test
  94. public void testResource() throws Exception {
  95. doTest("resource.fo",
  96. ResourceEventProducer.class.getName() + ".imageNotFound");
  97. }
  98. @Test
  99. public void testValidation() throws Exception {
  100. doTest("validation.fo",
  101. FOValidationEventProducer.class.getName() + ".invalidPropertyValue");
  102. }
  103. @Test
  104. public void testTable() throws Exception {
  105. doTest("table.fo",
  106. TableEventProducer.class.getName() + ".noTablePaddingWithCollapsingBorderModel");
  107. }
  108. @Test
  109. public void testBlockLevel() throws Exception {
  110. doTest("block-level.fo",
  111. BlockLevelEventProducer.class.getName() + ".overconstrainedAdjustEndIndent");
  112. }
  113. @Test
  114. public void testInlineLevel() throws Exception {
  115. doTest("inline-level.fo",
  116. InlineLevelEventProducer.class.getName() + ".lineOverflows");
  117. }
  118. @Test
  119. public void testViewportIPDOverflow() throws Exception {
  120. doTest("viewport-overflow.fo", BlockLevelEventProducer.class.getName() + ".viewportIPDOverflow");
  121. }
  122. @Test
  123. public void testViewportBPDOverflow() throws Exception {
  124. doTest("viewport-overflow.fo", BlockLevelEventProducer.class.getName() + ".viewportBPDOverflow");
  125. }
  126. @Test
  127. public void testPageOverflow() throws Exception {
  128. Map<String, Object> params = new HashMap<String, Object>();
  129. params.put("page", "1");
  130. doTest("region-body_overflow.fo", BlockLevelEventProducer.class.getName() + ".regionOverflow",
  131. params);
  132. }
  133. @Test
  134. public void testHyphenationNotFound() throws Exception {
  135. Map<String, Object> noParams = Collections.emptyMap();
  136. doTest(BASE_DIR.resolve("hyphenation.fo").toURL().openStream(),
  137. new File("test/events/hyphenationfop.xconf").toURI(),
  138. ResourceEventProducer.class.getName() + ".hyphenationNotFound", MimeConstants.MIME_PDF, noParams);
  139. }
  140. }