Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfCheckpointIndexTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 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 * Francois Chouinard - Adapted for TMF Trace Model 1.0
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.trace;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.net.URL;
20 import java.util.List;
21
22 import junit.framework.TestCase;
23
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;
36
37 /**
38 * Test suite for the TmfCheckpointIndexTest class.
39 */
40 @SuppressWarnings({"nls","javadoc"})
41 public class TmfCheckpointIndexTest extends TestCase {
42
43 // ------------------------------------------------------------------------
44 // Variables
45 // ------------------------------------------------------------------------
46
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;
53
54 // ------------------------------------------------------------------------
55 // Housekeeping
56 // ------------------------------------------------------------------------
57
58 /**
59 * @param name the test name
60 */
61 public TmfCheckpointIndexTest(final String name) {
62 super(name);
63 }
64
65 @Override
66 protected void setUp() throws Exception {
67 super.setUp();
68 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
69 }
70
71 @Override
72 protected void tearDown() throws Exception {
73 super.tearDown();
74 fTrace.dispose();
75 fTrace = null;
76 fEmptyTrace.dispose();
77 fEmptyTrace = null;
78 }
79
80 // ------------------------------------------------------------------------
81 // Helper classes
82 // ------------------------------------------------------------------------
83
84 private static class TestIndexer extends TmfCheckpointIndexer {
85 @SuppressWarnings({ })
86 public TestIndexer(TestTrace testTrace) {
87 super(testTrace, BLOCK_SIZE);
88 }
89 @SuppressWarnings({ })
90 public TestIndexer(EmptyTestTrace testTrace) {
91 super(testTrace, BLOCK_SIZE);
92 }
93 public List<ITmfCheckpoint> getCheckpoints() {
94 return getTraceIndex();
95 }
96 }
97
98 private class TestTrace extends TmfTraceStub {
99 public TestTrace(String path, int blockSize) throws TmfTraceException {
100 super(path, blockSize);
101 setIndexer(new TestIndexer(this));
102 }
103 @Override
104 public TestIndexer getIndexer() {
105 return (TestIndexer) super.getIndexer();
106 }
107 }
108
109 private class EmptyTestTrace extends TmfEmptyTraceStub {
110 public EmptyTestTrace() {
111 super();
112 setIndexer(new TestIndexer(this));
113 }
114 @Override
115 public TestIndexer getIndexer() {
116 return (TestIndexer) super.getIndexer();
117 }
118 }
119
120 // ------------------------------------------------------------------------
121 // Helper functions
122 // ------------------------------------------------------------------------
123
124 private synchronized void setupTrace(final String path) {
125 if (fTrace == null) {
126 try {
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);
130 fTrace.indexTrace();
131 } catch (final TmfTraceException e) {
132 e.printStackTrace();
133 } catch (final URISyntaxException e) {
134 e.printStackTrace();
135 } catch (final IOException e) {
136 e.printStackTrace();
137 }
138 }
139
140 if (fEmptyTrace == null) {
141 fEmptyTrace = new EmptyTestTrace();
142 fEmptyTrace.indexTrace();
143 }
144 }
145
146 // ------------------------------------------------------------------------
147 // Verify checkpoints
148 // ------------------------------------------------------------------------
149
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());
158
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());
163
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));
171 }
172 }
173
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());
182
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());
187
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));
195 }
196 }
197
198 }
This page took 0.035974 seconds and 6 git commands to generate.