1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
30
31
32
33
34 public abstract class StructuredFileTest extends TestCase
35 {
36
37
38
39 public static final int BLOCK_SIZE = 1;
40
41
42 private static final int ITERATIONS = 20;
43
44
45 private static final byte INIT_CHAR = ( byte ) ' ';
46
47
48
49
50
51 private StructuredFile structuredFile;
52
53
54 private byte[] expectedData;
55
56
57
58
59
60 public StructuredFileTest()
61 {
62 super();
63 }
64
65
66
67
68
69
70
71
72 protected abstract byte[] getStructuredData();
73
74
75
76
77
78
79
80 protected abstract StructuredFile getStructuredFile();
81
82
83
84
85 private byte[] getRandomBlock( final int size )
86 {
87 final byte[] random = new byte[ size ];
88 for ( int i = 0; i < random.length; i++ )
89 {
90 random[i] = ( byte ) ( Math.random() * 0xFF );
91 }
92
93 return random;
94 }
95
96 private byte[] getFilledBlock( final int size, final byte fill )
97 {
98 final byte[] filled = new byte[ size ];
99 Arrays.fill( filled, fill );
100 return filled;
101 }
102
103
104
105
106 protected void setUp() throws Exception
107 {
108 this.structuredFile = this.getStructuredFile();
109 this.expectedData = new byte[ StructuredFileTest.BLOCK_SIZE ];
110 Arrays.fill( this.expectedData, StructuredFileTest.INIT_CHAR );
111 }
112
113 protected void tearDown() throws Exception
114 {
115 this.structuredFile = null;
116 }
117
118 protected void runTest() throws Throwable
119 {
120 this.testGetBlockSize();
121 this.testGetBlockCount();
122 this.testInsertBlocks();
123 this.testDeleteBlocks();
124 this.testReadWriteBlock();
125 }
126
127
128
129
130
131
132
133 public void testGetBlockSize() throws Exception
134 {
135 Assert.assertEquals( StructuredFileTest.BLOCK_SIZE,
136 this.structuredFile.getBlockSize() );
137
138 }
139
140
141
142
143 public void testGetBlockCount() throws Exception
144 {
145 Assert.assertEquals( 0L, this.structuredFile.getBlockCount() );
146 }
147
148
149
150
151 public void testInsertBlocks() throws Exception
152 {
153 this.structuredFile.insertBlocks( 0L, 1 );
154 Assert.assertEquals( 1L, this.structuredFile.getBlockCount() );
155 }
156
157
158
159
160 public void testDeleteBlocks() throws Exception
161 {
162 this.structuredFile.deleteBlocks( 0L,
163 this.structuredFile.getBlockCount() );
164
165 Assert.assertEquals( 0L, this.structuredFile.getBlockCount() );
166 }
167
168
169
170
171 public void testReadWriteBlock() throws Exception
172 {
173 final long startTime = System.currentTimeMillis();
174
175 int i;
176 int j;
177 int written;
178 long count;
179 long insertIndex;
180 long writeIndex;
181 long maxIndex;
182 long oldBlocks;
183 byte[] filled;
184 final int blockSize = this.structuredFile.getBlockSize();
185 byte[] newExpectedData = {};
186 byte[] expectedData = {};
187 final byte[] read = new byte[ blockSize ];
188 final byte[] write = new byte[ blockSize ];
189 final byte[] initBlock = new byte[ blockSize ];
190
191 Arrays.fill( initBlock, StructuredFileTest.INIT_CHAR );
192
193
194
195 for ( i = 0, insertIndex = 0L; i <
196 StructuredFileTest.ITERATIONS; i++, insertIndex++ )
197 {
198
199 count = StructuredFileTest.ITERATIONS - insertIndex;
200 maxIndex = insertIndex + count;
201 filled =
202 this.getFilledBlock( ( int ) count * blockSize, ( byte ) i );
203
204 oldBlocks = this.structuredFile.getBlockCount();
205 this.structuredFile.insertBlocks( insertIndex, count );
206 Assert.assertEquals( oldBlocks + count,
207 this.structuredFile.getBlockCount() );
208
209
210 for ( j = 0; j < count; j++ )
211 {
212 this.structuredFile.writeBlock( insertIndex + j, 0,
213 initBlock );
214
215 }
216 newExpectedData = new byte[ expectedData.length + filled.length ];
217 System.arraycopy( expectedData, 0, newExpectedData, 0,
218 ( int ) ( insertIndex * blockSize ) );
219
220 System.arraycopy( expectedData, ( int ) ( insertIndex * blockSize ),
221 newExpectedData, ( int ) ( maxIndex * blockSize ),
222 ( int ) ( expectedData.length - insertIndex *
223 blockSize ) );
224
225 Arrays.fill( newExpectedData, ( int ) ( insertIndex * blockSize ),
226 ( int ) ( ( insertIndex + count ) * blockSize ),
227 StructuredFileTest.INIT_CHAR );
228
229 expectedData = newExpectedData;
230 Assert.assertEquals( true, Arrays.equals(
231 expectedData, this.getStructuredData() ) );
232
233 maxIndex = insertIndex + count;
234 for ( writeIndex = insertIndex, written = 0; writeIndex < maxIndex;
235 writeIndex++, written++ )
236 {
237
238 System.arraycopy( filled, written * blockSize, write, 0,
239 blockSize );
240
241 this.structuredFile.writeBlock( writeIndex, 0, write );
242 this.structuredFile.readBlock( writeIndex, 0, read );
243 Assert.assertEquals( true, Arrays.equals( write, read ) );
244
245 System.arraycopy( write, 0, expectedData,
246 ( int ) ( writeIndex * blockSize ), blockSize );
247
248 }
249
250 Assert.assertEquals( true, Arrays.equals(
251 expectedData, this.getStructuredData() ) );
252
253 }
254
255 Assert.assertEquals( true, Arrays.equals(
256 expectedData, this.getStructuredData() ) );
257
258 this.structuredFile.deleteBlocks( 0L,
259 this.structuredFile.getBlockCount() );
260
261 Assert.assertTrue( this.structuredFile.getBlockCount() == 0L );
262
263
264
265
266 }
267
268
269 }