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