Commit | Line | Data |
---|---|---|
95916b5f GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 École Polytechnique de Montréal | |
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.util.Arrays; | |
20 | ||
21 | import org.eclipse.jdt.annotation.NonNull; | |
22 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
23 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; | |
24 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtEvent; | |
25 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace; | |
26 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition; | |
27 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
28 | import org.junit.runner.RunWith; | |
29 | import org.junit.runners.Parameterized; | |
30 | import org.junit.runners.Parameterized.Parameters; | |
31 | ||
32 | /** | |
33 | * Test the events parsed by a custom txt trace | |
34 | * | |
35 | * @author Geneviève Bastien | |
36 | */ | |
37 | @RunWith(Parameterized.class) | |
38 | public class CustomTxtTraceDataTest extends AbstractCustomTraceDataTest { | |
39 | ||
40 | private static final String TRACE_PATH = TRACE_DIRECTORY + File.separator + "test.txt"; | |
41 | private static final String DEFINITION_PATH = "testfiles" + File.separator + "txt" + File.separator + "testTxtDefinition.xml"; | |
42 | ||
43 | /** | |
44 | * Constructor | |
45 | * | |
46 | * @param name The name of the test | |
47 | * @param data The test data | |
48 | */ | |
49 | public CustomTxtTraceDataTest(String name, @NonNull ICustomTestData data) { | |
50 | super(data); | |
51 | } | |
52 | ||
53 | ||
54 | private static CustomTxtTraceDefinition getDefinition(int index) { | |
55 | CustomTxtTraceDefinition[] definitions = CustomTxtTraceDefinition.loadAll(new File(DEFINITION_PATH).toString()); | |
56 | return definitions[index]; | |
57 | } | |
58 | ||
59 | private static final ICustomTestData CUSTOM_TXT = new ICustomTestData() { | |
60 | ||
61 | private static final int NB_EVENTS = 10; | |
62 | private CustomTxtTraceDefinition fDefinition; | |
63 | ||
64 | @Override | |
65 | public ITmfTrace getTrace() throws IOException, TmfTraceException { | |
66 | fDefinition = getDefinition(0); | |
67 | final File file = new File(TRACE_PATH); | |
68 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { | |
69 | for (int i = 0; i < NB_EVENTS; ++i) { | |
70 | String eventStr = i + " hello world\n"; | |
71 | writer.write(eventStr); | |
72 | int extra = i % 3; | |
73 | for (int j = 0; j < extra; j++) { | |
74 | writer.write("extra line\n"); | |
75 | } | |
76 | } | |
77 | } | |
78 | return new CustomTxtTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); | |
79 | } | |
80 | ||
81 | @Override | |
82 | public void validateEvent(ITmfEvent event) { | |
83 | assertTrue(event instanceof CustomTxtEvent); | |
84 | String name = fDefinition.definitionName; | |
85 | assertEquals("Event name", name, event.getName()); | |
86 | assertEquals("Event name and type", event.getType().getName(), event.getName()); | |
87 | } | |
88 | ||
89 | @Override | |
90 | public void validateEventCount(int eventCount) { | |
91 | assertEquals("Event count", NB_EVENTS, eventCount); | |
92 | } | |
93 | ||
94 | }; | |
95 | ||
96 | private static final ICustomTestData CUSTOM_TXT_EVENT_NAME = new ICustomTestData() { | |
97 | ||
98 | private static final int NB_EVENTS = 10; | |
99 | private static final String ODD_EVENT = "OddName"; | |
100 | private static final String EVEN_EVENT = "EvenName"; | |
101 | private CustomTxtTraceDefinition fDefinition; | |
102 | ||
103 | @Override | |
104 | public ITmfTrace getTrace() throws IOException, TmfTraceException { | |
105 | fDefinition = getDefinition(1); | |
106 | final File file = new File(TRACE_PATH); | |
107 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { | |
108 | for (int i = 1; i <= NB_EVENTS; ++i) { | |
109 | String evName = ((i % 2) == 0) ? EVEN_EVENT : ODD_EVENT; | |
110 | String eventStr = i + " " + evName + "\n"; | |
111 | writer.write(eventStr); | |
112 | int extra = i % 3; | |
113 | for (int j = 0; j < extra; j++) { | |
114 | writer.write("extra line\n"); | |
115 | } | |
116 | } | |
117 | } | |
118 | return new CustomTxtTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); | |
119 | } | |
120 | ||
121 | @Override | |
122 | public void validateEvent(ITmfEvent event) { | |
123 | assertTrue(event instanceof CustomTxtEvent); | |
124 | long ts = event.getTimestamp().getValue(); | |
125 | if (ts % 2 == 0) { | |
126 | assertEquals("Event name", EVEN_EVENT, event.getName()); | |
127 | } else { | |
128 | assertEquals("Event name", ODD_EVENT, event.getName()); | |
129 | } | |
130 | assertEquals("Event name and type", event.getType().getName(), event.getName()); | |
131 | } | |
132 | ||
133 | @Override | |
134 | public void validateEventCount(int eventCount) { | |
135 | assertEquals("Event count", NB_EVENTS, eventCount); | |
136 | } | |
137 | ||
138 | }; | |
139 | ||
140 | /** | |
141 | * @return The arrays of parameters | |
142 | */ | |
143 | @Parameters(name = "{index}: {0}") | |
144 | public static Iterable<Object[]> getParameters() { | |
145 | return Arrays.asList(new Object[][] { | |
146 | { "Base parser", CUSTOM_TXT }, | |
147 | { "Parse with event name", CUSTOM_TXT_EVENT_NAME } | |
148 | }); | |
149 | } | |
150 | ||
151 | } |