1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adapted for TMF Trace Model 1.0
12 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
.indexer
.checkpoint
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertTrue
;
21 import java
.io
.IOException
;
22 import java
.net
.URISyntaxException
;
24 import java
.util
.List
;
26 import org
.eclipse
.core
.runtime
.FileLocator
;
27 import org
.eclipse
.core
.runtime
.Path
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
36 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
37 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
38 import org
.junit
.After
;
39 import org
.junit
.Before
;
40 import org
.junit
.Test
;
43 * Test suite for the TmfCheckpointIndexTest class.
45 @SuppressWarnings("javadoc")
46 public class TmfCheckpointIndexTest
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 private static final String DIRECTORY
= "testfiles";
53 private static final String TEST_STREAM
= "A-Test-10K";
54 private static final int BLOCK_SIZE
= 100;
55 private static final int NB_EVENTS
= 10000;
56 private static TestTrace fTrace
= null;
57 private static EmptyTestTrace fEmptyTrace
= null;
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
65 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
69 public void tearDown() {
72 fEmptyTrace
.dispose();
76 // ------------------------------------------------------------------------
78 // ------------------------------------------------------------------------
80 private static class TestIndexer
extends TmfCheckpointIndexer
{
81 @SuppressWarnings({ })
82 public TestIndexer(TestTrace testTrace
) {
83 super(testTrace
, BLOCK_SIZE
);
85 @SuppressWarnings({ })
86 public TestIndexer(EmptyTestTrace testTrace
) {
87 super(testTrace
, BLOCK_SIZE
);
89 public List
<ITmfCheckpoint
> getCheckpoints() {
90 return getTraceIndex();
94 private class TestTrace
extends TmfTraceStub
{
95 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
96 super(path
, blockSize
);
97 setIndexer(new TestIndexer(this));
100 public TestIndexer
getIndexer() {
101 return (TestIndexer
) super.getIndexer();
105 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
106 public EmptyTestTrace() {
108 setIndexer(new TestIndexer(this));
111 public TestIndexer
getIndexer() {
112 return (TestIndexer
) super.getIndexer();
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
120 private synchronized void setupTrace(final String path
) {
121 if (fTrace
== null) {
123 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
124 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
125 fTrace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
126 fTrace
.indexTrace(true);
127 } catch (final TmfTraceException e
) {
129 } catch (final URISyntaxException e
) {
131 } catch (final IOException e
) {
136 if (fEmptyTrace
== null) {
137 fEmptyTrace
= new EmptyTestTrace();
138 fEmptyTrace
.indexTrace(true);
142 // ------------------------------------------------------------------------
143 // Verify checkpoints
144 // ------------------------------------------------------------------------
147 public void testTmfTraceIndexing() {
148 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
149 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
150 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
151 assertEquals("getRange-end", NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
152 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
153 assertEquals("getEndTime", NB_EVENTS
, fTrace
.getEndTime().getValue());
155 List
<ITmfCheckpoint
> checkpoints
= fTrace
.getIndexer().getCheckpoints();
156 int pageSize
= fTrace
.getCacheSize();
157 assertTrue("Checkpoints exist", checkpoints
!= null);
158 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
160 // Validate that each checkpoint points to the right event
161 for (int i
= 0; i
< checkpoints
.size(); i
++) {
162 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
163 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
164 ITmfEvent event
= fTrace
.parseEvent(context
);
165 assertTrue(context
.getRank() == i
* pageSize
);
166 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
171 public void testEmptyTmfTraceIndexing() {
172 assertEquals("getCacheSize", ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, fEmptyTrace
.getCacheSize());
173 assertEquals("getTraceSize", 0, fEmptyTrace
.getNbEvents());
174 assertEquals("getRange-start", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getStartTime());
175 assertEquals("getRange-end", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getEndTime());
176 assertEquals("getStartTime", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getStartTime());
177 assertEquals("getEndTime", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getEndTime());
179 List
<ITmfCheckpoint
> checkpoints
= fEmptyTrace
.getIndexer().getCheckpoints();
180 int pageSize
= fEmptyTrace
.getCacheSize();
181 assertTrue("Checkpoints exist", checkpoints
!= null);
182 assertEquals("Checkpoints size", 0, checkpoints
.size());
184 // Validate that each checkpoint points to the right event
185 for (int i
= 0; i
< checkpoints
.size(); i
++) {
186 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
187 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
188 ITmfEvent event
= fEmptyTrace
.parseEvent(context
);
189 assertTrue(context
.getRank() == i
* pageSize
);
190 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));