Adding more Junits to misc env
[aaf/authz.git] / misc / env / src / test / java / org / onap / aaf / misc / env / util / JU_StringBuilderOutputStreamTest.java
diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java b/misc/env/src/test/java/org/onap/aaf/misc/env/util/JU_StringBuilderOutputStreamTest.java
new file mode 100644 (file)
index 0000000..377a289
--- /dev/null
@@ -0,0 +1,135 @@
+/**\r
+ * ============LICENSE_START====================================================\r
+ * org.onap.aaf\r
+ * ===========================================================================\r
+ * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
+ * ===========================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END====================================================\r
+ *\r
+ */\r
+package org.onap.aaf.misc.env.util;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.fail;\r
+\r
+import java.io.IOException;\r
+\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+\r
+public class JU_StringBuilderOutputStreamTest {\r
+\r
+       StringBuilderOutputStream streamBuilder;\r
+\r
+       StringBuilder builder = new StringBuilder();\r
+\r
+       @Before\r
+       public void setUp() throws Exception {\r
+               streamBuilder = new StringBuilderOutputStream(builder);\r
+       }\r
+\r
+       @Test\r
+       public void testWriteIntAndReset() {\r
+               streamBuilder.write(123);\r
+\r
+               assertEquals("123", streamBuilder.toString());\r
+               streamBuilder.reset();\r
+               assertEquals("", streamBuilder.toString());\r
+       }\r
+\r
+       @Test\r
+       public void testWriteByteArrayWithoutException() throws IOException {\r
+               byte[] bytes = { 1, 2, 3, 4 };\r
+               streamBuilder.write(bytes);\r
+               assertEquals(4, streamBuilder.getBuffer().length());\r
+\r
+               streamBuilder.write(bytes, 1, 2);\r
+               assertEquals(6, streamBuilder.getBuffer().length());\r
+\r
+               streamBuilder.write(bytes, 1, 0);\r
+               assertEquals(6, streamBuilder.getBuffer().length());\r
+\r
+               streamBuilder.append(bytes[0]);\r
+               assertEquals(7, streamBuilder.getBuffer().length());\r
+       }\r
+\r
+       @Test\r
+       public void testWriteByteArrayWithIndexOutOfBoundException() {\r
+               byte[] bytes = { 1, 2, 3, 4 };\r
+\r
+               try {\r
+                       streamBuilder.write(bytes, -1, 2);\r
+                       fail("This is supposed to throw IndexOutOfBounds Excetpion");\r
+               } catch (IndexOutOfBoundsException e) {\r
+               } catch (Exception e) {\r
+                       fail("This should throw only IndexOutOfBounds Exception");\r
+               }\r
+               assertEquals(0, streamBuilder.getBuffer().length());\r
+\r
+       }\r
+\r
+       @Test\r
+       public void testDefaultConstructor() throws IOException {\r
+               StringBuilderOutputStream stream = new StringBuilderOutputStream();\r
+\r
+               assertNotNull(stream.getBuffer());\r
+               stream.close();\r
+       }\r
+\r
+       @Test\r
+       public void testConstructorWithPositiveDefaultCapacity() throws IOException {\r
+               StringBuilderOutputStream stream = new StringBuilderOutputStream(10);\r
+\r
+               assertNotNull(stream.getBuffer());\r
+               assertEquals(10, stream.getBuffer().capacity());\r
+               stream.close();\r
+       }\r
+\r
+       @Test\r
+       public void testConstructorWithNegativeCapacityException() {\r
+               try {\r
+                       StringBuilderOutputStream stream = new StringBuilderOutputStream(-1);\r
+                       fail("This should throw IllegalArgumentException");\r
+               } catch (IllegalArgumentException e) {\r
+               } catch (Exception e) {\r
+                       fail("This should throw only IllegalArgumentException");\r
+               }\r
+       }\r
+\r
+       @Test\r
+       public void testWriteString() {\r
+               streamBuilder.write("1234");\r
+\r
+               assertEquals("1234", streamBuilder.toString());\r
+\r
+               streamBuilder.write("1234", 1, 2);\r
+               assertEquals("12342", streamBuilder.toString());\r
+       }\r
+\r
+       @Test\r
+       public void testAppendCharSequence() {\r
+               streamBuilder.append("1234");\r
+               assertEquals("1234", streamBuilder.toString());\r
+\r
+               streamBuilder.append(null);\r
+               assertEquals("1234null", streamBuilder.toString());\r
+\r
+               streamBuilder.append("1234", 1, 2);\r
+               assertEquals("1234null2", streamBuilder.toString());\r
+\r
+               streamBuilder.append(null, 1, 2);\r
+               assertEquals("1234null2u", streamBuilder.toString());\r
+       }\r
+}\r