Rehost Chunked{ByteArray,InputStream,OutputStream}
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / io / ChunkedOutputStreamTest.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.io;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import java.io.IOException;
14 import java.util.List;
15 import org.junit.Test;
16
17 public class ChunkedOutputStreamTest {
18     private static final int INITIAL_SIZE = 256;
19     private static final int MAX_ARRAY_SIZE = 256 * 1024;
20
21     private final ChunkedOutputStream stream = new ChunkedOutputStream(INITIAL_SIZE, MAX_ARRAY_SIZE);
22
23     @Test
24     public void testBasicWrite() throws IOException {
25         for (int i = 0; i < INITIAL_SIZE; ++i) {
26             stream.write(i);
27         }
28
29         final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0);
30         assertEquals(INITIAL_SIZE, chunk.length);
31         for (int i = 0; i < INITIAL_SIZE; ++i) {
32             assertEquals((byte) i, chunk[i]);
33         }
34     }
35
36     @Test
37     public void testBasicLargeWrite() throws IOException {
38         final byte[] array = createArray(INITIAL_SIZE);
39         stream.write(array);
40         final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0);
41         assertArrayEquals(array, chunk);
42     }
43
44     @Test
45     public void testGrowWrite() throws IOException {
46         for (int i = 0; i < INITIAL_SIZE * 2; ++i) {
47             stream.write(i);
48         }
49
50         final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0);
51         assertEquals(INITIAL_SIZE * 2, chunk.length);
52         for (int i = 0; i < INITIAL_SIZE * 2; ++i) {
53             assertEquals((byte) i, chunk[i]);
54         }
55     }
56
57     @Test
58     public void testGrowLargeWrite() throws IOException {
59         final byte[] array = createArray(INITIAL_SIZE * 2);
60         stream.write(array);
61         final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0);
62         assertArrayEquals(array, chunk);
63     }
64
65     @Test
66     public void testTwoChunksWrite() throws IOException {
67         int size = MAX_ARRAY_SIZE + 1;
68         for (int i = 0; i < size; ++i) {
69             stream.write(i);
70         }
71
72         int counter = 0;
73         for (byte[] chunk: assertFinishedStream(size, 2)) {
74             for (byte actual: chunk) {
75                 assertEquals((byte) counter++, actual);
76             }
77         }
78     }
79
80     private List<byte[]> assertFinishedStream(final int expectedSize, final int expectedChunks) {
81         stream.close();
82         final ChunkedByteArray array = stream.toChunkedByteArray();
83         assertEquals(expectedSize, array.size());
84
85         final List<byte[]> chunks = array.getChunks();
86         assertEquals(expectedChunks, chunks.size());
87         return chunks;
88     }
89
90     private static byte[] createArray(final int size) {
91         final byte[] array = new byte[size];
92         for (int i = 0; i < size; ++i) {
93             array[i] = (byte) i;
94         }
95         return array;
96     }
97 }