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