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