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

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/util/io/Base64Test.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: Base64Test.java 750418 2009-03-05 11:03:54Z vhennebert $ */
19
 
 
20
 
package org.apache.xmlgraphics.util.io;
21
 
 
22
 
import java.io.File;
23
 
import java.io.PipedOutputStream;
24
 
import java.io.PipedInputStream;
25
 
import java.io.InputStream;
26
 
import java.io.OutputStream;
27
 
import java.io.IOException;
28
 
 
29
 
import java.net.URL;
30
 
 
31
 
import junit.framework.TestCase;
32
 
 
33
 
/**
34
 
 * This test validates that the Base64 encoder/decoders work properly.
35
 
 *
36
 
 * @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
37
 
 * @version $Id: Base64Test.java 750418 2009-03-05 11:03:54Z vhennebert $
38
 
 */
39
 
public class Base64Test extends TestCase {
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") &&
47
 
                 !action.equals("DECODE")) {
48
 
            fail("Bad action string");
49
 
        }
50
 
 
51
 
        InputStream refIS = ref.openStream();
52
 
 
53
 
        if (action.equals("ENCODE") ||
54
 
            action.equals("ROUND")) {
55
 
            // We need to encode the incomming data
56
 
            PipedOutputStream pos = new PipedOutputStream();
57
 
            OutputStream os = new Base64EncodeStream(pos);
58
 
 
59
 
            // Copy the input to the Base64 Encoder (in a seperate thread).
60
 
            Thread t = new StreamCopier(inIS, os);
61
 
 
62
 
            // Read that from the piped output stream.
63
 
            inIS = new PipedInputStream(pos);
64
 
            t.start();
65
 
        }
66
 
 
67
 
        if (action.equals("DECODE")||
68
 
            action.equals("ROUND")) {
69
 
            inIS = new Base64DecodeStream(inIS);
70
 
        }
71
 
 
72
 
 
73
 
        int mismatch = compareStreams(inIS, refIS, action.equals("ENCODE"));
74
 
 
75
 
        if (mismatch != -1) {
76
 
            fail("Wrong result");
77
 
        }
78
 
    }
79
 
 
80
 
    private void innerBase64Test(String action, String in, String ref) throws Exception {
81
 
        final String baseURL = "file:test/resources/org/apache/xmlgraphics/util/io/";
82
 
        innerBase64Test(action, new URL(baseURL + in), new URL(baseURL + ref));
83
 
    }
84
 
 
85
 
    private void innerBase64Test(String in) throws Exception {
86
 
        innerBase64Test("ROUND", in, in);
87
 
    }
88
 
 
89
 
    private void testBase64Group(String name) throws Exception {
90
 
        innerBase64Test("ENCODE", name, name + ".64");
91
 
        innerBase64Test("DECODE", name + ".64", name);
92
 
        innerBase64Test(name);
93
 
    }
94
 
 
95
 
    /**
96
 
     * This method will only throw exceptions if some aspect
97
 
     * of the test's internal operation fails.
98
 
     */
99
 
    public void testBase64() throws Exception {
100
 
        System.out.println(new File(".").getCanonicalPath());
101
 
        testBase64Group("zeroByte");
102
 
        testBase64Group("oneByte");
103
 
        testBase64Group("twoByte");
104
 
        testBase64Group("threeByte");
105
 
        testBase64Group("fourByte");
106
 
        testBase64Group("tenByte");
107
 
        testBase64Group("small");
108
 
        testBase64Group("medium");
109
 
        innerBase64Test("DECODE", "medium.pc.64", "medium");
110
 
        innerBase64Test("large");
111
 
}
112
 
 
113
 
    /**
114
 
     * Returns true if the contents of <tt>is1</tt> match the
115
 
     * contents of <tt>is2</tt>
116
 
     */
117
 
    public static int compareStreams(InputStream is1, InputStream is2,
118
 
                              boolean skipws) {
119
 
        byte[] data1 = new byte[100];
120
 
        byte[] data2 = new byte[100];
121
 
        int off1 = 0;
122
 
        int off2 = 0;
123
 
        int idx = 0;
124
 
 
125
 
        try {
126
 
            while (true) {
127
 
                int len1 = is1.read(data1, off1, data1.length - off1);
128
 
                int len2 = is2.read(data2, off2, data2.length - off2);
129
 
 
130
 
                if (off1 != 0) {
131
 
                    if (len1 == -1)
132
 
                        len1 = off1;
133
 
                    else
134
 
                        len1 += off1;
135
 
                }
136
 
 
137
 
                if (off2 != 0) {
138
 
                    if (len2 == -1)
139
 
                        len2 = off2;
140
 
                    else
141
 
                        len2 += off2;
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') &&
155
 
                            (data2[i2] != '\r') &&
156
 
                            (data2[i2] != ' '))
157
 
                            return idx + i2;
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
 
                    // Check if rest of is1 is whitespace...
168
 
                    for (int i1 = 0; i1 < len1; i1++)
169
 
                        if ((data1[i1] != '\n') &&
170
 
                            (data1[i1] != '\r') &&
171
 
                            (data1[i1] != ' '))
172
 
                            return idx + i1;
173
 
                    off1 = off2 = 0;
174
 
                    continue;
175
 
                }
176
 
 
177
 
                int i1=0;
178
 
                int i2=0;
179
 
                while((i1 < len1) && (i2 < len2)) {
180
 
                    if (skipws) {
181
 
                        if ((data1[i1] == '\n') ||
182
 
                            (data1[i1] == '\r') ||
183
 
                            (data1[i1] == ' ')) {
184
 
                            i1++;
185
 
                            continue;
186
 
                        }
187
 
                        if ((data2[i2] == '\n') ||
188
 
                            (data2[i2] == '\r') ||
189
 
                            (data2[i2] == ' ')) {
190
 
                            i2++;
191
 
                            continue;
192
 
                        }
193
 
                    }
194
 
                    if (data1[i1] != data2[i2])
195
 
                        return idx+i2;
196
 
 
197
 
                    i1++;
198
 
                    i2++;
199
 
                }
200
 
 
201
 
                if (i1 != len1)
202
 
                    System.arraycopy(data1, i1, data1, 0, len1 - i1);
203
 
                if (i2 != len2)
204
 
                    System.arraycopy(data2, i2, data2, 0, len2 - i2);
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) break;
234
 
 
235
 
                    dst.write(data, 0, len);
236
 
                }
237
 
            } catch (IOException ioe) {
238
 
                // Nothing
239
 
            }
240
 
            try {
241
 
                dst.close();
242
 
            } catch (IOException ioe) {
243
 
                // Nothing
244
 
            }
245
 
        }
246
 
    }
247
 
}