1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
.indexer
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertTrue
;
20 import java
.io
.IOException
;
21 import java
.io
.RandomAccessFile
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Random
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.indexer
.ICheckpointCollection
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLongLocation
;
31 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
32 import org
.junit
.After
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
37 * Common code for ICheckpointCollection test classes
39 * @author Marc-Andre Laperle
41 public abstract class AbstractCheckpointCollectionTest
{
43 private static final String INDEX_FILE_NAME
= "checkpoint.idx"; //$NON-NLS-1$
46 * The number of checkpoints to be inserted in insert tests
48 protected static final int CHECKPOINTS_INSERT_NUM
= 50000;
50 * The collection being tested
52 protected ICheckpointCollection fCheckpointCollection
= null;
54 private TmfTraceStub fTrace
;
55 private File fFile
= new File(INDEX_FILE_NAME
);
58 * Setup the test. Make sure the index is deleted.
62 fTrace
= new TmfTraceStub();
66 fCheckpointCollection
= createCollection();
70 * Tear down the test. Make sure the index is deleted.
73 public void tearDown() {
76 if (fCheckpointCollection
!= null) {
77 fCheckpointCollection
.dispose();
85 * Get the trace being tested.
87 * @return the trace being tested.
89 public ITmfTrace
getTrace() {
94 * Returns whether or not the collection is persisted to disk
96 * @return true if the collection is persisted to disk, false otherwise
98 public boolean isPersistableCollection() {
103 * Get the file used for the index being tested.
105 * @return the file used for the index being tested.
107 public File
getFile() {
112 * Test constructing a new checkpoint collection
115 public void testConstructor() {
116 if (isPersistableCollection()) {
117 assertTrue(fFile
.exists());
119 assertTrue(fCheckpointCollection
.isCreatedFromScratch());
123 * Test constructing a new checkpoint collection, existing file
126 public void testConstructorExistingFile() {
127 if (isPersistableCollection()) {
128 assertTrue(fFile
.exists());
129 fCheckpointCollection
.dispose();
131 fCheckpointCollection
= createCollection();
132 assertFalse(fCheckpointCollection
.isCreatedFromScratch());
137 * Test that a new checkpoint collection is considered created from scratch
141 public void testIsCreatedFromScratch() {
142 assertTrue(fCheckpointCollection
.isCreatedFromScratch());
144 if (isPersistableCollection()) {
145 fCheckpointCollection
.dispose();
146 fCheckpointCollection
= createCollection();
147 assertFalse(fCheckpointCollection
.isCreatedFromScratch());
152 * Test setTimeRange, getTimeRange
155 public void testSetGetTimeRange() {
156 if (isPersistableCollection()) {
157 TmfTimeRange timeRange
= new TmfTimeRange(new TmfTimestamp(0), new TmfTimestamp(100));
158 fCheckpointCollection
.setTimeRange(timeRange
);
159 assertEquals(timeRange
, fCheckpointCollection
.getTimeRange());
164 * Create a collection for the test
166 * @return the collection
168 abstract protected ICheckpointCollection
createCollection();
171 * Test setNbEvents, getNbEvents
174 public void testSetGetNbEvents() {
175 if (isPersistableCollection()) {
176 int expected
= 12345;
177 fCheckpointCollection
.setNbEvents(expected
);
178 assertEquals(expected
, fCheckpointCollection
.getNbEvents());
186 public void testGetSize() {
187 assertEquals(0, fCheckpointCollection
.size());
188 int expected
= CHECKPOINTS_INSERT_NUM
;
189 for (int i
= 0; i
< expected
; ++i
) {
190 fCheckpointCollection
.insert(new TmfCheckpoint(new TmfTimestamp(i
), new TmfLongLocation(i
), i
));
192 assertEquals(expected
, fCheckpointCollection
.size());
199 public void testDelete() {
200 if (isPersistableCollection()) {
201 assertTrue(fFile
.exists());
202 fCheckpointCollection
.delete();
203 assertFalse(fFile
.exists());
208 * Test version change
210 * @throws IOException
214 public void testVersionChange() throws IOException
{
215 fCheckpointCollection
.dispose();
216 try (RandomAccessFile f
= new RandomAccessFile(fFile
, "rw");) {
220 fCheckpointCollection
= createCollection();
221 assertTrue(fCheckpointCollection
.isCreatedFromScratch());
225 * Test version change
227 * @throws IOException
231 public void testDeleteWhenInvalidBug479675() throws IOException
{
233 try (RandomAccessFile f
= new RandomAccessFile(fFile
, "rw");) {
237 fCheckpointCollection
= createCollection();
238 if (isPersistableCollection()) {
239 ICheckpointCollection old
= fCheckpointCollection
;
241 fCheckpointCollection
= createCollection();
242 assertEquals(0, fCheckpointCollection
.size());
247 assertTrue(fCheckpointCollection
.isCreatedFromScratch());
251 * Test a single insertion
254 public void testInsert() {
255 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0);
256 fCheckpointCollection
.insert(checkpoint
);
258 long found
= fCheckpointCollection
.binarySearch(checkpoint
);
259 assertEquals(0, found
);
260 assertEquals(1, fCheckpointCollection
.size());
264 * Generate many checkpoints and insert them in the collection
266 * @return the list of generated checkpoints
268 protected ArrayList
<Integer
> insertAlot() {
269 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
270 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345 + i
), new TmfLongLocation(123456L + i
), i
);
271 fCheckpointCollection
.insert(checkpoint
);
274 assertEquals(CHECKPOINTS_INSERT_NUM
, fCheckpointCollection
.size());
275 if (isPersistableCollection()) {
276 fCheckpointCollection
.dispose();
279 boolean random
= true;
280 ArrayList
<Integer
> list
= new ArrayList
<>();
281 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
283 Random rand
= new Random();
284 list
.add(rand
.nextInt(CHECKPOINTS_INSERT_NUM
));
293 * Test many checkpoint insertions. Make sure they can be found after
294 * re-opening the file
297 public void testInsertAlot() {
298 ArrayList
<Integer
> list
= insertAlot();
300 if (isPersistableCollection()) {
301 fCheckpointCollection
= createCollection();
303 assertEquals(CHECKPOINTS_INSERT_NUM
, fCheckpointCollection
.size());
305 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
306 Integer randomCheckpoint
= list
.get(i
);
307 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345 + randomCheckpoint
), new TmfLongLocation(123456L + randomCheckpoint
), 0);
308 long found
= fCheckpointCollection
.binarySearch(checkpoint
);
309 assertEquals(randomCheckpoint
.intValue(), found
);
312 assertEquals(CHECKPOINTS_INSERT_NUM
, fCheckpointCollection
.size());
316 * Test many checkpoint insertions using the same timestamp. Make sure they
317 * can be found after re-opening the file
320 public void testInsertSameTimestamp() {
321 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
322 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L + i
), i
);
323 fCheckpointCollection
.insert(checkpoint
);
325 assertEquals(CHECKPOINTS_INSERT_NUM
, fCheckpointCollection
.size());
327 if (isPersistableCollection()) {
328 fCheckpointCollection
.dispose();
331 boolean random
= true;
332 ArrayList
<Integer
> list
= new ArrayList
<>();
333 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
335 Random rand
= new Random();
336 list
.add(rand
.nextInt(CHECKPOINTS_INSERT_NUM
));
342 if (isPersistableCollection()) {
343 fCheckpointCollection
= createCollection();
345 assertEquals(CHECKPOINTS_INSERT_NUM
, fCheckpointCollection
.size());
347 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
348 Integer randomCheckpoint
= list
.get(i
);
349 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L + randomCheckpoint
), 0);
350 long found
= fCheckpointCollection
.binarySearch(checkpoint
);
351 assertEquals(randomCheckpoint
.intValue(), found
);
356 * Tests that binarySearch find the correct checkpoint when the time stamp
357 * is between checkpoints
360 public void testBinarySearchFindInBetween() {
361 for (long i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
362 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(2 * i
), new TmfLongLocation(2 * i
), i
);
363 fCheckpointCollection
.insert(checkpoint
);
365 assertEquals(CHECKPOINTS_INSERT_NUM
, fCheckpointCollection
.size());
367 TmfCheckpoint searchedCheckpoint
= new TmfCheckpoint(new TmfTimestamp(123), new TmfLongLocation(123L), 123);
368 int expectedInsertionPoint
= 61;
369 int expectedRank
= -(expectedInsertionPoint
+ 2);
371 long rank
= fCheckpointCollection
.binarySearch(searchedCheckpoint
);
372 assertEquals(expectedRank
, rank
);
377 * Tests that binarySearch finds the correct checkpoint when searching for a
378 * checkpoint with a null location. It should return the previous checkpoint
379 * from the first checkpoint that matches the timestamp.
382 public void testBinarySearchInBetweenSameTimestamp() {
383 int checkpointNum
= 0;
384 for (; checkpointNum
< CHECKPOINTS_INSERT_NUM
/ 2; checkpointNum
++) {
385 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(0), new TmfLongLocation(checkpointNum
), checkpointNum
);
386 fCheckpointCollection
.insert(checkpoint
);
389 for (; checkpointNum
< CHECKPOINTS_INSERT_NUM
; checkpointNum
++) {
390 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(1), new TmfLongLocation(checkpointNum
), checkpointNum
);
391 fCheckpointCollection
.insert(checkpoint
);
393 assertEquals(CHECKPOINTS_INSERT_NUM
, fCheckpointCollection
.size());
395 final TmfCheckpoint searchedCheckpoint
= new TmfCheckpoint(new TmfTimestamp(1), null, 0);
397 long found
= fCheckpointCollection
.binarySearch(searchedCheckpoint
);
399 int expectedInsertionPoint
= CHECKPOINTS_INSERT_NUM
/ 2 - 1;
400 int expectedRank
= -(expectedInsertionPoint
+ 2);
402 assertEquals(expectedRank
, found
);
406 * Test checkpoint insertions after reopening the file.
409 public void testInsertAfterReopen() {
410 if (!isPersistableCollection()) {
414 fCheckpointCollection
.insert(new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0));
416 assertEquals(1, fCheckpointCollection
.size());
418 fCheckpointCollection
.dispose();
419 fCheckpointCollection
= createCollection();
420 assertEquals(1, fCheckpointCollection
.size());
422 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0);
423 long found
= fCheckpointCollection
.binarySearch(checkpoint
);
424 assertEquals(0, found
);
426 fCheckpointCollection
.insert(new TmfCheckpoint(new TmfTimestamp(12345 + 1), new TmfLongLocation(123456L + 1), 1));
427 assertEquals(2, fCheckpointCollection
.size());
429 fCheckpointCollection
.dispose();
430 fCheckpointCollection
= createCollection();
431 assertEquals(2, fCheckpointCollection
.size());
435 * Test that a checkpoint can be inserted after reopening an empty index.
438 public void testInsertAfterEmptyReopen() {
439 fCheckpointCollection
.dispose();
440 fCheckpointCollection
= createCollection();
441 fCheckpointCollection
.insert(new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0));
442 assertEquals(1, fCheckpointCollection
.size());