ea6b90d5147c327e7b58bf989ec62f7c035ca089
[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.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 DEFAULT_EVENT = "DefaultName";
100 private static final String ODD_EVENT = "OddName";
101 private static final String EVEN_EVENT = "EvenName";
102 private CustomTxtTraceDefinition fDefinition;
103
104 @Override
105 public ITmfTrace getTrace() throws IOException, TmfTraceException {
106 fDefinition = getDefinition(1);
107 final File file = new File(TRACE_PATH);
108 try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) {
109 for (int i = 1; i <= NB_EVENTS; ++i) {
110 String evName = (i % 5) == 0 ? DEFAULT_EVENT : ((i % 2) == 0) ? EVEN_EVENT : ODD_EVENT;
111 String eventStr = i + " " + evName + "\n";
112 writer.write(eventStr);
113 int extra = i % 3;
114 for (int j = 0; j < extra; j++) {
115 writer.write("extra line\n");
116 }
117 }
118 }
119 return new CustomTxtTrace(null, fDefinition, file.getPath(), BLOCK_SIZE);
120 }
121
122 @Override
123 public void validateEvent(ITmfEvent event) {
124 assertTrue(event instanceof CustomTxtEvent);
125 long ts = event.getTimestamp().getValue();
126 if (ts % 5 == 0) {
127 assertEquals("Event name", DEFAULT_EVENT, event.getName());
128 } else if (ts % 2 == 0) {
129 assertEquals("Event name", EVEN_EVENT, event.getName());
130 } else {
131 assertEquals("Event name", ODD_EVENT, event.getName());
132 }
133 assertEquals("Event name and type", event.getType().getName(), event.getName());
134 }
135
136 @Override
137 public void validateEventCount(int eventCount) {
138 assertEquals("Event count", NB_EVENTS, eventCount);
139 }
140
141 };
142
143 /**
144 * @return The arrays of parameters
145 */
146 @Parameters(name = "{index}: {0}")
147 public static Iterable<Object[]> getParameters() {
148 return Arrays.asList(new Object[][] {
149 { "Base parser", CUSTOM_TXT },
150 { "Parse with event name", CUSTOM_TXT_EVENT_NAME }
151 });
152 }
153
154 }
This page took 0.035271 seconds and 5 git commands to generate.