377a2891bc96d2b31a9fcf867a9c5f7c88ca4f3f
[aaf/authz.git] / misc / env / src / test / java / org / onap / aaf / misc / env / util / JU_StringBuilderOutputStreamTest.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\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
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \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
19  *\r
20  */\r
21 package org.onap.aaf.misc.env.util;\r
22 \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
26 \r
27 import java.io.IOException;\r
28 \r
29 import org.junit.Before;\r
30 import org.junit.Test;\r
31 \r
32 public class JU_StringBuilderOutputStreamTest {\r
33 \r
34         StringBuilderOutputStream streamBuilder;\r
35 \r
36         StringBuilder builder = new StringBuilder();\r
37 \r
38         @Before\r
39         public void setUp() throws Exception {\r
40                 streamBuilder = new StringBuilderOutputStream(builder);\r
41         }\r
42 \r
43         @Test\r
44         public void testWriteIntAndReset() {\r
45                 streamBuilder.write(123);\r
46 \r
47                 assertEquals("123", streamBuilder.toString());\r
48                 streamBuilder.reset();\r
49                 assertEquals("", streamBuilder.toString());\r
50         }\r
51 \r
52         @Test\r
53         public void testWriteByteArrayWithoutException() throws IOException {\r
54                 byte[] bytes = { 1, 2, 3, 4 };\r
55                 streamBuilder.write(bytes);\r
56                 assertEquals(4, streamBuilder.getBuffer().length());\r
57 \r
58                 streamBuilder.write(bytes, 1, 2);\r
59                 assertEquals(6, streamBuilder.getBuffer().length());\r
60 \r
61                 streamBuilder.write(bytes, 1, 0);\r
62                 assertEquals(6, streamBuilder.getBuffer().length());\r
63 \r
64                 streamBuilder.append(bytes[0]);\r
65                 assertEquals(7, streamBuilder.getBuffer().length());\r
66         }\r
67 \r
68         @Test\r
69         public void testWriteByteArrayWithIndexOutOfBoundException() {\r
70                 byte[] bytes = { 1, 2, 3, 4 };\r
71 \r
72                 try {\r
73                         streamBuilder.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
78                 }\r
79                 assertEquals(0, streamBuilder.getBuffer().length());\r
80 \r
81         }\r
82 \r
83         @Test\r
84         public void testDefaultConstructor() throws IOException {\r
85                 StringBuilderOutputStream stream = new StringBuilderOutputStream();\r
86 \r
87                 assertNotNull(stream.getBuffer());\r
88                 stream.close();\r
89         }\r
90 \r
91         @Test\r
92         public void testConstructorWithPositiveDefaultCapacity() throws IOException {\r
93                 StringBuilderOutputStream stream = new StringBuilderOutputStream(10);\r
94 \r
95                 assertNotNull(stream.getBuffer());\r
96                 assertEquals(10, stream.getBuffer().capacity());\r
97                 stream.close();\r
98         }\r
99 \r
100         @Test\r
101         public void testConstructorWithNegativeCapacityException() {\r
102                 try {\r
103                         StringBuilderOutputStream stream = new StringBuilderOutputStream(-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
108                 }\r
109         }\r
110 \r
111         @Test\r
112         public void testWriteString() {\r
113                 streamBuilder.write("1234");\r
114 \r
115                 assertEquals("1234", streamBuilder.toString());\r
116 \r
117                 streamBuilder.write("1234", 1, 2);\r
118                 assertEquals("12342", streamBuilder.toString());\r
119         }\r
120 \r
121         @Test\r
122         public void testAppendCharSequence() {\r
123                 streamBuilder.append("1234");\r
124                 assertEquals("1234", streamBuilder.toString());\r
125 \r
126                 streamBuilder.append(null);\r
127                 assertEquals("1234null", streamBuilder.toString());\r
128 \r
129                 streamBuilder.append("1234", 1, 2);\r
130                 assertEquals("1234null2", streamBuilder.toString());\r
131 \r
132                 streamBuilder.append(null, 1, 2);\r
133                 assertEquals("1234null2u", streamBuilder.toString());\r
134         }\r
135 }\r