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.

FOTreeTester.java 6.2KB

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