tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / trace / AbstractCustomTraceIndexTest.java
1 /*******************************************************************************
2 * Copyright (c) 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.tests.trace;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.File;
20
21 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
22 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
23 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
24 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
25 import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
26 import org.eclipse.linuxtools.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
27 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
28 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpointIndex;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 /**
34 * Common test code for custom trace indexes
35 *
36 * @author Marc-Andre Laperle
37 */
38 public abstract class AbstractCustomTraceIndexTest {
39
40 /**
41 * Time format use for event creation
42 */
43 protected static final String TIMESTAMP_FORMAT = "dd/MM/yyyy HH:mm:ss:SSS";
44 /**
45 * Block size used for the indexer
46 */
47 protected static final int BLOCK_SIZE = 100;
48 /**
49 * The total number of events in the generated trace
50 */
51 protected static final int NB_EVENTS = 10000;
52 private TestTrace fTrace = null;
53
54 /**
55 * A common test indexer for custom trace index tests
56 */
57 protected static class TestIndexer extends TmfBTreeTraceIndexer {
58
59 /**
60 * Constructs a new test indexer
61 *
62 * @param trace the trace
63 * @param interval the checkpoint interval
64 */
65 public TestIndexer(ITmfTrace trace, int interval) {
66 super(trace, interval);
67 }
68
69 /**
70 * Get the index
71 *
72 * @return the index
73 */
74 public ITmfCheckpointIndex getCheckpoints() {
75 return getTraceIndex();
76 }
77 }
78
79 interface TestTrace extends ITmfTrace {
80 TestIndexer getIndexer();
81 }
82
83 /**
84 * Setup the test
85 *
86 * @throws Exception when error occurs
87 */
88 @Before
89 public void setUp() throws Exception {
90 setupTrace();
91 }
92
93 private synchronized void setupTrace() throws Exception {
94 File traceDirectory = new File(getTraceDirectory());
95 if (traceDirectory.exists()) {
96 traceDirectory.delete();
97 }
98 traceDirectory.mkdir();
99 if (fTrace == null) {
100 fTrace = createTrace();
101 fTrace.indexTrace(true);
102 }
103 }
104
105 /**
106 * Create a test trace, varies between tests
107 *
108 * @return the test trace
109 * @throws Exception when error occurs
110 */
111 abstract protected TestTrace createTrace() throws Exception;
112 /**
113 * Return the trace directory for the generated trace
114 *
115 * @return the trace directory for the generated trace
116 */
117 abstract protected String getTraceDirectory();
118
119 /**
120 * Tear down the test
121 */
122 @After
123 public void tearDown() {
124 String directory = TmfTraceManager.getSupplementaryFileDir(fTrace);
125 try {
126 fTrace.dispose();
127 fTrace = null;
128 } finally {
129 File dir = new File(directory);
130 if (dir.exists()) {
131 File[] files = dir.listFiles();
132 for (File file : files) {
133 file.delete();
134 }
135 dir.delete();
136 }
137
138 File trace = new File(getTraceDirectory());
139 if (trace.exists()) {
140 trace.delete();
141 }
142 }
143
144 }
145
146 /**
147 * Test the content of the index after building the full index
148 */
149 @Test
150 public void testTmfTraceIndexing() {
151 verifyIndexContent();
152 }
153
154 private void verifyIndexContent() {
155 assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize());
156 assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents());
157 assertEquals("getRange-start", 0, fTrace.getTimeRange().getStartTime().getValue());
158 assertEquals("getRange-end", NB_EVENTS - 1, fTrace.getTimeRange().getEndTime().getValue());
159 assertEquals("getStartTime", 0, fTrace.getStartTime().getValue());
160 assertEquals("getEndTime", NB_EVENTS - 1, fTrace.getEndTime().getValue());
161
162 ITmfCheckpointIndex checkpoints = fTrace.getIndexer().getCheckpoints();
163 int pageSize = fTrace.getCacheSize();
164 assertTrue("Checkpoints exist", checkpoints != null);
165 assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size());
166
167 // Validate that each checkpoint points to the right event
168 for (int i = 0; i < checkpoints.size(); i++) {
169 ITmfCheckpoint checkpoint = checkpoints.get(i);
170 TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize);
171 ITmfEvent event = ((ITmfEventParser)fTrace).parseEvent(context);
172 assertTrue(context.getRank() == i * pageSize);
173 assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
174 }
175 }
176
177 /**
178 * Test that a fully built index has the same content when reloaded from disk
179 *
180 * @throws Exception when error occurs
181 */
182 @Test
183 public void testReopenIndex() throws Exception {
184 fTrace.dispose();
185 fTrace = createTrace();
186 assertFalse(fTrace.getIndexer().getCheckpoints().isCreatedFromScratch());
187 fTrace.indexTrace(true);
188
189 verifyIndexContent();
190 }
191 }
This page took 0.036817 seconds and 5 git commands to generate.