You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2011/10/26 12:56:26 UTC

svn commit: r1189127 - in /commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils: ./ CountingStreamTest.java

Author: bodewig
Date: Wed Oct 26 10:56:25 2011
New Revision: 1189127

URL: http://svn.apache.org/viewvc?rev=1189127&view=rev
Log:
Tests for Counting(In|Out)putStream

Added:
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/CountingStreamTest.java   (with props)

Added: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/CountingStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/CountingStreamTest.java?rev=1189127&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/CountingStreamTest.java (added)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/CountingStreamTest.java Wed Oct 26 10:56:25 2011
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.utils;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import org.junit.Test;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class CountingStreamTest {
+
+    @Test
+    public void output() throws Exception {
+        // I don't like "test all at once" tests either, but the class
+        // is soo trivial
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        CountingOutputStream o = new CountingOutputStream(bos);
+        o.write(1);
+        assertEquals(1, o.getBytesWritten());
+        o.write(new byte[] { 2, 3 });
+        assertEquals(3, o.getBytesWritten());
+        o.write(new byte[] { 2, 3, 4, 5, }, 2, 1);
+        assertEquals(4, o.getBytesWritten());
+        o.count(-1);
+        assertEquals(4, o.getBytesWritten());
+        o.count(-2);
+        assertEquals(2, o.getBytesWritten());
+        o.close();
+        assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bos.toByteArray());
+    }
+
+    @Test
+    public void input() throws Exception {
+        // I don't like "test all at once" tests either, but the class
+        // is soo trivial
+        ByteArrayInputStream bis =
+            new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 });
+        CountingInputStream i = new CountingInputStream(bis);
+        assertEquals(1, i.read());
+        assertEquals(1, i.getBytesRead());
+        byte[] b = new byte[2];
+        i.read(b);
+        assertEquals(3, i.getBytesRead());
+        assertArrayEquals(new byte[] { 2, 3 }, b);
+        b = new byte[3];
+        i.read(b, 1, 1);
+        assertArrayEquals(new byte[] { 0, 4, 0 }, b);
+        assertEquals(4, i.getBytesRead());
+        i.count(-1);
+        assertEquals(4, i.getBytesRead());
+        i.count(-2);
+        assertEquals(2, i.getBytesRead());
+        i.close();
+    }
+
+}

Propchange: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/utils/CountingStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native