1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.tests
.shared
;
16 import java
.lang
.reflect
.InvocationTargetException
;
18 import org
.eclipse
.core
.resources
.IFolder
;
19 import org
.eclipse
.core
.resources
.IProject
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.core
.runtime
.IPath
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.Path
;
26 import org
.eclipse
.swt
.widgets
.Display
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
31 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
32 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfCommonProjectElement
;
33 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfExperimentElement
;
34 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectElement
;
35 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
36 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTraceElement
;
37 import org
.eclipse
.tracecompass
.tmf
.ui
.project
.model
.TmfTracesFolder
;
38 import org
.eclipse
.ui
.PlatformUI
;
39 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
42 * Creates objects used for this package's testing purposes
44 * @author Geneviève Bastien
46 public class ProjectModelTestData
{
48 /** Default test project name */
49 public static final String PROJECT_NAME
= "Test_Project";
51 private static final TmfTestTrace testTrace
= TmfTestTrace
.A_TEST_10K
;
54 * Gets a project element with traces all initialized
56 * @return A project stub element
57 * @throws CoreException
58 * If something happened with the project creation
60 public static TmfProjectElement
getFilledProject() throws CoreException
{
62 IProject project
= TmfProjectRegistry
.createProject(PROJECT_NAME
, null, null);
63 IFolder traceFolder
= project
.getFolder(TmfTracesFolder
.TRACES_FOLDER_NAME
);
65 /* Create a trace, if it exist, it will be replaced */
66 File file
= new File(testTrace
.getFullPath());
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())) {
73 linkedTrace
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
,
74 "org.eclipse.linuxtools.tmf.core.tests.tracetype");
76 final TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
77 TmfTraceElement traceElement
= projectElement
.getTracesFolder().getTraces().get(0);
78 traceElement
.refreshTraceType();
80 projectElement
.refresh();
82 return projectElement
;
86 * Adds a new experiment to the project
88 * @param projectElement
89 * The project to add to
90 * @param experimentName
91 * Name of the experiment
92 * @return The newly created experiment
94 public static TmfExperimentElement
addExperiment(TmfProjectElement projectElement
, String experimentName
) {
95 IFolder experimentFolder
= projectElement
.getExperimentsFolder().getResource();
96 final IFolder folder
= experimentFolder
.getFolder(experimentName
);
98 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
100 public void execute(IProgressMonitor monitor
) throws CoreException
{
101 monitor
.beginTask("", 1000);
102 folder
.create(false, true, monitor
);
107 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
108 } catch (InterruptedException
| InvocationTargetException
| RuntimeException exception
) {
112 for (ITmfProjectModelElement el
: projectElement
.getExperimentsFolder().getChildren()) {
113 if (el
.getName().equals(experimentName
) && (el
instanceof TmfExperimentElement
)) {
114 return (TmfExperimentElement
) el
;
121 * Get the name of the test trace element
123 * @return The trace name
125 public static String
getTraceName() {
126 File file
= new File(testTrace
.getPath());
127 String path
= file
.getAbsolutePath();
128 final IPath pathString
= Path
.fromOSString(path
);
129 return pathString
.lastSegment();
138 public static void deleteProject(TmfProjectElement project
) {
139 /* Delete experiments */
140 ITmfProjectModelElement
[] experiments
= project
.getExperimentsFolder().getChildren().toArray(new ITmfProjectModelElement
[0]);
141 for (ITmfProjectModelElement element
: experiments
) {
142 if (element
instanceof TmfExperimentElement
) {
143 TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
144 IResource resource
= experiment
.getResource();
146 /* Close the experiment if open */
147 experiment
.closeEditors();
149 IPath path
= resource
.getLocation();
151 /* Delete supplementary files */
152 experiment
.deleteSupplementaryFolder();
155 /* Finally, delete the experiment */
157 resource
.delete(true, null);
158 } catch (CoreException e
) {
159 Activator
.getDefault().logError("Error deleting experiment element", e
);
165 ITmfProjectModelElement
[] traces
= project
.getTracesFolder().getChildren().toArray(new ITmfProjectModelElement
[0]);
166 for (ITmfProjectModelElement element
: traces
) {
167 if (element
instanceof TmfTraceElement
) {
168 TmfTraceElement trace
= (TmfTraceElement
) element
;
169 IResource resource
= trace
.getResource();
171 /* Close the trace if open */
172 trace
.closeEditors();
174 IPath path
= resource
.getLocation();
176 /* Delete supplementary files */
177 trace
.deleteSupplementaryFolder();
180 /* Finally, delete the trace */
182 resource
.delete(true, new NullProgressMonitor());
183 } catch (CoreException e
) {
184 Activator
.getDefault().logError("Error deleting trace element", e
);
189 /* Delete the project itself */
191 project
.getResource().delete(true, null);
192 } catch (CoreException e
) {
193 Activator
.getDefault().logError("Error deleting project", e
);
198 * Makes the main display thread sleep, so it gives a chance to other
199 * threads needing the main display to execute
201 * @param waitTimeMillis
202 * time to wait in millisecond
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()) {
216 Thread
.sleep(waitTimeMillis
);
217 } catch (final InterruptedException e
) {
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.
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.
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.
238 * @throws WaitTimeoutException
239 * If after the maximum number of delays the trace is still
240 * null, we throw a timeout exception, the trace has not opened.
242 public static void delayUntilTraceOpened(final ITmfProjectModelElement projectElement
) throws WaitTimeoutException
{
243 if (projectElement
instanceof TmfCommonProjectElement
) {
244 TmfCommonProjectElement traceElement
= (TmfCommonProjectElement
) projectElement
;
245 WaitUtils
.waitUntil(new IWaitCondition() {
247 public boolean test() throws Exception
{
248 return traceElement
.getTrace() != null;
252 public String
getFailureMessage() {
253 return "Timeout while waiting for " + traceElement
;