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
;
15 import static org
.junit
.Assume
.assumeTrue
;
19 import org
.eclipse
.core
.resources
.IFolder
;
20 import org
.eclipse
.core
.resources
.IProject
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.CoreException
;
23 import org
.eclipse
.core
.runtime
.IPath
;
24 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.Path
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.CtfTmfTestTrace
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
36 import org
.eclipse
.swt
.widgets
.Display
;
39 * Creates objects used for this package's testing purposes
41 * @author Geneviève Bastien
43 public class ProjectModelTestData
{
45 /** Default test project name */
46 public static final String PROJECT_NAME
= "Test_Project";
48 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
51 * Gets a project element with traces all initialized
53 * @return A project stub element
54 * @throws CoreException
55 * If something happened with the project creation
57 public static TmfProjectElement
getFilledProject() throws CoreException
{
59 assumeTrue(CtfTmfTestTrace
.KERNEL
.exists());
61 IProject project
= TmfProjectRegistry
.createProject(PROJECT_NAME
, null, null);
62 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
64 /* Create a trace, if it exist, it will be replaced */
65 File file
= new File(testTrace
.getPath());
66 String path
= file
.getAbsolutePath();
67 final IPath pathString
= Path
.fromOSString(path
);
68 IResource linkedTrace
= TmfImportHelper
.createLink(traceFolder
, pathString
, pathString
.lastSegment());
69 if (!(linkedTrace
!= null && linkedTrace
.exists())) {
72 linkedTrace
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
,
73 "org.eclipse.linuxtools.tmf.tests.ctf.tracetype");
75 final TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
76 TmfTraceElement traceElement
= projectElement
.getTracesFolder().getTraces().get(0);
77 traceElement
.refreshTraceType();
79 return projectElement
;
83 * Get the name of the test trace element
85 * @return The trace name
87 public static String
getTraceName() {
88 File file
= new File(testTrace
.getPath());
89 String path
= file
.getAbsolutePath();
90 final IPath pathString
= Path
.fromOSString(path
);
91 return pathString
.lastSegment();
100 public static void deleteProject(TmfProjectElement project
) {
101 /* Delete experiments */
102 for (ITmfProjectModelElement element
: project
.getExperimentsFolder().getChildren()) {
103 if (element
instanceof TmfExperimentElement
) {
104 TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
105 IResource resource
= experiment
.getResource();
107 /* Close the experiment if open */
108 experiment
.closeEditors();
110 IPath path
= resource
.getLocation();
112 /* Delete supplementary files */
113 experiment
.deleteSupplementaryFolder();
116 /* Finally, delete the experiment */
118 resource
.delete(true, null);
119 } catch (CoreException e
) {
120 Activator
.getDefault().logError("Error deleting experiment element", e
);
126 for (ITmfProjectModelElement element
: project
.getTracesFolder().getChildren()) {
127 if (element
instanceof TmfTraceElement
) {
128 TmfTraceElement trace
= (TmfTraceElement
) element
;
129 IResource resource
= trace
.getResource();
131 /* Close the trace if open */
132 trace
.closeEditors();
134 IPath path
= resource
.getLocation();
136 /* Delete supplementary files */
137 trace
.deleteSupplementaryFolder();
140 /* Finally, delete the trace */
142 resource
.delete(true, new NullProgressMonitor());
143 } catch (CoreException e
) {
144 Activator
.getDefault().logError("Error deleting trace element", e
);
149 /* Delete the project itself */
151 project
.getResource().delete(true, null);
152 } catch (CoreException e
) {
153 Activator
.getDefault().logError("Error deleting project", e
);
158 * Makes the main display thread sleep, so it gives a chance to other thread
159 * needing the main display to execute
161 * @param waitTimeMillis
162 * time to wait in millisecond
164 public static void delayThread(final long waitTimeMillis
) {
165 final Display display
= Display
.getCurrent();
166 if (display
!= null) {
167 final long endTimeMillis
= System
.currentTimeMillis() + waitTimeMillis
;
168 while (System
.currentTimeMillis() < endTimeMillis
) {
169 if (!display
.readAndDispatch()) {
176 Thread
.sleep(waitTimeMillis
);
177 } catch (final InterruptedException e
) {