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
.linuxtools
.tmf
.ui
.tests
.shared
;
15 import static org
.junit
.Assume
.assumeTrue
;
18 import java
.lang
.reflect
.InvocationTargetException
;
19 import java
.util
.concurrent
.TimeoutException
;
21 import org
.eclipse
.core
.resources
.IFolder
;
22 import org
.eclipse
.core
.resources
.IProject
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IPath
;
26 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
27 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
28 import org
.eclipse
.core
.runtime
.Path
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
30 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.CtfTmfTestTrace
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfCommonProjectElement
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
40 import org
.eclipse
.swt
.widgets
.Display
;
41 import org
.eclipse
.ui
.PlatformUI
;
42 import org
.eclipse
.ui
.actions
.WorkspaceModifyOperation
;
45 * Creates objects used for this package's testing purposes
47 * @author Geneviève Bastien
49 public class ProjectModelTestData
{
51 /* Maximum number of thread delays the main thread will do before timing out */
52 private static final int DELAY_COUNTER
= 1000;
53 /* Default delay time when having the main thread sleep. */
54 private static final long DEFAULT_DELAY
= 500;
56 /** Default test project name */
57 public static final String PROJECT_NAME
= "Test_Project";
59 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
62 * Gets a project element with traces all initialized
64 * @return A project stub element
65 * @throws CoreException
66 * If something happened with the project creation
68 public static TmfProjectElement
getFilledProject() throws CoreException
{
70 assumeTrue(CtfTmfTestTrace
.KERNEL
.exists());
72 IProject project
= TmfProjectRegistry
.createProject(PROJECT_NAME
, null, null);
73 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
75 /* Create a trace, if it exist, it will be replaced */
76 File file
= new File(testTrace
.getPath());
77 String path
= file
.getAbsolutePath();
78 final IPath pathString
= Path
.fromOSString(path
);
79 IResource linkedTrace
= TmfImportHelper
.createLink(traceFolder
, pathString
, pathString
.lastSegment());
80 if (!(linkedTrace
!= null && linkedTrace
.exists())) {
83 linkedTrace
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
,
84 "org.eclipse.linuxtools.tmf.core.tests.ctf.tracetype");
86 final TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
87 TmfTraceElement traceElement
= projectElement
.getTracesFolder().getTraces().get(0);
88 traceElement
.refreshTraceType();
90 projectElement
.refresh();
92 return projectElement
;
96 * Adds a new experiment to the project
98 * @param projectElement
99 * The project to add to
100 * @param experimentName
101 * Name of the experiment
102 * @return The newly created experiment
104 public static TmfExperimentElement
addExperiment(TmfProjectElement projectElement
, String experimentName
) {
105 IFolder experimentFolder
= projectElement
.getExperimentsFolder().getResource();
106 final IFolder folder
= experimentFolder
.getFolder(experimentName
);
108 WorkspaceModifyOperation operation
= new WorkspaceModifyOperation() {
110 public void execute(IProgressMonitor monitor
) throws CoreException
{
111 monitor
.beginTask("", 1000);
112 folder
.create(false, true, monitor
);
117 PlatformUI
.getWorkbench().getProgressService().busyCursorWhile(operation
);
118 } catch (InterruptedException
| InvocationTargetException
| RuntimeException exception
) {
122 for (ITmfProjectModelElement el
: projectElement
.getExperimentsFolder().getChildren()) {
123 if (el
.getName().equals(experimentName
) && (el
instanceof TmfExperimentElement
)) {
124 return (TmfExperimentElement
) el
;
131 * Get the name of the test trace element
133 * @return The trace name
135 public static String
getTraceName() {
136 File file
= new File(testTrace
.getPath());
137 String path
= file
.getAbsolutePath();
138 final IPath pathString
= Path
.fromOSString(path
);
139 return pathString
.lastSegment();
148 public static void deleteProject(TmfProjectElement project
) {
149 /* Delete experiments */
150 ITmfProjectModelElement
[] experiments
= project
.getExperimentsFolder().getChildren().toArray(new ITmfProjectModelElement
[0]);
151 for (ITmfProjectModelElement element
: experiments
) {
152 if (element
instanceof TmfExperimentElement
) {
153 TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
154 IResource resource
= experiment
.getResource();
156 /* Close the experiment if open */
157 experiment
.closeEditors();
159 IPath path
= resource
.getLocation();
161 /* Delete supplementary files */
162 experiment
.deleteSupplementaryFolder();
165 /* Finally, delete the experiment */
167 resource
.delete(true, null);
168 } catch (CoreException e
) {
169 Activator
.getDefault().logError("Error deleting experiment element", e
);
175 ITmfProjectModelElement
[] traces
= project
.getTracesFolder().getChildren().toArray(new ITmfProjectModelElement
[0]);
176 for (ITmfProjectModelElement element
: traces
) {
177 if (element
instanceof TmfTraceElement
) {
178 TmfTraceElement trace
= (TmfTraceElement
) element
;
179 IResource resource
= trace
.getResource();
181 /* Close the trace if open */
182 trace
.closeEditors();
184 IPath path
= resource
.getLocation();
186 /* Delete supplementary files */
187 trace
.deleteSupplementaryFolder();
190 /* Finally, delete the trace */
192 resource
.delete(true, new NullProgressMonitor());
193 } catch (CoreException e
) {
194 Activator
.getDefault().logError("Error deleting trace element", e
);
199 /* Delete the project itself */
201 project
.getResource().delete(true, null);
202 } catch (CoreException e
) {
203 Activator
.getDefault().logError("Error deleting project", e
);
208 * Makes the main display thread sleep, so it gives a chance to other
209 * threads needing the main display to execute
211 * @param waitTimeMillis
212 * time to wait in millisecond
214 public static void delayThread(final long waitTimeMillis
) {
215 final Display display
= Display
.getCurrent();
216 if (display
!= null) {
217 final long endTimeMillis
= System
.currentTimeMillis() + waitTimeMillis
;
218 while (System
.currentTimeMillis() < endTimeMillis
) {
219 if (!display
.readAndDispatch()) {
226 Thread
.sleep(waitTimeMillis
);
227 } catch (final InterruptedException e
) {
234 * Makes the main display thread sleep to give a chance to other threads to
235 * execute. It sleeps until the a trace element's corresponding trace is
236 * available (opened) or returns after a timeout. It allows to set short
237 * delays, while still not failing tests when it randomly takes a bit more
238 * time for the trace to open.
240 * If the project model element sent in parameter is not a trace element,
241 * then the thread is delayed only once by the default delay time. For
242 * longer delays in those cases, it is preferable to use the
243 * {@link ProjectModelTestData#delayThread(long)} instead.
245 * Timeout is DELAY_COUNTER * DEFAULT_DELAY ms
247 * @param projectElement
248 * The trace element we are waiting for. If the element if not of
249 * type TmfTraceElement, the thread is delayed only once.
250 * @throws TimeoutException
251 * If after the maximum number of delays the trace is still
252 * null, we throw a timeout exception, the trace has not opened.
254 public static void delayUntilTraceOpened(final ITmfProjectModelElement projectElement
) throws TimeoutException
{
255 if (projectElement
instanceof TmfCommonProjectElement
) {
256 TmfCommonProjectElement traceElement
= (TmfCommonProjectElement
) projectElement
;
257 final long deadline
= System
.nanoTime() + (DELAY_COUNTER
* DEFAULT_DELAY
* 1000000L);
259 delayThread(DEFAULT_DELAY
);
260 if (traceElement
.getTrace() != null) {
263 } while (System
.nanoTime() < deadline
);
264 throw new TimeoutException("Timeout while waiting for " + traceElement
);
266 delayThread(DEFAULT_DELAY
);