tmf: update calculation of confidence level of text trace validation
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / text / TextTraceTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.trace.text;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.FileLocator;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
31 import org.eclipse.core.runtime.preferences.InstanceScope;
32 import org.eclipse.linuxtools.internal.tmf.core.Activator;
33 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
34 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest.ExecutionType;
35 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
36 import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
37 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimePreferencesConstants;
38 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
39 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
40 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
41 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat;
42 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
43 import org.eclipse.linuxtools.tmf.core.trace.TraceValidationStatus;
44 import org.eclipse.linuxtools.tmf.core.trace.text.TextTraceEventContent;
45 import org.eclipse.linuxtools.tmf.tests.stubs.trace.text.SyslogEvent;
46 import org.eclipse.linuxtools.tmf.tests.stubs.trace.text.SyslogEventType.Index;
47 import org.eclipse.linuxtools.tmf.tests.stubs.trace.text.SyslogTrace;
48 import org.junit.AfterClass;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51
52 @SuppressWarnings({ "nls", "javadoc"})
53 public class TextTraceTest {
54
55 // ------------------------------------------------------------------------
56 // Variables
57 // ------------------------------------------------------------------------
58
59 private static final String NAME = "syslog";
60 private static final String PATH = "testfiles/" + NAME;
61
62 private static final String OTHER_PATH = "testfiles/" + "A-Test-10K";
63
64 private static SyslogTrace fTrace = null;
65 private static File fTestFile = null;
66
67 // ------------------------------------------------------------------------
68 // Housekeeping
69 // ------------------------------------------------------------------------
70
71 @BeforeClass
72 public static void setUp() throws Exception {
73 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
74 defaultPreferences.put(ITmfTimePreferencesConstants.DATIME, "MMM d HH:mm:ss");
75 defaultPreferences.put(ITmfTimePreferencesConstants.SUBSEC, ITmfTimePreferencesConstants.SUBSEC_NO_FMT);
76 TmfTimestampFormat.updateDefaultFormats();
77
78 if (fTrace == null) {
79 try {
80 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(PATH), null);
81 URI uri = FileLocator.toFileURL(location).toURI();
82 fTestFile = new File(uri);
83
84 fTrace = new SyslogTrace();
85 IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(PATH));
86 fTrace.initTrace(resource, uri.getPath(), SyslogEvent.class);
87 // Dummy request to force the trace indexing
88 TmfEventRequest request = new TmfEventRequest(
89 SyslogEvent.class,
90 TmfTimeRange.ETERNITY,
91 0,
92 ITmfEventRequest.ALL_DATA,
93 ExecutionType.FOREGROUND) {
94 };
95 fTrace.sendRequest(request);
96 request.waitForCompletion();
97 } catch (URISyntaxException e) {
98 e.printStackTrace();
99 } catch (IOException e) {
100 e.printStackTrace();
101 }
102 }
103 }
104
105 @AfterClass
106 public static void tearDown() {
107 fTrace.dispose();
108 fTrace = null;
109 IEclipsePreferences defaultPreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
110 defaultPreferences.put(ITmfTimePreferencesConstants.DATIME, ITmfTimePreferencesConstants.TIME_HOUR_FMT);
111 defaultPreferences.put(ITmfTimePreferencesConstants.SUBSEC, ITmfTimePreferencesConstants.SUBSEC_NANO_FMT);
112 TmfTimestampFormat.updateDefaultFormats();
113 }
114
115 // ------------------------------------------------------------------------
116 // Constructors
117 // ------------------------------------------------------------------------
118 @Test
119 public void testEmptyConstructor() {
120 SyslogTrace trace = new SyslogTrace();
121 assertEquals("getType", null, trace.getType());
122 assertEquals("getPath", null, trace.getPath());
123 assertEquals("getName", "", trace.getName());
124 assertEquals("getCacheSize", 100, trace.getCacheSize());
125
126 TmfTimestamp initRange = new TmfTimestamp(60, ITmfTimestamp.SECOND_SCALE);
127 assertEquals("getInitialRangeOffset", initRange, trace.getInitialRangeOffset());
128 }
129
130 @Test
131 public void testValidation() throws URISyntaxException, IOException {
132 SyslogTrace trace = new SyslogTrace();
133 String validTracePath = fTestFile.getAbsolutePath();
134 IStatus status = trace.validate(null, validTracePath);
135 assertTrue(status.isOK());
136 assertTrue(status instanceof TraceValidationStatus);
137 assertEquals(180, ((TraceValidationStatus) status).getConfidence());
138
139 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(OTHER_PATH), null);
140 URI uri = FileLocator.toFileURL(location).toURI();
141 File otherFile = new File(uri);
142
143 String validNoConfidenceTrace = otherFile.getAbsolutePath();
144 status = trace.validate(null, validNoConfidenceTrace);
145 assertTrue(status instanceof TraceValidationStatus);
146 assertEquals(0, ((TraceValidationStatus) status).getConfidence());
147 assertTrue(status.isOK());
148
149 String invalidTrace = fTestFile.getParentFile().getAbsolutePath();
150 status = trace.validate(null, invalidTrace);
151 assertFalse(status.isOK());
152 }
153
154 @Test
155 public void testInitTrace() throws Exception {
156 URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(PATH), null);
157 String path = FileLocator.toFileURL(location).toURI().getPath();
158 SyslogTrace trace = new SyslogTrace();
159 IResource resource = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(PATH));
160 trace.initTrace(resource, path, SyslogEvent.class);
161 assertEquals("getType", SyslogEvent.class, trace.getType());
162 assertEquals("getPath", fTestFile.toURI().getPath(), trace.getPath());
163 assertEquals("getName", NAME, trace.getName());
164 assertEquals("getCacheSize", 100, trace.getCacheSize());
165 }
166
167 // ------------------------------------------------------------------------
168 // Indexing
169 // ------------------------------------------------------------------------
170
171 @Test
172 public void testTraceIndexing() {
173 assertEquals("getNbEvents", 6, fTrace.getNbEvents());
174
175 TmfTimestamp initRange = new TmfTimestamp(60, ITmfTimestamp.SECOND_SCALE);
176 assertEquals("getInitialRangeOffset", initRange, fTrace.getInitialRangeOffset());
177 }
178
179 // ------------------------------------------------------------------------
180 // Parsing
181 // ------------------------------------------------------------------------
182
183 @Test
184 public void testTraceParsing() {
185 ITmfContext context = fTrace.seekEvent(0);
186 SyslogEvent event = fTrace.getNext(context);
187 TextTraceEventContent content = event.getContent();
188 assertEquals("getTimestamp", "Jan 1 01:01:01", event.getTimestamp().toString());
189 assertEquals("getField:TIMESTAMP", "Jan 1 01:01:01", content.getFieldValue(Index.TIMESTAMP));
190 assertEquals("getField:HOST", "HostA", content.getFieldValue(Index.HOST));
191 assertEquals("getField:LOGGER", "LoggerA", content.getFieldValue(Index.LOGGER));
192 assertEquals("getField:MESSAGE", "Message A", content.getFieldValue(Index.MESSAGE).toString());
193 event = fTrace.getNext(context);
194 content = event.getContent();
195 assertEquals("getTimestamp", "Jan 1 02:02:02", event.getTimestamp().toString());
196 assertEquals("getField:TIMESTAMP", "Jan 1 02:02:02", content.getFieldValue(Index.TIMESTAMP));
197 assertEquals("getField:HOST", "HostB", content.getFieldValue(Index.HOST));
198 assertEquals("getField:LOGGER", "LoggerB", content.getFieldValue(Index.LOGGER));
199 assertEquals("getField:MESSAGE", "Message B", content.getFieldValue(Index.MESSAGE).toString());
200 event = fTrace.getNext(context);
201 content = event.getContent();
202 assertEquals("getTimestamp", "Jan 1 03:03:03", event.getTimestamp().toString());
203 assertEquals("getField:TIMESTAMP", "Jan 1 03:03:03", content.getFieldValue(Index.TIMESTAMP));
204 assertEquals("getField:HOST", "HostC", content.getFieldValue(Index.HOST));
205 assertEquals("getField:LOGGER", "LoggerC", content.getFieldValue(Index.LOGGER));
206 assertEquals("getField:MESSAGE", "Message C", content.getFieldValue(Index.MESSAGE).toString());
207 event = fTrace.getNext(context);
208 content = event.getContent();
209 assertEquals("getTimestamp", "Jan 1 04:04:04", event.getTimestamp().toString());
210 assertEquals("getField:TIMESTAMP", "Jan 1 04:04:04", content.getFieldValue(Index.TIMESTAMP));
211 assertEquals("getField:HOST", "HostD", content.getFieldValue(Index.HOST));
212 assertEquals("getField:LOGGER", "LoggerD", content.getFieldValue(Index.LOGGER));
213 assertEquals("getField:MESSAGE", "Message D", content.getFieldValue(Index.MESSAGE).toString());
214 event = fTrace.getNext(context);
215 content = event.getContent();
216 assertEquals("getTimestamp", "Jan 1 05:05:05", event.getTimestamp().toString());
217 assertEquals("getField:TIMESTAMP", "Jan 1 05:05:05", content.getFieldValue(Index.TIMESTAMP));
218 assertEquals("getField:HOST", "HostE", content.getFieldValue(Index.HOST));
219 assertEquals("getField:LOGGER", "LoggerE", content.getFieldValue(Index.LOGGER));
220 assertEquals("getField:MESSAGE", "", content.getFieldValue(Index.MESSAGE).toString());
221 event = fTrace.getNext(context);
222 content = event.getContent();
223 assertEquals("getTimestamp", "Jan 1 06:06:06", event.getTimestamp().toString());
224 assertEquals("getField:TIMESTAMP", "Jan 1 06:06:06", content.getFieldValue(Index.TIMESTAMP));
225 assertEquals("getField:HOST", "HostF", content.getFieldValue(Index.HOST));
226 assertEquals("getField:LOGGER", "LoggerF", content.getFieldValue(Index.LOGGER));
227 assertEquals("getField:MESSAGE", "Message F", content.getFieldValue(Index.MESSAGE).toString());
228 event = fTrace.getNext(context);
229 assertEquals("event", null, event);
230 context.dispose();
231 }
232
233 @Test
234 public void testLocationRatio() {
235 ITmfContext context = fTrace.seekEvent(3);
236 double ratio = fTrace.getLocationRatio(context.getLocation());
237 SyslogEvent event = fTrace.getNext(context);
238 TextTraceEventContent content = event.getContent();
239 Object logger = content.getFieldValue(Index.LOGGER);
240 context.dispose();
241 context = fTrace.seekEvent(ratio);
242 event = fTrace.getNext(context);
243 content = event.getContent();
244 assertEquals("getField:LOGGER", logger.toString(), content.getFieldValue(Index.LOGGER).toString());
245 context.dispose();
246 context = fTrace.seekEvent(0.0);
247 event = fTrace.getNext(context);
248 content = event.getContent();
249 assertEquals("getField:LOGGER", "LoggerA", content.getFieldValue(Index.LOGGER));
250 context.dispose();
251 context = fTrace.seekEvent(1.0);
252 event = fTrace.getNext(context);
253 assertEquals("event", null, event);
254 context.dispose();
255 }
256 }
This page took 0.043502 seconds and 5 git commands to generate.