custom parsers: Add unit tests for custom event names
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / parsers / custom / AbstractCustomTraceDataTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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
10 package org.eclipse.tracecompass.tmf.core.tests.parsers.custom;
11
12 import java.io.File;
13 import java.io.IOException;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
17 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
18 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
19 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
20 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24
25 /**
26 * Abstract class to test custom traces
27 *
28 * @author Geneviève Bastien
29 */
30 public abstract class AbstractCustomTraceDataTest {
31
32 /**
33 * Time format use for event creation
34 */
35 protected static final String TIMESTAMP_FORMAT = "dd/MM/yyyy HH:mm:ss:SSS";
36
37 /**
38 * Block size used for the indexer
39 */
40 protected static final int BLOCK_SIZE = 100;
41
42 /** The trace directory */
43 protected static final String TRACE_DIRECTORY = TmfTraceManager.getTemporaryDirPath() + File.separator + "dummyTrace";
44
45
46 /**
47 * Interface to be implemented by concrete test cases to get the necessary
48 * test data
49 *
50 * @author Geneviève Bastien
51 */
52 protected static interface ICustomTestData {
53 /**
54 * Get the trace for this test case
55 *
56 * @return The initialized trace
57 * @throws IOException
58 * If an exception occurred while getting the trace
59 * @throws TmfTraceException
60 * If an exception occurred while getting the trace
61 */
62 public ITmfTrace getTrace() throws IOException, TmfTraceException;
63
64 /**
65 * Validate the event for this test case. This method should contain the
66 * necessary asserts.
67 *
68 * @param event
69 * The event to validate
70 */
71 public void validateEvent(ITmfEvent event);
72
73 /**
74 * Validate the event count. This method will be called after reading
75 * the whole trace.
76 *
77 * @param eventCount
78 * The event count to validate
79 */
80 public void validateEventCount(int eventCount);
81 }
82
83 private final @NonNull ICustomTestData fTestData;
84 private ITmfTrace fTrace;
85
86 /**
87 * Constructor
88 *
89 * @param data
90 * The custom trace test data
91 */
92 public AbstractCustomTraceDataTest(@NonNull ICustomTestData data) {
93 fTestData = data;
94 }
95
96 /**
97 * Setup the test
98 * @throws Exception Exceptions that occurred during setup
99 */
100 @Before
101 public void setUp() throws Exception {
102 setupTrace();
103 }
104
105 private synchronized void setupTrace() throws Exception {
106 File traceDirectory = new File(TRACE_DIRECTORY);
107 if (traceDirectory.exists()) {
108 traceDirectory.delete();
109 }
110 traceDirectory.mkdir();
111 if (fTrace == null) {
112 fTrace = fTestData.getTrace();
113 }
114 }
115
116 /**
117 * Tear down the test
118 */
119 @After
120 public void tearDown() {
121 String directory = TmfTraceManager.getSupplementaryFileDir(fTrace);
122 try {
123 fTrace.dispose();
124 fTrace = null;
125 } finally {
126 File dir = new File(directory);
127 if (dir.exists()) {
128 File[] files = dir.listFiles();
129 for (File file : files) {
130 file.delete();
131 }
132 dir.delete();
133 }
134
135 File trace = new File(TRACE_DIRECTORY);
136 if (trace.exists()) {
137 trace.delete();
138 }
139 }
140
141 }
142
143 /**
144 * Test reading the events of the trace
145 */
146 @Test
147 public void testReadingEvents() {
148 ITmfTrace trace = fTrace;
149
150 ITmfContext ctx = trace.seekEvent(0L);
151 int eventCount = 0;
152 ITmfEvent event = trace.getNext(ctx);
153 while (event != null) {
154 fTestData.validateEvent(event);
155 eventCount++;
156 event = trace.getNext(ctx);
157 }
158 fTestData.validateEventCount(eventCount);
159 }
160
161 }
This page took 0.075816 seconds and 5 git commands to generate.