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 8641 2012-09-27 06:45:17Z 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 | /** |
60 | * Reads the contents of the {@code StructuredFile} implementation |
61 | * beeing tested as an array of byte. |
62 | * |
63 | * @return contents of the {@code StructuredFile} implementation beeing |
64 | * tested. |
65 | */ |
66 | protected abstract byte[] getStructuredData(); |
67 | |
68 | /** |
69 | * Gets a new instance of the {@code StructuredFile} implementation to test. |
70 | * |
71 | * @return a new instance of the {@code StructuredFile} implementation to |
72 | * test. |
73 | */ |
74 | protected abstract StructuredFile getStructuredFile(); |
75 | |
76 | //------------------------------------------------------StructuredFileTest-- |
77 | //--Helpermethods----------------------------------------------------------- |
78 | |
79 | private byte[] getRandomBlock( final int size ) |
80 | { |
81 | final byte[] random = new byte[ size ]; |
82 | for ( int i = 0; i < random.length; i++ ) |
83 | { |
84 | random[i] = ( byte ) ( Math.random() * 0xFF ); |
85 | } |
86 | |
87 | return random; |
88 | } |
89 | |
90 | private byte[] getFilledBlock( final int size, final byte fill ) |
91 | { |
92 | final byte[] filled = new byte[ size ]; |
93 | Arrays.fill( filled, fill ); |
94 | return filled; |
95 | } |
96 | |
97 | //-----------------------------------------------------------Helpermethods-- |
98 | //--TestCase---------------------------------------------------------------- |
99 | |
100 | protected void setUp() throws Exception |
101 | { |
102 | this.structuredFile = this.getStructuredFile(); |
103 | this.expectedData = new byte[ StructuredFileTest.BLOCK_SIZE ]; |
104 | Arrays.fill( this.expectedData, StructuredFileTest.INIT_CHAR ); |
105 | } |
106 | |
107 | protected void tearDown() throws Exception |
108 | { |
109 | this.structuredFile = null; |
110 | } |
111 | |
112 | protected void runTest() throws Throwable |
113 | { |
114 | this.testGetBlockSize(); |
115 | this.testGetBlockCount(); |
116 | this.testInsertBlocks(); |
117 | this.testDeleteBlocks(); |
118 | this.testReadWriteBlock(); |
119 | } |
120 | |
121 | //----------------------------------------------------------------TestCase-- |
122 | //--Tests------------------------------------------------------------------- |
123 | |
124 | /** |
125 | * Test of getBlockSize method of class org.jdtaus.common.io.StructuredFile. |
126 | */ |
127 | public void testGetBlockSize() throws Exception |
128 | { |
129 | Assert.assertEquals( StructuredFileTest.BLOCK_SIZE, |
130 | this.structuredFile.getBlockSize() ); |
131 | |
132 | } |
133 | |
134 | /** |
135 | * Test of getBlockCount method of class org.jdtaus.common.io.StructuredFile. |
136 | */ |
137 | public void testGetBlockCount() throws Exception |
138 | { |
139 | Assert.assertEquals( 0L, this.structuredFile.getBlockCount() ); |
140 | } |
141 | |
142 | /** |
143 | * Test of insertBlocks method of class org.jdtaus.common.io.StructuredFile. |
144 | */ |
145 | public void testInsertBlocks() throws Exception |
146 | { |
147 | this.structuredFile.insertBlocks( 0L, 1 ); |
148 | Assert.assertEquals( 1L, this.structuredFile.getBlockCount() ); |
149 | } |
150 | |
151 | /** |
152 | * Test of deleteBlocks method of class org.jdtaus.common.io.StructuredFile. |
153 | */ |
154 | public void testDeleteBlocks() throws Exception |
155 | { |
156 | this.structuredFile.deleteBlocks( 0L, |
157 | this.structuredFile.getBlockCount() ); |
158 | |
159 | Assert.assertEquals( 0L, this.structuredFile.getBlockCount() ); |
160 | } |
161 | |
162 | /** |
163 | * Test of writeBlock method of class org.jdtaus.common.io.StructuredFile. |
164 | */ |
165 | public void testReadWriteBlock() throws Exception |
166 | { |
167 | final long startTime = System.currentTimeMillis(); |
168 | |
169 | int i; |
170 | int j; |
171 | int written; |
172 | long count; |
173 | long insertIndex; |
174 | long writeIndex; |
175 | long maxIndex; |
176 | long oldBlocks; |
177 | byte[] filled; |
178 | final int blockSize = this.structuredFile.getBlockSize(); |
179 | byte[] newExpectedData = {}; |
180 | byte[] expectedData = {}; |
181 | final byte[] read = new byte[ blockSize ]; |
182 | final byte[] write = new byte[ blockSize ]; |
183 | final byte[] initBlock = new byte[ blockSize ]; |
184 | |
185 | Arrays.fill( initBlock, StructuredFileTest.INIT_CHAR ); |
186 | |
187 | // Creates blocks filled with random data and checks that written |
188 | // data is read unchanged. |
189 | for ( i = 0, insertIndex = 0L; i < |
190 | StructuredFileTest.ITERATIONS; i++, insertIndex++ ) |
191 | { |
192 | |
193 | count = StructuredFileTest.ITERATIONS - insertIndex; |
194 | maxIndex = insertIndex + count; |
195 | filled = |
196 | this.getFilledBlock( ( int ) count * blockSize, ( byte ) i ); |
197 | |
198 | oldBlocks = this.structuredFile.getBlockCount(); |
199 | this.structuredFile.insertBlocks( insertIndex, count ); |
200 | Assert.assertEquals( oldBlocks + count, |
201 | this.structuredFile.getBlockCount() ); |
202 | |
203 | // Initialize new blocks. |
204 | for ( j = 0; j < count; j++ ) |
205 | { |
206 | this.structuredFile.writeBlock( insertIndex + j, 0, |
207 | initBlock ); |
208 | |
209 | } |
210 | newExpectedData = new byte[ expectedData.length + filled.length ]; |
211 | System.arraycopy( expectedData, 0, newExpectedData, 0, |
212 | ( int ) ( insertIndex * blockSize ) ); |
213 | |
214 | System.arraycopy( expectedData, ( int ) ( insertIndex * blockSize ), |
215 | newExpectedData, ( int ) ( maxIndex * blockSize ), |
216 | ( int ) ( expectedData.length - insertIndex * |
217 | blockSize ) ); |
218 | |
219 | Arrays.fill( newExpectedData, ( int ) ( insertIndex * blockSize ), |
220 | ( int ) ( ( insertIndex + count ) * blockSize ), |
221 | StructuredFileTest.INIT_CHAR ); |
222 | |
223 | expectedData = newExpectedData; |
224 | Assert.assertEquals( true, Arrays.equals( |
225 | expectedData, this.getStructuredData() ) ); |
226 | |
227 | maxIndex = insertIndex + count; |
228 | for ( writeIndex = insertIndex, written = 0; writeIndex < maxIndex; |
229 | writeIndex++, written++ ) |
230 | { |
231 | |
232 | System.arraycopy( filled, written * blockSize, write, 0, |
233 | blockSize ); |
234 | |
235 | this.structuredFile.writeBlock( writeIndex, 0, write ); |
236 | this.structuredFile.readBlock( writeIndex, 0, read ); |
237 | Assert.assertEquals( true, Arrays.equals( write, read ) ); |
238 | |
239 | System.arraycopy( write, 0, expectedData, |
240 | ( int ) ( writeIndex * blockSize ), blockSize ); |
241 | |
242 | } |
243 | |
244 | Assert.assertEquals( true, Arrays.equals( |
245 | expectedData, this.getStructuredData() ) ); |
246 | |
247 | } |
248 | |
249 | Assert.assertEquals( true, Arrays.equals( |
250 | expectedData, this.getStructuredData() ) ); |
251 | |
252 | this.structuredFile.deleteBlocks( 0L, |
253 | this.structuredFile.getBlockCount() ); |
254 | |
255 | Assert.assertTrue( this.structuredFile.getBlockCount() == 0L ); |
256 | |
257 | //System.out.println("Ran " + (System.currentTimeMillis() - startTime) + |
258 | // " ms."); |
259 | |
260 | } |
261 | |
262 | //-------------------------------------------------------------------Tests-- |
263 | } |