KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutengine > LayoutEngineTestSuite


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
18 /* $Id: LayoutEngineTestSuite.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutengine;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.xml.transform.Result JavaDoc;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.TransformerFactory JavaDoc;
34 import javax.xml.transform.sax.SAXResult JavaDoc;
35 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
48 import org.xml.sax.SAXException JavaDoc;
49 import org.xml.sax.helpers.DefaultHandler JavaDoc;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 /**
56  * JUnit test suit for running layout engine test under JUnit control.
57  */

58 public class LayoutEngineTestSuite {
59
60     static {
61         DebugHelper.registerStandardElementListObservers();
62     }
63     
64     public static String JavaDoc[] readDisabledTestcases(File JavaDoc f) throws IOException JavaDoc {
65         List JavaDoc lines = new java.util.ArrayList JavaDoc();
66         Source JavaDoc stylesheet = new StreamSource JavaDoc(
67                 new File JavaDoc("test/layoutengine/disabled-testcase2filename.xsl"));
68         Source JavaDoc source = new StreamSource JavaDoc(f);
69         Result JavaDoc result = new SAXResult JavaDoc(new FilenameHandler(lines));
70         try {
71             Transformer JavaDoc transformer = TransformerFactory.newInstance().newTransformer(stylesheet);
72             transformer.transform(source, result);
73         } catch (TransformerConfigurationException JavaDoc tce) {
74             throw new RuntimeException JavaDoc(tce.getMessage());
75         } catch (TransformerException JavaDoc te) {
76             throw new RuntimeException JavaDoc(te.getMessage());
77         }
78         return (String JavaDoc[])lines.toArray(new String JavaDoc[lines.size()]);
79     }
80     
81     private static class FilenameHandler extends DefaultHandler JavaDoc {
82         private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
83         private boolean readingFilename = false;
84         private List JavaDoc filenames;
85
86         public FilenameHandler(List JavaDoc filenames) {
87             this.filenames = filenames;
88         }
89
90         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName,
91                 Attributes JavaDoc atts) throws SAXException JavaDoc {
92             if (qName != null && qName.equals("file")) {
93                 buffer.setLength(0);
94                 readingFilename = true;
95             } else {
96                 throw new RuntimeException JavaDoc(
97                         "Unexpected element while reading disabled testcase file names: " + qName);
98             }
99         }
100
101         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
102                     throws SAXException JavaDoc {
103             if (qName != null && qName.equals("file")) {
104                 readingFilename = false;
105                 filenames.add(buffer.toString());
106             } else {
107                 throw new RuntimeException JavaDoc(
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 JavaDoc {
113             if (readingFilename) {
114                 buffer.append(ch, start, length);
115             }
116         }
117     }
118     
119     public static IOFileFilter decorateWithDisabledList(IOFileFilter filter) throws IOException JavaDoc {
120         String JavaDoc 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 JavaDoc(disabled)))),
124                     filter);
125         }
126         return filter;
127     }
128     
129     /**
130      * @return a Collection of File instances containing all the test cases set up for processing.
131      * @throws IOException if there's a problem gathering the list of test files
132      */

133     public static Collection JavaDoc getTestFiles() throws IOException JavaDoc {
134         File JavaDoc mainDir = new File JavaDoc("test/layoutengine");
135         IOFileFilter filter;
136         String JavaDoc single = System.getProperty("fop.layoutengine.single");
137         String JavaDoc 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 JavaDoc testset = System.getProperty("fop.layoutengine.testset");
148         if (testset == null) {
149             testset = "standard";
150         }
151         Collection JavaDoc files = FileUtils.listFiles(new File JavaDoc(mainDir, testset + "-testcases"),
152                 filter, TrueFileFilter.INSTANCE);
153         String JavaDoc privateTests = System.getProperty("fop.layoutengine.private");
154         if ("true".equalsIgnoreCase(privateTests)) {
155             Collection JavaDoc privateFiles = FileUtils.listFiles(
156                     new File JavaDoc(mainDir, "private-testcases"),
157                     filter, TrueFileFilter.INSTANCE);
158             files.addAll(privateFiles);
159         }
160         return files;
161     }
162     
163     /**
164      * @return the test suite with all the tests (one for each XML file)
165      * @throws IOException in case of an I/O problem
166      */

167     public static Test suite() throws IOException JavaDoc {
168         TestSuite suite = new TestSuite();
169
170         File JavaDoc backupDir = new File JavaDoc("build/test-results/layoutengine");
171         backupDir.mkdirs();
172
173         Collection JavaDoc files = getTestFiles();
174
175         final LayoutEngineTester tester = new LayoutEngineTester(backupDir);
176         Iterator JavaDoc i = files.iterator();
177         while (i.hasNext()) {
178             File JavaDoc f = (File JavaDoc)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 JavaDoc f) {
187         suite.addTest(new LayoutEngineTestCase(f.getName()) {
188             public void runTest() throws Exception JavaDoc {
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 JavaDoc testFile;
201         
202         public LayoutEngineTestCase(String JavaDoc name) {
203             super(name);
204         }
205         
206         public void prepare(LayoutEngineTester tester, File JavaDoc testFile) {
207             //super(testFile.getName());
208
this.tester = tester;
209             this.testFile = testFile;
210         }
211         
212         public void testMain() throws Exception JavaDoc {
213             tester.runTest(testFile);
214         }
215     }
216 }
217
Popular Tags