aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/afp/AFPResourceUtilTestCase.java
blob: 178e5c6ad39264c6e7c9ce729ca3106463a75d9e (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * 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.afp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

import org.junit.Test;

import static org.junit.Assert.assertTrue;

import org.apache.commons.io.IOUtils;

import org.apache.fop.afp.util.AFPResourceUtil;

/**
 * Tests the {@link AFPResourceUtil} class.
 */
public class AFPResourceUtilTestCase {

    private static final String RESOURCE_FILENAME = "expected_resource.afp";
    private static final String NAMED_RESOURCE_FILENAME = "expected_named_resource.afp";

    private static final String RESOURCE_ANY_NAME = "resource_any_name.afp";
    private static final String RESOURCE_NAME_MATCH = "resource_name_match.afp";
    private static final String RESOURCE_NAME_MISMATCH = "resource_name_mismatch.afp";
    private static final String RESOURCE_NO_END_NAME = "resource_no_end_name.afp";

    private static final String PSEG_A = "XFEATHER";
    private static final String PSEG_B = "S1CODEQR";

    /**
     * Tests copyResourceFile()
     * @throws Exception -
     */
    @Test
    public void testCopyResourceFile() throws Exception {
        compareResources(new ResourceCopier() {
            public void copy(InputStream in, OutputStream out) throws IOException {
                AFPResourceUtil.copyResourceFile(in, out);
            }
        }, RESOURCE_FILENAME, RESOURCE_FILENAME);
    }

    /**
     * Tests copyNamedResource()
     * @throws Exception -
     */
    @Test
    public void testCopyNamedResource() throws Exception {
        compareResources(new ResourceCopier() {
            public void copy(InputStream in, OutputStream out) throws IOException {
                AFPResourceUtil.copyNamedResource(PSEG_A, in, out);
            }
        }, RESOURCE_FILENAME, NAMED_RESOURCE_FILENAME);
    }

    private void compareResources(ResourceCopier copyResource, String resourceA, String resourceB)
            throws IOException {
        ByteArrayOutputStream baos = copyResource(resourceA, copyResource);
        byte[] expectedBytes = resourceAsByteArray(resourceB);
        assertTrue(Arrays.equals(expectedBytes, baos.toByteArray()));
    }

    private ByteArrayOutputStream copyResource(String resource, ResourceCopier resourceCopier)
            throws IOException {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream in = null;
        try {
            in = getClass().getResourceAsStream(resource);
            resourceCopier.copy(in, baos);
        } finally {
            in.close();
        }
        return baos;
    }

    private byte[] resourceAsByteArray(String resource) throws IOException {
        InputStream in = null;
        byte[] expectedBytes = null;
        try {
            in = getClass().getResourceAsStream(resource);
            expectedBytes = IOUtils.toByteArray(in);
        } finally {
            in.close();
        }
        return expectedBytes;
    }

    /**
     * Tests the validity of a closing structured field having an FF FF name which
     * allows it to match any existing matching starting field
     * @throws Exception -
     */
    @Test
    public void testResourceAnyName() throws Exception {
        testResource(RESOURCE_ANY_NAME, PSEG_B);
    }

    /**
     * Tests a matching end structured field name
     * @throws Exception -
     */
    @Test
    public void testResourceNameMatch() throws Exception {
        testResource(RESOURCE_NAME_MATCH, PSEG_B);
    }

    /**
     * Tests to see whether a matching structured field pair with mismatching
     * names fails.
     * @throws Exception -
     */
    @Test(expected = Exception.class)
    public void testResourceNameMismatch() throws Exception {
        testResource(RESOURCE_NAME_MISMATCH, PSEG_B);
    }

    /**
     * Tests a matching structured end field with no name
     * @throws Exception -
     */
    @Test
    public void testResourceNoEndName() throws Exception {
        testResource(RESOURCE_NO_END_NAME, PSEG_B);
    }

    private void testResource(String resource, final String pseg) throws Exception {
        copyResource(resource, new ResourceCopier() {
            public void copy(InputStream in, OutputStream out) throws IOException {
                AFPResourceUtil.copyNamedResource(pseg, in, out);
            }
        });
    }

    private interface ResourceCopier {
        void copy(InputStream in, OutputStream out) throws IOException;
    }
}