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
61
62
63
64
65
66 protected abstract byte[] getStructuredData();
67
68
69
70
71
72
73
74 protected abstract StructuredFile getStructuredFile();
75
76
77
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
98
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
122
123
124
125
126
127 public void testGetBlockSize() throws Exception
128 {
129 Assert.assertEquals( StructuredFileTest.BLOCK_SIZE,
130 this.structuredFile.getBlockSize() );
131
132 }
133
134
135
136
137 public void testGetBlockCount() throws Exception
138 {
139 Assert.assertEquals( 0L, this.structuredFile.getBlockCount() );
140 }
141
142
143
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
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
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
188
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
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
258
259
260 }
261
262
263 }