Commit | Line | Data |
---|---|---|
ace6413c MJ |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 EfficiOS Inc., Michael Jeanson | |
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.lami.core.tests; | |
11 | ||
12 | import static org.junit.Assert.fail; | |
13 | ||
14 | import java.io.BufferedReader; | |
15 | import java.io.IOException; | |
16 | import java.io.InputStream; | |
17 | import java.io.InputStreamReader; | |
18 | import java.net.URL; | |
19 | import java.util.Collections; | |
20 | import java.util.List; | |
21 | import java.util.stream.Collectors; | |
22 | ||
46f0c09c | 23 | import org.eclipse.core.runtime.CoreException; |
ace6413c MJ |
24 | import org.eclipse.core.runtime.IProgressMonitor; |
25 | import org.eclipse.jdt.annotation.NonNull; | |
26 | import org.eclipse.jdt.annotation.Nullable; | |
27 | import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module.LamiAnalysis; | |
28 | import org.eclipse.tracecompass.internal.provisional.analysis.lami.core.module.LamiChartModel; | |
ace6413c MJ |
29 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
30 | ||
31 | import com.google.common.collect.ImmutableMultimap; | |
32 | import com.google.common.collect.Multimap; | |
33 | ||
34 | /** | |
35 | * Extension of {@link LamiAnalysis} used for tests. | |
36 | */ | |
37 | public class LamiAnalysisStub extends LamiAnalysis { | |
38 | ||
39 | private final String fMetaDatafilename; | |
40 | private final String fResultFilename; | |
41 | ||
42 | /** | |
43 | * Constructor. | |
44 | * | |
45 | * @param metaDatafilename | |
46 | * Filename of the JSON metadata file. | |
47 | * @param resultFilename | |
48 | * Filename of the JSON results file. | |
49 | */ | |
50 | protected LamiAnalysisStub(String metaDatafilename, String resultFilename) { | |
51 | super("Stub Analysis", false, o -> true, Collections.singletonList("StubExecutable")); | |
52 | fMetaDatafilename = metaDatafilename; | |
53 | fResultFilename = resultFilename; | |
54 | } | |
55 | ||
56 | @Override | |
57 | public @NonNull String getName() { | |
58 | return "StubName"; | |
59 | } | |
60 | ||
61 | @Override | |
62 | protected @NonNull Multimap<@NonNull String, @NonNull LamiChartModel> getPredefinedCharts() { | |
63 | return ImmutableMultimap.of(); | |
64 | } | |
65 | ||
66 | @Override | |
67 | protected String getResultsFromCommand(List<String> command, IProgressMonitor monitor) | |
46f0c09c | 68 | throws CoreException { |
ace6413c MJ |
69 | return readLamiFile(fResultFilename); |
70 | } | |
71 | ||
72 | @Override | |
73 | protected @Nullable String getOutputFromCommand(List<String> command) { | |
74 | return readLamiFile(fMetaDatafilename); | |
75 | } | |
76 | ||
77 | @Override | |
78 | public boolean canExecute(ITmfTrace trace) { | |
79 | initialize(); | |
80 | return true; | |
81 | } | |
82 | ||
83 | @Override | |
84 | protected synchronized void initialize() { | |
85 | checkMetadata(); | |
86 | } | |
87 | ||
88 | private static String readLamiFile(String filename) { | |
89 | String fileContent = ""; | |
90 | try { | |
91 | URL url = new URL("platform:/plugin/org.eclipse.tracecompass.analysis.lami.core.tests/testfiles/" + filename); | |
92 | ||
93 | try (InputStream inputStream = url.openConnection().getInputStream()) { | |
94 | BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); | |
95 | fileContent = in.lines().collect(Collectors.joining()); | |
96 | } | |
97 | } catch (IOException e) { | |
98 | fail(e.getMessage()); | |
99 | } | |
100 | ||
101 | return fileContent; | |
102 | } | |
103 | } |