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.

FOTreeTestCase.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.fotreetest;
  19. import java.io.File;
  20. import java.util.Collection;
  21. import java.util.List;
  22. import javax.xml.parsers.SAXParser;
  23. import javax.xml.parsers.SAXParserFactory;
  24. import org.junit.BeforeClass;
  25. import org.junit.Test;
  26. import org.junit.runner.RunWith;
  27. import org.junit.runners.Parameterized;
  28. import org.junit.runners.Parameterized.Parameters;
  29. import org.xml.sax.SAXException;
  30. import org.xml.sax.XMLReader;
  31. import org.xml.sax.helpers.XMLFilterImpl;
  32. import org.apache.fop.DebugHelper;
  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.FopFactoryBuilder;
  37. import org.apache.fop.apps.FopFactoryConfig;
  38. import org.apache.fop.apps.MutableConfig;
  39. import org.apache.fop.fotreetest.ext.TestElementMapping;
  40. import org.apache.fop.layoutengine.LayoutEngineTestUtils;
  41. import org.apache.fop.layoutengine.TestFilesConfiguration;
  42. import org.apache.fop.util.ConsoleEventListenerForTests;
  43. /**
  44. * Test driver class for FO tree tests.
  45. */
  46. @RunWith(Parameterized.class)
  47. public class FOTreeTestCase {
  48. private static final String BASE_DIR = "test/fotree/";
  49. private static final String TEST_CASES = "testcases";
  50. @BeforeClass
  51. public static void registerElementListObservers() {
  52. DebugHelper.registerStandardElementListObservers();
  53. }
  54. /**
  55. * Gets the parameters to run the FO tree test cases.
  56. * @return a collection of file arrays containing the test files
  57. */
  58. @Parameters
  59. public static Collection<File[]> getParameters() {
  60. TestFilesConfiguration.Builder builder = new TestFilesConfiguration.Builder();
  61. builder.testDir(BASE_DIR)
  62. .singleProperty("fop.fotree.single")
  63. .startsWithProperty("fop.fotree.starts-with")
  64. .suffix(".fo")
  65. .testSet("testcases")
  66. .disabledProperty("fop.layoutengine.disabled", "test/fotree/disabled-testcases.xml")
  67. .privateTestsProperty("fop.fotree.private");
  68. TestFilesConfiguration testConfig = builder.build();
  69. return LayoutEngineTestUtils.getTestFiles(testConfig);
  70. }
  71. private final File testFile;
  72. /**
  73. * Main constructor
  74. *
  75. * @param testFile the FO file to test
  76. */
  77. public FOTreeTestCase(File testFile) {
  78. this.testFile = testFile;
  79. }
  80. /**
  81. * Runs a test.
  82. * @throws Exception if a test or FOP itself fails
  83. */
  84. @Test
  85. public void runTest() throws Exception {
  86. try {
  87. ResultCollector collector = ResultCollector.getInstance();
  88. collector.reset();
  89. SAXParserFactory spf = SAXParserFactory.newInstance();
  90. spf.setNamespaceAware(true);
  91. spf.setValidating(false);
  92. SAXParser parser = spf.newSAXParser();
  93. XMLReader reader = parser.getXMLReader();
  94. FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI());
  95. // Resetting values modified by processing instructions
  96. builder.setBreakIndentInheritanceOnReferenceAreaBoundary(
  97. FopFactoryConfig.DEFAULT_BREAK_INDENT_INHERITANCE);
  98. builder.setSourceResolution(FopFactoryConfig.DEFAULT_SOURCE_RESOLUTION);
  99. MutableConfig mutableConfig = new MutableConfig(builder);
  100. FopFactory fopFactory = FopFactory.newInstance(mutableConfig);
  101. fopFactory.addElementMapping(new TestElementMapping());
  102. FOUserAgent ua = fopFactory.newFOUserAgent();
  103. ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua));
  104. ua.getEventBroadcaster().addEventListener(
  105. new ConsoleEventListenerForTests(testFile.getName()));
  106. // Used to set values in the user agent through processing instructions
  107. reader = new PIListener(reader, mutableConfig);
  108. Fop fop = fopFactory.newFop(ua);
  109. reader.setContentHandler(fop.getDefaultHandler());
  110. reader.setDTDHandler(fop.getDefaultHandler());
  111. reader.setErrorHandler(fop.getDefaultHandler());
  112. reader.setEntityResolver(fop.getDefaultHandler());
  113. try {
  114. reader.parse(testFile.toURI().toURL().toExternalForm());
  115. } catch (Exception e) {
  116. collector.notifyError(e.getLocalizedMessage());
  117. throw e;
  118. }
  119. List<String> results = collector.getResults();
  120. if (results.size() > 0) {
  121. for (String result : results) {
  122. System.out.println(result);
  123. }
  124. throw new IllegalStateException(results.get(0));
  125. }
  126. } catch (Exception e) {
  127. org.apache.commons.logging.LogFactory.getLog(this.getClass()).info(
  128. "Error on " + testFile.getName());
  129. throw e;
  130. }
  131. }
  132. private static class PIListener extends XMLFilterImpl {
  133. private final MutableConfig fopConfig;
  134. public PIListener(XMLReader parent, MutableConfig fopConfig) {
  135. super(parent);
  136. this.fopConfig = fopConfig;
  137. }
  138. /** @see org.xml.sax.helpers.XMLFilterImpl */
  139. public void processingInstruction(String target, String data) throws SAXException {
  140. if ("fop-useragent-break-indent-inheritance".equals(target)) {
  141. fopConfig.setBreakIndentInheritanceOnReferenceAreaBoundary(
  142. Boolean.valueOf(data));
  143. } else if ("fop-source-resolution".equals(target)) {
  144. fopConfig.setSourceResolution(Float.parseFloat(data));
  145. }
  146. super.processingInstruction(target, data);
  147. }
  148. }
  149. }