001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ASCII85InputStreamTestCase.java 426584 2006-07-28 16:01:47Z jeremias $ */
019:
020: package org.apache.xmlgraphics.util.io;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025:
026: import org.apache.commons.io.IOUtils;
027: import org.apache.commons.io.output.ByteArrayOutputStream;
028:
029: import org.apache.xmlgraphics.util.HexUtil;
030:
031: import junit.framework.TestCase;
032:
033: /**
034: * Test case for ASCII85InputStream.
035: * <p>
036: * ATTENTION: Some of the tests here depend on the correct behaviour of
037: * ASCII85OutputStream. If something fails here make sure
038: * ASCII85OutputStreamTestCase runs!
039: */
040: public class ASCII85InputStreamTestCase extends TestCase {
041:
042: private static final boolean DEBUG = false;
043:
044: /**
045: * @see junit.framework.TestCase#TestCase(String)
046: */
047: public ASCII85InputStreamTestCase(String name) {
048: super (name);
049: }
050:
051: private byte[] decode(String text) throws Exception {
052: byte[] ascii85 = text.getBytes("US-ASCII");
053: InputStream in = new ByteArrayInputStream(ascii85);
054: InputStream decoder = new ASCII85InputStream(in);
055: return IOUtils.toByteArray(decoder);
056: }
057:
058: private byte[] getChunk(int count) {
059: byte[] buf = new byte[count];
060: System.arraycopy(ASCII85OutputStreamTestCase.DATA, 0, buf, 0,
061: buf.length);
062: return buf;
063: }
064:
065: private String encode(byte[] data, int len) throws Exception {
066: ByteArrayOutputStream baout = new ByteArrayOutputStream();
067: java.io.OutputStream out = new ASCII85OutputStream(baout);
068: out.write(data, 0, len);
069: out.close();
070: return new String(baout.toByteArray(), "US-ASCII");
071: }
072:
073: private void innerTestDecode(byte[] data) throws Exception {
074: String encoded = encode(data, data.length);
075: if (DEBUG) {
076: if (data[0] == 0) {
077: System.out.println("self-encode: " + data.length
078: + " chunk 000102030405...");
079: } else {
080: System.out.println("self-encode: "
081: + new String(data, "US-ASCII") + " "
082: + HexUtil.toHex(data));
083: }
084: System.out.println(" ---> " + encoded);
085: }
086: byte[] decoded = decode(encoded);
087: if (DEBUG) {
088: if (data[0] == 0) {
089: System.out.println("decoded: " + data.length
090: + " chunk 000102030405...");
091: } else {
092: System.out.println("decoded: "
093: + new String(decoded, "US-ASCII") + " "
094: + HexUtil.toHex(decoded));
095: }
096: }
097: assertEquals(HexUtil.toHex(data), HexUtil.toHex(decoded));
098: }
099:
100: /**
101: * Tests the output of ASCII85.
102: * @throws Exception if an error occurs
103: */
104: public void testDecode() throws Exception {
105: byte[] buf;
106: innerTestDecode("1. Bodypart".getBytes("US-ASCII"));
107: if (DEBUG) {
108: System.out
109: .println("===========================================");
110: }
111:
112: innerTestDecode(getChunk(1));
113: innerTestDecode(getChunk(2));
114: innerTestDecode(getChunk(3));
115: innerTestDecode(getChunk(4));
116: innerTestDecode(getChunk(5));
117: if (DEBUG) {
118: System.out
119: .println("===========================================");
120: }
121:
122: innerTestDecode(getChunk(10));
123: innerTestDecode(getChunk(62));
124: innerTestDecode(getChunk(63));
125: innerTestDecode(getChunk(64));
126: innerTestDecode(getChunk(65));
127:
128: if (DEBUG) {
129: System.out
130: .println("===========================================");
131: }
132: String sz;
133: sz = HexUtil.toHex(decode("zz~>"));
134: assertEquals(HexUtil
135: .toHex(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }), sz);
136: sz = HexUtil.toHex(decode("z\t \0z\n~>"));
137: assertEquals(HexUtil
138: .toHex(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }), sz);
139: if (DEBUG) {
140: System.out
141: .println("===========================================");
142: }
143: try {
144: decode("vz~>");
145: fail("Illegal character should be detected");
146: } catch (IOException ioe) {
147: //expected
148: }
149: /* DISABLED because of try/catch in InputStream.read(byte[], int, int).
150: * Only the exception happening on the first byte in a block is being
151: * reported. BUG in JDK???
152: *
153: try {
154: decode("zv~>");
155: fail("Illegal character should be detected");
156: } catch (IOException ioe) {
157: //expected
158: }*/
159: }
160:
161: private byte[] getFullASCIIRange() {
162: java.io.ByteArrayOutputStream baout = new java.io.ByteArrayOutputStream(
163: 256);
164: for (int i = 254; i < 256; i++) {
165: baout.write(i);
166: }
167: return baout.toByteArray();
168: }
169:
170: /**
171: * Tests the full 8-bit ASCII range.
172: * @throws Exception if an error occurs
173: */
174: public void testFullASCIIRange() throws Exception {
175: innerTestDecode(getFullASCIIRange());
176: }
177:
178: }
|