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