1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal and others
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.parsers
.custom
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertTrue
;
15 import java
.io
.BufferedWriter
;
17 import java
.io
.FileWriter
;
18 import java
.io
.IOException
;
19 import java
.util
.Arrays
;
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
;
33 * Test the events parsed by a custom txt trace
35 * @author Geneviève Bastien
37 @RunWith(Parameterized
.class)
38 public class CustomTxtTraceDataTest
extends AbstractCustomTraceDataTest
{
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";
46 * @param name The name of the test
47 * @param data The test data
49 public CustomTxtTraceDataTest(String name
, @NonNull ICustomTestData data
) {
54 private static CustomTxtTraceDefinition
getDefinition(int index
) {
55 CustomTxtTraceDefinition
[] definitions
= CustomTxtTraceDefinition
.loadAll(new File(DEFINITION_PATH
).toString());
56 return definitions
[index
];
59 private static final ICustomTestData CUSTOM_TXT
= new ICustomTestData() {
61 private static final int NB_EVENTS
= 10;
62 private CustomTxtTraceDefinition fDefinition
;
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
);
73 for (int j
= 0; j
< extra
; j
++) {
74 writer
.write("extra line\n");
78 return new CustomTxtTrace(null, fDefinition
, file
.getPath(), BLOCK_SIZE
);
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());
90 public void validateEventCount(int eventCount
) {
91 assertEquals("Event count", NB_EVENTS
, eventCount
);
96 private static final ICustomTestData CUSTOM_TXT_EVENT_NAME
= new ICustomTestData() {
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
;
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
);
114 for (int j
= 0; j
< extra
; j
++) {
115 writer
.write("extra line\n");
119 return new CustomTxtTrace(null, fDefinition
, file
.getPath(), BLOCK_SIZE
);
123 public void validateEvent(ITmfEvent event
) {
124 assertTrue(event
instanceof CustomTxtEvent
);
125 long ts
= event
.getTimestamp().getValue();
127 assertEquals("Event name", DEFAULT_EVENT
, event
.getName());
128 } else if (ts
% 2 == 0) {
129 assertEquals("Event name", EVEN_EVENT
, event
.getName());
131 assertEquals("Event name", ODD_EVENT
, event
.getName());
133 assertEquals("Event name and type", event
.getType().getName(), event
.getName());
137 public void validateEventCount(int eventCount
) {
138 assertEquals("Event count", NB_EVENTS
, eventCount
);
144 * @return The arrays of parameters
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
}