1 | /* |
2 | * jDTAUS Core Test Suite |
3 | * Copyright (C) 2005 Christian Schulte |
4 | * <cs@schulte.it> |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | * |
20 | */ |
21 | package org.jdtaus.core.io.it; |
22 | |
23 | import java.util.Arrays; |
24 | import junit.framework.Assert; |
25 | import junit.framework.TestCase; |
26 | import org.jdtaus.core.io.StructuredFile; |
27 | |
28 | /** |
29 | * Testcase for {@code StructuredFile} implementations. |
30 | * |
31 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
32 | * @version $JDTAUS: StructuredFileTest.java 8853 2014-01-10 13:50:00Z schulte $ |
33 | */ |
34 | public abstract class StructuredFileTest extends TestCase |
35 | { |
36 | //--Constants--------------------------------------------------------------- |
37 | |
38 | /** Value for property {@code blockSize} the tests are run with. */ |
39 | public static final int BLOCK_SIZE = 1; |
40 | |
41 | /** Number of iterations to test. */ |
42 | private static final int ITERATIONS = 20; |
43 | |
44 | /** Value to initialize new blocks with. */ |
45 | private static final byte INIT_CHAR = ( byte ) ' '; |
46 | |
47 | //---------------------------------------------------------------Constants-- |
48 | //--Fields------------------------------------------------------------------ |
49 | |
50 | /** The implementation to test. */ |
51 | private StructuredFile structuredFile; |
52 | |
53 | /** Expected data to read for known written data. */ |
54 | private byte[] expectedData; |
55 | |
56 | //------------------------------------------------------------------Fields-- |
57 | //--StructuredFileTest------------------------------------------------------ |
58 | |
59 | /** Creates a new {@code StructuredFileTest} instance. */ |
60 | public StructuredFileTest() |
61 | { |
62 | super(); |
63 | } |
64 | |
65 | /** |
66 | * Reads the contents of the {@code StructuredFile} implementation |
67 | * beeing tested as an array of byte. |
68 | * |
69 | * @return contents of the {@code StructuredFile} implementation beeing |
70 | * tested. |
71 | */ |
72 | protected abstract byte[] getStructuredData(); |
73 | |
74 | /** |
75 | * Gets a new instance of the {@code StructuredFile} implementation to test. |
76 | * |
77 | * @return a new instance of the {@code StructuredFile} implementation to |
78 | * test. |
79 | */ |
80 | protected abstract StructuredFile getStructuredFile(); |
81 | |
82 | //------------------------------------------------------StructuredFileTest-- |
83 | //--Helpermethods----------------------------------------------------------- |
84 | |
85 | private byte[] getFilledBlock( final int size, final byte fill ) |
86 | { |
87 | final byte[] filled = new byte[ size ]; |
88 | Arrays.fill( filled, fill ); |
89 | return filled; |
90 | } |
91 | |
92 | //-----------------------------------------------------------Helpermethods-- |
93 | //--TestCase---------------------------------------------------------------- |
94 | |
95 | protected void setUp() throws Exception |
96 | { |
97 | this.structuredFile = this.getStructuredFile(); |
98 | this.expectedData = new byte[ StructuredFileTest.BLOCK_SIZE ]; |
99 | Arrays.fill( this.expectedData, StructuredFileTest.INIT_CHAR ); |
100 | } |
101 | |
102 | protected void tearDown() throws Exception |
103 | { |
104 | this.structuredFile = null; |
105 | } |
106 | |
107 | protected void runTest() throws Throwable |
108 | { |
109 | this.testGetBlockSize(); |
110 | this.testGetBlockCount(); |
111 | this.testInsertBlocks(); |
112 | this.testDeleteBlocks(); |
113 | this.testReadWriteBlock(); |
114 | } |
115 | |
116 | //----------------------------------------------------------------TestCase-- |
117 | //--Tests------------------------------------------------------------------- |
118 | |
119 | /** |
120 | * Test of getBlockSize method of class org.jdtaus.common.io.StructuredFile. |
121 | */ |
122 | public void testGetBlockSize() throws Exception |
123 | { |
124 | Assert.assertEquals( StructuredFileTest.BLOCK_SIZE, |
125 | this.structuredFile.getBlockSize() ); |
126 | |
127 | } |
128 | |
129 | /** |
130 | * Test of getBlockCount method of class org.jdtaus.common.io.StructuredFile. |
131 | */ |
132 | public void testGetBlockCount() throws Exception |
133 | { |
134 | Assert.assertEquals( 0L, this.structuredFile.getBlockCount() ); |
135 | } |
136 | |
137 | /** |
138 | * Test of insertBlocks method of class org.jdtaus.common.io.StructuredFile. |
139 | */ |
140 | public void testInsertBlocks() throws Exception |
141 | { |
142 | this.structuredFile.insertBlocks( 0L, 1 ); |
143 | Assert.assertEquals( 1L, this.structuredFile.getBlockCount() ); |
144 | } |
145 | |
146 | /** |
147 | * Test of deleteBlocks method of class org.jdtaus.common.io.StructuredFile. |
148 | */ |
149 | public void testDeleteBlocks() throws Exception |
150 | { |
151 | this.structuredFile.deleteBlocks( 0L, |
152 | this.structuredFile.getBlockCount() ); |
153 | |
154 | Assert.assertEquals( 0L, this.structuredFile.getBlockCount() ); |
155 | } |
156 | |
157 | /** |
158 | * Test of writeBlock method of class org.jdtaus.common.io.StructuredFile. |
159 | */ |
160 | public void testReadWriteBlock() throws Exception |
161 | { |
162 | // final long startTime = System.currentTimeMillis(); |
163 | |
164 | int i; |
165 | int j; |
166 | int written; |
167 | long count; |
168 | long insertIndex; |
169 | long writeIndex; |
170 | long maxIndex; |
171 | long oldBlocks; |
172 | byte[] filled; |
173 | final int blockSize = this.structuredFile.getBlockSize(); |
174 | byte[] newExpectedData = {}; |
175 | byte[] expectedData = {}; |
176 | final byte[] read = new byte[ blockSize ]; |
177 | final byte[] write = new byte[ blockSize ]; |
178 | final byte[] initBlock = new byte[ blockSize ]; |
179 | |
180 | Arrays.fill( initBlock, StructuredFileTest.INIT_CHAR ); |
181 | |
182 | // Creates blocks filled with random data and checks that written |
183 | // data is read unchanged. |
184 | for ( i = 0, insertIndex = 0L; i < |
185 | StructuredFileTest.ITERATIONS; i++, insertIndex++ ) |
186 | { |
187 | |
188 | count = StructuredFileTest.ITERATIONS - insertIndex; |
189 | maxIndex = insertIndex + count; |
190 | filled = |
191 | this.getFilledBlock( ( int ) count * blockSize, ( byte ) i ); |
192 | |
193 | oldBlocks = this.structuredFile.getBlockCount(); |
194 | this.structuredFile.insertBlocks( insertIndex, count ); |
195 | Assert.assertEquals( oldBlocks + count, |
196 | this.structuredFile.getBlockCount() ); |
197 | |
198 | // Initialize new blocks. |
199 | for ( j = 0; j < count; j++ ) |
200 | { |
201 | this.structuredFile.writeBlock( insertIndex + j, 0, |
202 | initBlock ); |
203 | |
204 | } |
205 | newExpectedData = new byte[ expectedData.length + filled.length ]; |
206 | System.arraycopy( expectedData, 0, newExpectedData, 0, |
207 | ( int ) ( insertIndex * blockSize ) ); |
208 | |
209 | System.arraycopy( expectedData, ( int ) ( insertIndex * blockSize ), |
210 | newExpectedData, ( int ) ( maxIndex * blockSize ), |
211 | ( int ) ( expectedData.length - insertIndex * |
212 | blockSize ) ); |
213 | |
214 | Arrays.fill( newExpectedData, ( int ) ( insertIndex * blockSize ), |
215 | ( int ) ( ( insertIndex + count ) * blockSize ), |
216 | StructuredFileTest.INIT_CHAR ); |
217 | |
218 | expectedData = newExpectedData; |
219 | Assert.assertEquals( true, Arrays.equals( |
220 | expectedData, this.getStructuredData() ) ); |
221 | |
222 | maxIndex = insertIndex + count; |
223 | for ( writeIndex = insertIndex, written = 0; writeIndex < maxIndex; |
224 | writeIndex++, written++ ) |
225 | { |
226 | |
227 | System.arraycopy( filled, written * blockSize, write, 0, |
228 | blockSize ); |
229 | |
230 | this.structuredFile.writeBlock( writeIndex, 0, write ); |
231 | this.structuredFile.readBlock( writeIndex, 0, read ); |
232 | Assert.assertEquals( true, Arrays.equals( write, read ) ); |
233 | |
234 | System.arraycopy( write, 0, expectedData, |
235 | ( int ) ( writeIndex * blockSize ), blockSize ); |
236 | |
237 | } |
238 | |
239 | Assert.assertEquals( true, Arrays.equals( |
240 | expectedData, this.getStructuredData() ) ); |
241 | |
242 | } |
243 | |
244 | Assert.assertEquals( true, Arrays.equals( |
245 | expectedData, this.getStructuredData() ) ); |
246 | |
247 | this.structuredFile.deleteBlocks( 0L, |
248 | this.structuredFile.getBlockCount() ); |
249 | |
250 | Assert.assertTrue( this.structuredFile.getBlockCount() == 0L ); |
251 | |
252 | //System.out.println("Ran " + (System.currentTimeMillis() - startTime) + |
253 | // " ms."); |
254 | |
255 | } |
256 | |
257 | //-------------------------------------------------------------------Tests-- |
258 | } |