aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/intermediate/IFTestCase.java
blob: a6ff171e39928c03ba039692ea265a31658125b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.intermediate;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerFactory;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


/**
 * Test case for the IF output.
 */
@RunWith(Parameterized.class)
public class IFTestCase extends AbstractIFTestCase {

    /**
     * Gets the files for this test.
     *
     * @return a collection of file arrays containing the files to test
     * @throws IOException if an error occurs when reading the test files
     */
    @Parameters
    public static Collection<File[]> getParameters() throws IOException {
        File testDir = new File("test/intermediate");
        String[] tests = testDir.list(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(".xml");
            }
        });

        Collection<File[]> parameters = new ArrayList<File[]>();
        for (String test : tests) {
            parameters.add(new File[] { new File(testDir, test) });
        }
        return parameters;
    }

    private static IFTester ifTester;

    @BeforeClass
    public static void setupTestEnvironment() {
        File backupDir = new File("build/test-results/intermediate");
        backupDir.mkdirs();
        ifTester = new IFTester(TransformerFactory.newInstance(), backupDir);
    }

    /**
     * Creates a new test case.
     *
     * @param test the file containing the test case
     * @param ifTester the helper instance that will perform checks
     * @throws IOException if an I/O error occurs while loading the test case
     */
    public IFTestCase(File test) throws IOException {
        super(test);
        this.testDir = test.getParentFile();
    }

    @Override
    @Test
    public void runTest() throws Exception {
        Element testRoot = testAssistant.getTestRoot(testFile);
        NodeList nodes = testRoot.getElementsByTagName("if-checks");
        if (nodes.getLength() == 0) {
            throw new RuntimeException("No IF check found");
        }
        Element ifChecks = (Element) nodes.item(0);

        Document doc = buildIntermediateDocument(testAssistant.getTestcase2FOStylesheet());
        ifTester.doIFChecks(testFile.getName(), ifChecks, doc);
    }

    @Override
    protected void parseAndRender(Source src, OutputStream out) throws Exception {
        throw new IllegalStateException("Not applicable to this test");
    }

    @Override
    protected Document parseAndRenderToIntermediateFormat(Source src) throws Exception {
        throw new IllegalStateException("Not applicable to this test");
    }

}