Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FOTreeTestCase.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.net.URI;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import javax.xml.parsers.SAXParser;
  26. import javax.xml.parsers.SAXParserFactory;
  27. import org.junit.BeforeClass;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.junit.runners.Parameterized;
  31. import org.junit.runners.Parameterized.Parameters;
  32. import org.xml.sax.SAXException;
  33. import org.xml.sax.XMLReader;
  34. import org.xml.sax.helpers.XMLFilterImpl;
  35. import org.apache.avalon.framework.configuration.Configuration;
  36. import org.apache.xmlgraphics.image.loader.ImageManager;
  37. import org.apache.fop.DebugHelper;
  38. import org.apache.fop.apps.FOUserAgent;
  39. import org.apache.fop.apps.Fop;
  40. import org.apache.fop.apps.FopFactory;
  41. import org.apache.fop.apps.FopFactoryBuilder;
  42. import org.apache.fop.apps.FopFactoryConfig;
  43. import org.apache.fop.apps.io.ResourceResolver;
  44. import org.apache.fop.fonts.FontManager;
  45. import org.apache.fop.fotreetest.ext.TestElementMapping;
  46. import org.apache.fop.layoutengine.LayoutEngineTestUtils;
  47. import org.apache.fop.layoutengine.TestFilesConfiguration;
  48. import org.apache.fop.layoutmgr.LayoutManagerMaker;
  49. import org.apache.fop.util.ConsoleEventListenerForTests;
  50. /**
  51. * Test driver class for FO tree tests.
  52. */
  53. @RunWith(Parameterized.class)
  54. public class FOTreeTestCase {
  55. private static final String BASE_DIR = "test/fotree/";
  56. private static final String TEST_CASES = "testcases";
  57. @BeforeClass
  58. public static void registerElementListObservers() {
  59. DebugHelper.registerStandardElementListObservers();
  60. }
  61. /**
  62. * Gets the parameters to run the FO tree test cases.
  63. * @return a collection of file arrays containing the test files
  64. */
  65. @Parameters
  66. public static Collection<File[]> getParameters() {
  67. TestFilesConfiguration.Builder builder = new TestFilesConfiguration.Builder();
  68. builder.testDir(BASE_DIR)
  69. .singleProperty("fop.fotree.single")
  70. .startsWithProperty("fop.fotree.starts-with")
  71. .suffix(".fo")
  72. .testSet("testcases")
  73. .disabledProperty("fop.layoutengine.disabled", "test/fotree/disabled-testcases.xml")
  74. .privateTestsProperty("fop.fotree.private");
  75. TestFilesConfiguration testConfig = builder.build();
  76. return LayoutEngineTestUtils.getTestFiles(testConfig);
  77. }
  78. private final File testFile;
  79. /**
  80. * Main constructor
  81. *
  82. * @param testFile the FO file to test
  83. */
  84. public FOTreeTestCase(File testFile) {
  85. this.testFile = testFile;
  86. }
  87. /**
  88. * Runs a test.
  89. * @throws Exception if a test or FOP itself fails
  90. */
  91. @Test
  92. public void runTest() throws Exception {
  93. try {
  94. ResultCollector collector = ResultCollector.getInstance();
  95. collector.reset();
  96. SAXParserFactory spf = SAXParserFactory.newInstance();
  97. spf.setNamespaceAware(true);
  98. spf.setValidating(false);
  99. SAXParser parser = spf.newSAXParser();
  100. XMLReader reader = parser.getXMLReader();
  101. FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI());
  102. // Resetting values modified by processing instructions
  103. builder.setBreakIndentInheritanceOnReferenceAreaBoundary(
  104. FopFactoryConfig.DEFAULT_BREAK_INDENT_INHERITANCE);
  105. builder.setSourceResolution(FopFactoryConfig.DEFAULT_SOURCE_RESOLUTION);
  106. MutableConfig mutableConfig = new MutableConfig(builder.buildConfig());
  107. FopFactory fopFactory = FopFactory.newInstance(mutableConfig);
  108. fopFactory.addElementMapping(new TestElementMapping());
  109. FOUserAgent ua = fopFactory.newFOUserAgent();
  110. ua.setFOEventHandlerOverride(new DummyFOEventHandler(ua));
  111. ua.getEventBroadcaster().addEventListener(
  112. new ConsoleEventListenerForTests(testFile.getName()));
  113. // Used to set values in the user agent through processing instructions
  114. reader = new PIListener(reader, mutableConfig);
  115. Fop fop = fopFactory.newFop(ua);
  116. reader.setContentHandler(fop.getDefaultHandler());
  117. reader.setDTDHandler(fop.getDefaultHandler());
  118. reader.setErrorHandler(fop.getDefaultHandler());
  119. reader.setEntityResolver(fop.getDefaultHandler());
  120. try {
  121. reader.parse(testFile.toURI().toURL().toExternalForm());
  122. } catch (Exception e) {
  123. collector.notifyError(e.getLocalizedMessage());
  124. throw e;
  125. }
  126. List<String> results = collector.getResults();
  127. if (results.size() > 0) {
  128. for (int i = 0; i < results.size(); i++) {
  129. System.out.println(results.get(i));
  130. }
  131. throw new IllegalStateException(results.get(0));
  132. }
  133. } catch (Exception e) {
  134. org.apache.commons.logging.LogFactory.getLog(this.getClass()).info(
  135. "Error on " + testFile.getName());
  136. throw e;
  137. }
  138. }
  139. private static class PIListener extends XMLFilterImpl {
  140. private final MutableConfig fopConfig;
  141. public PIListener(XMLReader parent, MutableConfig fopConfig) {
  142. super(parent);
  143. this.fopConfig = fopConfig;
  144. }
  145. /** @see org.xml.sax.helpers.XMLFilterImpl */
  146. public void processingInstruction(String target, String data) throws SAXException {
  147. if ("fop-useragent-break-indent-inheritance".equals(target)) {
  148. fopConfig.setBreakIndentInheritanceOnReferenceAreaBoundary(
  149. Boolean.valueOf(data).booleanValue());
  150. } else if ("fop-source-resolution".equals(target)) {
  151. fopConfig.setSourceResolution(Float.parseFloat(data));
  152. }
  153. super.processingInstruction(target, data);
  154. }
  155. }
  156. private static final class MutableConfig implements FopFactoryConfig {
  157. private final FopFactoryConfig delegate;
  158. private boolean setBreakInheritance;
  159. private float sourceResolution;
  160. private MutableConfig(FopFactoryConfig wrappedConfig) {
  161. delegate = wrappedConfig;
  162. setBreakInheritance = delegate.isBreakIndentInheritanceOnReferenceAreaBoundary();
  163. sourceResolution = delegate.getSourceResolution();
  164. }
  165. public boolean isAccessibilityEnabled() {
  166. return delegate.isAccessibilityEnabled();
  167. }
  168. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  169. return delegate.getLayoutManagerMakerOverride();
  170. }
  171. public ResourceResolver getResourceResolver() {
  172. return delegate.getResourceResolver();
  173. }
  174. public URI getBaseURI() {
  175. return delegate.getBaseURI();
  176. }
  177. public boolean validateStrictly() {
  178. return delegate.validateStrictly();
  179. }
  180. public boolean validateUserConfigStrictly() {
  181. return delegate.validateUserConfigStrictly();
  182. }
  183. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  184. return setBreakInheritance;
  185. }
  186. public void setBreakIndentInheritanceOnReferenceAreaBoundary(boolean value) {
  187. setBreakInheritance = value;
  188. }
  189. public float getSourceResolution() {
  190. return sourceResolution;
  191. }
  192. public void setSourceResolution(float srcRes) {
  193. sourceResolution = srcRes;
  194. }
  195. public float getTargetResolution() {
  196. return delegate.getTargetResolution();
  197. }
  198. public String getPageHeight() {
  199. return delegate.getPageHeight();
  200. }
  201. public String getPageWidth() {
  202. return delegate.getPageWidth();
  203. }
  204. public Set<String> getIgnoredNamespaces() {
  205. return delegate.getIgnoredNamespaces();
  206. }
  207. public boolean isNamespaceIgnored(String namespace) {
  208. return delegate.isNamespaceIgnored(namespace);
  209. }
  210. public Configuration getUserConfig() {
  211. return delegate.getUserConfig();
  212. }
  213. public boolean preferRenderer() {
  214. return delegate.preferRenderer();
  215. }
  216. public FontManager getFontManager() {
  217. return delegate.getFontManager();
  218. }
  219. public ImageManager getImageManager() {
  220. return delegate.getImageManager();
  221. }
  222. public boolean isComplexScriptFeaturesEnabled() {
  223. return delegate.isComplexScriptFeaturesEnabled();
  224. }
  225. public Map<String, String> getHyphPatNames() {
  226. return delegate.getHyphPatNames();
  227. }
  228. }
  229. }