tmf: Fix intermittent fail in ProjectModelOutputTest.testListOutputs
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui.tests / shared / org / eclipse / tracecompass / tmf / ui / tests / shared / ProjectModelTestData.java
CommitLineData
87644443 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
87644443
GB
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 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
2bdf0193 13package org.eclipse.tracecompass.tmf.ui.tests.shared;
87644443
GB
14
15import java.io.File;
6a6adab9 16import java.lang.reflect.InvocationTargetException;
87644443
GB
17
18import org.eclipse.core.resources.IFolder;
19import org.eclipse.core.resources.IProject;
20import org.eclipse.core.resources.IResource;
21import org.eclipse.core.runtime.CoreException;
22import org.eclipse.core.runtime.IPath;
6a6adab9 23import org.eclipse.core.runtime.IProgressMonitor;
87644443
GB
24import org.eclipse.core.runtime.NullProgressMonitor;
25import org.eclipse.core.runtime.Path;
87644443 26import org.eclipse.swt.widgets.Display;
2bdf0193
AM
27import org.eclipse.tracecompass.internal.tmf.ui.Activator;
28import org.eclipse.tracecompass.internal.tmf.ui.project.model.TmfImportHelper;
29import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
30import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
31import org.eclipse.tracecompass.tmf.ui.project.model.ITmfProjectModelElement;
32import org.eclipse.tracecompass.tmf.ui.project.model.TmfCommonProjectElement;
33import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement;
34import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement;
35import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry;
36import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement;
37import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder;
6a6adab9
GB
38import org.eclipse.ui.PlatformUI;
39import org.eclipse.ui.actions.WorkspaceModifyOperation;
87644443
GB
40
41/**
42 * Creates objects used for this package's testing purposes
c068a752
GB
43 *
44 * @author Geneviève Bastien
87644443
GB
45 */
46public class ProjectModelTestData {
47
48 /** Default test project name */
49 public static final String PROJECT_NAME = "Test_Project";
50
4d2857be 51 private static final TmfTestTrace testTrace = TmfTestTrace.A_TEST_10K;
87644443
GB
52
53 /**
54 * Gets a project element with traces all initialized
55 *
56 * @return A project stub element
57 * @throws CoreException
58 * If something happened with the project creation
59 */
60 public static TmfProjectElement getFilledProject() throws CoreException {
61
62 IProject project = TmfProjectRegistry.createProject(PROJECT_NAME, null, null);
339d539c 63 IFolder traceFolder = project.getFolder(TmfTracesFolder.TRACES_FOLDER_NAME);
87644443
GB
64
65 /* Create a trace, if it exist, it will be replaced */
4d2857be 66 File file = new File(testTrace.getFullPath());
87644443
GB
67 String path = file.getAbsolutePath();
68 final IPath pathString = Path.fromOSString(path);
69 IResource linkedTrace = TmfImportHelper.createLink(traceFolder, pathString, pathString.lastSegment());
70 if (!(linkedTrace != null && linkedTrace.exists())) {
71 return null;
72 }
73 linkedTrace.setPersistentProperty(TmfCommonConstants.TRACETYPE,
4d2857be 74 "org.eclipse.linuxtools.tmf.core.tests.tracetype");
87644443
GB
75
76 final TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
77 TmfTraceElement traceElement = projectElement.getTracesFolder().getTraces().get(0);
78 traceElement.refreshTraceType();
79
94227c30
GB
80 projectElement.refresh();
81
87644443
GB
82 return projectElement;
83 }
84
6a6adab9
GB
85 /**
86 * Adds a new experiment to the project
87 *
88 * @param projectElement
89 * The project to add to
90 * @param experimentName
91 * Name of the experiment
92 * @return The newly created experiment
93 */
94 public static TmfExperimentElement addExperiment(TmfProjectElement projectElement, String experimentName) {
95 IFolder experimentFolder = projectElement.getExperimentsFolder().getResource();
96 final IFolder folder = experimentFolder.getFolder(experimentName);
97
98 WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
99 @Override
100 public void execute(IProgressMonitor monitor) throws CoreException {
101 monitor.beginTask("", 1000);
102 folder.create(false, true, monitor);
103 monitor.done();
104 }
105 };
106 try {
107 PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation);
108 } catch (InterruptedException | InvocationTargetException | RuntimeException exception) {
109
110 }
111
112 for (ITmfProjectModelElement el : projectElement.getExperimentsFolder().getChildren()) {
113 if (el.getName().equals(experimentName) && (el instanceof TmfExperimentElement)) {
114 return (TmfExperimentElement) el;
115 }
116 }
117 return null;
118 }
119
87644443
GB
120 /**
121 * Get the name of the test trace element
122 *
123 * @return The trace name
124 */
125 public static String getTraceName() {
9ac63b5b 126 File file = new File(testTrace.getPath());
87644443
GB
127 String path = file.getAbsolutePath();
128 final IPath pathString = Path.fromOSString(path);
129 return pathString.lastSegment();
130 }
131
132 /**
133 * Deletes a project
134 *
135 * @param project
136 * Project to delete
87644443 137 */
c068a752 138 public static void deleteProject(TmfProjectElement project) {
87644443 139 /* Delete experiments */
f537c959
PT
140 ITmfProjectModelElement[] experiments = project.getExperimentsFolder().getChildren().toArray(new ITmfProjectModelElement[0]);
141 for (ITmfProjectModelElement element : experiments) {
87644443
GB
142 if (element instanceof TmfExperimentElement) {
143 TmfExperimentElement experiment = (TmfExperimentElement) element;
144 IResource resource = experiment.getResource();
145
146 /* Close the experiment if open */
147 experiment.closeEditors();
148
149 IPath path = resource.getLocation();
150 if (path != null) {
151 /* Delete supplementary files */
152 experiment.deleteSupplementaryFolder();
153 }
154
155 /* Finally, delete the experiment */
c068a752
GB
156 try {
157 resource.delete(true, null);
158 } catch (CoreException e) {
159 Activator.getDefault().logError("Error deleting experiment element", e);
160 }
87644443
GB
161 }
162 }
163
164 /* Delete traces */
f537c959
PT
165 ITmfProjectModelElement[] traces = project.getTracesFolder().getChildren().toArray(new ITmfProjectModelElement[0]);
166 for (ITmfProjectModelElement element : traces) {
87644443
GB
167 if (element instanceof TmfTraceElement) {
168 TmfTraceElement trace = (TmfTraceElement) element;
169 IResource resource = trace.getResource();
170
171 /* Close the trace if open */
172 trace.closeEditors();
173
174 IPath path = resource.getLocation();
175 if (path != null) {
176 /* Delete supplementary files */
177 trace.deleteSupplementaryFolder();
178 }
179
180 /* Finally, delete the trace */
c068a752
GB
181 try {
182 resource.delete(true, new NullProgressMonitor());
183 } catch (CoreException e) {
184 Activator.getDefault().logError("Error deleting trace element", e);
185 }
87644443
GB
186 }
187 }
188
189 /* Delete the project itself */
c068a752
GB
190 try {
191 project.getResource().delete(true, null);
192 } catch (CoreException e) {
193 Activator.getDefault().logError("Error deleting project", e);
194 }
87644443
GB
195 }
196
197 /**
3d2e4ad3
GB
198 * Makes the main display thread sleep, so it gives a chance to other
199 * threads needing the main display to execute
87644443
GB
200 *
201 * @param waitTimeMillis
202 * time to wait in millisecond
203 */
204 public static void delayThread(final long waitTimeMillis) {
205 final Display display = Display.getCurrent();
206 if (display != null) {
207 final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
208 while (System.currentTimeMillis() < endTimeMillis) {
209 if (!display.readAndDispatch()) {
210 display.sleep();
211 }
212 display.update();
213 }
214 } else {
215 try {
216 Thread.sleep(waitTimeMillis);
217 } catch (final InterruptedException e) {
218 // Ignored
219 }
220 }
221 }
222
bc5f2035
GB
223 /**
224 * Makes the main display thread sleep to give a chance to other threads to
225 * execute. It sleeps until the a trace element's corresponding trace is
226 * available (opened) or returns after a timeout. It allows to set short
227 * delays, while still not failing tests when it randomly takes a bit more
228 * time for the trace to open.
229 *
230 * If the project model element sent in parameter is not a trace element,
231 * then the thread is delayed only once by the default delay time. For
232 * longer delays in those cases, it is preferable to use the
233 * {@link ProjectModelTestData#delayThread(long)} instead.
234 *
bc5f2035
GB
235 * @param projectElement
236 * The trace element we are waiting for. If the element if not of
237 * type TmfTraceElement, the thread is delayed only once.
6db82092 238 * @throws WaitTimeoutException
bc5f2035
GB
239 * If after the maximum number of delays the trace is still
240 * null, we throw a timeout exception, the trace has not opened.
241 */
6db82092 242 public static void delayUntilTraceOpened(final ITmfProjectModelElement projectElement) throws WaitTimeoutException {
6a6adab9
GB
243 if (projectElement instanceof TmfCommonProjectElement) {
244 TmfCommonProjectElement traceElement = (TmfCommonProjectElement) projectElement;
6db82092
MAL
245 WaitUtils.waitUntil(new IWaitCondition() {
246 @Override
247 public boolean test() throws Exception {
248 return traceElement.getTrace() != null;
249 }
250
251 @Override
252 public String getFailureMessage() {
253 return "Timeout while waiting for " + traceElement;
bc5f2035 254 }
6db82092 255 });
bc5f2035 256 }
bc5f2035 257
6db82092 258 }
87644443 259}
This page took 0.088855 seconds and 5 git commands to generate.