1 /*******************************************************************************
2 * Copyright (c) 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.experiment
;
16 import java
.io
.IOException
;
17 import java
.net
.URISyntaxException
;
19 import java
.util
.List
;
21 import junit
.framework
.TestCase
;
23 import org
.eclipse
.core
.runtime
.FileLocator
;
24 import org
.eclipse
.core
.runtime
.Path
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.experiment
.TmfExperiment
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.experiment
.TmfExperimentContext
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.experiment
.TmfExperimentLocation
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.experiment
.TmfLocationArray
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.TmfCoreTestPlugin
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpoint
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpointIndexer
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
38 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
41 * Test suite for the TmfCheckpointIndexTest class.
43 @SuppressWarnings("nls")
44 public class TmfExperimentCheckpointIndexTest
extends TestCase
{
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 private static final String DIRECTORY
= "testfiles";
51 private static final String TEST_STREAM1
= "O-Test-10K";
52 private static final String TEST_STREAM2
= "E-Test-10K";
53 private static final String EXPERIMENT
= "MyExperiment";
54 private static int NB_EVENTS
= 20000;
55 private static int BLOCK_SIZE
= 1000;
57 private static ITmfTrace
<?
>[] fTraces
;
58 private static TestExperiment fExperiment
;
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 private class TestIndexer
extends TmfCheckpointIndexer
<ITmfTrace
<ITmfEvent
>> {
65 @SuppressWarnings({ "unchecked", "rawtypes" })
66 public TestIndexer(TestExperiment testExperiment
) {
67 super((ITmfTrace
) testExperiment
, BLOCK_SIZE
);
69 public List
<TmfCheckpoint
> getCheckpoints() {
70 return getTraceIndex();
74 private class TestExperiment
extends TmfExperiment
<ITmfEvent
> {
75 @SuppressWarnings("unchecked")
76 public TestExperiment() {
77 super(ITmfEvent
.class, EXPERIMENT
, (ITmfTrace
<ITmfEvent
>[]) fTraces
, TmfTimestamp
.ZERO
, BLOCK_SIZE
, false);
78 setIndexer(new TestIndexer(this));
79 getIndexer().buildIndex(true);
82 public TestIndexer
getIndexer() {
83 return (TestIndexer
) super.getIndexer();
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
91 public TmfExperimentCheckpointIndexTest(final String name
) throws Exception
{
96 protected void setUp() throws Exception
{
98 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM1
, DIRECTORY
+ File
.separator
+ TEST_STREAM2
);
99 if (fExperiment
== null) {
100 fExperiment
= new TestExperiment();
105 protected void tearDown() throws Exception
{
107 fExperiment
.dispose();
111 private static ITmfTrace
<?
>[] setupTrace(final String path1
, final String path2
) {
112 if (fTraces
== null) {
113 fTraces
= new ITmfTrace
[2];
115 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path1
), null);
116 File test
= new File(FileLocator
.toFileURL(location
).toURI());
117 final TmfTraceStub trace1
= new TmfTraceStub(test
.getPath(), 0, true);
119 location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(path2
), null);
120 test
= new File(FileLocator
.toFileURL(location
).toURI());
121 final TmfTraceStub trace2
= new TmfTraceStub(test
.getPath(), 0, true);
123 } catch (final TmfTraceException e
) {
125 } catch (final URISyntaxException e
) {
127 } catch (final IOException e
) {
134 // ------------------------------------------------------------------------
135 // Verify checkpoints
136 // ------------------------------------------------------------------------
138 public void testTmfTraceIndexing() throws Exception
{
139 assertEquals("getCacheSize", BLOCK_SIZE
, fExperiment
.getCacheSize());
140 assertEquals("getTraceSize", NB_EVENTS
, fExperiment
.getNbEvents());
141 assertEquals("getRange-start", 1, fExperiment
.getTimeRange().getStartTime().getValue());
142 assertEquals("getRange-end", NB_EVENTS
, fExperiment
.getTimeRange().getEndTime().getValue());
143 assertEquals("getStartTime", 1, fExperiment
.getStartTime().getValue());
144 assertEquals("getEndTime", NB_EVENTS
, fExperiment
.getEndTime().getValue());
146 List
<TmfCheckpoint
> checkpoints
= fExperiment
.getIndexer().getCheckpoints();
147 int pageSize
= fExperiment
.getCacheSize();
148 assertTrue("Checkpoints exist", checkpoints
!= null);
149 assertEquals("Checkpoints size", NB_EVENTS
/ BLOCK_SIZE
, checkpoints
.size());
151 // Validate that each checkpoint points to the right event
152 for (int i
= 0; i
< checkpoints
.size(); i
++) {
153 TmfCheckpoint checkpoint
= checkpoints
.get(i
);
154 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) checkpoint
.getLocation();
155 TmfLocationArray locations
= expLocation
.getLocation();
156 ITmfContext
[] trcContexts
= new ITmfContext
[2];
157 trcContexts
[0] = new TmfContext(locations
.getLocations()[0], (i
* pageSize
) / 2);
158 trcContexts
[1] = new TmfContext(locations
.getLocations()[1], (i
* pageSize
) / 2);
159 TmfExperimentContext expContext
= new TmfExperimentContext(trcContexts
);
160 expContext
.getEvents()[0] = fTraces
[0].readNextEvent(fTraces
[0].seekEvent((i
* pageSize
) / 2));
161 expContext
.getEvents()[1] = fTraces
[1].readNextEvent(fTraces
[1].seekEvent((i
* pageSize
) / 2));
162 ITmfEvent event
= fExperiment
.parseEvent(expContext
);
163 assertTrue(expContext
.getRank() == i
* pageSize
);
164 assertTrue((checkpoint
.getTimestamp().compareTo(event
.getTimestamp(), false) == 0));