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: IFTestCase.java 1198853 2011-11-07 18:18:29Z vhennebert $ */
19 
20 package org.apache.fop.intermediate;
21 
22 import java.io.File;
23 import java.io.FilenameFilter;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 
29 import javax.xml.transform.Source;
30 import javax.xml.transform.TransformerFactory;
31 
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.Parameterized;
36 import org.junit.runners.Parameterized.Parameters;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.NodeList;
40 
41 
42 /**
43  * Test case for the IF output.
44  */
45 @RunWith(Parameterized.class)
46 public class IFTestCase extends AbstractIFTest {
47 
48     /**
49      * Gets the files for this test.
50      *
51      * @return a collection of file arrays containing the files to test
52      * @throws IOException if an error occurs when reading the test files
53      */
54     @Parameters
getParameters()55     public static Collection<File[]> getParameters() throws IOException {
56         File testDir = new File("test/intermediate");
57         String[] tests = testDir.list(new FilenameFilter() {
58 
59             public boolean accept(File dir, String name) {
60                 return name.endsWith(".xml");
61             }
62         });
63 
64         Collection<File[]> parameters = new ArrayList<File[]>();
65         for (String test : tests) {
66             parameters.add(new File[] { new File(testDir, test) });
67         }
68         return parameters;
69     }
70 
71     private static IFTester ifTester;
72 
73     @BeforeClass
setupTestEnvironment()74     public static void setupTestEnvironment() {
75         File backupDir = new File("build/test-results/intermediate");
76         backupDir.mkdirs();
77         ifTester = new IFTester(TransformerFactory.newInstance(), backupDir);
78     }
79 
80     /**
81      * Creates a new test case.
82      *
83      * @param test the file containing the test case
84      * @param ifTester the helper instance that will perform checks
85      * @throws IOException if an I/O error occurs while loading the test case
86      */
IFTestCase(File test)87     public IFTestCase(File test) throws IOException {
88         super(test);
89         this.testDir = test.getParentFile();
90     }
91 
92     @Override
93     @Test
runTest()94     public void runTest() throws Exception {
95         Element testRoot = testAssistant.getTestRoot(testFile);
96         NodeList nodes = testRoot.getElementsByTagName("if-checks");
97         if (nodes.getLength() == 0) {
98             throw new RuntimeException("No IF check found");
99         }
100         Element ifChecks = (Element) nodes.item(0);
101 
102         Document doc = buildIntermediateDocument(testAssistant.getTestcase2FOStylesheet());
103         ifTester.doIFChecks(testFile.getName(), ifChecks, doc);
104     }
105 
106     @Override
parseAndRender(Source src, OutputStream out)107     protected void parseAndRender(Source src, OutputStream out) throws Exception {
108         throw new IllegalStateException("Not applicable to this test");
109     }
110 
111     @Override
parseAndRenderToIntermediateFormat(Source src)112     protected Document parseAndRenderToIntermediateFormat(Source src) throws Exception {
113         throw new IllegalStateException("Not applicable to this test");
114     }
115 
116 }
117