1 17 18 19 20 package org.apache.fop.layoutengine; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.xml.transform.Result ; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerConfigurationException ; 32 import javax.xml.transform.TransformerException ; 33 import javax.xml.transform.TransformerFactory ; 34 import javax.xml.transform.sax.SAXResult ; 35 import javax.xml.transform.stream.StreamSource ; 36 37 import org.apache.commons.io.FileUtils; 38 import org.apache.commons.io.filefilter.AndFileFilter; 39 import org.apache.commons.io.filefilter.IOFileFilter; 40 import org.apache.commons.io.filefilter.NameFileFilter; 41 import org.apache.commons.io.filefilter.NotFileFilter; 42 import org.apache.commons.io.filefilter.PrefixFileFilter; 43 import org.apache.commons.io.filefilter.SuffixFileFilter; 44 import org.apache.commons.io.filefilter.TrueFileFilter; 45 import org.apache.fop.DebugHelper; 46 47 import org.xml.sax.Attributes ; 48 import org.xml.sax.SAXException ; 49 import org.xml.sax.helpers.DefaultHandler ; 50 51 import junit.framework.Test; 52 import junit.framework.TestCase; 53 import junit.framework.TestSuite; 54 55 58 public class LayoutEngineTestSuite { 59 60 static { 61 DebugHelper.registerStandardElementListObservers(); 62 } 63 64 public static String [] readDisabledTestcases(File f) throws IOException { 65 List lines = new java.util.ArrayList (); 66 Source stylesheet = new StreamSource ( 67 new File ("test/layoutengine/disabled-testcase2filename.xsl")); 68 Source source = new StreamSource (f); 69 Result result = new SAXResult (new FilenameHandler(lines)); 70 try { 71 Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesheet); 72 transformer.transform(source, result); 73 } catch (TransformerConfigurationException tce) { 74 throw new RuntimeException (tce.getMessage()); 75 } catch (TransformerException te) { 76 throw new RuntimeException (te.getMessage()); 77 } 78 return (String [])lines.toArray(new String [lines.size()]); 79 } 80 81 private static class FilenameHandler extends DefaultHandler { 82 private StringBuffer buffer = new StringBuffer (128); 83 private boolean readingFilename = false; 84 private List filenames; 85 86 public FilenameHandler(List filenames) { 87 this.filenames = filenames; 88 } 89 90 public void startElement(String namespaceURI, String localName, String qName, 91 Attributes atts) throws SAXException { 92 if (qName != null && qName.equals("file")) { 93 buffer.setLength(0); 94 readingFilename = true; 95 } else { 96 throw new RuntimeException ( 97 "Unexpected element while reading disabled testcase file names: " + qName); 98 } 99 } 100 101 public void endElement(String namespaceURI, String localName, String qName) 102 throws SAXException { 103 if (qName != null && qName.equals("file")) { 104 readingFilename = false; 105 filenames.add(buffer.toString()); 106 } else { 107 throw new RuntimeException ( 108 "Unexpected element while reading disabled testcase file names: " + qName); 109 } 110 } 111 112 public void characters(char[] ch, int start, int length) throws SAXException { 113 if (readingFilename) { 114 buffer.append(ch, start, length); 115 } 116 } 117 } 118 119 public static IOFileFilter decorateWithDisabledList(IOFileFilter filter) throws IOException { 120 String disabled = System.getProperty("fop.layoutengine.disabled"); 121 if (disabled != null && disabled.length() > 0) { 122 filter = new AndFileFilter(new NotFileFilter( 123 new NameFileFilter(readDisabledTestcases(new File (disabled)))), 124 filter); 125 } 126 return filter; 127 } 128 129 133 public static Collection getTestFiles() throws IOException { 134 File mainDir = new File ("test/layoutengine"); 135 IOFileFilter filter; 136 String single = System.getProperty("fop.layoutengine.single"); 137 String startsWith = System.getProperty("fop.layoutengine.starts-with"); 138 if (single != null) { 139 filter = new NameFileFilter(single); 140 } else if (startsWith != null) { 141 filter = new PrefixFileFilter(startsWith); 142 filter = new AndFileFilter(filter, new SuffixFileFilter(".xml")); 143 } else { 144 filter = new SuffixFileFilter(".xml"); 145 filter = decorateWithDisabledList(filter); 146 } 147 String testset = System.getProperty("fop.layoutengine.testset"); 148 if (testset == null) { 149 testset = "standard"; 150 } 151 Collection files = FileUtils.listFiles(new File (mainDir, testset + "-testcases"), 152 filter, TrueFileFilter.INSTANCE); 153 String privateTests = System.getProperty("fop.layoutengine.private"); 154 if ("true".equalsIgnoreCase(privateTests)) { 155 Collection privateFiles = FileUtils.listFiles( 156 new File (mainDir, "private-testcases"), 157 filter, TrueFileFilter.INSTANCE); 158 files.addAll(privateFiles); 159 } 160 return files; 161 } 162 163 167 public static Test suite() throws IOException { 168 TestSuite suite = new TestSuite(); 169 170 File backupDir = new File ("build/test-results/layoutengine"); 171 backupDir.mkdirs(); 172 173 Collection files = getTestFiles(); 174 175 final LayoutEngineTester tester = new LayoutEngineTester(backupDir); 176 Iterator i = files.iterator(); 177 while (i.hasNext()) { 178 File f = (File )i.next(); 179 addTestCase(suite, tester, f); 180 } 181 182 return suite; 183 } 184 185 private static void addTestCase(TestSuite suite, 186 final LayoutEngineTester tester, final File f) { 187 suite.addTest(new LayoutEngineTestCase(f.getName()) { 188 public void runTest() throws Exception { 189 org.apache.commons.logging.LogFactory.getLog( 190 this.getClass()).info("Starting " + f.getName()); 191 prepare(tester, f); 192 testMain(); 193 } 194 }); 195 } 196 197 private static class LayoutEngineTestCase extends TestCase { 198 199 private LayoutEngineTester tester; 200 private File testFile; 201 202 public LayoutEngineTestCase(String name) { 203 super(name); 204 } 205 206 public void prepare(LayoutEngineTester tester, File testFile) { 207 this.tester = tester; 209 this.testFile = testFile; 210 } 211 212 public void testMain() throws Exception { 213 tester.runTest(testFile); 214 } 215 } 216 } 217 | Popular Tags |