Commit | Line | Data |
---|---|---|
13cb5f43 | 1 | /******************************************************************************* |
0316808c | 2 | * Copyright (c) 2009, 2010, 2012 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 | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.core.tests.trace; | |
15 | ||
16 | import java.io.File; | |
13cb5f43 FC |
17 | import java.io.IOException; |
18 | import java.net.URISyntaxException; | |
19 | import java.net.URL; | |
0316808c | 20 | import java.util.List; |
13cb5f43 FC |
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; | |
b75d6b65 | 27 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
b4f71e4a | 28 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
13cb5f43 | 29 | import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; |
3427112b | 30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfCheckpoint; |
13cb5f43 | 31 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
13cb5f43 FC |
32 | import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer; |
33 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; | |
b75d6b65 | 34 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfEmptyTraceStub; |
13cb5f43 FC |
35 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; |
36 | ||
37 | /** | |
0316808c | 38 | * Test suite for the TmfCheckpointIndexTest class. |
13cb5f43 | 39 | */ |
54a7a54c | 40 | @SuppressWarnings({"nls","javadoc"}) |
13cb5f43 FC |
41 | public class TmfCheckpointIndexTest extends TestCase { |
42 | ||
43 | // ------------------------------------------------------------------------ | |
44 | // Variables | |
45 | // ------------------------------------------------------------------------ | |
46 | ||
b75d6b65 FC |
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; | |
13cb5f43 FC |
53 | |
54 | // ------------------------------------------------------------------------ | |
55 | // Housekeeping | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
54a7a54c FC |
58 | /** |
59 | * @param name the test name | |
60 | */ | |
61 | public TmfCheckpointIndexTest(final String name) { | |
13cb5f43 FC |
62 | super(name); |
63 | } | |
64 | ||
65 | @Override | |
66 | protected void setUp() throws Exception { | |
67 | super.setUp(); | |
b75d6b65 | 68 | setupTrace(DIRECTORY + File.separator + TEST_STREAM); |
13cb5f43 FC |
69 | } |
70 | ||
71 | @Override | |
72 | protected void tearDown() throws Exception { | |
73 | super.tearDown(); | |
74 | fTrace.dispose(); | |
75 | fTrace = null; | |
b75d6b65 FC |
76 | fEmptyTrace.dispose(); |
77 | fEmptyTrace = null; | |
13cb5f43 FC |
78 | } |
79 | ||
80 | // ------------------------------------------------------------------------ | |
81 | // Helper classes | |
82 | // ------------------------------------------------------------------------ | |
83 | ||
6256d8ad AM |
84 | private static class TestIndexer extends TmfCheckpointIndexer { |
85 | @SuppressWarnings({ }) | |
13cb5f43 | 86 | public TestIndexer(TestTrace testTrace) { |
6256d8ad | 87 | super(testTrace, BLOCK_SIZE); |
13cb5f43 | 88 | } |
6256d8ad | 89 | @SuppressWarnings({ }) |
b75d6b65 | 90 | public TestIndexer(EmptyTestTrace testTrace) { |
6256d8ad | 91 | super(testTrace, BLOCK_SIZE); |
b75d6b65 | 92 | } |
3427112b | 93 | public List<ITmfCheckpoint> getCheckpoints() { |
0316808c | 94 | return getTraceIndex(); |
13cb5f43 FC |
95 | } |
96 | } | |
97 | ||
98 | private class TestTrace extends TmfTraceStub { | |
b4f71e4a | 99 | public TestTrace(String path, int blockSize) throws TmfTraceException { |
13cb5f43 | 100 | super(path, blockSize); |
0316808c | 101 | setIndexer(new TestIndexer(this)); |
13cb5f43 | 102 | } |
0316808c | 103 | @Override |
13cb5f43 | 104 | public TestIndexer getIndexer() { |
0316808c | 105 | return (TestIndexer) super.getIndexer(); |
13cb5f43 FC |
106 | } |
107 | } | |
108 | ||
b75d6b65 FC |
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 | ||
13cb5f43 FC |
120 | // ------------------------------------------------------------------------ |
121 | // Helper functions | |
122 | // ------------------------------------------------------------------------ | |
123 | ||
b75d6b65 | 124 | private synchronized void setupTrace(final String path) { |
13cb5f43 FC |
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(); | |
b4f71e4a FC |
131 | } catch (final TmfTraceException e) { |
132 | e.printStackTrace(); | |
13cb5f43 FC |
133 | } catch (final URISyntaxException e) { |
134 | e.printStackTrace(); | |
135 | } catch (final IOException e) { | |
136 | e.printStackTrace(); | |
137 | } | |
138 | } | |
b75d6b65 FC |
139 | |
140 | if (fEmptyTrace == null) { | |
141 | fEmptyTrace = new EmptyTestTrace(); | |
142 | fEmptyTrace.indexTrace(); | |
143 | } | |
13cb5f43 FC |
144 | } |
145 | ||
146 | // ------------------------------------------------------------------------ | |
147 | // Verify checkpoints | |
148 | // ------------------------------------------------------------------------ | |
149 | ||
54a7a54c FC |
150 | @SuppressWarnings("null") |
151 | public void testTmfTraceIndexing() { | |
13cb5f43 FC |
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 | ||
3427112b | 159 | List<ITmfCheckpoint> checkpoints = fTrace.getIndexer().getCheckpoints(); |
13cb5f43 FC |
160 | int pageSize = fTrace.getCacheSize(); |
161 | assertTrue("Checkpoints exist", checkpoints != null); | |
07671572 | 162 | assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size()); |
13cb5f43 FC |
163 | |
164 | // Validate that each checkpoint points to the right event | |
165 | for (int i = 0; i < checkpoints.size(); i++) { | |
3427112b | 166 | ITmfCheckpoint checkpoint = checkpoints.get(i); |
13cb5f43 FC |
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 | ||
54a7a54c FC |
174 | @SuppressWarnings("null") |
175 | public void testEmptyTmfTraceIndexing() { | |
b75d6b65 FC |
176 | assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, fEmptyTrace.getCacheSize()); |
177 | assertEquals("getTraceSize", 0, fEmptyTrace.getNbEvents()); | |
9cbe7899 | 178 | assertEquals("getRange-start", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getStartTime()); |
b75d6b65 | 179 | assertEquals("getRange-end", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getEndTime()); |
9cbe7899 | 180 | assertEquals("getStartTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getStartTime()); |
b75d6b65 FC |
181 | assertEquals("getEndTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getEndTime()); |
182 | ||
3427112b | 183 | List<ITmfCheckpoint> checkpoints = fEmptyTrace.getIndexer().getCheckpoints(); |
b75d6b65 FC |
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++) { | |
3427112b | 190 | ITmfCheckpoint checkpoint = checkpoints.get(i); |
b75d6b65 FC |
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 | ||
13cb5f43 | 198 | } |