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
.shared
;
15 import static org
.junit
.Assume
.assumeTrue
;
18 import java
.util
.concurrent
.TimeoutException
;
20 import org
.eclipse
.core
.resources
.IFolder
;
21 import org
.eclipse
.core
.resources
.IProject
;
22 import org
.eclipse
.core
.resources
.IResource
;
23 import org
.eclipse
.core
.runtime
.CoreException
;
24 import org
.eclipse
.core
.runtime
.IPath
;
25 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.Path
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.model
.TmfImportHelper
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.CtfTmfTestTrace
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.ITmfProjectModelElement
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectElement
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfProjectRegistry
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceFolder
;
37 import org
.eclipse
.swt
.widgets
.Display
;
40 * Creates objects used for this package's testing purposes
42 * @author Geneviève Bastien
44 public class ProjectModelTestData
{
46 /* Maximum number of thread delays the main thread will do before timing out */
47 private static final int DELAY_COUNTER
= 10;
48 /* Default delay time when having the main thread sleep. */
49 private static final int DEFAULT_DELAY
= 500;
51 /** Default test project name */
52 public static final String PROJECT_NAME
= "Test_Project";
54 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
57 * Gets a project element with traces all initialized
59 * @return A project stub element
60 * @throws CoreException
61 * If something happened with the project creation
63 public static TmfProjectElement
getFilledProject() throws CoreException
{
65 assumeTrue(CtfTmfTestTrace
.KERNEL
.exists());
67 IProject project
= TmfProjectRegistry
.createProject(PROJECT_NAME
, null, null);
68 IFolder traceFolder
= project
.getFolder(TmfTraceFolder
.TRACE_FOLDER_NAME
);
70 /* Create a trace, if it exist, it will be replaced */
71 File file
= new File(testTrace
.getPath());
72 String path
= file
.getAbsolutePath();
73 final IPath pathString
= Path
.fromOSString(path
);
74 IResource linkedTrace
= TmfImportHelper
.createLink(traceFolder
, pathString
, pathString
.lastSegment());
75 if (!(linkedTrace
!= null && linkedTrace
.exists())) {
78 linkedTrace
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
,
79 "org.eclipse.linuxtools.tmf.tests.ctf.tracetype");
81 final TmfProjectElement projectElement
= TmfProjectRegistry
.getProject(project
, true);
82 TmfTraceElement traceElement
= projectElement
.getTracesFolder().getTraces().get(0);
83 traceElement
.refreshTraceType();
85 return projectElement
;
89 * Get the name of the test trace element
91 * @return The trace name
93 public static String
getTraceName() {
94 File file
= new File(testTrace
.getPath());
95 String path
= file
.getAbsolutePath();
96 final IPath pathString
= Path
.fromOSString(path
);
97 return pathString
.lastSegment();
106 public static void deleteProject(TmfProjectElement project
) {
107 /* Delete experiments */
108 for (ITmfProjectModelElement element
: project
.getExperimentsFolder().getChildren()) {
109 if (element
instanceof TmfExperimentElement
) {
110 TmfExperimentElement experiment
= (TmfExperimentElement
) element
;
111 IResource resource
= experiment
.getResource();
113 /* Close the experiment if open */
114 experiment
.closeEditors();
116 IPath path
= resource
.getLocation();
118 /* Delete supplementary files */
119 experiment
.deleteSupplementaryFolder();
122 /* Finally, delete the experiment */
124 resource
.delete(true, null);
125 } catch (CoreException e
) {
126 Activator
.getDefault().logError("Error deleting experiment element", e
);
132 for (ITmfProjectModelElement element
: project
.getTracesFolder().getChildren()) {
133 if (element
instanceof TmfTraceElement
) {
134 TmfTraceElement trace
= (TmfTraceElement
) element
;
135 IResource resource
= trace
.getResource();
137 /* Close the trace if open */
138 trace
.closeEditors();
140 IPath path
= resource
.getLocation();
142 /* Delete supplementary files */
143 trace
.deleteSupplementaryFolder();
146 /* Finally, delete the trace */
148 resource
.delete(true, new NullProgressMonitor());
149 } catch (CoreException e
) {
150 Activator
.getDefault().logError("Error deleting trace element", e
);
155 /* Delete the project itself */
157 project
.getResource().delete(true, null);
158 } catch (CoreException e
) {
159 Activator
.getDefault().logError("Error deleting project", e
);
164 * Makes the main display thread sleep, so it gives a chance to other
165 * threads needing the main display to execute
167 * @param waitTimeMillis
168 * time to wait in millisecond
170 public static void delayThread(final long waitTimeMillis
) {
171 final Display display
= Display
.getCurrent();
172 if (display
!= null) {
173 final long endTimeMillis
= System
.currentTimeMillis() + waitTimeMillis
;
174 while (System
.currentTimeMillis() < endTimeMillis
) {
175 if (!display
.readAndDispatch()) {
182 Thread
.sleep(waitTimeMillis
);
183 } catch (final InterruptedException e
) {
190 * Makes the main display thread sleep to give a chance to other threads to
191 * execute. It sleeps until the a trace element's corresponding trace is
192 * available (opened) or returns after a timeout. It allows to set short
193 * delays, while still not failing tests when it randomly takes a bit more
194 * time for the trace to open.
196 * If the project model element sent in parameter is not a trace element,
197 * then the thread is delayed only once by the default delay time. For
198 * longer delays in those cases, it is preferable to use the
199 * {@link ProjectModelTestData#delayThread(long)} instead.
201 * Timeout is DELAY_COUNTER * DEFAULT_DELAY ms
203 * @param projectElement
204 * The trace element we are waiting for. If the element if not of
205 * type TmfTraceElement, the thread is delayed only once.
206 * @throws TimeoutException
207 * If after the maximum number of delays the trace is still
208 * null, we throw a timeout exception, the trace has not opened.
210 public static void delayUntilTraceOpened(final ITmfProjectModelElement projectElement
) throws TimeoutException
{
211 if (projectElement
instanceof TmfTraceElement
) {
212 TmfTraceElement traceElement
= (TmfTraceElement
) projectElement
;
213 final long deadline
= System
.nanoTime() + (DELAY_COUNTER
* DEFAULT_DELAY
* 1000000);
215 delayThread(DEFAULT_DELAY
);
216 if (traceElement
.getTrace() != null) {
219 } while (System
.nanoTime() < deadline
);
220 throw new TimeoutException("Timeout while waiting for " + traceElement
);
222 delayThread(DEFAULT_DELAY
);