tmf: Update copyright headers in tmf.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfCheckpointIndexTest.java
CommitLineData
13cb5f43 1/*******************************************************************************
61759503 2 * Copyright (c) 2009, 2013 Ericsson
13cb5f43
FC
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
6e1886bc 12 * Alexandre Montplaisir - Port to JUnit4
13cb5f43
FC
13 *******************************************************************************/
14
15package org.eclipse.linuxtools.tmf.core.tests.trace;
16
6e1886bc
AM
17import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertTrue;
19
13cb5f43 20import java.io.File;
13cb5f43
FC
21import java.io.IOException;
22import java.net.URISyntaxException;
23import java.net.URL;
0316808c 24import java.util.List;
13cb5f43 25
13cb5f43
FC
26import org.eclipse.core.runtime.FileLocator;
27import org.eclipse.core.runtime.Path;
28import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
b4f71e4a 29import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
13cb5f43 30import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
3bd46eef 31import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
3427112b 32import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint;
13cb5f43 33import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
13cb5f43
FC
34import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer;
35import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
b75d6b65 36import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfEmptyTraceStub;
13cb5f43 37import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
6e1886bc
AM
38import org.junit.After;
39import org.junit.Before;
40import org.junit.Test;
13cb5f43
FC
41
42/**
0316808c 43 * Test suite for the TmfCheckpointIndexTest class.
13cb5f43 44 */
54a7a54c 45@SuppressWarnings({"nls","javadoc"})
6e1886bc 46public class TmfCheckpointIndexTest {
13cb5f43
FC
47
48 // ------------------------------------------------------------------------
49 // Variables
50 // ------------------------------------------------------------------------
51
b75d6b65
FC
52 private static final String DIRECTORY = "testfiles";
53 private static final String TEST_STREAM = "A-Test-10K";
54 private static final int BLOCK_SIZE = 100;
55 private static final int NB_EVENTS = 10000;
56 private static TestTrace fTrace = null;
57 private static EmptyTestTrace fEmptyTrace = null;
13cb5f43
FC
58
59 // ------------------------------------------------------------------------
60 // Housekeeping
61 // ------------------------------------------------------------------------
62
6e1886bc
AM
63 @Before
64 public void setUp() {
b75d6b65 65 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
13cb5f43
FC
66 }
67
6e1886bc
AM
68 @After
69 public void tearDown() {
13cb5f43
FC
70 fTrace.dispose();
71 fTrace = null;
b75d6b65
FC
72 fEmptyTrace.dispose();
73 fEmptyTrace = null;
13cb5f43
FC
74 }
75
76 // ------------------------------------------------------------------------
77 // Helper classes
78 // ------------------------------------------------------------------------
79
6256d8ad
AM
80 private static class TestIndexer extends TmfCheckpointIndexer {
81 @SuppressWarnings({ })
13cb5f43 82 public TestIndexer(TestTrace testTrace) {
6256d8ad 83 super(testTrace, BLOCK_SIZE);
13cb5f43 84 }
6256d8ad 85 @SuppressWarnings({ })
b75d6b65 86 public TestIndexer(EmptyTestTrace testTrace) {
6256d8ad 87 super(testTrace, BLOCK_SIZE);
b75d6b65 88 }
3427112b 89 public List<ITmfCheckpoint> getCheckpoints() {
0316808c 90 return getTraceIndex();
13cb5f43
FC
91 }
92 }
93
94 private class TestTrace extends TmfTraceStub {
b4f71e4a 95 public TestTrace(String path, int blockSize) throws TmfTraceException {
13cb5f43 96 super(path, blockSize);
0316808c 97 setIndexer(new TestIndexer(this));
13cb5f43 98 }
0316808c 99 @Override
13cb5f43 100 public TestIndexer getIndexer() {
0316808c 101 return (TestIndexer) super.getIndexer();
13cb5f43
FC
102 }
103 }
104
b75d6b65
FC
105 private class EmptyTestTrace extends TmfEmptyTraceStub {
106 public EmptyTestTrace() {
107 super();
108 setIndexer(new TestIndexer(this));
109 }
110 @Override
111 public TestIndexer getIndexer() {
112 return (TestIndexer) super.getIndexer();
113 }
114 }
115
13cb5f43
FC
116 // ------------------------------------------------------------------------
117 // Helper functions
118 // ------------------------------------------------------------------------
119
b75d6b65 120 private synchronized void setupTrace(final String path) {
13cb5f43
FC
121 if (fTrace == null) {
122 try {
123 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
124 final File test = new File(FileLocator.toFileURL(location).toURI());
125 fTrace = new TestTrace(test.toURI().getPath(), BLOCK_SIZE);
126 fTrace.indexTrace();
b4f71e4a
FC
127 } catch (final TmfTraceException e) {
128 e.printStackTrace();
13cb5f43
FC
129 } catch (final URISyntaxException e) {
130 e.printStackTrace();
131 } catch (final IOException e) {
132 e.printStackTrace();
133 }
134 }
b75d6b65
FC
135
136 if (fEmptyTrace == null) {
137 fEmptyTrace = new EmptyTestTrace();
138 fEmptyTrace.indexTrace();
139 }
13cb5f43
FC
140 }
141
142 // ------------------------------------------------------------------------
143 // Verify checkpoints
144 // ------------------------------------------------------------------------
145
6e1886bc 146 @Test
54a7a54c
FC
147 @SuppressWarnings("null")
148 public void testTmfTraceIndexing() {
13cb5f43
FC
149 assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize());
150 assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents());
151 assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue());
152 assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue());
153 assertEquals("getStartTime", 1, fTrace.getStartTime().getValue());
154 assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue());
155
3427112b 156 List<ITmfCheckpoint> checkpoints = fTrace.getIndexer().getCheckpoints();
13cb5f43
FC
157 int pageSize = fTrace.getCacheSize();
158 assertTrue("Checkpoints exist", checkpoints != null);
07671572 159 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
13cb5f43
FC
160
161 // Validate that each checkpoint points to the right event
162 for (int i = 0; i < checkpoints.size(); i++) {
3427112b 163 ITmfCheckpoint checkpoint = checkpoints.get(i);
13cb5f43
FC
164 TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize);
165 ITmfEvent event = fTrace.parseEvent(context);
166 assertTrue(context.getRank() == i * pageSize);
167 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
168 }
169 }
170
6e1886bc 171 @Test
54a7a54c
FC
172 @SuppressWarnings("null")
173 public void testEmptyTmfTraceIndexing() {
b75d6b65
FC
174 assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, fEmptyTrace.getCacheSize());
175 assertEquals("getTraceSize", 0, fEmptyTrace.getNbEvents());
9cbe7899 176 assertEquals("getRange-start", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getStartTime());
b75d6b65 177 assertEquals("getRange-end", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getEndTime());
9cbe7899 178 assertEquals("getStartTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getStartTime());
b75d6b65
FC
179 assertEquals("getEndTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getEndTime());
180
3427112b 181 List<ITmfCheckpoint> checkpoints = fEmptyTrace.getIndexer().getCheckpoints();
b75d6b65
FC
182 int pageSize = fEmptyTrace.getCacheSize();
183 assertTrue("Checkpoints exist", checkpoints != null);
184 assertEquals("Checkpoints size", 0, checkpoints.size());
185
186 // Validate that each checkpoint points to the right event
187 for (int i = 0; i < checkpoints.size(); i++) {
3427112b 188 ITmfCheckpoint checkpoint = checkpoints.get(i);
b75d6b65
FC
189 TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize);
190 ITmfEvent event = fEmptyTrace.parseEvent(context);
191 assertTrue(context.getRank() == i * pageSize);
192 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
193 }
194 }
195
13cb5f43 196}
This page took 0.041982 seconds and 5 git commands to generate.