Commit | Line | Data |
---|---|---|
13cb5f43 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010, 20112 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.FileNotFoundException; | |
18 | import java.io.IOException; | |
19 | import java.net.URISyntaxException; | |
20 | import java.net.URL; | |
21 | import java.util.Vector; | |
22 | ||
23 | import junit.framework.TestCase; | |
24 | ||
25 | import org.eclipse.core.runtime.FileLocator; | |
26 | import org.eclipse.core.runtime.Path; | |
27 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
28 | import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin; | |
29 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
30 | import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint; | |
31 | import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer; | |
32 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; | |
33 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; | |
34 | ||
35 | /** | |
36 | * <b><u>TmfTraceTest</u></b> | |
37 | * <p> | |
38 | * Test suite for the TmfTrace class. | |
39 | */ | |
40 | @SuppressWarnings("nls") | |
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 = 500; | |
50 | private static final int NB_EVENTS = 10000; | |
51 | private static TestTrace fTrace = null; | |
52 | ||
53 | // ------------------------------------------------------------------------ | |
54 | // Housekeeping | |
55 | // ------------------------------------------------------------------------ | |
56 | ||
57 | public TmfCheckpointIndexTest(final String name) throws Exception { | |
58 | super(name); | |
59 | } | |
60 | ||
61 | @Override | |
62 | protected void setUp() throws Exception { | |
63 | super.setUp(); | |
64 | fTrace = setupTrace(DIRECTORY + File.separator + TEST_STREAM); | |
65 | } | |
66 | ||
67 | @Override | |
68 | protected void tearDown() throws Exception { | |
69 | super.tearDown(); | |
70 | fTrace.dispose(); | |
71 | fTrace = null; | |
72 | } | |
73 | ||
74 | // ------------------------------------------------------------------------ | |
75 | // Helper classes | |
76 | // ------------------------------------------------------------------------ | |
77 | ||
78 | private class TestIndexer extends TmfCheckpointIndexer<ITmfTrace<ITmfEvent>> { | |
79 | @SuppressWarnings({ "unchecked", "rawtypes" }) | |
80 | public TestIndexer(TestTrace testTrace) { | |
81 | super((ITmfTrace) testTrace); | |
82 | } | |
83 | public Vector<TmfCheckpoint> getCheckpoints() { | |
84 | return fTraceIndex; | |
85 | } | |
86 | } | |
87 | ||
88 | private class TestTrace extends TmfTraceStub { | |
89 | public TestTrace(String path, int blockSize) throws FileNotFoundException { | |
90 | super(path, blockSize); | |
91 | fIndexer = new TestIndexer(this); | |
92 | } | |
93 | public TestIndexer getIndexer() { | |
94 | return (TestIndexer) fIndexer; | |
95 | } | |
96 | } | |
97 | ||
98 | // ------------------------------------------------------------------------ | |
99 | // Helper functions | |
100 | // ------------------------------------------------------------------------ | |
101 | ||
102 | private TestTrace setupTrace(final String path) { | |
103 | if (fTrace == null) { | |
104 | try { | |
105 | final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null); | |
106 | final File test = new File(FileLocator.toFileURL(location).toURI()); | |
107 | fTrace = new TestTrace(test.toURI().getPath(), BLOCK_SIZE); | |
108 | fTrace.indexTrace(); | |
109 | } catch (final URISyntaxException e) { | |
110 | e.printStackTrace(); | |
111 | } catch (final IOException e) { | |
112 | e.printStackTrace(); | |
113 | } | |
114 | } | |
115 | return fTrace; | |
116 | } | |
117 | ||
118 | // ------------------------------------------------------------------------ | |
119 | // Verify checkpoints | |
120 | // ------------------------------------------------------------------------ | |
121 | ||
122 | public void testTmfTraceIndexing() throws Exception { | |
123 | assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize()); | |
124 | assertEquals("getTraceSize", NB_EVENTS, fTrace.getNbEvents()); | |
125 | assertEquals("getRange-start", 1, fTrace.getTimeRange().getStartTime().getValue()); | |
126 | assertEquals("getRange-end", NB_EVENTS, fTrace.getTimeRange().getEndTime().getValue()); | |
127 | assertEquals("getStartTime", 1, fTrace.getStartTime().getValue()); | |
128 | assertEquals("getEndTime", NB_EVENTS, fTrace.getEndTime().getValue()); | |
129 | ||
130 | Vector<TmfCheckpoint> checkpoints = fTrace.getIndexer().getCheckpoints(); | |
131 | int pageSize = fTrace.getCacheSize(); | |
132 | assertTrue("Checkpoints exist", checkpoints != null); | |
133 | ||
134 | // Validate that each checkpoint points to the right event | |
135 | for (int i = 0; i < checkpoints.size(); i++) { | |
136 | TmfCheckpoint checkpoint = checkpoints.get(i); | |
137 | TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize); | |
138 | ITmfEvent event = fTrace.parseEvent(context); | |
139 | assertTrue(context.getRank() == i * pageSize); | |
140 | assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0)); | |
141 | } | |
142 | } | |
143 | ||
144 | } |