~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/util/io/Base64TestCase.java

  • Committer: Hans Joachim Desserud
  • Date: 2015-11-11 18:22:53 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: hans_joachim_desserud-20151111182253-zwi0frfm97j0wddn
  * Merge from Debian unstable.  Remaining changes:
    - d/control: Drop dependencies required for unit testing as they
      include libmockito-java which would pull maven into main, disable unit
      test execution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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$ */
 
19
 
 
20
package org.apache.xmlgraphics.util.io;
 
21
 
 
22
import java.io.File;
 
23
import java.io.IOException;
 
24
import java.io.InputStream;
 
25
import java.io.OutputStream;
 
26
import java.io.PipedInputStream;
 
27
import java.io.PipedOutputStream;
 
28
import java.net.URL;
 
29
 
 
30
import org.junit.Test;
 
31
 
 
32
import static org.junit.Assert.fail;
 
33
 
 
34
/**
 
35
 * This test validates that the Base64 encoder/decoders work properly.
 
36
 *
 
37
 * @version $Id$
 
38
 */
 
39
public class Base64TestCase {
 
40
 
 
41
    private void innerBase64Test(String action, URL in, URL ref) throws Exception {
 
42
        InputStream inIS = in.openStream();
 
43
 
 
44
        if (action.equals("ROUND")) {
 
45
            ref = in;
 
46
        } else if (!action.equals("ENCODE") && !action.equals("DECODE")) {
 
47
            fail("Bad action string");
 
48
        }
 
49
 
 
50
        InputStream refIS = ref.openStream();
 
51
 
 
52
        if (action.equals("ENCODE") || action.equals("ROUND")) {
 
53
            // We need to encode the incomming data
 
54
            PipedOutputStream pos = new PipedOutputStream();
 
55
            OutputStream os = new Base64EncodeStream(pos);
 
56
 
 
57
            // Copy the input to the Base64 Encoder (in a seperate thread).
 
58
            Thread t = new StreamCopier(inIS, os);
 
59
 
 
60
            // Read that from the piped output stream.
 
61
            inIS = new PipedInputStream(pos);
 
62
            t.start();
 
63
        }
 
64
 
 
65
        if (action.equals("DECODE") || action.equals("ROUND")) {
 
66
            inIS = new Base64DecodeStream(inIS);
 
67
        }
 
68
 
 
69
 
 
70
        int mismatch = compareStreams(inIS, refIS, action.equals("ENCODE"));
 
71
 
 
72
        if (mismatch != -1) {
 
73
            fail("Wrong result");
 
74
        }
 
75
    }
 
76
 
 
77
    private void innerBase64Test(String action, String in, String ref) throws Exception {
 
78
        final String baseURL = "file:test/resources/org/apache/xmlgraphics/util/io/";
 
79
        innerBase64Test(action, new URL(baseURL + in), new URL(baseURL + ref));
 
80
    }
 
81
 
 
82
    private void innerBase64Test(String in) throws Exception {
 
83
        innerBase64Test("ROUND", in, in);
 
84
    }
 
85
 
 
86
    private void testBase64Group(String name) throws Exception {
 
87
        innerBase64Test("ENCODE", name, name + ".64");
 
88
        innerBase64Test("DECODE", name + ".64", name);
 
89
        innerBase64Test(name);
 
90
    }
 
91
 
 
92
    /**
 
93
     * This method will only throw exceptions if some aspect
 
94
     * of the test's internal operation fails.
 
95
     */
 
96
    @Test
 
97
    public void testBase64() throws Exception {
 
98
        System.out.println(new File(".").getCanonicalPath());
 
99
        testBase64Group("zeroByte");
 
100
        testBase64Group("oneByte");
 
101
        testBase64Group("twoByte");
 
102
        testBase64Group("threeByte");
 
103
        testBase64Group("fourByte");
 
104
        testBase64Group("tenByte");
 
105
        testBase64Group("small");
 
106
        testBase64Group("medium");
 
107
        innerBase64Test("DECODE", "medium.pc.64", "medium");
 
108
        innerBase64Test("large");
 
109
}
 
110
 
 
111
    /**
 
112
     * Returns true if the contents of <tt>is1</tt> match the
 
113
     * contents of <tt>is2</tt>
 
114
     */
 
115
    public static int compareStreams(InputStream is1, InputStream is2,
 
116
                              boolean skipws) {
 
117
        byte[] data1 = new byte[100];
 
118
        byte[] data2 = new byte[100];
 
119
        int off1 = 0;
 
120
        int off2 = 0;
 
121
        int idx = 0;
 
122
 
 
123
        try {
 
124
            while (true) {
 
125
                int len1 = is1.read(data1, off1, data1.length - off1);
 
126
                int len2 = is2.read(data2, off2, data2.length - off2);
 
127
 
 
128
                if (off1 != 0) {
 
129
                    if (len1 == -1) {
 
130
                        len1 = off1;
 
131
                    } else {
 
132
                        len1 += off1;
 
133
                    }
 
134
                }
 
135
 
 
136
                if (off2 != 0) {
 
137
                    if (len2 == -1) {
 
138
                        len2 = off2;
 
139
                    } else {
 
140
                        len2 += off2;
 
141
                    }
 
142
                }
 
143
 
 
144
                if (len1 == -1) {
 
145
                    if (len2 == -1) {
 
146
                        break; // Both done...
 
147
                    }
 
148
                    // Only is1 is done...
 
149
                    if (!skipws) {
 
150
                        return idx;
 
151
                    }
 
152
                    // check if the rest of is2 is whitespace...
 
153
                    for (int i2 = 0; i2 < len2; i2++) {
 
154
                        if ((data2[i2] != '\n') && (data2[i2] != '\r') && (data2[i2] != ' ')) {
 
155
                            return idx + i2;
 
156
                        }
 
157
                    }
 
158
                    off1 = off2 = 0;
 
159
                    continue;
 
160
                }
 
161
 
 
162
                if (len2 == -1) {
 
163
                    // Only is2 is done...
 
164
                    if (!skipws) {
 
165
                        return idx;
 
166
                    }
 
167
 
 
168
                    // Check if rest of is1 is whitespace...
 
169
                    for (int i1 = 0; i1 < len1; i1++) {
 
170
                        if ((data1[i1] != '\n') && (data1[i1] != '\r') && (data1[i1] != ' ')) {
 
171
                            return idx + i1;
 
172
                        }
 
173
                    }
 
174
                    off1 = off2 = 0;
 
175
                    continue;
 
176
                }
 
177
 
 
178
                int i1 = 0;
 
179
                int i2 = 0;
 
180
                while ((i1 < len1) && (i2 < len2)) {
 
181
                    if (skipws) {
 
182
                        if ((data1[i1] == '\n') || (data1[i1] == '\r') || (data1[i1] == ' ')) {
 
183
                            i1++;
 
184
                            continue;
 
185
                        }
 
186
                        if ((data2[i2] == '\n') || (data2[i2] == '\r') || (data2[i2] == ' ')) {
 
187
                            i2++;
 
188
                            continue;
 
189
                        }
 
190
                    }
 
191
                    if (data1[i1] != data2[i2]) {
 
192
                        return idx + i2;
 
193
                    }
 
194
 
 
195
                    i1++;
 
196
                    i2++;
 
197
                }
 
198
 
 
199
                if (i1 != len1) {
 
200
                    System.arraycopy(data1, i1, data1, 0, len1 - i1);
 
201
                }
 
202
                if (i2 != len2) {
 
203
                    System.arraycopy(data2, i2, data2, 0, len2 - i2);
 
204
                }
 
205
                off1 = len1 - i1;
 
206
                off2 = len2 - i2;
 
207
                idx += i2;
 
208
            }
 
209
        } catch (IOException ioe) {
 
210
            ioe.printStackTrace();
 
211
            return idx;
 
212
        }
 
213
 
 
214
        return -1;
 
215
    }
 
216
 
 
217
 
 
218
    static class StreamCopier extends Thread {
 
219
        InputStream src;
 
220
        OutputStream dst;
 
221
 
 
222
        public StreamCopier(InputStream src,
 
223
                            OutputStream dst) {
 
224
            this.src = src;
 
225
            this.dst = dst;
 
226
        }
 
227
 
 
228
        public void run() {
 
229
            try {
 
230
                byte[] data = new byte[1000];
 
231
                while (true) {
 
232
                    int len = src.read(data, 0, data.length);
 
233
                    if (len == -1) {
 
234
                        break;
 
235
                    }
 
236
 
 
237
                    dst.write(data, 0, len);
 
238
                }
 
239
            } catch (IOException ioe) {
 
240
                // Nothing
 
241
            }
 
242
            try {
 
243
                dst.close();
 
244
            } catch (IOException ioe) {
 
245
                // Nothing
 
246
            }
 
247
        }
 
248
    }
 
249
}