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
;
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
.tests
.shared
.TmfTestTrace
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfCheckpoint
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpointIndexer
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
37 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
38 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
39 import org
.junit
.After
;
40 import org
.junit
.Before
;
41 import org
.junit
.Test
;
44 * Test suite for the TmfCheckpointIndexTest class.
46 @SuppressWarnings("javadoc")
47 public class TmfCheckpointIndexTest
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private static final int BLOCK_SIZE
= 100;
54 private static final int NB_EVENTS
= 10000;
55 private static TestTrace fTrace
= null;
56 private static EmptyTestTrace fEmptyTrace
= null;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
64 setupTrace(TmfTestTrace
.A_TEST_10K
.getFullPath());
68 public void tearDown() {
71 fEmptyTrace
.dispose();
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
79 private static class TestIndexer
extends TmfCheckpointIndexer
{
80 @SuppressWarnings({ })
81 public TestIndexer(TestTrace testTrace
) {
82 super(testTrace
, BLOCK_SIZE
);
84 @SuppressWarnings({ })
85 public TestIndexer(EmptyTestTrace testTrace
) {
86 super(testTrace
, BLOCK_SIZE
);
88 public List
<ITmfCheckpoint
> getCheckpoints() {
89 return getTraceIndex();
93 private class TestTrace
extends TmfTraceStub
{
94 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
95 super(path
, blockSize
);
96 setIndexer(new TestIndexer(this));
99 public TestIndexer
getIndexer() {
100 return (TestIndexer
) super.getIndexer();
104 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
105 public EmptyTestTrace() {
107 setIndexer(new TestIndexer(this));
110 public TestIndexer
getIndexer() {
111 return (TestIndexer
) super.getIndexer();
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
119 private synchronized void setupTrace(final String path
) {
120 if (fTrace
== null) {
122 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
123 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
124 fTrace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
125 fTrace
.indexTrace(true);
126 } catch (final TmfTraceException e
) {
128 } catch (final URISyntaxException e
) {
130 } catch (final IOException e
) {
135 if (fEmptyTrace
== null) {
136 fEmptyTrace
= new EmptyTestTrace();
137 fEmptyTrace
.indexTrace(true);
141 // ------------------------------------------------------------------------
142 // Verify checkpoints
143 // ------------------------------------------------------------------------
146 public void testTmfTraceIndexing() {
147 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
148 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
149 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
150 assertEquals("getRange-end", NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
151 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
152 assertEquals("getEndTime", NB_EVENTS
, fTrace
.getEndTime().getValue());
154 List
<ITmfCheckpoint
> checkpoints
= fTrace
.getIndexer().getCheckpoints();
155 int pageSize
= fTrace
.getCacheSize();
156 assertTrue("Checkpoints exist", checkpoints
!= null);
157 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
159 // Validate that each checkpoint points to the right event
160 for (int i
= 0; i
< checkpoints
.size(); i
++) {
161 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
162 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
163 ITmfEvent event
= fTrace
.parseEvent(context
);
164 assertTrue(context
.getRank() == i
* pageSize
);
165 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
170 public void testEmptyTmfTraceIndexing() {
171 assertEquals("getCacheSize", ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, fEmptyTrace
.getCacheSize());
172 assertEquals("getTraceSize", 0, fEmptyTrace
.getNbEvents());
173 assertEquals("getRange-start", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getStartTime());
174 assertEquals("getRange-end", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getEndTime());
175 assertEquals("getStartTime", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getStartTime());
176 assertEquals("getEndTime", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getEndTime());
178 List
<ITmfCheckpoint
> checkpoints
= fEmptyTrace
.getIndexer().getCheckpoints();
179 int pageSize
= fEmptyTrace
.getCacheSize();
180 assertTrue("Checkpoints exist", checkpoints
!= null);
181 assertEquals("Checkpoints size", 0, checkpoints
.size());
183 // Validate that each checkpoint points to the right event
184 for (int i
= 0; i
< checkpoints
.size(); i
++) {
185 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
186 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
187 ITmfEvent event
= fEmptyTrace
.parseEvent(context
);
188 assertTrue(context
.getRank() == i
* pageSize
);
189 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));