View Javadoc
1   package org.apache.commons.jcs3.admin;
2   
3   
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * Tests for the counting only output stream.
28   */
29  public class CountingStreamUnitTest
30      extends TestCase
31  {
32  
33      /**
34       * Write a single byte and verify the count.
35       *
36       * @throws Exception
37       */
38      public void testSingleByte() throws Exception
39      {
40          final CountingOnlyOutputStream out = new CountingOnlyOutputStream();
41          out.write( 1 );
42          assertEquals( "Wrong number of bytes written.", 1, out.getCount() );
43          out.write( 1 );
44          assertEquals( "Wrong number of bytes written.", 2, out.getCount() );
45          out.close();
46      }
47  
48      /**
49       * This should count the size of the array.
50       *
51       * @throws Exception
52       */
53      public void testByteArray() throws Exception
54      {
55          final CountingOnlyOutputStream out = new CountingOnlyOutputStream();
56          final byte[] array = new byte[]{1,2,3,4,5};
57          out.write( array );
58          assertEquals( "Wrong number of bytes written.", array.length, out.getCount() );
59          out.close();
60      }
61  
62      /**
63       * This should count the len -- the third arg
64       *
65       * @throws Exception
66       */
67      public void testByteArrayLenCount() throws Exception
68      {
69          final CountingOnlyOutputStream out = new CountingOnlyOutputStream();
70          final byte[] array = new byte[]{1,2,3,4,5};
71          final int len = 3;
72          out.write( array, 0, len );
73          assertEquals( "Wrong number of bytes written.", len, out.getCount() );
74          out.close();
75      }
76  }