Refactor TmfTrace and TmfExperiment
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / experiment / TmfExperimentCheckpointIndexTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.experiment;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.net.URL;
19 import java.util.List;
20
21 import junit.framework.TestCase;
22
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;
39
40 /**
41 * Test suite for the TmfCheckpointIndexTest class.
42 */
43 @SuppressWarnings("nls")
44 public class TmfExperimentCheckpointIndexTest extends TestCase {
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49
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;
56
57 private static ITmfTrace<?>[] fTraces;
58 private static TestExperiment fExperiment;
59
60 // ------------------------------------------------------------------------
61 // Helper classes
62 // ------------------------------------------------------------------------
63
64 private class TestIndexer extends TmfCheckpointIndexer<ITmfTrace<ITmfEvent>> {
65 @SuppressWarnings({ "unchecked", "rawtypes" })
66 public TestIndexer(TestExperiment testExperiment) {
67 super((ITmfTrace) testExperiment, BLOCK_SIZE);
68 }
69 public List<TmfCheckpoint> getCheckpoints() {
70 return getTraceIndex();
71 }
72 }
73
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);
80 }
81 @Override
82 public TestIndexer getIndexer() {
83 return (TestIndexer) super.getIndexer();
84 }
85 }
86
87 // ------------------------------------------------------------------------
88 // Housekeeping
89 // ------------------------------------------------------------------------
90
91 public TmfExperimentCheckpointIndexTest(final String name) throws Exception {
92 super(name);
93 }
94
95 @Override
96 protected void setUp() throws Exception {
97 super.setUp();
98 setupTrace(DIRECTORY + File.separator + TEST_STREAM1, DIRECTORY + File.separator + TEST_STREAM2);
99 if (fExperiment == null) {
100 fExperiment = new TestExperiment();
101 }
102 }
103
104 @Override
105 protected void tearDown() throws Exception {
106 super.tearDown();
107 fExperiment.dispose();
108 fExperiment = null;
109 }
110
111 private static ITmfTrace<?>[] setupTrace(final String path1, final String path2) {
112 if (fTraces == null) {
113 fTraces = new ITmfTrace[2];
114 try {
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);
118 fTraces[0] = trace1;
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);
122 fTraces[1] = trace2;
123 } catch (final TmfTraceException e) {
124 e.printStackTrace();
125 } catch (final URISyntaxException e) {
126 e.printStackTrace();
127 } catch (final IOException e) {
128 e.printStackTrace();
129 }
130 }
131 return fTraces;
132 }
133
134 // ------------------------------------------------------------------------
135 // Verify checkpoints
136 // ------------------------------------------------------------------------
137
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());
145
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());
150
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));
165 }
166 }
167
168 }
This page took 0.033371 seconds and 5 git commands to generate.