Commit | Line | Data |
---|---|---|
95916b5f | 1 | /******************************************************************************* |
d6650bca | 2 | * Copyright (c) 2016 École Polytechnique de Montréal and others |
95916b5f GB |
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 static org.junit.Assert.assertEquals; | |
efeeb733 GB |
13 | import static org.junit.Assert.assertNotNull; |
14 | import static org.junit.Assert.assertNull; | |
95916b5f | 15 | import static org.junit.Assert.assertTrue; |
efeeb733 | 16 | import static org.junit.Assert.fail; |
95916b5f GB |
17 | |
18 | import java.io.BufferedWriter; | |
19 | import java.io.File; | |
20 | import java.io.FileWriter; | |
21 | import java.io.IOException; | |
22 | import java.text.SimpleDateFormat; | |
32877ae9 | 23 | import java.util.ArrayList; |
95916b5f GB |
24 | import java.util.Arrays; |
25 | import java.util.Date; | |
26 | ||
27 | import org.eclipse.jdt.annotation.NonNull; | |
28 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
32877ae9 PT |
29 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
30 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects; | |
95916b5f | 31 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
efeeb733 | 32 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition; |
95916b5f GB |
33 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlEvent; |
34 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace; | |
35 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition; | |
efeeb733 | 36 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTraceDefinition.Tag; |
95916b5f GB |
37 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
38 | import org.junit.runner.RunWith; | |
39 | import org.junit.runners.Parameterized; | |
40 | import org.junit.runners.Parameterized.Parameters; | |
41 | ||
32877ae9 PT |
42 | import com.google.common.collect.Lists; |
43 | ||
95916b5f GB |
44 | /** |
45 | * Test the events parsed by a custom XML trace | |
46 | * | |
47 | * @author Geneviève Bastien | |
48 | */ | |
49 | @RunWith(Parameterized.class) | |
50 | public class CustomXmlTraceDataTest extends AbstractCustomTraceDataTest { | |
51 | ||
52 | private static final String TRACE_PATH = TRACE_DIRECTORY + File.separator + "test.xml"; | |
53 | private static final String DEFINITION_PATH = "testfiles" + File.separator + "xml" + File.separator + "testDefinition.xml"; | |
54 | ||
55 | /** | |
56 | * Constructor | |
57 | * | |
58 | * @param name | |
59 | * The name of this test | |
60 | * @param data | |
61 | * The custom test data for this test case | |
62 | */ | |
63 | public CustomXmlTraceDataTest(String name, @NonNull ICustomTestData data) { | |
64 | super(data); | |
65 | } | |
66 | ||
67 | private static CustomXmlTraceDefinition getDefinition(int index) { | |
68 | CustomXmlTraceDefinition[] definitions = CustomXmlTraceDefinition.loadAll(new File(DEFINITION_PATH).toString()); | |
69 | return definitions[index]; | |
70 | } | |
71 | ||
72 | private static final ICustomTestData CUSTOM_XML = new ICustomTestData() { | |
73 | ||
74 | private static final int NB_EVENTS = 10; | |
75 | private CustomXmlTraceDefinition fDefinition; | |
32877ae9 | 76 | private ITmfEventAspect<?> fTimestampAspect; |
95916b5f GB |
77 | |
78 | @Override | |
79 | public ITmfTrace getTrace() throws IOException, TmfTraceException { | |
80 | fDefinition = getDefinition(0); | |
81 | final File file = new File(TRACE_PATH); | |
82 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { | |
83 | writer.write("<trace>"); | |
84 | for (int i = 0; i < NB_EVENTS; ++i) { | |
85 | SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT); | |
86 | String eventStr = "<element time=\"" + f.format(new Date(i)) + "\">message</element>\n"; | |
87 | writer.write(eventStr); | |
88 | } | |
89 | writer.write("</trace>"); | |
90 | } | |
32877ae9 PT |
91 | ITmfTrace trace = new CustomXmlTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); |
92 | ArrayList<@NonNull ITmfEventAspect<?>> aspects = Lists.newArrayList(trace.getEventAspects()); | |
93 | fTimestampAspect = aspects.stream().filter(aspect -> aspect.getName().equals("Timestamp")).findFirst().get(); | |
94 | return trace; | |
95916b5f GB |
95 | } |
96 | ||
97 | @Override | |
98 | public void validateEvent(ITmfEvent event) { | |
99 | assertTrue(event instanceof CustomXmlEvent); | |
100 | String name = fDefinition.definitionName; | |
101 | assertEquals("Event name", name, event.getName()); | |
102 | assertEquals("Event name and type", event.getType().getName(), event.getName()); | |
32877ae9 | 103 | assertEquals("Timestamp", Long.toString(event.getTimestamp().toNanos()), fTimestampAspect.resolve(event)); |
95916b5f GB |
104 | } |
105 | ||
106 | @Override | |
107 | public void validateEventCount(int eventCount) { | |
108 | assertEquals("Event count", NB_EVENTS, eventCount); | |
109 | } | |
110 | ||
111 | }; | |
112 | ||
113 | private static final ICustomTestData CUSTOM_XML_EVENT_NAME = new ICustomTestData() { | |
114 | ||
115 | private static final int NB_EVENTS = 10; | |
d6650bca PT |
116 | private static final String DEFAULT_EVENT = "DefaultName"; |
117 | private static final String ATTRIBUTE_EVENT = "AttributeName"; | |
118 | private static final String ELEMENT_EVENT = "ElementName"; | |
95916b5f | 119 | private CustomXmlTraceDefinition fDefinition; |
32877ae9 | 120 | private ITmfEventAspect<?> fTimestampAspect; |
95916b5f GB |
121 | |
122 | @Override | |
123 | public ITmfTrace getTrace() throws IOException, TmfTraceException { | |
124 | fDefinition = getDefinition(1); | |
125 | final File file = new File(TRACE_PATH); | |
126 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { | |
127 | writer.write("<trace>"); | |
128 | for (int i = 0; i < NB_EVENTS; ++i) { | |
d6650bca PT |
129 | String attribute = (i % 5) != 0 ? String.format(" type=\"%s\"", ATTRIBUTE_EVENT) : ""; |
130 | String element = (i % 5) != 0 && i % 2 != 0 ? String.format("<type>%s</type>", ELEMENT_EVENT) : ""; | |
131 | String eventStr = String.format("<element time=\"" + i + "\"%s>%s</element>\n", attribute, element); | |
95916b5f GB |
132 | writer.write(eventStr); |
133 | } | |
134 | writer.write("</trace>"); | |
135 | } | |
32877ae9 PT |
136 | ITmfTrace trace = new CustomXmlTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); |
137 | ArrayList<@NonNull ITmfEventAspect<?>> aspects = Lists.newArrayList(trace.getEventAspects()); | |
138 | fTimestampAspect = aspects.stream().filter(aspect -> aspect.getName().equals("Timestamp")).findFirst().get(); | |
139 | return trace; | |
95916b5f GB |
140 | } |
141 | ||
142 | @Override | |
143 | public void validateEvent(ITmfEvent event) { | |
144 | assertTrue(event instanceof CustomXmlEvent); | |
145 | long ts = event.getTimestamp().getValue(); | |
d6650bca PT |
146 | if (ts % 5 == 0) { |
147 | assertEquals("Event name", DEFAULT_EVENT, event.getName()); | |
148 | } else if (ts % 2 == 0) { | |
149 | assertEquals("Event name", ATTRIBUTE_EVENT, event.getName()); | |
95916b5f | 150 | } else { |
d6650bca | 151 | assertEquals("Event name", ELEMENT_EVENT, event.getName()); |
95916b5f GB |
152 | } |
153 | assertEquals("Event name and type", event.getType().getName(), event.getName()); | |
32877ae9 | 154 | assertEquals("Timestamp", TmfBaseAspects.getTimestampAspect().resolve(event), fTimestampAspect.resolve(event)); |
95916b5f GB |
155 | } |
156 | ||
157 | @Override | |
158 | public void validateEventCount(int eventCount) { | |
159 | assertEquals("Event count", NB_EVENTS, eventCount); | |
160 | } | |
161 | ||
162 | }; | |
163 | ||
efeeb733 GB |
164 | private static final ICustomTestData CUSTOM_XML_EXTRA_FIELDS = new ICustomTestData() { |
165 | ||
166 | private static final int NB_EVENTS = 5; | |
167 | private static final String FOO = "foo"; | |
168 | private static final String BAR = "bar"; | |
169 | private static final String BAZ = "baz"; | |
170 | private static final String MESSAGE = "message"; | |
171 | private CustomXmlTraceDefinition fDefinition; | |
172 | ||
173 | @Override | |
174 | public ITmfTrace getTrace() throws IOException, TmfTraceException { | |
175 | fDefinition = getDefinition(2); | |
176 | final File file = new File(TRACE_PATH); | |
177 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { | |
178 | writer.write("<trace>"); | |
179 | // Event with one field to set | |
180 | String eventStr = String.format("<event timestamp=\"%s\" name=\"%s\">\n", "1", MESSAGE); | |
181 | eventStr += String.format("<field name=\"%s\" value=\"%s\"/>\n", FOO, BAR); | |
182 | eventStr += "</event>\n"; | |
183 | writer.write(eventStr); | |
184 | // Event with 2 different fields and different values | |
185 | eventStr = String.format("<event timestamp=\"%s\" name=\"%s\">\n", "2", MESSAGE); | |
186 | eventStr += String.format("<field name=\"%s\" value=\"%s\"/>\n", FOO, BAR); | |
187 | eventStr += String.format("<field name=\"%s\" value=\"%s\"/>\n", BAR, FOO); | |
188 | eventStr += "</event>\n"; | |
189 | writer.write(eventStr); | |
190 | // Event with an extra field that conflicts with a built-in field | |
191 | eventStr = String.format("<event timestamp=\"%s\" name=\"%s\">\n", "3", MESSAGE); | |
192 | eventStr += String.format("<field name=\"Message\" value=\"%s\"/>\n", FOO); | |
193 | eventStr += "</event>\n"; | |
194 | writer.write(eventStr); | |
195 | // Event with 2 extra fields with same name where the values | |
196 | // should be appended | |
197 | eventStr = String.format("<event timestamp=\"%s\" name=\"%s\">\n", "4", MESSAGE); | |
198 | eventStr += String.format("<field name=\"%s\" value=\"%s\"/>\n", FOO, BAR); | |
199 | eventStr += String.format("<field name=\"%s\" value=\"%s\"/>\n", FOO, BAZ); | |
200 | eventStr += "</event>\n"; | |
201 | writer.write(eventStr); | |
202 | // Event with 2 non matching number extra field names/values | |
203 | eventStr = String.format("<event timestamp=\"%s\" name=\"%s\">\n", "5", MESSAGE); | |
204 | eventStr += String.format("<fieldName value=\"%s\"/>\n", FOO); | |
205 | eventStr += String.format("<fieldValue value=\"%s\"/>\n", BAR); | |
206 | eventStr += String.format("<fieldValue value=\"%s\"/>\n", BAZ); | |
207 | eventStr += "</event>\n"; | |
208 | writer.write(eventStr); | |
209 | writer.write("</trace>"); | |
210 | } | |
211 | return new CustomXmlTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); | |
212 | } | |
213 | ||
214 | @Override | |
215 | public void validateEvent(ITmfEvent event) { | |
216 | assertTrue(event instanceof CustomXmlEvent); | |
217 | long ts = event.getTimestamp().getValue(); | |
218 | switch ((int) ts) { | |
219 | case 1: | |
220 | assertNotNull(event.getContent().getField(FOO)); | |
221 | assertEquals(BAR, event.getContent().getField(FOO).getValue()); | |
222 | assertNull(event.getContent().getField(BAR)); | |
223 | break; | |
224 | case 2: | |
225 | assertNotNull(event.getContent().getField(FOO)); | |
226 | assertEquals(BAR, event.getContent().getField(FOO).getValue()); | |
227 | assertNotNull(event.getContent().getField(BAR)); | |
228 | assertEquals(FOO, event.getContent().getField(BAR).getValue()); | |
229 | break; | |
230 | case 3: | |
231 | assertNotNull(event.getContent().getField(Tag.MESSAGE.toString())); | |
232 | assertEquals(MESSAGE, event.getContent().getField(Tag.MESSAGE.toString()).getValue()); | |
233 | break; | |
234 | case 4: | |
235 | assertNotNull(event.getContent().getField(FOO)); | |
236 | assertEquals(BAR + CustomTraceDefinition.SEPARATOR + BAZ, event.getContent().getField(FOO).getValue()); | |
237 | assertNull(event.getContent().getField(BAR)); | |
238 | break; | |
239 | case 5: | |
240 | assertNotNull(event.getContent().getField(FOO)); | |
241 | assertEquals(BAZ, event.getContent().getField(FOO).getValue()); | |
242 | assertNull(event.getContent().getField(BAR)); | |
243 | break; | |
244 | default: | |
245 | fail("unknown timestamp " + ts); | |
246 | } | |
247 | assertEquals("Event name and type", event.getType().getName(), event.getName()); | |
248 | } | |
249 | ||
250 | @Override | |
251 | public void validateEventCount(int eventCount) { | |
252 | assertEquals("Event count", NB_EVENTS, eventCount); | |
253 | } | |
254 | ||
255 | }; | |
256 | ||
95916b5f GB |
257 | /** |
258 | * @return The arrays of parameters | |
259 | */ | |
260 | @Parameters(name = "{index}: {0}") | |
261 | public static Iterable<Object[]> getParameters() { | |
262 | return Arrays.asList(new Object[][] { | |
263 | { "Base parser", CUSTOM_XML }, | |
efeeb733 GB |
264 | { "Parse with event name", CUSTOM_XML_EVENT_NAME }, |
265 | { "Parse with extra fields", CUSTOM_XML_EXTRA_FIELDS } | |
95916b5f GB |
266 | }); |
267 | } | |
268 | ||
269 | } |