aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/afp/modca/AbstractAFPObjectTest.java
blob: 65c0224c257a57aad81a0cf48f735c66bfe2e5cc (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * 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.modca;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.fop.afp.Streamable;
import org.junit.Test;

/**
 * Tests the {@link AbstractAFPObject} class.
 */
public abstract class AbstractAFPObjectTest<S extends AbstractAFPObject> {

    private S sut;

    protected final S getSut() {
        return sut;
    }

    protected final void setSut(S sut) {
        if (this.sut == null) {
            this.sut = sut;
        }
    }


    private byte[] header = new byte[] {
            0x5A, // Structured field identifier
            0x00, // Length byte 1
            0x10, // Length byte 2
            0x00, // Structured field id byte 1
            0x00, // Structured field id byte 2
            0x00, // Structured field id byte 3
            0x00, // Flags
            0x00, // Reserved
            0x00 // Reserved
    };

    @Test
    public void testCopySFStatic() {
        byte[] actual = new byte[9];
        Arrays.fill(actual, (byte)-1);

        S.copySF(actual, (byte)0, (byte)0, (byte)0);

        assertTrue(Arrays.equals(actual, header));

        byte[] expected2 =  new byte[9];
        System.arraycopy(header, 0, expected2, 0, header.length);

        final byte clazz = (byte) 0x01;
        final byte type = (byte) 0x02;
        final byte catagory = (byte) 0x03;
        expected2[3] = clazz;
        expected2[4] = type;
        expected2[5] = catagory;

        AbstractAFPObject.copySF(actual, clazz, type, catagory);

        assertTrue(Arrays.equals(actual, expected2));
    }

    @Test
    public void testCopySF() {
        byte[] expected = new byte[9];
        S.copySF(expected, (byte) 0xD3, (byte)0, (byte)0);

        byte[] actual = new byte[9];
        Arrays.fill(actual, (byte)-1);

        getSut().copySF(actual, (byte)0, (byte)0);

        assertTrue(Arrays.equals(actual, expected));

        byte[] expected2 =  new byte[9];
        System.arraycopy(expected, 0, expected2, 0, expected.length);

        final byte type = (byte)1;
        final byte catagory = (byte)2;
        expected2[4] = type;
        expected2[5] = catagory;

        getSut().copySF(actual, type, catagory);

        assertTrue(Arrays.equals(actual, expected2));
    }

    /**
     *
     */
    @Test
    public void testwriteObjects() {
       final byte[][] expected = {{(byte)0, (byte)1}, {(byte)2, (byte)3}, {(byte)4, (byte)5}};

        List<Streamable> objects = new ArrayList<Streamable>() {
            {
           add(StreamableObject.instance(expected[0]));
           add(StreamableObject.instance(expected[1]));
           add(StreamableObject.instance(expected[2]));
       } };

       ByteArrayOutputStream baos = new ByteArrayOutputStream();

       try {
           getSut().writeObjects(objects, baos);
       } catch (IOException e) {
           fail();
       }

       byte[] actual = baos.toByteArray();

       int index = 0;
       for (int i = 0; i < expected.length; i++) {
           for (int j = 0; j < expected[i].length; j++) {
               assertTrue("" + index, actual[index] == expected[i][j]);
               index++;
           }
       }
    }

    /**
     *
     */
    @Test
    public void testTruncate() {
        String expected = "abc";
        assertTrue(AbstractAFPObject.truncate(expected, 4)  == expected);
        assertTrue(AbstractAFPObject.truncate(expected, 3) == expected);
        assertEquals(AbstractAFPObject.truncate(expected + "d", 3), expected);
        assertEquals(AbstractAFPObject.truncate(expected, 0), "");
        try {
            assertTrue(AbstractAFPObject.truncate(null, 4) == null);
            fail();
        } catch (NullPointerException e) {
            // PASS
        }
    }

    /**
     *
     */
    @Test
    public void testWriteChunksToStream() throws IOException {
        final byte[] data = new byte[256];
        int counter = 0;
        for (int i = 0; i < data.length; i++) {
            data[i] = (byte) counter++;
        }

        byte[] header = new byte[9];
        // Test when chunk size % data.length == 0
        testWithGivenChunkSize(data, header, 16);

        // test when chunk size % data.length != 0
        testWithGivenChunkSize(data, header, 10);

        // test with an odd number...
        testWithGivenChunkSize(data, header, 13);
    }

    private void testWithGivenChunkSize(byte[] data, byte[] header, int chunkSize)
            throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        S.writeChunksToStream(data, header, 0, chunkSize, baos);
        byte[] testData = baos.toByteArray();

        int numberOfFullDataChunks = data.length / chunkSize;
        int lastChunkSize = data.length % chunkSize;
        int lengthOfTestData = numberOfFullDataChunks * (chunkSize + header.length);
        lengthOfTestData += lastChunkSize == 0 ? 0 : header.length + lastChunkSize;

        putLengthInHeader(header, chunkSize);

        assertEquals(lengthOfTestData, testData.length);
        int testIndex = 0;
        int expectedIndex = 0;
        for (int i = 0; i < numberOfFullDataChunks; i++) {
            checkHeaderAndData(header, data, testData, expectedIndex, testIndex, chunkSize);
            expectedIndex += chunkSize + header.length;
            testIndex += chunkSize;
        }

        putLengthInHeader(header, lastChunkSize);
        // check last chunk
        if (lastChunkSize != 0) {
            checkHeaderAndData(header, data, testData, expectedIndex, testIndex, lastChunkSize);
        }
    }

    private void putLengthInHeader(byte[] header, int chunkSize) {
        header[0] = 0;
        header[1] = (byte) (chunkSize + header.length);
    }

    private void checkHeaderAndData(byte[] header, byte[] data, byte[] testData, int expectedIndex,
            int testIndex, int chunkSize) {
        for (int i = 0; i < header.length; i++) {
            assertEquals(testData[expectedIndex++], header[i]);
        }
        for (int i = 0; i < chunkSize; i++) {
            assertEquals(testData[expectedIndex++], data[i + testIndex]);
        }
    }

    /**
     *
     */
    private static class StreamableObject implements Streamable {
        private byte[] bytes;

        StreamableObject(byte[] bytes) {
            this.bytes = new byte[bytes.length];
            System.arraycopy(bytes, 0, this.bytes, 0, bytes.length);
        }

        private static Streamable instance(byte[] bytes) {
            return new StreamableObject(bytes);
        }

        public void writeToStream(OutputStream os) throws IOException {
            os.write(bytes);
        }
    }
}