Commit | Line | Data |
---|---|---|
eadf9801 | 1 | /******************************************************************************* |
5b12450f | 2 | * Copyright (c) 2014, 2015 Ericsson |
eadf9801 BH |
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 | |
5b12450f | 11 | * Patrick Tasse - Move field declarations to trace |
eadf9801 BH |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.core.tests.trace.text; |
eadf9801 BH |
15 | |
16 | import static org.junit.Assert.assertEquals; | |
17 | import static org.junit.Assert.assertFalse; | |
4c4e2816 | 18 | import static org.junit.Assert.assertNotNull; |
eadf9801 BH |
19 | import static org.junit.Assert.assertTrue; |
20 | ||
21 | import java.io.File; | |
22 | import java.io.IOException; | |
23 | import java.net.URI; | |
24 | import java.net.URISyntaxException; | |
25 | import java.net.URL; | |
8765b2d4 | 26 | import java.util.Locale; |
eadf9801 BH |
27 | |
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; | |
2bdf0193 AM |
35 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
36 | import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest; | |
2bdf0193 | 37 | import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest.ExecutionType; |
5b12450f | 38 | import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest; |
2bdf0193 AM |
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; | |
5b12450f | 50 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.text.SyslogTrace.Field; |
eadf9801 BH |
51 | import org.junit.AfterClass; |
52 | import org.junit.BeforeClass; | |
53 | import org.junit.Test; | |
54 | ||
55 | @SuppressWarnings({ "nls", "javadoc"}) | |
56 | public class TextTraceTest { | |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Variables | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
62 | private static final String NAME = "syslog"; | |
63 | private static final String PATH = "testfiles/" + NAME; | |
64 | ||
65 | private static final String OTHER_PATH = "testfiles/" + "A-Test-10K"; | |
66 | ||
67 | private static SyslogTrace fTrace = null; | |
68 | private static File fTestFile = null; | |
69 | ||
70 | // ------------------------------------------------------------------------ | |
71 | // Housekeeping | |
72 | // ------------------------------------------------------------------------ | |
73 | ||
74 | @BeforeClass | |
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); | |
8765b2d4 | 79 | defaultPreferences.put(ITmfTimePreferencesConstants.LOCALE, Locale.CANADA.toLanguageTag()); |
eadf9801 BH |
80 | TmfTimestampFormat.updateDefaultFormats(); |
81 | ||
82 | if (fTrace == null) { | |
83 | try { | |
84 | URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(PATH), null); | |
85 | URI uri = FileLocator.toFileURL(location).toURI(); | |
86 | fTestFile = new File(uri); | |
87 | ||
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( | |
93 | SyslogEvent.class, | |
94 | TmfTimeRange.ETERNITY, | |
95 | 0, | |
96 | ITmfEventRequest.ALL_DATA, | |
97 | ExecutionType.FOREGROUND) { | |
98 | }; | |
99 | fTrace.sendRequest(request); | |
100 | request.waitForCompletion(); | |
101 | } catch (URISyntaxException e) { | |
102 | e.printStackTrace(); | |
103 | } catch (IOException e) { | |
104 | e.printStackTrace(); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | @AfterClass | |
110 | public static void tearDown() { | |
111 | fTrace.dispose(); | |
112 | fTrace = null; | |
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); | |
8765b2d4 | 116 | defaultPreferences.put(ITmfTimePreferencesConstants.LOCALE, Locale.getDefault().toLanguageTag()); |
eadf9801 BH |
117 | TmfTimestampFormat.updateDefaultFormats(); |
118 | } | |
119 | ||
120 | // ------------------------------------------------------------------------ | |
121 | // Constructors | |
122 | // ------------------------------------------------------------------------ | |
123 | @Test | |
124 | public void testEmptyConstructor() { | |
125 | SyslogTrace trace = new SyslogTrace(); | |
ea652979 | 126 | assertEquals("getEventType", null, trace.getEventType()); |
eadf9801 BH |
127 | assertEquals("getPath", null, trace.getPath()); |
128 | assertEquals("getName", "", trace.getName()); | |
129 | assertEquals("getCacheSize", 100, trace.getCacheSize()); | |
130 | ||
131 | TmfTimestamp initRange = new TmfTimestamp(60, ITmfTimestamp.SECOND_SCALE); | |
132 | assertEquals("getInitialRangeOffset", initRange, trace.getInitialRangeOffset()); | |
133 | } | |
134 | ||
135 | @Test | |
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); | |
32528869 | 142 | assertEquals(185, ((TraceValidationStatus) status).getConfidence()); |
eadf9801 BH |
143 | |
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); | |
147 | ||
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()); | |
153 | ||
154 | String invalidTrace = fTestFile.getParentFile().getAbsolutePath(); | |
155 | status = trace.validate(null, invalidTrace); | |
156 | assertFalse(status.isOK()); | |
157 | } | |
158 | ||
159 | @Test | |
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); | |
ea652979 | 166 | assertEquals("getEventType", SyslogEvent.class, trace.getEventType()); |
eadf9801 BH |
167 | assertEquals("getPath", fTestFile.toURI().getPath(), trace.getPath()); |
168 | assertEquals("getName", NAME, trace.getName()); | |
169 | assertEquals("getCacheSize", 100, trace.getCacheSize()); | |
170 | } | |
171 | ||
172 | // ------------------------------------------------------------------------ | |
173 | // Indexing | |
174 | // ------------------------------------------------------------------------ | |
175 | ||
176 | @Test | |
177 | public void testTraceIndexing() { | |
178 | assertEquals("getNbEvents", 6, fTrace.getNbEvents()); | |
179 | ||
180 | TmfTimestamp initRange = new TmfTimestamp(60, ITmfTimestamp.SECOND_SCALE); | |
181 | assertEquals("getInitialRangeOffset", initRange, fTrace.getInitialRangeOffset()); | |
182 | } | |
183 | ||
184 | // ------------------------------------------------------------------------ | |
185 | // Parsing | |
186 | // ------------------------------------------------------------------------ | |
187 | ||
188 | @Test | |
189 | public void testTraceParsing() { | |
190 | ITmfContext context = fTrace.seekEvent(0); | |
191 | SyslogEvent event = fTrace.getNext(context); | |
4c4e2816 | 192 | assertNotNull(event); |
5ece050b | 193 | TextTraceEventContent content = event.getContent(); |
eadf9801 | 194 | assertEquals("getTimestamp", "Jan 1 01:01:01", event.getTimestamp().toString()); |
5b12450f PT |
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()); | |
eadf9801 | 200 | event = fTrace.getNext(context); |
4c4e2816 | 201 | assertNotNull(event); |
5ece050b | 202 | content = event.getContent(); |
eadf9801 | 203 | assertEquals("getTimestamp", "Jan 1 02:02:02", event.getTimestamp().toString()); |
5b12450f PT |
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()); | |
eadf9801 | 209 | event = fTrace.getNext(context); |
4c4e2816 | 210 | assertNotNull(event); |
5ece050b | 211 | content = event.getContent(); |
eadf9801 | 212 | assertEquals("getTimestamp", "Jan 1 03:03:03", event.getTimestamp().toString()); |
5b12450f PT |
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()); | |
eadf9801 | 218 | event = fTrace.getNext(context); |
4c4e2816 | 219 | assertNotNull(event); |
5ece050b | 220 | content = event.getContent(); |
eadf9801 | 221 | assertEquals("getTimestamp", "Jan 1 04:04:04", event.getTimestamp().toString()); |
5b12450f PT |
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()); | |
eadf9801 | 227 | event = fTrace.getNext(context); |
4c4e2816 | 228 | assertNotNull(event); |
5ece050b | 229 | content = event.getContent(); |
eadf9801 | 230 | assertEquals("getTimestamp", "Jan 1 05:05:05", event.getTimestamp().toString()); |
5b12450f PT |
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()); | |
eadf9801 | 236 | event = fTrace.getNext(context); |
4c4e2816 | 237 | assertNotNull(event); |
5ece050b | 238 | content = event.getContent(); |
eadf9801 | 239 | assertEquals("getTimestamp", "Jan 1 06:06:06", event.getTimestamp().toString()); |
5b12450f PT |
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()); | |
eadf9801 BH |
245 | event = fTrace.getNext(context); |
246 | assertEquals("event", null, event); | |
247 | context.dispose(); | |
248 | } | |
249 | ||
250 | @Test | |
251 | public void testLocationRatio() { | |
252 | ITmfContext context = fTrace.seekEvent(3); | |
253 | double ratio = fTrace.getLocationRatio(context.getLocation()); | |
254 | SyslogEvent event = fTrace.getNext(context); | |
4c4e2816 | 255 | assertNotNull(event); |
5ece050b | 256 | TextTraceEventContent content = event.getContent(); |
5b12450f | 257 | Object logger = content.getFieldValue(Field.LOGGER); |
eadf9801 BH |
258 | context.dispose(); |
259 | context = fTrace.seekEvent(ratio); | |
260 | event = fTrace.getNext(context); | |
4c4e2816 | 261 | assertNotNull(event); |
5ece050b | 262 | content = event.getContent(); |
5b12450f | 263 | assertEquals("getField:LOGGER", logger.toString(), content.getFieldValue(Field.LOGGER).toString()); |
eadf9801 BH |
264 | context.dispose(); |
265 | context = fTrace.seekEvent(0.0); | |
266 | event = fTrace.getNext(context); | |
4c4e2816 | 267 | assertNotNull(event); |
5ece050b | 268 | content = event.getContent(); |
5b12450f | 269 | assertEquals("getField:LOGGER", "LoggerA", content.getFieldValue(Field.LOGGER)); |
eadf9801 BH |
270 | context.dispose(); |
271 | context = fTrace.seekEvent(1.0); | |
272 | event = fTrace.getNext(context); | |
273 | assertEquals("event", null, event); | |
274 | context.dispose(); | |
275 | } | |
32528869 | 276 | } |