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
.text
.SimpleDateFormat
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Arrays
;
22 import java
.util
.Date
;
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
;
37 import com
.google
.common
.collect
.Lists
;
40 * Test the events parsed by a custom txt trace
42 * @author Geneviève Bastien
44 @RunWith(Parameterized
.class)
45 public class CustomTxtTraceDataTest
extends AbstractCustomTraceDataTest
{
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";
53 * @param name The name of the test
54 * @param data The test data
56 public CustomTxtTraceDataTest(String name
, @NonNull ICustomTestData data
) {
61 private static CustomTxtTraceDefinition
getDefinition(int index
) {
62 CustomTxtTraceDefinition
[] definitions
= CustomTxtTraceDefinition
.loadAll(new File(DEFINITION_PATH
).toString());
63 return definitions
[index
];
66 private static final ICustomTestData CUSTOM_TXT
= new ICustomTestData() {
68 private static final int NB_EVENTS
= 10;
69 private CustomTxtTraceDefinition fDefinition
;
70 private ITmfEventAspect
<?
> fTimestampAspect
;
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
);
82 for (int j
= 0; j
< extra
; j
++) {
83 writer
.write("extra line\n");
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();
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
));
103 public void validateEventCount(int eventCount
) {
104 assertEquals("Event count", NB_EVENTS
, eventCount
);
109 private static final ICustomTestData CUSTOM_TXT_EVENT_NAME
= new ICustomTestData() {
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
;
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
);
128 for (int j
= 0; j
< extra
; j
++) {
129 writer
.write("extra line\n");
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();
140 public void validateEvent(ITmfEvent event
) {
141 assertTrue(event
instanceof CustomTxtEvent
);
142 long ts
= event
.getTimestamp().getValue();
144 assertEquals("Event name", DEFAULT_EVENT
, event
.getName());
145 } else if (ts
% 2 == 0) {
146 assertEquals("Event name", EVEN_EVENT
, event
.getName());
148 assertEquals("Event name", ODD_EVENT
, event
.getName());
150 assertEquals("Event name and type", event
.getType().getName(), event
.getName());
151 assertEquals("Timestamp", TmfBaseAspects
.getTimestampAspect().resolve(event
), fTimestampAspect
.resolve(event
));
155 public void validateEventCount(int eventCount
) {
156 assertEquals("Event count", NB_EVENTS
, eventCount
);
162 * @return The arrays of parameters
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
}