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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
/* $Id: Base64Test.java 750418 2009-03-05 11:03:54Z vhennebert $ */
20
package org.apache.xmlgraphics.util.io;
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;
31
import junit.framework.TestCase;
34
* This test validates that the Base64 encoder/decoders work properly.
36
* @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
37
* @version $Id: Base64Test.java 750418 2009-03-05 11:03:54Z vhennebert $
39
public class Base64Test extends TestCase {
41
private void innerBase64Test(String action, URL in, URL ref) throws Exception {
42
InputStream inIS = in.openStream();
44
if (action.equals("ROUND"))
46
else if (!action.equals("ENCODE") &&
47
!action.equals("DECODE")) {
48
fail("Bad action string");
51
InputStream refIS = ref.openStream();
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);
59
// Copy the input to the Base64 Encoder (in a seperate thread).
60
Thread t = new StreamCopier(inIS, os);
62
// Read that from the piped output stream.
63
inIS = new PipedInputStream(pos);
67
if (action.equals("DECODE")||
68
action.equals("ROUND")) {
69
inIS = new Base64DecodeStream(inIS);
73
int mismatch = compareStreams(inIS, refIS, action.equals("ENCODE"));
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));
85
private void innerBase64Test(String in) throws Exception {
86
innerBase64Test("ROUND", in, in);
89
private void testBase64Group(String name) throws Exception {
90
innerBase64Test("ENCODE", name, name + ".64");
91
innerBase64Test("DECODE", name + ".64", name);
92
innerBase64Test(name);
96
* This method will only throw exceptions if some aspect
97
* of the test's internal operation fails.
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");
114
* Returns true if the contents of <tt>is1</tt> match the
115
* contents of <tt>is2</tt>
117
public static int compareStreams(InputStream is1, InputStream is2,
119
byte[] data1 = new byte[100];
120
byte[] data2 = new byte[100];
127
int len1 = is1.read(data1, off1, data1.length - off1);
128
int len2 = is2.read(data2, off2, data2.length - off2);
146
break; // Both done...
148
// Only is1 is done...
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') &&
163
// Only is2 is done...
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') &&
179
while((i1 < len1) && (i2 < len2)) {
181
if ((data1[i1] == '\n') ||
182
(data1[i1] == '\r') ||
183
(data1[i1] == ' ')) {
187
if ((data2[i2] == '\n') ||
188
(data2[i2] == '\r') ||
189
(data2[i2] == ' ')) {
194
if (data1[i1] != data2[i2])
202
System.arraycopy(data1, i1, data1, 0, len1 - i1);
204
System.arraycopy(data2, i2, data2, 0, len2 - i2);
209
} catch(IOException ioe) {
210
ioe.printStackTrace();
218
static class StreamCopier extends Thread {
222
public StreamCopier(InputStream src,
230
byte[] data = new byte[1000];
232
int len = src.read(data, 0, data.length);
233
if (len == -1) break;
235
dst.write(data, 0, len);
237
} catch (IOException ioe) {
242
} catch (IOException ioe) {