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; | |
13 | import static org.junit.Assert.assertTrue; | |
14 | ||
15 | import java.io.BufferedWriter; | |
16 | import java.io.File; | |
17 | import java.io.FileWriter; | |
18 | import java.io.IOException; | |
19 | import java.text.SimpleDateFormat; | |
32877ae9 | 20 | import java.util.ArrayList; |
95916b5f GB |
21 | import java.util.Arrays; |
22 | import java.util.Date; | |
23 | ||
24 | import org.eclipse.jdt.annotation.NonNull; | |
25 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
32877ae9 PT |
26 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
27 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects; | |
95916b5f GB |
28 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
29 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlEvent; | |
30 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace; | |
31 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition; | |
32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
33 | import org.junit.runner.RunWith; | |
34 | import org.junit.runners.Parameterized; | |
35 | import org.junit.runners.Parameterized.Parameters; | |
36 | ||
32877ae9 PT |
37 | import com.google.common.collect.Lists; |
38 | ||
95916b5f GB |
39 | /** |
40 | * Test the events parsed by a custom XML trace | |
41 | * | |
42 | * @author Geneviève Bastien | |
43 | */ | |
44 | @RunWith(Parameterized.class) | |
45 | public class CustomXmlTraceDataTest extends AbstractCustomTraceDataTest { | |
46 | ||
47 | private static final String TRACE_PATH = TRACE_DIRECTORY + File.separator + "test.xml"; | |
48 | private static final String DEFINITION_PATH = "testfiles" + File.separator + "xml" + File.separator + "testDefinition.xml"; | |
49 | ||
50 | /** | |
51 | * Constructor | |
52 | * | |
53 | * @param name | |
54 | * The name of this test | |
55 | * @param data | |
56 | * The custom test data for this test case | |
57 | */ | |
58 | public CustomXmlTraceDataTest(String name, @NonNull ICustomTestData data) { | |
59 | super(data); | |
60 | } | |
61 | ||
62 | private static CustomXmlTraceDefinition getDefinition(int index) { | |
63 | CustomXmlTraceDefinition[] definitions = CustomXmlTraceDefinition.loadAll(new File(DEFINITION_PATH).toString()); | |
64 | return definitions[index]; | |
65 | } | |
66 | ||
67 | private static final ICustomTestData CUSTOM_XML = new ICustomTestData() { | |
68 | ||
69 | private static final int NB_EVENTS = 10; | |
70 | private CustomXmlTraceDefinition fDefinition; | |
32877ae9 | 71 | private ITmfEventAspect<?> fTimestampAspect; |
95916b5f GB |
72 | |
73 | @Override | |
74 | public ITmfTrace getTrace() throws IOException, TmfTraceException { | |
75 | fDefinition = getDefinition(0); | |
76 | final File file = new File(TRACE_PATH); | |
77 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { | |
78 | writer.write("<trace>"); | |
79 | for (int i = 0; i < NB_EVENTS; ++i) { | |
80 | SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT); | |
81 | String eventStr = "<element time=\"" + f.format(new Date(i)) + "\">message</element>\n"; | |
82 | writer.write(eventStr); | |
83 | } | |
84 | writer.write("</trace>"); | |
85 | } | |
32877ae9 PT |
86 | ITmfTrace trace = new CustomXmlTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); |
87 | ArrayList<@NonNull ITmfEventAspect<?>> aspects = Lists.newArrayList(trace.getEventAspects()); | |
88 | fTimestampAspect = aspects.stream().filter(aspect -> aspect.getName().equals("Timestamp")).findFirst().get(); | |
89 | return trace; | |
95916b5f GB |
90 | } |
91 | ||
92 | @Override | |
93 | public void validateEvent(ITmfEvent event) { | |
94 | assertTrue(event instanceof CustomXmlEvent); | |
95 | String name = fDefinition.definitionName; | |
96 | assertEquals("Event name", name, event.getName()); | |
97 | assertEquals("Event name and type", event.getType().getName(), event.getName()); | |
32877ae9 | 98 | assertEquals("Timestamp", Long.toString(event.getTimestamp().toNanos()), fTimestampAspect.resolve(event)); |
95916b5f GB |
99 | } |
100 | ||
101 | @Override | |
102 | public void validateEventCount(int eventCount) { | |
103 | assertEquals("Event count", NB_EVENTS, eventCount); | |
104 | } | |
105 | ||
106 | }; | |
107 | ||
108 | private static final ICustomTestData CUSTOM_XML_EVENT_NAME = new ICustomTestData() { | |
109 | ||
110 | private static final int NB_EVENTS = 10; | |
d6650bca PT |
111 | private static final String DEFAULT_EVENT = "DefaultName"; |
112 | private static final String ATTRIBUTE_EVENT = "AttributeName"; | |
113 | private static final String ELEMENT_EVENT = "ElementName"; | |
95916b5f | 114 | private CustomXmlTraceDefinition fDefinition; |
32877ae9 | 115 | private ITmfEventAspect<?> fTimestampAspect; |
95916b5f GB |
116 | |
117 | @Override | |
118 | public ITmfTrace getTrace() throws IOException, TmfTraceException { | |
119 | fDefinition = getDefinition(1); | |
120 | final File file = new File(TRACE_PATH); | |
121 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { | |
122 | writer.write("<trace>"); | |
123 | for (int i = 0; i < NB_EVENTS; ++i) { | |
d6650bca PT |
124 | String attribute = (i % 5) != 0 ? String.format(" type=\"%s\"", ATTRIBUTE_EVENT) : ""; |
125 | String element = (i % 5) != 0 && i % 2 != 0 ? String.format("<type>%s</type>", ELEMENT_EVENT) : ""; | |
126 | String eventStr = String.format("<element time=\"" + i + "\"%s>%s</element>\n", attribute, element); | |
95916b5f GB |
127 | writer.write(eventStr); |
128 | } | |
129 | writer.write("</trace>"); | |
130 | } | |
32877ae9 PT |
131 | ITmfTrace trace = new CustomXmlTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); |
132 | ArrayList<@NonNull ITmfEventAspect<?>> aspects = Lists.newArrayList(trace.getEventAspects()); | |
133 | fTimestampAspect = aspects.stream().filter(aspect -> aspect.getName().equals("Timestamp")).findFirst().get(); | |
134 | return trace; | |
95916b5f GB |
135 | } |
136 | ||
137 | @Override | |
138 | public void validateEvent(ITmfEvent event) { | |
139 | assertTrue(event instanceof CustomXmlEvent); | |
140 | long ts = event.getTimestamp().getValue(); | |
d6650bca PT |
141 | if (ts % 5 == 0) { |
142 | assertEquals("Event name", DEFAULT_EVENT, event.getName()); | |
143 | } else if (ts % 2 == 0) { | |
144 | assertEquals("Event name", ATTRIBUTE_EVENT, event.getName()); | |
95916b5f | 145 | } else { |
d6650bca | 146 | assertEquals("Event name", ELEMENT_EVENT, event.getName()); |
95916b5f GB |
147 | } |
148 | assertEquals("Event name and type", event.getType().getName(), event.getName()); | |
32877ae9 | 149 | assertEquals("Timestamp", TmfBaseAspects.getTimestampAspect().resolve(event), fTimestampAspect.resolve(event)); |
95916b5f GB |
150 | } |
151 | ||
152 | @Override | |
153 | public void validateEventCount(int eventCount) { | |
154 | assertEquals("Event count", NB_EVENTS, eventCount); | |
155 | } | |
156 | ||
157 | }; | |
158 | ||
159 | /** | |
160 | * @return The arrays of parameters | |
161 | */ | |
162 | @Parameters(name = "{index}: {0}") | |
163 | public static Iterable<Object[]> getParameters() { | |
164 | return Arrays.asList(new Object[][] { | |
165 | { "Base parser", CUSTOM_XML }, | |
166 | { "Parse with event name", CUSTOM_XML_EVENT_NAME } | |
167 | }); | |
168 | } | |
169 | ||
170 | } |