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 7.5KB

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