| 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.CustomXmlEvent; |
| 30 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace; |
| 31 | import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition; |
| 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 XML trace |
| 41 | * |
| 42 | * @author Geneviève Bastien |
| 43 | */ |
| 44 | @RunWith(Parameterized.class) |
| 45 | public class CustomXmlTraceDataTest extends AbstractCustomTraceDataTest { |
| 46 | |
| 47 | private static final String TRACE_PATH = TRACE_DIRECTORY + File.separator + "test.xml"; |
| 48 | private static final String DEFINITION_PATH = "testfiles" + File.separator + "xml" + File.separator + "testDefinition.xml"; |
| 49 | |
| 50 | /** |
| 51 | * Constructor |
| 52 | * |
| 53 | * @param name |
| 54 | * The name of this test |
| 55 | * @param data |
| 56 | * The custom test data for this test case |
| 57 | */ |
| 58 | public CustomXmlTraceDataTest(String name, @NonNull ICustomTestData data) { |
| 59 | super(data); |
| 60 | } |
| 61 | |
| 62 | private static CustomXmlTraceDefinition getDefinition(int index) { |
| 63 | CustomXmlTraceDefinition[] definitions = CustomXmlTraceDefinition.loadAll(new File(DEFINITION_PATH).toString()); |
| 64 | return definitions[index]; |
| 65 | } |
| 66 | |
| 67 | private static final ICustomTestData CUSTOM_XML = new ICustomTestData() { |
| 68 | |
| 69 | private static final int NB_EVENTS = 10; |
| 70 | private CustomXmlTraceDefinition fDefinition; |
| 71 | private ITmfEventAspect<?> fTimestampAspect; |
| 72 | |
| 73 | @Override |
| 74 | public ITmfTrace getTrace() throws IOException, TmfTraceException { |
| 75 | fDefinition = getDefinition(0); |
| 76 | final File file = new File(TRACE_PATH); |
| 77 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { |
| 78 | writer.write("<trace>"); |
| 79 | for (int i = 0; i < NB_EVENTS; ++i) { |
| 80 | SimpleDateFormat f = new SimpleDateFormat(TIMESTAMP_FORMAT); |
| 81 | String eventStr = "<element time=\"" + f.format(new Date(i)) + "\">message</element>\n"; |
| 82 | writer.write(eventStr); |
| 83 | } |
| 84 | writer.write("</trace>"); |
| 85 | } |
| 86 | ITmfTrace trace = new CustomXmlTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); |
| 87 | ArrayList<@NonNull ITmfEventAspect<?>> aspects = Lists.newArrayList(trace.getEventAspects()); |
| 88 | fTimestampAspect = aspects.stream().filter(aspect -> aspect.getName().equals("Timestamp")).findFirst().get(); |
| 89 | return trace; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void validateEvent(ITmfEvent event) { |
| 94 | assertTrue(event instanceof CustomXmlEvent); |
| 95 | String name = fDefinition.definitionName; |
| 96 | assertEquals("Event name", name, event.getName()); |
| 97 | assertEquals("Event name and type", event.getType().getName(), event.getName()); |
| 98 | assertEquals("Timestamp", Long.toString(event.getTimestamp().toNanos()), fTimestampAspect.resolve(event)); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void validateEventCount(int eventCount) { |
| 103 | assertEquals("Event count", NB_EVENTS, eventCount); |
| 104 | } |
| 105 | |
| 106 | }; |
| 107 | |
| 108 | private static final ICustomTestData CUSTOM_XML_EVENT_NAME = new ICustomTestData() { |
| 109 | |
| 110 | private static final int NB_EVENTS = 10; |
| 111 | private static final String DEFAULT_EVENT = "DefaultName"; |
| 112 | private static final String ATTRIBUTE_EVENT = "AttributeName"; |
| 113 | private static final String ELEMENT_EVENT = "ElementName"; |
| 114 | private CustomXmlTraceDefinition fDefinition; |
| 115 | private ITmfEventAspect<?> fTimestampAspect; |
| 116 | |
| 117 | @Override |
| 118 | public ITmfTrace getTrace() throws IOException, TmfTraceException { |
| 119 | fDefinition = getDefinition(1); |
| 120 | final File file = new File(TRACE_PATH); |
| 121 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file));) { |
| 122 | writer.write("<trace>"); |
| 123 | for (int i = 0; i < NB_EVENTS; ++i) { |
| 124 | String attribute = (i % 5) != 0 ? String.format(" type=\"%s\"", ATTRIBUTE_EVENT) : ""; |
| 125 | String element = (i % 5) != 0 && i % 2 != 0 ? String.format("<type>%s</type>", ELEMENT_EVENT) : ""; |
| 126 | String eventStr = String.format("<element time=\"" + i + "\"%s>%s</element>\n", attribute, element); |
| 127 | writer.write(eventStr); |
| 128 | } |
| 129 | writer.write("</trace>"); |
| 130 | } |
| 131 | ITmfTrace trace = new CustomXmlTrace(null, fDefinition, file.getPath(), BLOCK_SIZE); |
| 132 | ArrayList<@NonNull ITmfEventAspect<?>> aspects = Lists.newArrayList(trace.getEventAspects()); |
| 133 | fTimestampAspect = aspects.stream().filter(aspect -> aspect.getName().equals("Timestamp")).findFirst().get(); |
| 134 | return trace; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void validateEvent(ITmfEvent event) { |
| 139 | assertTrue(event instanceof CustomXmlEvent); |
| 140 | long ts = event.getTimestamp().getValue(); |
| 141 | if (ts % 5 == 0) { |
| 142 | assertEquals("Event name", DEFAULT_EVENT, event.getName()); |
| 143 | } else if (ts % 2 == 0) { |
| 144 | assertEquals("Event name", ATTRIBUTE_EVENT, event.getName()); |
| 145 | } else { |
| 146 | assertEquals("Event name", ELEMENT_EVENT, event.getName()); |
| 147 | } |
| 148 | assertEquals("Event name and type", event.getType().getName(), event.getName()); |
| 149 | assertEquals("Timestamp", TmfBaseAspects.getTimestampAspect().resolve(event), fTimestampAspect.resolve(event)); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void validateEventCount(int eventCount) { |
| 154 | assertEquals("Event count", NB_EVENTS, eventCount); |
| 155 | } |
| 156 | |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * @return The arrays of parameters |
| 161 | */ |
| 162 | @Parameters(name = "{index}: {0}") |
| 163 | public static Iterable<Object[]> getParameters() { |
| 164 | return Arrays.asList(new Object[][] { |
| 165 | { "Base parser", CUSTOM_XML }, |
| 166 | { "Parse with event name", CUSTOM_XML_EVENT_NAME } |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | } |