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
21 package org.onap.aaf.misc.env.util;
\r
23 import static org.junit.Assert.assertEquals;
\r
24 import static org.junit.Assert.assertNotNull;
\r
25 import static org.junit.Assert.fail;
\r
27 import java.io.IOException;
\r
29 import org.junit.Before;
\r
30 import org.junit.Test;
\r
32 public class JU_StringBuilderWriterTest {
\r
34 StringBuilderWriter streamWriter;
\r
36 StringBuilder builder = new StringBuilder();
\r
39 public void setUp() throws Exception {
\r
40 streamWriter = new StringBuilderWriter(builder);
\r
44 public void testWriteIntAndReset() {
\r
45 streamWriter.write(1);
\r
47 assertEquals(1, streamWriter.getBuffer().length());
\r
48 streamWriter.reset();
\r
49 assertEquals("", streamWriter.toString());
\r
53 public void testWriteByteArrayWithoutException() throws IOException {
\r
54 char[] bytes = { 1, 2, 3, 4 };
\r
55 streamWriter.write(bytes);
\r
56 assertEquals(4, streamWriter.getBuffer().length());
\r
58 streamWriter.write(bytes, 1, 2);
\r
59 assertEquals(6, streamWriter.getBuffer().length());
\r
61 streamWriter.write(bytes, 1, 0);
\r
62 assertEquals(6, streamWriter.getBuffer().length());
\r
64 streamWriter.append(bytes[0]);
\r
65 assertEquals(7, streamWriter.getBuffer().length());
\r
69 public void testWriteByteArrayWithIndexOutOfBoundException() {
\r
70 char[] bytes = { 1, 2, 3, 4 };
\r
73 streamWriter.write(bytes, -1, 2);
\r
74 fail("This is supposed to throw IndexOutOfBounds Excetpion");
\r
75 } catch (IndexOutOfBoundsException e) {
\r
76 } catch (Exception e) {
\r
77 fail("This should throw only IndexOutOfBounds Exception");
\r
79 assertEquals(0, streamWriter.getBuffer().length());
\r
84 public void testDefaultConstructor() throws IOException {
\r
85 StringBuilderWriter stream = new StringBuilderWriter();
\r
87 assertNotNull(stream.getBuffer());
\r
92 public void testConstructorWithPositiveDefaultCapacity() throws IOException {
\r
93 StringBuilderWriter stream = new StringBuilderWriter(10);
\r
95 assertNotNull(stream.getBuffer());
\r
96 assertEquals(10, stream.getBuffer().capacity());
\r
101 public void testConstructorWithNegativeCapacityException() {
\r
103 StringBuilderWriter stream = new StringBuilderWriter(-1);
\r
104 fail("This should throw IllegalArgumentException");
\r
105 } catch (IllegalArgumentException e) {
\r
106 } catch (Exception e) {
\r
107 fail("This should throw only IllegalArgumentException");
\r
112 public void testWriteString() {
\r
113 streamWriter.write("1234");
\r
115 assertEquals("1234", streamWriter.toString());
\r
117 streamWriter.write("1234", 1, 2);
\r
118 assertEquals("123423", streamWriter.toString());
\r
122 public void testAppendCharSequence() {
\r
123 streamWriter.append("1234");
\r
124 assertEquals("1234", streamWriter.toString());
\r
126 streamWriter.append(null);
\r
127 assertEquals("1234null", streamWriter.toString());
\r
129 streamWriter.append("1234", 1, 2);
\r
130 assertEquals("1234null2", streamWriter.toString());
\r
132 streamWriter.append(null, 1, 2);
\r
133 assertEquals("1234null2u", streamWriter.toString());
\r