1 /*******************************************************************************
2 * Copyright (c) 2013 É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
.project
.model
;
17 import org
.eclipse
.core
.resources
.IFolder
;
18 import org
.eclipse
.core
.resources
.IProject
;
19 import org
.eclipse
.core
.resources
.IResource
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.core
.runtime
.IPath
;
22 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.Path
;
24 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.CtfTmfTestTraces
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
33 import org
.eclipse
.swt
.widgets
.Display
;
36 * Creates objects used for this package's testing purposes
38 public class ProjectModelTestData
{
40 /** Default test project name */
41 public static final String PROJECT_NAME
= "Test_Project";
43 private static final int TRACE_INDEX
= 0;
44 private static final String PATH
= CtfTmfTestTraces
.getTestTracePath(TRACE_INDEX
);
47 * Gets a project element with traces all initialized
49 * @return A project stub element
50 * @throws CoreException
51 * If something happened with the project creation
53 public static TmfProjectElement
getFilledProject() throws CoreException
{
55 IProject project
= TmfProjectRegistry
.createProject(PROJECT_NAME
, null, null);
56 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
58 /* Create a trace, if it exist, it will be replaced */
59 File file
= new File(PATH
);
60 String path
= file
.getAbsolutePath();
61 final IPath pathString
= Path
.fromOSString(path
);
62 IResource linkedTrace
= TmfImportHelper
.createLink(traceFolder
, pathString
, pathString
.lastSegment());
63 if (!(linkedTrace
!= null && linkedTrace
.exists())) {
66 linkedTrace
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
,
67 "org.eclipse.linuxtools.tmf.ui.type.ctf");
69 final TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
70 TmfTraceElement traceElement
= projectElement
.getTracesFolder().getTraces().get(0);
71 traceElement
.refreshTraceType();
73 return projectElement
;
77 * Get the name of the test trace element
79 * @return The trace name
81 public static String
getTraceName() {
82 File file
= new File(PATH
);
83 String path
= file
.getAbsolutePath();
84 final IPath pathString
= Path
.fromOSString(path
);
85 return pathString
.lastSegment();
93 * @throws CoreException
94 * Thrown by the resource deletion
96 public static void deleteProject(TmfProjectElement project
) throws CoreException
{
97 /* Delete experiments */
98 for (ITmfProjectModelElement element
: project
.getExperimentsFolder().getChildren()) {
99 if (element
instanceof TmfExperimentElement
) {
100 TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
101 IResource resource
= experiment
.getResource();
103 /* Close the experiment if open */
104 experiment
.closeEditors();
106 IPath path
= resource
.getLocation();
108 /* Delete supplementary files */
109 experiment
.deleteSupplementaryFolder();
112 /* Finally, delete the experiment */
113 resource
.delete(true, null);
118 for (ITmfProjectModelElement element
: project
.getTracesFolder().getChildren()) {
119 if (element
instanceof TmfTraceElement
) {
120 TmfTraceElement trace
= (TmfTraceElement
) element
;
121 IResource resource
= trace
.getResource();
123 /* Close the trace if open */
124 trace
.closeEditors();
126 IPath path
= resource
.getLocation();
128 /* Delete supplementary files */
129 trace
.deleteSupplementaryFolder();
132 /* Finally, delete the trace */
133 resource
.delete(true, new NullProgressMonitor());
137 /* Delete the project itself */
138 project
.getResource().delete(true, null);
142 * Makes the main display thread sleep, so it gives a chance to other thread
143 * needing the main display to execute
145 * @param waitTimeMillis
146 * time to wait in millisecond
148 public static void delayThread(final long waitTimeMillis
) {
149 final Display display
= Display
.getCurrent();
150 if (display
!= null) {
151 final long endTimeMillis
= System
.currentTimeMillis() + waitTimeMillis
;
152 while (System
.currentTimeMillis() < endTimeMillis
) {
153 if (!display
.readAndDispatch()) {
160 Thread
.sleep(waitTimeMillis
);
161 } catch (final InterruptedException e
) {