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.

LayoutEngineTestUtils.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.layoutengine;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import javax.xml.transform.Result;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Transformer;
  26. import javax.xml.transform.TransformerConfigurationException;
  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.xml.sax.Attributes;
  32. import org.xml.sax.SAXException;
  33. import org.xml.sax.helpers.DefaultHandler;
  34. import org.apache.commons.io.FileUtils;
  35. import org.apache.commons.io.filefilter.AndFileFilter;
  36. import org.apache.commons.io.filefilter.IOFileFilter;
  37. import org.apache.commons.io.filefilter.NameFileFilter;
  38. import org.apache.commons.io.filefilter.NotFileFilter;
  39. import org.apache.commons.io.filefilter.PrefixFileFilter;
  40. import org.apache.commons.io.filefilter.SuffixFileFilter;
  41. import org.apache.commons.io.filefilter.TrueFileFilter;
  42. /**
  43. * Utility class for layout engine tests.
  44. */
  45. public final class LayoutEngineTestUtils {
  46. /** Set this to true to get the correspondence between test number and test file. */
  47. private static final boolean DEBUG = false;
  48. private LayoutEngineTestUtils() {
  49. }
  50. private static class FilenameHandler extends DefaultHandler {
  51. private StringBuffer buffer = new StringBuffer(128);
  52. private boolean readingFilename = false;
  53. private List<String> filenames;
  54. public FilenameHandler(List<String> filenames) {
  55. this.filenames = filenames;
  56. }
  57. public void startElement(String namespaceURI, String localName, String qName,
  58. Attributes atts) throws SAXException {
  59. if (qName != null && qName.equals("file")) {
  60. buffer.setLength(0);
  61. readingFilename = true;
  62. } else {
  63. throw new RuntimeException(
  64. "Unexpected element while reading disabled testcase file names: " + qName);
  65. }
  66. }
  67. public void endElement(String namespaceURI, String localName, String qName)
  68. throws SAXException {
  69. if (qName != null && qName.equals("file")) {
  70. readingFilename = false;
  71. filenames.add(buffer.toString());
  72. } else {
  73. throw new RuntimeException(
  74. "Unexpected element while reading disabled testcase file names: " + qName);
  75. }
  76. }
  77. public void characters(char[] ch, int start, int length) throws SAXException {
  78. if (readingFilename) {
  79. buffer.append(ch, start, length);
  80. }
  81. }
  82. }
  83. /**
  84. * Removes from {@code filter} any tests that have been disabled.
  85. *
  86. * @param filter the filter populated with tests
  87. * @param disabled name of the file containing disabled test cases. If null or empty,
  88. * no file is read
  89. * @return {@code filter} minus any disabled tests
  90. */
  91. public static IOFileFilter decorateWithDisabledList(IOFileFilter filter, String disabled) {
  92. if (disabled != null && disabled.length() > 0) {
  93. filter = new AndFileFilter(new NotFileFilter(new NameFileFilter(
  94. LayoutEngineTestUtils.readDisabledTestcases(new File(disabled)))), filter);
  95. }
  96. return filter;
  97. }
  98. private static String[] readDisabledTestcases(File f) {
  99. List<String> lines = new ArrayList<String>();
  100. Source stylesheet = new StreamSource(
  101. new File("test/layoutengine/disabled-testcase2filename.xsl"));
  102. Source source = new StreamSource(f);
  103. Result result = new SAXResult(new FilenameHandler(lines));
  104. try {
  105. Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesheet);
  106. transformer.transform(source, result);
  107. } catch (TransformerConfigurationException tce) {
  108. throw new RuntimeException(tce);
  109. } catch (TransformerException te) {
  110. throw new RuntimeException(te);
  111. }
  112. return (String[]) lines.toArray(new String[lines.size()]);
  113. }
  114. /**
  115. * Returns the test files matching the given configuration.
  116. *
  117. * @param testConfig the test configuration
  118. * @return the applicable test cases
  119. */
  120. public static Collection<File[]> getTestFiles(TestFilesConfiguration testConfig) {
  121. File mainDir = testConfig.getTestDirectory();
  122. IOFileFilter filter;
  123. String single = testConfig.getSingleTest();
  124. String startsWith = testConfig.getStartsWith();
  125. if (single != null) {
  126. filter = new NameFileFilter(single);
  127. } else if (startsWith != null) {
  128. filter = new PrefixFileFilter(startsWith);
  129. filter = new AndFileFilter(filter, new SuffixFileFilter(testConfig.getFileSuffix()));
  130. filter = decorateWithDisabledList(filter, testConfig.getDisabledTests());
  131. } else {
  132. filter = new SuffixFileFilter(testConfig.getFileSuffix());
  133. filter = decorateWithDisabledList(filter, testConfig.getDisabledTests());
  134. }
  135. String testset = testConfig.getTestSet();
  136. Collection<File> files = FileUtils.listFiles(new File(mainDir, testset), filter,
  137. TrueFileFilter.INSTANCE);
  138. if (testConfig.hasPrivateTests()) {
  139. Collection<File> privateFiles = FileUtils.listFiles(new File(mainDir,
  140. "private-testcases"), filter, TrueFileFilter.INSTANCE);
  141. files.addAll(privateFiles);
  142. }
  143. Collection<File[]> parametersForJUnit4 = new ArrayList<File[]>();
  144. int index = 0;
  145. for (File f : files) {
  146. parametersForJUnit4.add(new File[] { f });
  147. if (DEBUG) {
  148. System.out.println(String.format("%3d %s", index++, f));
  149. }
  150. }
  151. return parametersForJUnit4;
  152. }
  153. /**
  154. * This is a helper method that uses the standard parameters for FOP's layout engine tests and
  155. * returns a set of test files. These pull in System parameters to configure the layout tests
  156. * to run.
  157. *
  158. * @return A collection of file arrays that contain the test files
  159. */
  160. public static Collection<File[]> getLayoutTestFiles() {
  161. String testSet = System.getProperty("fop.layoutengine.testset");
  162. testSet = (testSet != null ? testSet : "standard") + "-testcases";
  163. return getLayoutTestFiles(testSet);
  164. }
  165. /**
  166. * This is a helper method that uses the standard parameters for FOP's layout engine tests,
  167. * given a test set name returns a set of test files.
  168. *
  169. * @param testSetName the name of the test set
  170. * @return A collection of file arrays that contain the test files
  171. */
  172. public static Collection<File[]> getLayoutTestFiles(String testSetName) {
  173. TestFilesConfiguration.Builder builder = new TestFilesConfiguration.Builder();
  174. builder.testDir("test/layoutengine")
  175. .singleProperty("fop.layoutengine.single")
  176. .startsWithProperty("fop.layoutengine.starts-with")
  177. .suffix(".xml")
  178. .testSet(testSetName)
  179. .disabledProperty("fop.layoutengine.disabled",
  180. "test/layoutengine/disabled-testcases.xml")
  181. .privateTestsProperty("fop.layoutengine.private");
  182. TestFilesConfiguration testConfig = builder.build();
  183. return getTestFiles(testConfig);
  184. }
  185. }