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.

LayoutEngineTestSuite.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.layoutengine;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.util.Collection;
  21. import java.util.Iterator;
  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.apache.commons.io.FileUtils;
  32. import org.apache.commons.io.filefilter.AndFileFilter;
  33. import org.apache.commons.io.filefilter.IOFileFilter;
  34. import org.apache.commons.io.filefilter.NameFileFilter;
  35. import org.apache.commons.io.filefilter.NotFileFilter;
  36. import org.apache.commons.io.filefilter.PrefixFileFilter;
  37. import org.apache.commons.io.filefilter.SuffixFileFilter;
  38. import org.apache.commons.io.filefilter.TrueFileFilter;
  39. import org.apache.fop.DebugHelper;
  40. import org.xml.sax.Attributes;
  41. import org.xml.sax.SAXException;
  42. import org.xml.sax.helpers.DefaultHandler;
  43. import junit.framework.Test;
  44. import junit.framework.TestCase;
  45. import junit.framework.TestSuite;
  46. /**
  47. * JUnit test suit for running layout engine test under JUnit control.
  48. */
  49. public class LayoutEngineTestSuite {
  50. static {
  51. DebugHelper.registerStandardElementListObservers();
  52. }
  53. public static String[] readDisabledTestcases(File f) throws IOException {
  54. List lines = new java.util.ArrayList();
  55. Source stylesheet = new StreamSource(
  56. new File("test/layoutengine/disabled-testcase2filename.xsl"));
  57. Source source = new StreamSource(f);
  58. Result result = new SAXResult(new FilenameHandler(lines));
  59. try {
  60. Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesheet);
  61. transformer.transform(source, result);
  62. } catch (TransformerConfigurationException tce) {
  63. throw new RuntimeException(tce.getMessage());
  64. } catch (TransformerException te) {
  65. throw new RuntimeException(te.getMessage());
  66. }
  67. return (String[])lines.toArray(new String[lines.size()]);
  68. }
  69. private static class FilenameHandler extends DefaultHandler {
  70. private StringBuffer buffer = new StringBuffer(128);
  71. private boolean readingFilename = false;
  72. private List filenames;
  73. public FilenameHandler(List filenames) {
  74. this.filenames = filenames;
  75. }
  76. public void startElement(String namespaceURI, String localName, String qName,
  77. Attributes atts) throws SAXException {
  78. if (qName != null && qName.equals("file")) {
  79. buffer.setLength(0);
  80. readingFilename = true;
  81. } else {
  82. throw new RuntimeException(
  83. "Unexpected element while reading disabled testcase file names: " + qName);
  84. }
  85. }
  86. public void endElement(String namespaceURI, String localName, String qName)
  87. throws SAXException {
  88. if (qName != null && qName.equals("file")) {
  89. readingFilename = false;
  90. filenames.add(buffer.toString());
  91. } else {
  92. throw new RuntimeException(
  93. "Unexpected element while reading disabled testcase file names: " + qName);
  94. }
  95. }
  96. public void characters(char[] ch, int start, int length) throws SAXException {
  97. if (readingFilename) {
  98. buffer.append(ch, start, length);
  99. }
  100. }
  101. }
  102. public static IOFileFilter decorateWithDisabledList(IOFileFilter filter) throws IOException {
  103. String disabled = System.getProperty("fop.layoutengine.disabled");
  104. if (disabled != null && disabled.length() > 0) {
  105. filter = new AndFileFilter(new NotFileFilter(
  106. new NameFileFilter(readDisabledTestcases(new File(disabled)))),
  107. filter);
  108. }
  109. return filter;
  110. }
  111. /**
  112. * @return a Collection of File instances containing all the test cases set up for processing.
  113. * @throws IOException if there's a problem gathering the list of test files
  114. */
  115. public static Collection getTestFiles() throws IOException {
  116. File mainDir = new File("test/layoutengine");
  117. IOFileFilter filter;
  118. String single = System.getProperty("fop.layoutengine.single");
  119. String startsWith = System.getProperty("fop.layoutengine.starts-with");
  120. if (single != null) {
  121. filter = new NameFileFilter(single);
  122. } else if (startsWith != null) {
  123. filter = new PrefixFileFilter(startsWith);
  124. filter = new AndFileFilter(filter, new SuffixFileFilter(".xml"));
  125. } else {
  126. filter = new SuffixFileFilter(".xml");
  127. filter = decorateWithDisabledList(filter);
  128. }
  129. String testset = System.getProperty("fop.layoutengine.testset");
  130. if (testset == null) {
  131. testset = "standard";
  132. }
  133. Collection files = FileUtils.listFiles(new File(mainDir, testset + "-testcases"),
  134. filter, TrueFileFilter.INSTANCE);
  135. String privateTests = System.getProperty("fop.layoutengine.private");
  136. if ("true".equalsIgnoreCase(privateTests)) {
  137. Collection privateFiles = FileUtils.listFiles(
  138. new File(mainDir, "private-testcases"),
  139. filter, TrueFileFilter.INSTANCE);
  140. files.addAll(privateFiles);
  141. }
  142. return files;
  143. }
  144. /**
  145. * @return the test suite with all the tests (one for each XML file)
  146. * @throws IOException in case of an I/O problem
  147. */
  148. public static Test suite() throws IOException {
  149. TestSuite suite = new TestSuite();
  150. File backupDir = new File("build/test-results/layoutengine");
  151. backupDir.mkdirs();
  152. Collection files = getTestFiles();
  153. final LayoutEngineTester tester = new LayoutEngineTester(backupDir);
  154. Iterator i = files.iterator();
  155. while (i.hasNext()) {
  156. File f = (File)i.next();
  157. addTestCase(suite, tester, f);
  158. }
  159. return suite;
  160. }
  161. private static void addTestCase(TestSuite suite,
  162. final LayoutEngineTester tester, final File f) {
  163. suite.addTest(new LayoutEngineTestCase(f.getName()) {
  164. public void runTest() throws Exception {
  165. org.apache.commons.logging.LogFactory.getLog(
  166. this.getClass()).info("Starting " + f.getName());
  167. prepare(tester, f);
  168. testMain();
  169. }
  170. });
  171. }
  172. private static class LayoutEngineTestCase extends TestCase {
  173. private LayoutEngineTester tester;
  174. private File testFile;
  175. public LayoutEngineTestCase(String name) {
  176. super(name);
  177. }
  178. public void prepare(LayoutEngineTester tester, File testFile) {
  179. //super(testFile.getName());
  180. this.tester = tester;
  181. this.testFile = testFile;
  182. }
  183. public void testMain() throws Exception {
  184. tester.runTest(testFile);
  185. }
  186. }
  187. }