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