| 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; |
| 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.ITmfCheckpoint; |
| 33 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 34 | import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer; |
| 35 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
| 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({"nls","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); |
| 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(); |
| 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(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // ------------------------------------------------------------------------ |
| 143 | // Verify checkpoints |
| 144 | // ------------------------------------------------------------------------ |
| 145 | |
| 146 | @Test |
| 147 | @SuppressWarnings("null") |
| 148 | public void testTmfTraceIndexing() { |
| 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 | |
| 156 | List<ITmfCheckpoint> checkpoints = fTrace.getIndexer().getCheckpoints(); |
| 157 | int pageSize = fTrace.getCacheSize(); |
| 158 | assertTrue("Checkpoints exist", checkpoints != null); |
| 159 | assertEquals("Checkpoints size", NB_EVENTS / BLOCK_SIZE, checkpoints.size()); |
| 160 | |
| 161 | // Validate that each checkpoint points to the right event |
| 162 | for (int i = 0; i < checkpoints.size(); i++) { |
| 163 | ITmfCheckpoint checkpoint = checkpoints.get(i); |
| 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 | |
| 171 | @Test |
| 172 | @SuppressWarnings("null") |
| 173 | public void testEmptyTmfTraceIndexing() { |
| 174 | assertEquals("getCacheSize", ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, fEmptyTrace.getCacheSize()); |
| 175 | assertEquals("getTraceSize", 0, fEmptyTrace.getNbEvents()); |
| 176 | assertEquals("getRange-start", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getStartTime()); |
| 177 | assertEquals("getRange-end", TmfTimestamp.BIG_BANG, fEmptyTrace.getTimeRange().getEndTime()); |
| 178 | assertEquals("getStartTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getStartTime()); |
| 179 | assertEquals("getEndTime", TmfTimestamp.BIG_BANG, fEmptyTrace.getEndTime()); |
| 180 | |
| 181 | List<ITmfCheckpoint> checkpoints = fEmptyTrace.getIndexer().getCheckpoints(); |
| 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++) { |
| 188 | ITmfCheckpoint checkpoint = checkpoints.get(i); |
| 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 | |
| 196 | } |