2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.persisted;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import java.io.IOException;
14 import java.util.List;
15 import org.junit.Test;
17 public class ChunkedOutputStreamTest {
18 private static final int INITIAL_SIZE = 256;
20 private final ChunkedOutputStream stream = new ChunkedOutputStream(INITIAL_SIZE);
23 public void testBasicWrite() throws IOException {
24 for (int i = 0; i < INITIAL_SIZE; ++i) {
28 final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0);
29 assertEquals(INITIAL_SIZE, chunk.length);
30 for (int i = 0; i < INITIAL_SIZE; ++i) {
31 assertEquals((byte) i, chunk[i]);
36 public void testBasicLargeWrite() throws IOException {
37 final byte[] array = createArray(INITIAL_SIZE);
39 final byte[] chunk = assertFinishedStream(INITIAL_SIZE, 1).get(0);
40 assertArrayEquals(array, chunk);
44 public void testGrowWrite() throws IOException {
45 for (int i = 0; i < INITIAL_SIZE * 2; ++i) {
49 final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0);
50 assertEquals(INITIAL_SIZE * 2, chunk.length);
51 for (int i = 0; i < INITIAL_SIZE * 2; ++i) {
52 assertEquals((byte) i, chunk[i]);
57 public void testGrowLargeWrite() throws IOException {
58 final byte[] array = createArray(INITIAL_SIZE * 2);
60 final byte[] chunk = assertFinishedStream(INITIAL_SIZE * 2, 1).get(0);
61 assertArrayEquals(array, chunk);
65 public void testTwoChunksWrite() throws IOException {
66 int size = ChunkedOutputStream.MAX_ARRAY_SIZE + 1;
67 for (int i = 0; i < size; ++i) {
72 for (byte[] chunk: assertFinishedStream(size, 2)) {
73 for (byte actual: chunk) {
74 assertEquals((byte) counter++, actual);
79 private List<byte[]> assertFinishedStream(final int expectedSize, final int expectedChunks) {
81 final ChunkedByteArray array = stream.toChunkedByteArray();
82 assertEquals(expectedSize, array.size());
84 final List<byte[]> chunks = array.getChunks();
85 assertEquals(expectedChunks, chunks.size());
89 private static byte[] createArray(final int size) {
90 final byte[] array = new byte[size];
91 for (int i = 0; i < size; ++i) {