1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 * Patrick Tasse - Move field declarations to trace
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
.text
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertNotNull
;
19 import static org
.junit
.Assert
.assertTrue
;
22 import java
.io
.IOException
;
24 import java
.net
.URISyntaxException
;
26 import java
.util
.Locale
;
28 import org
.eclipse
.core
.resources
.IResource
;
29 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
30 import org
.eclipse
.core
.runtime
.FileLocator
;
31 import org
.eclipse
.core
.runtime
.IStatus
;
32 import org
.eclipse
.core
.runtime
.Path
;
33 import org
.eclipse
.core
.runtime
.preferences
.IEclipsePreferences
;
34 import org
.eclipse
.core
.runtime
.preferences
.InstanceScope
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
.ExecutionType
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimePreferencesConstants
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestampFormat
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.text
.TextTraceEventContent
;
48 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.text
.SyslogEvent
;
49 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.text
.SyslogTrace
;
50 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.text
.SyslogTrace
.Field
;
51 import org
.junit
.AfterClass
;
52 import org
.junit
.BeforeClass
;
53 import org
.junit
.Test
;
55 @SuppressWarnings({ "nls", "javadoc"})
56 public class TextTraceTest
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 private static final String NAME
= "syslog";
63 private static final String PATH
= "testfiles/" + NAME
;
65 private static final String OTHER_PATH
= "testfiles/" + "A-Test-10K";
67 private static SyslogTrace fTrace
= null;
68 private static File fTestFile
= null;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 public static void setUp() throws Exception
{
76 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
77 defaultPreferences
.put(ITmfTimePreferencesConstants
.DATIME
, "MMM d HH:mm:ss");
78 defaultPreferences
.put(ITmfTimePreferencesConstants
.SUBSEC
, ITmfTimePreferencesConstants
.SUBSEC_NO_FMT
);
79 defaultPreferences
.put(ITmfTimePreferencesConstants
.LOCALE
, Locale
.CANADA
.toLanguageTag());
80 TmfTimestampFormat
.updateDefaultFormats();
84 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(PATH
), null);
85 URI uri
= FileLocator
.toFileURL(location
).toURI();
86 fTestFile
= new File(uri
);
88 fTrace
= new SyslogTrace();
89 IResource resource
= ResourcesPlugin
.getWorkspace().getRoot().getFile(new Path(PATH
));
90 fTrace
.initTrace(resource
, uri
.getPath(), SyslogEvent
.class);
91 // Dummy request to force the trace indexing
92 TmfEventRequest request
= new TmfEventRequest(
94 TmfTimeRange
.ETERNITY
,
96 ITmfEventRequest
.ALL_DATA
,
97 ExecutionType
.FOREGROUND
) {
99 fTrace
.sendRequest(request
);
100 request
.waitForCompletion();
101 } catch (URISyntaxException e
) {
103 } catch (IOException e
) {
110 public static void tearDown() {
113 IEclipsePreferences defaultPreferences
= InstanceScope
.INSTANCE
.getNode(Activator
.PLUGIN_ID
);
114 defaultPreferences
.put(ITmfTimePreferencesConstants
.DATIME
, ITmfTimePreferencesConstants
.TIME_HOUR_FMT
);
115 defaultPreferences
.put(ITmfTimePreferencesConstants
.SUBSEC
, ITmfTimePreferencesConstants
.SUBSEC_NANO_FMT
);
116 defaultPreferences
.put(ITmfTimePreferencesConstants
.LOCALE
, Locale
.getDefault().toLanguageTag());
117 TmfTimestampFormat
.updateDefaultFormats();
120 // ------------------------------------------------------------------------
122 // ------------------------------------------------------------------------
124 public void testEmptyConstructor() {
125 SyslogTrace trace
= new SyslogTrace();
126 assertEquals("getEventType", null, trace
.getEventType());
127 assertEquals("getPath", null, trace
.getPath());
128 assertEquals("getName", "", trace
.getName());
129 assertEquals("getCacheSize", 100, trace
.getCacheSize());
131 ITmfTimestamp initRange
= TmfTimestamp
.fromSeconds(60);
132 assertEquals("getInitialRangeOffset", initRange
, trace
.getInitialRangeOffset());
136 public void testValidation() throws URISyntaxException
, IOException
{
137 SyslogTrace trace
= new SyslogTrace();
138 String validTracePath
= fTestFile
.getAbsolutePath();
139 IStatus status
= trace
.validate(null, validTracePath
);
140 assertTrue(status
.isOK());
141 assertTrue(status
instanceof TraceValidationStatus
);
142 assertEquals(185, ((TraceValidationStatus
) status
).getConfidence());
144 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(OTHER_PATH
), null);
145 URI uri
= FileLocator
.toFileURL(location
).toURI();
146 File otherFile
= new File(uri
);
148 String validNoConfidenceTrace
= otherFile
.getAbsolutePath();
149 status
= trace
.validate(null, validNoConfidenceTrace
);
150 assertTrue(status
instanceof TraceValidationStatus
);
151 assertEquals(0, ((TraceValidationStatus
) status
).getConfidence());
152 assertTrue(status
.isOK());
154 String invalidTrace
= fTestFile
.getParentFile().getAbsolutePath();
155 status
= trace
.validate(null, invalidTrace
);
156 assertFalse(status
.isOK());
160 public void testInitTrace() throws Exception
{
161 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(PATH
), null);
162 String path
= FileLocator
.toFileURL(location
).toURI().getPath();
163 SyslogTrace trace
= new SyslogTrace();
164 IResource resource
= ResourcesPlugin
.getWorkspace().getRoot().getFile(new Path(PATH
));
165 trace
.initTrace(resource
, path
, SyslogEvent
.class);
166 assertEquals("getEventType", SyslogEvent
.class, trace
.getEventType());
167 assertEquals("getPath", fTestFile
.toURI().getPath(), trace
.getPath());
168 assertEquals("getName", NAME
, trace
.getName());
169 assertEquals("getCacheSize", 100, trace
.getCacheSize());
172 // ------------------------------------------------------------------------
174 // ------------------------------------------------------------------------
177 public void testTraceIndexing() {
178 assertEquals("getNbEvents", 6, fTrace
.getNbEvents());
180 ITmfTimestamp initRange
= TmfTimestamp
.fromSeconds(60);
181 assertEquals("getInitialRangeOffset", initRange
, fTrace
.getInitialRangeOffset());
184 // ------------------------------------------------------------------------
186 // ------------------------------------------------------------------------
189 public void testTraceParsing() {
190 ITmfContext context
= fTrace
.seekEvent(0);
191 SyslogEvent event
= fTrace
.getNext(context
);
192 assertNotNull(event
);
193 TextTraceEventContent content
= event
.getContent();
194 assertEquals("getTimestamp", "Jan 1 01:01:01", event
.getTimestamp().toString());
195 assertEquals("getField:HOST", "HostA", content
.getFieldValue(Field
.HOST
));
196 assertEquals("getField:LOGGER", "LoggerA", content
.getFieldValue(Field
.LOGGER
));
197 assertEquals("getField:FILE", "SourceFileA", content
.getFieldValue(Field
.FILE
));
198 assertEquals("getField:LINE", "4", content
.getFieldValue(Field
.LINE
));
199 assertEquals("getField:MESSAGE", "Message A", content
.getFieldValue(Field
.MESSAGE
).toString());
200 event
= fTrace
.getNext(context
);
201 assertNotNull(event
);
202 content
= event
.getContent();
203 assertEquals("getTimestamp", "Jan 1 02:02:02", event
.getTimestamp().toString());
204 assertEquals("getField:HOST", "HostB", content
.getFieldValue(Field
.HOST
));
205 assertEquals("getField:LOGGER", "LoggerB", content
.getFieldValue(Field
.LOGGER
));
206 assertEquals("getField:FILE", "SourceFileB", content
.getFieldValue(Field
.FILE
));
207 assertEquals("getField:LINE", "5", content
.getFieldValue(Field
.LINE
));
208 assertEquals("getField:MESSAGE", "Message B", content
.getFieldValue(Field
.MESSAGE
).toString());
209 event
= fTrace
.getNext(context
);
210 assertNotNull(event
);
211 content
= event
.getContent();
212 assertEquals("getTimestamp", "Jan 1 03:03:03", event
.getTimestamp().toString());
213 assertEquals("getField:HOST", "HostC", content
.getFieldValue(Field
.HOST
));
214 assertEquals("getField:LOGGER", "LoggerC", content
.getFieldValue(Field
.LOGGER
));
215 assertEquals("getField:FILE", "SourceFileC", content
.getFieldValue(Field
.FILE
));
216 assertEquals("getField:LINE", "6", content
.getFieldValue(Field
.LINE
));
217 assertEquals("getField:MESSAGE", "Message C", content
.getFieldValue(Field
.MESSAGE
).toString());
218 event
= fTrace
.getNext(context
);
219 assertNotNull(event
);
220 content
= event
.getContent();
221 assertEquals("getTimestamp", "Jan 1 04:04:04", event
.getTimestamp().toString());
222 assertEquals("getField:HOST", "HostD", content
.getFieldValue(Field
.HOST
));
223 assertEquals("getField:LOGGER", "LoggerD", content
.getFieldValue(Field
.LOGGER
));
224 assertEquals("getField:FILE", "SourceFileD", content
.getFieldValue(Field
.FILE
));
225 assertEquals("getField:LINE", "7", content
.getFieldValue(Field
.LINE
));
226 assertEquals("getField:MESSAGE", "Message D", content
.getFieldValue(Field
.MESSAGE
).toString());
227 event
= fTrace
.getNext(context
);
228 assertNotNull(event
);
229 content
= event
.getContent();
230 assertEquals("getTimestamp", "Jan 1 05:05:05", event
.getTimestamp().toString());
231 assertEquals("getField:HOST", "HostE", content
.getFieldValue(Field
.HOST
));
232 assertEquals("getField:LOGGER", "LoggerE", content
.getFieldValue(Field
.LOGGER
));
233 assertEquals("getField:FILE", "SourceFileE", content
.getFieldValue(Field
.FILE
));
234 assertEquals("getField:LINE", "8", content
.getFieldValue(Field
.LINE
));
235 assertEquals("getField:MESSAGE", "", content
.getFieldValue(Field
.MESSAGE
).toString());
236 event
= fTrace
.getNext(context
);
237 assertNotNull(event
);
238 content
= event
.getContent();
239 assertEquals("getTimestamp", "Jan 1 06:06:06", event
.getTimestamp().toString());
240 assertEquals("getField:HOST", "HostF", content
.getFieldValue(Field
.HOST
));
241 assertEquals("getField:LOGGER", "LoggerF", content
.getFieldValue(Field
.LOGGER
));
242 assertEquals("getField:FILE", "SourceFileF", content
.getFieldValue(Field
.FILE
));
243 assertEquals("getField:LINE", "9", content
.getFieldValue(Field
.LINE
));
244 assertEquals("getField:MESSAGE", "Message F", content
.getFieldValue(Field
.MESSAGE
).toString());
245 event
= fTrace
.getNext(context
);
246 assertEquals("event", null, event
);
251 public void testLocationRatio() {
252 ITmfContext context
= fTrace
.seekEvent(3);
253 double ratio
= fTrace
.getLocationRatio(context
.getLocation());
254 SyslogEvent event
= fTrace
.getNext(context
);
255 assertNotNull(event
);
256 TextTraceEventContent content
= event
.getContent();
257 Object logger
= content
.getFieldValue(Field
.LOGGER
);
259 context
= fTrace
.seekEvent(ratio
);
260 event
= fTrace
.getNext(context
);
261 assertNotNull(event
);
262 content
= event
.getContent();
263 assertEquals("getField:LOGGER", logger
.toString(), content
.getFieldValue(Field
.LOGGER
).toString());
265 context
= fTrace
.seekEvent(0.0);
266 event
= fTrace
.getNext(context
);
267 assertNotNull(event
);
268 content
= event
.getContent();
269 assertEquals("getField:LOGGER", "LoggerA", content
.getFieldValue(Field
.LOGGER
));
271 context
= fTrace
.seekEvent(1.0);
272 event
= fTrace
.getNext(context
);
273 assertEquals("event", null, event
);