| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 École Polytechnique de Montréal |
| 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.analysis.os.linux.core.tests.stubs; |
| 11 | |
| 12 | import static org.junit.Assert.fail; |
| 13 | |
| 14 | import java.util.Collections; |
| 15 | import java.util.HashMap; |
| 16 | import java.util.List; |
| 17 | import java.util.Map; |
| 18 | import java.util.Set; |
| 19 | |
| 20 | import org.apache.commons.io.FilenameUtils; |
| 21 | import org.eclipse.core.runtime.IPath; |
| 22 | import org.eclipse.core.runtime.IStatus; |
| 23 | import org.eclipse.tracecompass.analysis.os.linux.core.tests.Activator; |
| 24 | import org.eclipse.tracecompass.analysis.os.linux.core.tests.stubs.trace.TmfXmlKernelTraceStub; |
| 25 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
| 26 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; |
| 27 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; |
| 28 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; |
| 29 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
| 30 | |
| 31 | /** |
| 32 | * Describe a test case for a linux unit test |
| 33 | * |
| 34 | * @author Geneviève Bastien |
| 35 | */ |
| 36 | public class LinuxTestCase { |
| 37 | |
| 38 | private final String fTraceFile; |
| 39 | |
| 40 | /** |
| 41 | * Class to group an attribute path and its intervals |
| 42 | */ |
| 43 | public static class IntervalInfo { |
| 44 | |
| 45 | private final String[] fAttributePath; |
| 46 | private final List<ITmfStateInterval> fIntervals; |
| 47 | |
| 48 | /** |
| 49 | * Constructor |
| 50 | * |
| 51 | * @param intervals |
| 52 | * The list of intervals for the full time range of the |
| 53 | * attribute |
| 54 | * @param attributePath |
| 55 | * The attribute path |
| 56 | */ |
| 57 | public IntervalInfo(List<ITmfStateInterval> intervals, String... attributePath) { |
| 58 | fAttributePath = attributePath; |
| 59 | fIntervals = intervals; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the attribute path |
| 64 | * |
| 65 | * @return The attribute path |
| 66 | */ |
| 67 | public String[] getAttributePath() { |
| 68 | return fAttributePath; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the list of intervals |
| 73 | * |
| 74 | * @return The list of intervals |
| 75 | */ |
| 76 | public List<ITmfStateInterval> getIntervals() { |
| 77 | return fIntervals; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Class to group a timestamp with a map of attributes and their expected |
| 83 | * values |
| 84 | */ |
| 85 | public static class PunctualInfo { |
| 86 | |
| 87 | private final long fTs; |
| 88 | private final Map<String[], ITmfStateValue> fValueMap; |
| 89 | |
| 90 | /** |
| 91 | * Constructor |
| 92 | * |
| 93 | * @param ts |
| 94 | * Timestamp of this test data |
| 95 | */ |
| 96 | public PunctualInfo(long ts) { |
| 97 | fTs = ts; |
| 98 | fValueMap = new HashMap<>(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the timestamp this data is applied to |
| 103 | * |
| 104 | * @return The timestamp of this test data |
| 105 | */ |
| 106 | public long getTimestamp() { |
| 107 | return fTs; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the test values |
| 112 | * |
| 113 | * @return The map of attribute path and values |
| 114 | */ |
| 115 | public Map<String[], ITmfStateValue> getValues() { |
| 116 | return fValueMap; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Add an attribute value to verify |
| 121 | * |
| 122 | * @param key |
| 123 | * The attribute path |
| 124 | * @param value |
| 125 | * The value of this attribute at timestamp |
| 126 | */ |
| 127 | public void addValue(String[] key, ITmfStateValue value) { |
| 128 | fValueMap.put(key, value); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Constructor |
| 134 | * |
| 135 | * @param filename |
| 136 | * The filename of the trace file |
| 137 | */ |
| 138 | public LinuxTestCase(String filename) { |
| 139 | fTraceFile = filename; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get the last part of the file name containing the test trace |
| 144 | * |
| 145 | * @return The name of the file |
| 146 | */ |
| 147 | public String getTraceFileName() { |
| 148 | return NonNullUtils.checkNotNull(FilenameUtils.getName(fTraceFile)); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Initializes the trace for this test case. This method will always create |
| 153 | * a new trace. The caller must dispose of it the proper way. |
| 154 | * |
| 155 | * @return The {@link TmfXmlKernelTraceStub} created for this test case |
| 156 | */ |
| 157 | public TmfXmlKernelTraceStub getKernelTrace() { |
| 158 | TmfXmlKernelTraceStub trace = new TmfXmlKernelTraceStub(); |
| 159 | IPath filePath = Activator.getAbsoluteFilePath(fTraceFile); |
| 160 | IStatus status = trace.validate(null, filePath.toOSString()); |
| 161 | if (!status.isOK()) { |
| 162 | fail(status.getException().getMessage()); |
| 163 | } |
| 164 | try { |
| 165 | trace.initTrace(null, filePath.toOSString(), TmfEvent.class); |
| 166 | } catch (TmfTraceException e) { |
| 167 | fail(e.getMessage()); |
| 168 | } |
| 169 | return trace; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * This method will return a set of attributes and their corresponding |
| 174 | * expected intervals that will be tested with the actual intervals obtained |
| 175 | * from the state system. It does not have to return all attributes of a |
| 176 | * given state system, only the ones interesting for this test case. |
| 177 | * |
| 178 | * @return A set of {@link IntervalInfo} objects to verify |
| 179 | */ |
| 180 | public Set<IntervalInfo> getTestIntervals() { |
| 181 | return Collections.EMPTY_SET; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * This method will return a set of timestamps and their corresponding map |
| 186 | * of attributes and state values. The attribute list does not have to |
| 187 | * contain all attributes in the state system, only the ones that should be |
| 188 | * tested. |
| 189 | * |
| 190 | * @return A set of {@link PunctualInfo} objects to verify |
| 191 | */ |
| 192 | public Set<PunctualInfo> getPunctualTestData() { |
| 193 | return Collections.EMPTY_SET; |
| 194 | } |
| 195 | |
| 196 | } |