| 1 | package org.eclipse.linuxtools.lttng.tests.trace; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.net.URL; |
| 5 | |
| 6 | import junit.framework.TestCase; |
| 7 | |
| 8 | import org.eclipse.core.runtime.FileLocator; |
| 9 | import org.eclipse.core.runtime.Path; |
| 10 | import org.eclipse.linuxtools.lttng.tests.LTTngCoreTestPlugin; |
| 11 | import org.eclipse.linuxtools.lttng.trace.LTTngTextTrace; |
| 12 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
| 13 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; |
| 14 | import org.eclipse.linuxtools.tmf.trace.TmfContext; |
| 15 | import org.eclipse.linuxtools.tmf.trace.TmfLocation; |
| 16 | |
| 17 | /* |
| 18 | Functions tested here : |
| 19 | public LTTngTextTrace(String path) throws Exception |
| 20 | public LTTngTextTrace(String path, boolean skipIndexing) throws Exception |
| 21 | |
| 22 | public TmfTraceContext seekLocation(Object location) { |
| 23 | public TmfTraceContext seekEvent(TmfTimestamp timestamp) { |
| 24 | public TmfTraceContext seekEvent(long position) { |
| 25 | |
| 26 | public TmfEvent getNextEvent(TmfTraceContext context) { |
| 27 | public Object getCurrentLocation() { |
| 28 | |
| 29 | public LttngEvent parseEvent(TmfTraceContext context) { |
| 30 | |
| 31 | public int getCpuNumber() { |
| 32 | */ |
| 33 | |
| 34 | @SuppressWarnings("nls") |
| 35 | public class LTTngTextTraceTest extends TestCase { |
| 36 | |
| 37 | private final static String tracepath1 = "traceset/trace-15316events_nolost_newformat.txt"; |
| 38 | private final static String wrongTracePath = "/somewhere/that/does/not/exist"; |
| 39 | |
| 40 | private final static int traceCpuNumber = 1; |
| 41 | |
| 42 | private final static boolean skipIndexing = true; |
| 43 | |
| 44 | private final static long firstEventTimestamp = 13589759412128L; |
| 45 | private final static long secondEventTimestamp = 13589759419903L; |
| 46 | private final static Long locationAfterFirstEvent = 311L; |
| 47 | |
| 48 | private final static String tracename = "traceset/trace-15316events_nolost_newformat"; |
| 49 | |
| 50 | private final static long indexToSeekFirst = 0; |
| 51 | private final static Long locationToSeekFirst = 0L; |
| 52 | private final static long contextValueAfterFirstEvent = 13589759412128L; |
| 53 | private final static String firstEventReference = tracename + "/metadata_0"; |
| 54 | |
| 55 | private final static long timestampToSeekTest1 = 13589826657302L; |
| 56 | private final static Long indexToSeekTest1 = 7497L; |
| 57 | private final static long locationToSeekTest1 = 2177044; |
| 58 | private final static long contextValueAfterSeekTest1 = 13589826657302L; |
| 59 | private final static String seek1EventReference = tracename + "/vm_state_0"; |
| 60 | |
| 61 | private final static long timestampToSeekLast = 13589906758692L; |
| 62 | private final static Long indexToSeekLast = 15315L; |
| 63 | private final static long locationToSeekLast = 4420634; |
| 64 | private final static long contextValueAfterSeekLast = 13589906758692L; |
| 65 | private final static String seekLastEventReference = tracename + "/kernel_0"; |
| 66 | |
| 67 | private static LTTngTextTrace testStream = null; |
| 68 | |
| 69 | private synchronized LTTngTextTrace prepareStreamToTest() { |
| 70 | if (testStream == null) { |
| 71 | try { |
| 72 | URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null); |
| 73 | File testfile = new File(FileLocator.toFileURL(location).toURI()); |
| 74 | LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getPath()); |
| 75 | testStream = tmpStream; |
| 76 | } catch (Exception e) { |
| 77 | System.out.println("ERROR : Could not open " + tracepath1); |
| 78 | testStream = null; |
| 79 | } |
| 80 | } else { |
| 81 | testStream.seekEvent(0); |
| 82 | } |
| 83 | |
| 84 | return testStream; |
| 85 | } |
| 86 | |
| 87 | public void testTraceConstructorFailure() { |
| 88 | // Default constructor |
| 89 | // Test constructor with argument on a wrong tracepath, skipping |
| 90 | // indexing |
| 91 | try { |
| 92 | LTTngTextTrace testStream = new LTTngTextTrace(wrongTracePath, skipIndexing); |
| 93 | fail("Construction with wrong tracepath should fail!"); |
| 94 | testStream.dispose(); |
| 95 | } catch (Exception e) { |
| 96 | } |
| 97 | } |
| 98 | /* |
| 99 | public void testTraceConstructor() { |
| 100 | // Test constructor with argument on a correct tracepath, skipping |
| 101 | // indexing |
| 102 | try { |
| 103 | URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null); |
| 104 | File testfile = new File(FileLocator.toFileURL(location).toURI()); |
| 105 | LTTngTextTrace testStream = new LTTngTextTrace(testfile.getPath(), skipIndexing); |
| 106 | testStream.dispose(); |
| 107 | } catch (Exception e) { |
| 108 | fail("Construction with correct tracepath failed!"); |
| 109 | } |
| 110 | } |
| 111 | */ |
| 112 | public void testGetNextEvent() { |
| 113 | TmfEvent tmpEvent = null; |
| 114 | LTTngTextTrace testStream1 = prepareStreamToTest(); |
| 115 | |
| 116 | TmfContext tmpContext = new TmfContext(null, 0); |
| 117 | // We should be at the beginning of the trace, so we will just read the |
| 118 | // first event now |
| 119 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 120 | assertNotSame("tmpEvent is null after first getNextEvent()", null, tmpEvent); |
| 121 | assertEquals("tmpEvent has wrong timestamp after first getNextEvent()", firstEventTimestamp, tmpEvent.getTimestamp().getValue()); |
| 122 | |
| 123 | // Read the next event as well |
| 124 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 125 | assertNotSame("tmpEvent is null after second getNextEvent()", null, tmpEvent); |
| 126 | assertEquals("tmpEvent has wrong timestamp after second getNextEvent()", secondEventTimestamp, tmpEvent.getTimestamp().getValue()); |
| 127 | } |
| 128 | |
| 129 | public void testParseEvent() { |
| 130 | TmfEvent tmpEvent = null; |
| 131 | LTTngTextTrace testStream1 = prepareStreamToTest(); |
| 132 | |
| 133 | TmfContext tmpContext = new TmfContext(null, 0); |
| 134 | // We should be at the beginning of the trace, so we will just parse the |
| 135 | // first event now |
| 136 | tmpEvent = testStream1.parseEvent(tmpContext); |
| 137 | assertNotSame("tmpEvent is null after first parseEvent()", null, tmpEvent); |
| 138 | assertEquals("tmpEvent has wrong timestamp after first parseEvent()", firstEventTimestamp, tmpEvent.getTimestamp().getValue()); |
| 139 | |
| 140 | // Use parseEvent again. Should be the same event |
| 141 | tmpEvent = testStream1.parseEvent(tmpContext); |
| 142 | assertNotSame("tmpEvent is null after first parseEvent()", null, tmpEvent); |
| 143 | assertEquals("tmpEvent has wrong timestamp after first parseEvent()", firstEventTimestamp, tmpEvent.getTimestamp().getValue()); |
| 144 | } |
| 145 | |
| 146 | public void testSeekEventTimestamp() { |
| 147 | TmfEvent tmpEvent = null; |
| 148 | TmfContext tmpContext = new TmfContext(null, 0); |
| 149 | LTTngTextTrace testStream1 = prepareStreamToTest(); |
| 150 | |
| 151 | // We should be at the beginning of the trace, we will seek at a certain |
| 152 | // timestamp |
| 153 | tmpContext = testStream1.seekEvent(new TmfTimestamp(timestampToSeekTest1, (byte) -9, 0)); |
| 154 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 155 | assertNotSame("tmpContext is null after first seekEvent()", null, tmpContext); |
| 156 | assertEquals("tmpContext has wrong timestamp after first seekEvent()", contextValueAfterSeekTest1, tmpEvent.getTimestamp().getValue()); |
| 157 | assertNotSame("tmpEvent is null after first seekEvent()", null, tmpEvent); |
| 158 | assertTrue("tmpEvent has wrong reference after first seekEvent()", ((String) tmpEvent.getReference().getReference()).contains(seek1EventReference)); |
| 159 | |
| 160 | // Seek to the last timestamp |
| 161 | tmpContext = testStream1.seekEvent(new TmfTimestamp(timestampToSeekLast, (byte) -9, 0)); |
| 162 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 163 | assertNotSame("tmpContext is null after seekEvent() to last", null, tmpContext); |
| 164 | assertEquals("tmpContext has wrong timestamp after seekEvent() to last", contextValueAfterSeekLast, tmpEvent.getTimestamp().getValue()); |
| 165 | assertNotSame("tmpEvent is null after seekEvent() to last ", null, tmpEvent); |
| 166 | assertTrue("tmpEvent has wrong reference after seekEvent() to last", ((String) tmpEvent.getReference().getReference()).contains(seekLastEventReference)); |
| 167 | |
| 168 | // Seek to the first timestamp (startTime) |
| 169 | tmpContext = testStream1.seekEvent(new TmfTimestamp(firstEventTimestamp, (byte) -9, 0)); |
| 170 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 171 | assertNotSame("tmpEvent is null after seekEvent() to start ", null, tmpEvent); |
| 172 | assertTrue("tmpEvent has wrong reference after seekEvent() to start", ((String) tmpEvent.getReference().getReference()).contains(firstEventReference)); |
| 173 | assertNotSame("tmpContext is null after seekEvent() to first", null, tmpContext); |
| 174 | assertEquals("tmpContext has wrong timestamp after seekEvent() to first", contextValueAfterFirstEvent, tmpEvent.getTimestamp().getValue()); |
| 175 | } |
| 176 | |
| 177 | public void testSeekEventIndex() { |
| 178 | TmfEvent tmpEvent = null; |
| 179 | TmfContext tmpContext = new TmfContext(null, 0); |
| 180 | LTTngTextTrace testStream1 = prepareStreamToTest(); |
| 181 | |
| 182 | // We should be at the beginning of the trace, we will seek at a certain |
| 183 | // timestamp |
| 184 | tmpContext = testStream1.seekEvent(indexToSeekTest1); |
| 185 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 186 | assertNotSame("tmpContext is null after first seekEvent()", null, tmpContext); |
| 187 | assertEquals("tmpContext has wrong timestamp after first seekEvent()", contextValueAfterSeekTest1, tmpEvent.getTimestamp().getValue()); |
| 188 | assertNotSame("tmpEvent is null after first seekEvent()", null, tmpEvent); |
| 189 | assertTrue("tmpEvent has wrong reference after first seekEvent()", ((String) tmpEvent.getReference().getReference()).contains(seek1EventReference)); |
| 190 | |
| 191 | // Seek to the last timestamp |
| 192 | tmpContext = testStream1.seekEvent(indexToSeekLast); |
| 193 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 194 | assertNotSame("tmpContext is null after first seekEvent()", null, tmpContext); |
| 195 | assertEquals("tmpContext has wrong timestamp after first seekEvent()", contextValueAfterSeekLast, tmpEvent.getTimestamp().getValue()); |
| 196 | assertNotSame("tmpEvent is null after seekEvent() to last ", null, tmpEvent); |
| 197 | assertTrue("tmpEvent has wrong reference after seekEvent() to last", ((String) tmpEvent.getReference().getReference()).contains(seekLastEventReference)); |
| 198 | |
| 199 | // Seek to the first timestamp (startTime) |
| 200 | tmpContext = testStream1.seekEvent(indexToSeekFirst); |
| 201 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 202 | assertNotSame("tmpContext is null after first seekEvent()", null, tmpContext); |
| 203 | assertEquals("tmpContext has wrong timestamp after first seekEvent()", contextValueAfterFirstEvent, tmpEvent.getTimestamp().getValue()); |
| 204 | assertNotSame("tmpEvent is null after seekEvent() to start ", null, tmpEvent); |
| 205 | assertTrue("tmpEvent has wrong reference after seekEvent() to start", ((String) tmpEvent.getReference().getReference()).contains(firstEventReference)); |
| 206 | } |
| 207 | |
| 208 | public void testSeekLocation() { |
| 209 | TmfEvent tmpEvent = null; |
| 210 | TmfContext tmpContext = new TmfContext(null, 0); |
| 211 | LTTngTextTrace testStream1 = prepareStreamToTest(); |
| 212 | |
| 213 | // We should be at the beginning of the trace, we will seek at a certain |
| 214 | // timestamp |
| 215 | tmpContext = testStream1.seekLocation(new TmfLocation<Long>(locationToSeekTest1)); |
| 216 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 217 | assertNotSame("tmpContext is null after first seekLocation()", null, tmpContext); |
| 218 | assertEquals("tmpContext has wrong timestamp after first seekLocation()", contextValueAfterSeekTest1, tmpEvent.getTimestamp().getValue()); |
| 219 | assertNotSame("tmpEvent is null after first seekLocation()", null, tmpEvent); |
| 220 | assertTrue("tmpEvent has wrong reference after first seekLocation()", ((String) tmpEvent.getReference().getReference()).contains(seek1EventReference)); |
| 221 | |
| 222 | // Seek to the last timestamp |
| 223 | tmpContext = testStream1.seekLocation(new TmfLocation<Long>(locationToSeekLast)); |
| 224 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 225 | assertNotSame("tmpContext is null after first seekLocation()", null, tmpContext); |
| 226 | assertEquals("tmpContext has wrong timestamp after first seekLocation()", contextValueAfterSeekLast, tmpEvent.getTimestamp().getValue()); |
| 227 | assertNotSame("tmpEvent is null after seekLocation() to last ", null, tmpEvent); |
| 228 | assertTrue("tmpEvent has wrong reference after seekLocation() to last", ((String) tmpEvent.getReference().getReference()).contains(seekLastEventReference)); |
| 229 | |
| 230 | // Seek to the first timestamp (startTime) |
| 231 | tmpContext = testStream1.seekLocation(new TmfLocation<Long>(locationToSeekFirst)); |
| 232 | tmpEvent = testStream1.getNextEvent(tmpContext); |
| 233 | assertNotSame("tmpContext is null after first seekLocation()", null, tmpContext); |
| 234 | assertEquals("tmpContext has wrong timestamp after first seekLocation()", contextValueAfterFirstEvent, tmpEvent.getTimestamp().getValue()); |
| 235 | assertNotSame("tmpEvent is null after seekLocation() to start ", null, tmpEvent); |
| 236 | assertTrue("tmpEvent has wrong reference after seekLocation() to start", ((String) tmpEvent.getReference().getReference()).contains(firstEventReference)); |
| 237 | } |
| 238 | |
| 239 | @SuppressWarnings("unchecked") |
| 240 | public void testGetter() { |
| 241 | TmfEvent tmpEvent = null; |
| 242 | LTTngTextTrace testStream1 = prepareStreamToTest(); |
| 243 | TmfContext tmpContext = new TmfContext(null, 0); |
| 244 | |
| 245 | // Move to the first event to have something to play with |
| 246 | tmpEvent = testStream1.parseEvent(tmpContext); |
| 247 | |
| 248 | // Test current event |
| 249 | assertNotSame("tmpEvent is null after first event", null, tmpEvent); |
| 250 | assertTrue("tmpEvent has wrong reference after first event", ((String) tmpEvent.getReference().getReference()).contains(firstEventReference)); |
| 251 | assertNotSame("tmpContext is null after first seekEvent()", null, testStream1.getCurrentLocation()); |
| 252 | assertEquals("tmpContext has wrong timestamp after first seekEvent()", locationAfterFirstEvent, ((TmfLocation<Long>) testStream1.getCurrentLocation()).getLocation()); |
| 253 | // Test CPU number of the trace |
| 254 | assertSame("getCpuNumber() return wrong number of cpu", traceCpuNumber, testStream1.getCpuNumber()); |
| 255 | } |
| 256 | |
| 257 | public void testToString() { |
| 258 | LTTngTextTrace testStream1 = prepareStreamToTest(); |
| 259 | |
| 260 | // Move to the first event to have something to play with |
| 261 | testStream1.parseEvent(new TmfContext(null, 0)); |
| 262 | |
| 263 | // Just make sure toString() does not return null or the java reference |
| 264 | assertNotSame("toString returned null", null, testStream1.toString()); |
| 265 | assertNotSame("toString is not overridded!", testStream1.getClass().getName() + '@' + Integer.toHexString(testStream1.hashCode()), testStream1.toString()); |
| 266 | } |
| 267 | |
| 268 | } |