1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.jdkim.canon;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
28
29 public class CompoundOutputStreamTest extends AbstractOutputStreamTestCase {
30
31 private byte[] testData;
32
33 protected void setUp() throws Exception {
34 testData = new byte[4096];
35 for (int i = 0; i < testData.length; i++) {
36 testData[i] = (byte) ((i * i * 4095 + (testData.length - i) * 17) % 128);
37 }
38 }
39
40 public void testSingleBytes() throws NoSuchAlgorithmException, IOException {
41 List/* ByteArrayOutputStream */oss = new LinkedList();
42 for (int i = 0; i < 5; i++) {
43 oss.add(new ByteArrayOutputStream());
44 }
45 CompoundOutputStream os = new CompoundOutputStream(oss);
46 for (int i = 0; i < testData.length; i++) {
47 os.write(testData[i]);
48 }
49 os.close();
50 for (Iterator i = oss.iterator(); i.hasNext();) {
51 ByteArrayOutputStream bos = (ByteArrayOutputStream) i.next();
52 assertArrayEquals(testData, bos.toByteArray());
53 }
54 }
55
56 public void testChunks() throws NoSuchAlgorithmException, IOException {
57 List/* ByteArrayOutputStream */oss = new LinkedList();
58 for (int i = 0; i < 5; i++) {
59 oss.add(new ByteArrayOutputStream());
60 }
61 CompoundOutputStream os = new CompoundOutputStream(oss);
62 chunker(testData, os);
63 for (Iterator i = oss.iterator(); i.hasNext();) {
64 ByteArrayOutputStream bos = (ByteArrayOutputStream) i.next();
65 assertArrayEquals(testData, bos.toByteArray());
66 }
67 }
68
69 }