1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.asn1.ber.digester.rules ;
18  
19  
20  import junit.framework.TestCase ;
21  
22  import java.nio.ByteBuffer ;
23  
24  import org.apache.asn1.ber.digester.rules.ByteAccumulator;
25  
26  
27  /***
28   *  Tests the ByteAccumulator class.
29   *
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev: 157644 $
32   */
33  public class ByteAccumulatorTest extends TestCase
34  {
35      ByteAccumulator accumulator = null ;
36  
37  
38      protected void setUp() throws Exception
39      {
40          super.setUp();
41          accumulator = new ByteAccumulator() ;
42      }
43  
44  
45      protected void tearDown() throws Exception
46      {
47          super.tearDown();
48          accumulator = null ;
49      }
50  
51  
52      public void testByteAccumulatorInt()
53      {
54          ByteAccumulator ba = new ByteAccumulator( 72 ) ;
55          assertEquals( ba.getInitialSize(), ba.getCapacity() ) ;
56          assertEquals( ba.getInitialSize(), ba.getRemainingSpace() ) ;
57          assertEquals( 0, ba.getPosition() ) ;
58  
59          // shuts up clover
60          ba = new ByteAccumulator( -1 ) ;
61          assertEquals( ba.getInitialSize(), ba.getCapacity() ) ;
62          assertEquals( ba.getInitialSize(), ba.getRemainingSpace() ) ;
63          assertEquals( 0, ba.getPosition() ) ;
64      }
65  
66  
67      /***
68       * Fills the buffer by varying amounts so that either the remaining
69       * amount is used for growth increment or the set increment value is
70       * used.  Basically tests the growth behavior and byte accounting.
71       */
72      public void testFill()
73      {
74          assertEquals( accumulator.getInitialSize(),
75                  accumulator.getCapacity() ) ;
76          assertEquals( accumulator.getInitialSize(),
77                  accumulator.getRemainingSpace() ) ;
78          assertEquals( 0, accumulator.getPosition() ) ;
79  
80          ByteBuffer buf = ByteBuffer.allocate( 70 ) ;
81          buf.position( 70 ).flip() ;
82          accumulator.fill( buf ) ;
83          assertEquals( 100, accumulator.getCapacity() ) ;
84          assertEquals( 30, accumulator.getRemainingSpace() ) ;
85          assertEquals( 70, accumulator.getPosition() ) ;
86  
87          buf = ByteBuffer.allocate( 70 ) ;
88          buf.position( 70 ).flip() ;
89          accumulator.fill( buf ) ;
90          assertEquals( 200, accumulator.getCapacity() ) ;
91          assertEquals( 60, accumulator.getRemainingSpace() ) ;
92  
93          buf = ByteBuffer.allocate( 160 ) ;
94          buf.position( 160 ).flip() ;
95          accumulator.fill( buf ) ;
96          assertEquals( 300, accumulator.getCapacity() ) ;
97          assertEquals( 0, accumulator.getRemainingSpace() ) ;
98  
99          buf = ByteBuffer.allocate( 110 ) ;
100         buf.position( 110 ).flip() ;
101         accumulator.fill( buf ) ;
102         assertEquals( 410, accumulator.getCapacity() ) ;
103 
104         // test the trivial case
105         accumulator.fill( buf ) ;
106         assertEquals( 410, accumulator.getCapacity() ) ;
107         assertEquals( 0, accumulator.getRemainingSpace() ) ;
108     }
109 
110 
111     /***
112      * Tests to make sure drains reset the backing store to the initial state
113      * and the right amount of compacted buffer is returned.  Make sure the
114      * inputs are the same as the outputs.
115      */
116     public void testDrain()
117     {
118         ByteBuffer buf = ByteBuffer.allocate( 200 ) ;
119         buf.position( 200 ).flip() ;
120         accumulator.fill( buf ) ;
121         assertEquals( 200, accumulator.getCapacity() ) ;
122         ByteBuffer total = accumulator.drain() ;
123         assertEquals( 200, total.remaining() ) ;
124         assertEquals( 0, accumulator.getCapacity() ) ;
125         assertEquals( 0, accumulator.getRemainingSpace() ) ;
126 
127         buf = ByteBuffer.allocate( 30 ) ;
128         buf.position( 30 ).flip() ;
129         accumulator.fill( buf ) ;
130         assertEquals( 100, accumulator.getCapacity() ) ;
131         assertEquals( 70, accumulator.getRemainingSpace() ) ;
132         total = accumulator.drain() ;
133         assertEquals( 30, total.remaining() ) ;
134         assertEquals( 0, accumulator.getCapacity() ) ;
135         assertEquals( 0, accumulator.getRemainingSpace() ) ;
136 
137         buf = ByteBuffer.allocate( 3 ) ;
138         buf.put( (byte) 0x01 ) ;
139         buf.put( (byte) 0x02 ) ;
140         buf.put( (byte) 0x03 ) ;
141         buf.flip() ;
142         accumulator.fill( buf ) ;
143         assertEquals( 100, accumulator.getCapacity() ) ;
144         assertEquals( 97, accumulator.getRemainingSpace() ) ;
145         buf = accumulator.drain() ;
146         assertEquals( 0, accumulator.getCapacity() ) ;
147         assertEquals( 0, accumulator.getRemainingSpace() ) ;
148         assertEquals( 0x01, buf.get() ) ;
149         assertEquals( 0x02, buf.get() ) ;
150         assertEquals( 0x03, buf.get() ) ;
151         assertFalse( buf.hasRemaining() ) ;
152 
153         ByteAccumulator ba = new ByteAccumulator( 1 ) ;
154         ba.fill( ByteBuffer.allocate( 1 ) ) ;
155         buf = ba.drain() ;
156         buf.get() ;
157         assertFalse( buf.hasRemaining() ) ;
158     }
159 
160 
161     public void testDrainInt0()
162     {
163         accumulator.fill( ByteBuffer.allocate( 200 ) ) ;
164         accumulator.drain( -1 ) ;
165         assertEquals( 0, accumulator.getCapacity() ) ;
166         assertEquals( 0, accumulator.getRemainingSpace() ) ;
167     }
168 
169 
170     public void testDrainInt1()
171     {
172         accumulator.fill( ByteBuffer.allocate( 200 ) ) ;
173         accumulator.drain( 10 ) ;
174         assertEquals( 10, accumulator.getCapacity() ) ;
175         assertEquals( 10, accumulator.getRemainingSpace() ) ;
176     }
177 
178 
179     public void testDrainInt2()
180     {
181         accumulator.fill( ByteBuffer.allocate( 20 ) ) ;
182         accumulator.drain( 10 ) ;
183         assertEquals( 10, accumulator.getCapacity() ) ;
184         assertEquals( 10, accumulator.getRemainingSpace() ) ;
185     }
186 
187 
188     public void testEnsureCapacity()
189     {
190         accumulator.ensureCapacity( 150 ) ;
191         accumulator.ensureCapacity( 10 ) ;
192         assertEquals( 150, accumulator.getCapacity() ) ;
193         assertEquals( 150, accumulator.getRemainingSpace() ) ;
194         accumulator.ensureCapacity( 222 ) ;
195         assertEquals( 222, accumulator.getCapacity() ) ;
196         assertEquals( 222, accumulator.getRemainingSpace() ) ;
197     }
198 
199 
200     public void testGrowthIncrement()
201     {
202         int increment = accumulator.getGrowthIncrement() ;
203         ByteBuffer buf = ByteBuffer.allocate(
204                 accumulator.getInitialSize() + 1 ) ;
205         buf.position( buf.capacity() ).flip() ;
206         accumulator.fill( buf ) ;
207         assertEquals( increment + accumulator.getInitialSize(),
208                 accumulator.getCapacity() ) ;
209     }
210 }