1a62b5add66843c4967f8a71a69dc81bd5d59321
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / parsers / custom / CustomTxtTraceDataTest.java
1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal and others
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;
20 import java.util.ArrayList;
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;
26 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
27 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
28 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
29 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtEvent;
30 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
31 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
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
37 import com.google.common.collect.Lists;
38
39 /**
40 * Test the events parsed by a custom txt trace
41 *
42 * @author Geneviève Bastien
43 */
44 @RunWith(Parameterized.class)
45 public class CustomTxtTraceDataTest extends AbstractCustomTraceDataTest {
46
47 private static final String TRACE_PATH = TRACE_DIRECTORY + File.separator + "test.txt";
48 private static final String DEFINITION_PATH = "testfiles" + File.separator + "txt" + File.separator + "testTxtDefinition.xml";
49
50 /**
51 * Constructor
52 *
53 * @param name The name of the test
54 * @param data The test data
55 */
56 public CustomTxtTraceDataTest(String name, @NonNull ICustomTestData data) {
57 super(data);
58 }
59
60
61 private static CustomTxtTraceDefinition getDefinition(int index) {
62 CustomTxtTraceDefinition[] definitions = CustomTxtTraceDefinition.loadAll(new File(DEFINITION_PATH).toString());
63 return definitions[index];
64 }
65
66 private static final ICustomTestData CUSTOM_TXT = new ICustomTestData() {
67
68 private static final int NB_EVENTS = 10;
69 private CustomTxtTraceDefinition fDefinition;
70 private ITmfEventAspect<?> fTimestampAspect;
71
72 @Override
73 public ITmfTrace getTrace() throws IOException, TmfTraceException {
74 fDefinition = getDefinition(0);
75 final File file = new File(TRACE_PATH);
76 try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) {
77 for (int i = 0; i < NB_EVENTS; ++i) {
78 SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT);
79 String eventStr = f.format(new Date(i)) + " hello world\n";
80 writer.write(eventStr);
81 int extra = i % 3;
82 for (int j = 0; j < extra; j++) {
83 writer.write("extra line\n");
84 }
85 }
86 }
87 ITmfTrace trace = new CustomTxtTrace(null, fDefinition, file.getPath(), BLOCK_SIZE);
88 ArrayList<@NonNull ITmfEventAspect<?>> aspects = Lists.newArrayList(trace.getEventAspects());
89 fTimestampAspect = aspects.stream().filter(aspect -> aspect.getName().equals("Timestamp")).findFirst().get();
90 return trace;
91 }
92
93 @Override
94 public void validateEvent(ITmfEvent event) {
95 assertTrue(event instanceof CustomTxtEvent);
96 String name = fDefinition.definitionName;
97 assertEquals("Event name", name, event.getName());
98 assertEquals("Event name and type", event.getType().getName(), event.getName());
99 assertEquals("Timestamp", Long.toString(event.getTimestamp().toNanos()), fTimestampAspect.resolve(event));
100 }
101
102 @Override
103 public void validateEventCount(int eventCount) {
104 assertEquals("Event count", NB_EVENTS, eventCount);
105 }
106
107 };
108
109 private static final ICustomTestData CUSTOM_TXT_EVENT_NAME = new ICustomTestData() {
110
111 private static final int NB_EVENTS = 10;
112 private static final String DEFAULT_EVENT = "DefaultName";
113 private static final String ODD_EVENT = "OddName";
114 private static final String EVEN_EVENT = "EvenName";
115 private CustomTxtTraceDefinition fDefinition;
116 private ITmfEventAspect<?> fTimestampAspect;
117
118 @Override
119 public ITmfTrace getTrace() throws IOException, TmfTraceException {
120 fDefinition = getDefinition(1);
121 final File file = new File(TRACE_PATH);
122 try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) {
123 for (int i = 1; i <= NB_EVENTS; ++i) {
124 String evName = (i % 5) == 0 ? DEFAULT_EVENT : ((i % 2) == 0) ? EVEN_EVENT : ODD_EVENT;
125 String eventStr = i + " " + evName + "\n";
126 writer.write(eventStr);
127 int extra = i % 3;
128 for (int j = 0; j < extra; j++) {
129 writer.write("extra line\n");
130 }
131 }
132 }
133 ITmfTrace trace = new CustomTxtTrace(null, fDefinition, file.getPath(), BLOCK_SIZE);
134 ArrayList<@NonNull ITmfEventAspect<?>> aspects = Lists.newArrayList(trace.getEventAspects());
135 fTimestampAspect = aspects.stream().filter(aspect -> aspect.getName().equals("Timestamp")).findFirst().get();
136 return trace;
137 }
138
139 @Override
140 public void validateEvent(ITmfEvent event) {
141 assertTrue(event instanceof CustomTxtEvent);
142 long ts = event.getTimestamp().getValue();
143 if (ts % 5 == 0) {
144 assertEquals("Event name", DEFAULT_EVENT, event.getName());
145 } else if (ts % 2 == 0) {
146 assertEquals("Event name", EVEN_EVENT, event.getName());
147 } else {
148 assertEquals("Event name", ODD_EVENT, event.getName());
149 }
150 assertEquals("Event name and type", event.getType().getName(), event.getName());
151 assertEquals("Timestamp", TmfBaseAspects.getTimestampAspect().resolve(event), fTimestampAspect.resolve(event));
152 }
153
154 @Override
155 public void validateEventCount(int eventCount) {
156 assertEquals("Event count", NB_EVENTS, eventCount);
157 }
158
159 };
160
161 /**
162 * @return The arrays of parameters
163 */
164 @Parameters(name = "{index}: {0}")
165 public static Iterable<Object[]> getParameters() {
166 return Arrays.asList(new Object[][] {
167 { "Base parser", CUSTOM_TXT },
168 { "Parse with event name", CUSTOM_TXT_EVENT_NAME }
169 });
170 }
171
172 }
This page took 0.033656 seconds and 4 git commands to generate.