1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: RecordBufferManagerTest.java 372 2005-12-28 04:22:43Z mat007 $
29   */
30  
31  package palmed.buffer;
32  
33  import java.io.ByteArrayInputStream;
34  import java.io.IOException;
35  import palmed.file.IFile;
36  import palmed.util.EasyMockTestCase;
37  
38  /***
39   * RecordBufferManager test case.
40   *
41   * @author Mathieu Champlon
42   * @version $Revision: 372 $ $Date: 2005-12-28 13:22:43 +0900 (mer., 28 déc. 2005) $
43   */
44  public class RecordBufferManagerTest extends EasyMockTestCase
45  {
46      /***
47       * Tested object.
48       */
49      private RecordBufferManager manager_;
50      /***
51       * Mock objects.
52       */
53      private IBufferFactory mockFactory_;
54      private IRecordBuffer mockBuffer_, mockBuffer2_;
55      private IFile mockFile_;
56  
57      protected void setUp() throws IOException
58      {
59          mockFactory_ = (IBufferFactory)createMock( IBufferFactory.class );
60          mockBuffer_ = (IRecordBuffer)createMock( IRecordBuffer.class );
61          mockBuffer2_ = (IRecordBuffer)createMock( IRecordBuffer.class );
62          mockFile_ = (IFile)createMock( IFile.class );
63          manager_ = new RecordBufferManager( mockFactory_ );
64          forceBufferCreation();
65      }
66  
67      private void forceBufferCreation() throws IOException
68      {
69          reset();
70          mockFactory_.createBuffer();
71          control( mockFactory_ ).setReturnValue( mockBuffer_ );
72          mockBuffer_.restore();
73          replay();
74          manager_.unmarshall( new ByteArrayInputStream( new byte[0] ) );
75          verify();
76      }
77  
78      public void testNextBufferWithOnlyOneBufferIsNoOp() throws Exception
79      {
80          replay();
81          manager_.next();
82      }
83  
84      public void testPreviousBufferWithOnlyOneBufferIsNoOp() throws Exception
85      {
86          replay();
87          manager_.previous();
88      }
89  
90      public void testOpenAddsAnotherBuffer() throws Exception
91      {
92          mockBuffer_.persist();
93          mockFactory_.createBuffer();
94          control( mockFactory_ ).setReturnValue( mockBuffer2_ );
95          replay();
96          manager_.open();
97      }
98  
99      public void testCloseLastBufferOpensNewOne() throws Exception
100     {
101         mockBuffer_.delete();
102         mockFactory_.createBuffer();
103         control( mockFactory_ ).setReturnValue( mockBuffer2_ );
104         mockBuffer2_.restore();
105         replay();
106         manager_.close();
107     }
108 
109     public void testOpenAndCloseBuffer() throws Exception
110     {
111         mockBuffer_.persist();
112         mockFactory_.createBuffer();
113         control( mockFactory_ ).setReturnValue( mockBuffer2_ );
114         replay();
115         manager_.open();
116         reset();
117         mockBuffer2_.delete();
118         mockBuffer_.restore();
119         replay();
120         manager_.close();
121     }
122 
123     public void testOpenAndNextSwapsBuffers() throws Exception
124     {
125         mockBuffer_.persist();
126         mockFactory_.createBuffer();
127         control( mockFactory_ ).setReturnValue( mockBuffer2_ );
128         replay();
129         manager_.open();
130         reset();
131         mockBuffer2_.persist();
132         mockBuffer_.restore();
133         replay();
134         manager_.next();
135     }
136 
137     public void testOpenFileNotAlreadyOpenedCreatesBuffer() throws Exception
138     {
139         mockBuffer_.persist();
140         mockBuffer_.hasFile( mockFile_ );
141         control( mockBuffer_ ).setReturnValue( false );
142         mockFactory_.createBuffer();
143         mockBuffer_.open( mockFile_ );
144         control( mockFactory_ ).setReturnValue( mockBuffer_ );
145         replay();
146         manager_.open( mockFile_ );
147     }
148 
149     public void testOpenFileAlreadyOpenedSwitchesToItsBuffer() throws Exception
150     {
151         mockBuffer_.persist();
152         mockBuffer_.hasFile( mockFile_ );
153         control( mockBuffer_ ).setReturnValue( true );
154         mockBuffer_.restore();
155         replay();
156         manager_.open( mockFile_ );
157     }
158 }