Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / misc / env / src / test / java / org / onap / aaf / misc / env / util / JU_StringBuilderWriterTest.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_StringBuilderWriterTest {\r
33 \r
34     StringBuilderWriter streamWriter;\r
35 \r
36     StringBuilder builder = new StringBuilder();\r
37 \r
38     @Before\r
39     public void setUp() throws Exception {\r
40         streamWriter = new StringBuilderWriter(builder);\r
41     }\r
42 \r
43     @Test\r
44     public void testWriteIntAndReset() {\r
45         streamWriter.write(1);\r
46 \r
47         assertEquals(1, streamWriter.getBuffer().length());\r
48         streamWriter.reset();\r
49         assertEquals("", streamWriter.toString());\r
50     }\r
51 \r
52     @Test\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
57 \r
58         streamWriter.write(bytes, 1, 2);\r
59         assertEquals(6, streamWriter.getBuffer().length());\r
60 \r
61         streamWriter.write(bytes, 1, 0);\r
62         assertEquals(6, streamWriter.getBuffer().length());\r
63 \r
64         streamWriter.append(bytes[0]);\r
65         assertEquals(7, streamWriter.getBuffer().length());\r
66     }\r
67 \r
68     @Test\r
69     public void testWriteByteArrayWithIndexOutOfBoundException() {\r
70         char[] bytes = { 1, 2, 3, 4 };\r
71 \r
72         try {\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
78         }\r
79         assertEquals(0, streamWriter.getBuffer().length());\r
80 \r
81     }\r
82 \r
83     @Test\r
84     public void testDefaultConstructor() throws IOException {\r
85         StringBuilderWriter stream = new StringBuilderWriter();\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         StringBuilderWriter stream = new StringBuilderWriter(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             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
108         }\r
109     }\r
110 \r
111     @Test\r
112     public void testWriteString() {\r
113         streamWriter.write("1234");\r
114 \r
115         assertEquals("1234", streamWriter.toString());\r
116 \r
117         streamWriter.write("1234", 1, 2);\r
118         assertEquals("123423", streamWriter.toString());\r
119     }\r
120 \r
121     @Test\r
122     public void testAppendCharSequence() {\r
123         streamWriter.append("1234");\r
124         assertEquals("1234", streamWriter.toString());\r
125 \r
126         streamWriter.append(null);\r
127         assertEquals("1234null", streamWriter.toString());\r
128 \r
129         streamWriter.append("1234", 1, 2);\r
130         assertEquals("1234null2", streamWriter.toString());\r
131 \r
132         streamWriter.append(null, 1, 2);\r
133         assertEquals("1234null2u", streamWriter.toString());\r
134     }\r
135 }\r