1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
17 import java
.io
.IOException
;
18 import java
.net
.URISyntaxException
;
20 import java
.util
.List
;
22 import junit
.framework
.TestCase
;
24 import org
.eclipse
.core
.runtime
.FileLocator
;
25 import org
.eclipse
.core
.runtime
.Path
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfCheckpoint
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpointIndexer
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfEmptyTraceStub
;
35 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
38 * Test suite for the TmfCheckpointIndexTest class.
40 @SuppressWarnings({"nls","javadoc"})
41 public class TmfCheckpointIndexTest
extends TestCase
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 private static final String DIRECTORY
= "testfiles";
48 private static final String TEST_STREAM
= "A-Test-10K";
49 private static final int BLOCK_SIZE
= 100;
50 private static final int NB_EVENTS
= 10000;
51 private static TestTrace fTrace
= null;
52 private static EmptyTestTrace fEmptyTrace
= null;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * @param name the test name
61 public TmfCheckpointIndexTest(final String name
) {
66 protected void setUp() throws Exception
{
68 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
72 protected void tearDown() throws Exception
{
76 fEmptyTrace
.dispose();
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
84 private static class TestIndexer
extends TmfCheckpointIndexer
<ITmfTrace
<ITmfEvent
>> {
85 @SuppressWarnings({ "unchecked", "rawtypes" })
86 public TestIndexer(TestTrace testTrace
) {
87 super((ITmfTrace
) testTrace
, BLOCK_SIZE
);
89 @SuppressWarnings({ "unchecked", "rawtypes" })
90 public TestIndexer(EmptyTestTrace testTrace
) {
91 super((ITmfTrace
) testTrace
, BLOCK_SIZE
);
93 public List
<ITmfCheckpoint
> getCheckpoints() {
94 return getTraceIndex();
98 private class TestTrace
extends TmfTraceStub
{
99 public TestTrace(String path
, int blockSize
) throws TmfTraceException
{
100 super(path
, blockSize
);
101 setIndexer(new TestIndexer(this));
104 public TestIndexer
getIndexer() {
105 return (TestIndexer
) super.getIndexer();
109 private class EmptyTestTrace
extends TmfEmptyTraceStub
{
110 public EmptyTestTrace() {
112 setIndexer(new TestIndexer(this));
115 public TestIndexer
getIndexer() {
116 return (TestIndexer
) super.getIndexer();
120 // ------------------------------------------------------------------------
122 // ------------------------------------------------------------------------
124 private synchronized void setupTrace(final String path
) {
125 if (fTrace
== null) {
127 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path
), null);
128 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
129 fTrace
= new TestTrace(test
.toURI().getPath(), BLOCK_SIZE
);
131 } catch (final TmfTraceException e
) {
133 } catch (final URISyntaxException e
) {
135 } catch (final IOException e
) {
140 if (fEmptyTrace
== null) {
141 fEmptyTrace
= new EmptyTestTrace();
142 fEmptyTrace
.indexTrace();
146 // ------------------------------------------------------------------------
147 // Verify checkpoints
148 // ------------------------------------------------------------------------
150 @SuppressWarnings("null")
151 public void testTmfTraceIndexing() {
152 assertEquals("getCacheSize", BLOCK_SIZE
, fTrace
.getCacheSize());
153 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
154 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
155 assertEquals("getRange-end", NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
156 assertEquals("getStartTime", 1, fTrace
.getStartTime().getValue());
157 assertEquals("getEndTime", NB_EVENTS
, fTrace
.getEndTime().getValue());
159 List
<ITmfCheckpoint
> checkpoints
= fTrace
.getIndexer().getCheckpoints();
160 int pageSize
= fTrace
.getCacheSize();
161 assertTrue("Checkpoints exist", checkpoints
!= null);
162 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
164 // Validate that each checkpoint points to the right event
165 for (int i
= 0; i
< checkpoints
.size(); i
++) {
166 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
167 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
168 ITmfEvent event
= fTrace
.parseEvent(context
);
169 assertTrue(context
.getRank() == i
* pageSize
);
170 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));
174 @SuppressWarnings("null")
175 public void testEmptyTmfTraceIndexing() {
176 assertEquals("getCacheSize", ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, fEmptyTrace
.getCacheSize());
177 assertEquals("getTraceSize", 0, fEmptyTrace
.getNbEvents());
178 assertEquals("getRange-start", TmfTimestamp
.BIG_CRUNCH
, fEmptyTrace
.getTimeRange().getStartTime());
179 assertEquals("getRange-end", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getTimeRange().getEndTime());
180 assertEquals("getStartTime", TmfTimestamp
.BIG_CRUNCH
, fEmptyTrace
.getStartTime());
181 assertEquals("getEndTime", TmfTimestamp
.BIG_BANG
, fEmptyTrace
.getEndTime());
183 List
<ITmfCheckpoint
> checkpoints
= fEmptyTrace
.getIndexer().getCheckpoints();
184 int pageSize
= fEmptyTrace
.getCacheSize();
185 assertTrue("Checkpoints exist", checkpoints
!= null);
186 assertEquals("Checkpoints size", 0, checkpoints
.size());
188 // Validate that each checkpoint points to the right event
189 for (int i
= 0; i
< checkpoints
.size(); i
++) {
190 ITmfCheckpoint checkpoint
= checkpoints
.get(i
);
191 TmfContext context
= new TmfContext(checkpoint
.getLocation(), i
* pageSize
);
192 ITmfEvent event
= fEmptyTrace
.parseEvent(context
);
193 assertTrue(context
.getRank() == i
* pageSize
);
194 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));