2 * ============LICENSE_START====================================================
\r
4 * ===========================================================================
\r
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
\r
6 * ===========================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END====================================================
\r
22 package org.onap.aaf.misc.env.util;
\r
24 import static org.junit.Assert.assertEquals;
\r
25 import static org.junit.Assert.assertNotNull;
\r
26 import static org.junit.Assert.fail;
\r
28 import java.io.IOException;
\r
30 import org.junit.Before;
\r
31 import org.junit.Test;
\r
33 public class JU_StringBuilderOutputStreamTest {
\r
35 StringBuilderOutputStream streamBuilder;
\r
37 StringBuilder builder = new StringBuilder();
\r
40 public void setUp() throws Exception {
\r
41 streamBuilder = new StringBuilderOutputStream(builder);
\r
45 public void testWriteIntAndReset() {
\r
46 streamBuilder.write(123);
\r
48 assertEquals("123", streamBuilder.toString());
\r
49 streamBuilder.reset();
\r
50 assertEquals("", streamBuilder.toString());
\r
54 public void testWriteByteArrayWithoutException() throws IOException {
\r
55 byte[] bytes = { 1, 2, 3, 4 };
\r
56 streamBuilder.write(bytes);
\r
57 assertEquals(4, streamBuilder.getBuffer().length());
\r
59 streamBuilder.write(bytes, 1, 2);
\r
60 assertEquals(6, streamBuilder.getBuffer().length());
\r
62 streamBuilder.write(bytes, 1, 0);
\r
63 assertEquals(6, streamBuilder.getBuffer().length());
\r
65 streamBuilder.append(bytes[0]);
\r
66 assertEquals(7, streamBuilder.getBuffer().length());
\r
70 public void testWriteByteArrayWithIndexOutOfBoundException() {
\r
71 byte[] bytes = { 1, 2, 3, 4 };
\r
74 streamBuilder.write(bytes, -1, 2);
\r
75 fail("This is supposed to throw IndexOutOfBounds Excetpion");
\r
76 } catch (IndexOutOfBoundsException e) {
\r
77 } catch (Exception e) {
\r
78 fail("This should throw only IndexOutOfBounds Exception");
\r
80 assertEquals(0, streamBuilder.getBuffer().length());
\r
85 public void testDefaultConstructor() throws IOException {
\r
86 StringBuilderOutputStream stream = new StringBuilderOutputStream();
\r
88 assertNotNull(stream.getBuffer());
\r
93 public void testConstructorWithPositiveDefaultCapacity() throws IOException {
\r
94 StringBuilderOutputStream stream = new StringBuilderOutputStream(10);
\r
96 assertNotNull(stream.getBuffer());
\r
97 assertEquals(10, stream.getBuffer().capacity());
\r
102 public void testConstructorWithNegativeCapacityException() {
\r
104 StringBuilderOutputStream stream = new StringBuilderOutputStream(-1);
\r
105 fail("This should throw IllegalArgumentException");
\r
106 } catch (IllegalArgumentException e) {
\r
107 } catch (Exception e) {
\r
108 fail("This should throw only IllegalArgumentException");
\r
113 public void testWriteString() {
\r
114 streamBuilder.write("1234");
\r
116 assertEquals("1234", streamBuilder.toString());
\r
118 streamBuilder.write("1234", 1, 2);
\r
119 assertEquals("12342", streamBuilder.toString());
\r
123 public void testAppendCharSequence() {
\r
124 streamBuilder.append("1234");
\r
125 assertEquals("1234", streamBuilder.toString());
\r
127 streamBuilder.append(null);
\r
128 assertEquals("1234null", streamBuilder.toString());
\r
130 streamBuilder.append("1234", 1, 2);
\r
131 assertEquals("1234null2", streamBuilder.toString());
\r
133 streamBuilder.append(null, 1, 2);
\r
134 assertEquals("1234null2u", streamBuilder.toString());
\r