\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
+ 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