Commit | Line | Data |
---|---|---|
87644443 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 École Polytechnique de Montréal | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
3d2e4ad3 | 13 | package org.eclipse.linuxtools.tmf.ui.tests.shared; |
87644443 | 14 | |
c068a752 GB |
15 | import static org.junit.Assume.assumeTrue; |
16 | ||
87644443 | 17 | import java.io.File; |
bc5f2035 | 18 | import java.util.concurrent.TimeoutException; |
87644443 GB |
19 | |
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; | |
c068a752 | 27 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
87644443 GB |
28 | import org.eclipse.linuxtools.internal.tmf.ui.project.model.TmfImportHelper; |
29 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; | |
9ac63b5b | 30 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; |
87644443 GB |
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; | |
38 | ||
39 | /** | |
40 | * Creates objects used for this package's testing purposes | |
c068a752 GB |
41 | * |
42 | * @author Geneviève Bastien | |
87644443 GB |
43 | */ |
44 | public class ProjectModelTestData { | |
45 | ||
bc5f2035 GB |
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. */ | |
e44f940d | 49 | private static final long DEFAULT_DELAY = 500; |
bc5f2035 | 50 | |
87644443 GB |
51 | /** Default test project name */ |
52 | public static final String PROJECT_NAME = "Test_Project"; | |
53 | ||
9ac63b5b | 54 | private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL; |
87644443 GB |
55 | |
56 | /** | |
57 | * Gets a project element with traces all initialized | |
58 | * | |
59 | * @return A project stub element | |
60 | * @throws CoreException | |
61 | * If something happened with the project creation | |
62 | */ | |
63 | public static TmfProjectElement getFilledProject() throws CoreException { | |
64 | ||
c068a752 GB |
65 | assumeTrue(CtfTmfTestTrace.KERNEL.exists()); |
66 | ||
87644443 GB |
67 | IProject project = TmfProjectRegistry.createProject(PROJECT_NAME, null, null); |
68 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); | |
69 | ||
70 | /* Create a trace, if it exist, it will be replaced */ | |
9ac63b5b | 71 | File file = new File(testTrace.getPath()); |
87644443 GB |
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())) { | |
76 | return null; | |
77 | } | |
78 | linkedTrace.setPersistentProperty(TmfCommonConstants.TRACETYPE, | |
c068a752 | 79 | "org.eclipse.linuxtools.tmf.tests.ctf.tracetype"); |
87644443 GB |
80 | |
81 | final TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true); | |
82 | TmfTraceElement traceElement = projectElement.getTracesFolder().getTraces().get(0); | |
83 | traceElement.refreshTraceType(); | |
84 | ||
85 | return projectElement; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Get the name of the test trace element | |
90 | * | |
91 | * @return The trace name | |
92 | */ | |
93 | public static String getTraceName() { | |
9ac63b5b | 94 | File file = new File(testTrace.getPath()); |
87644443 GB |
95 | String path = file.getAbsolutePath(); |
96 | final IPath pathString = Path.fromOSString(path); | |
97 | return pathString.lastSegment(); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Deletes a project | |
102 | * | |
103 | * @param project | |
104 | * Project to delete | |
87644443 | 105 | */ |
c068a752 | 106 | public static void deleteProject(TmfProjectElement project) { |
87644443 | 107 | /* Delete experiments */ |
f537c959 PT |
108 | ITmfProjectModelElement[] experiments = project.getExperimentsFolder().getChildren().toArray(new ITmfProjectModelElement[0]); |
109 | for (ITmfProjectModelElement element : experiments) { | |
87644443 GB |
110 | if (element instanceof TmfExperimentElement) { |
111 | TmfExperimentElement experiment = (TmfExperimentElement) element; | |
112 | IResource resource = experiment.getResource(); | |
113 | ||
114 | /* Close the experiment if open */ | |
115 | experiment.closeEditors(); | |
116 | ||
117 | IPath path = resource.getLocation(); | |
118 | if (path != null) { | |
119 | /* Delete supplementary files */ | |
120 | experiment.deleteSupplementaryFolder(); | |
121 | } | |
122 | ||
123 | /* Finally, delete the experiment */ | |
c068a752 GB |
124 | try { |
125 | resource.delete(true, null); | |
126 | } catch (CoreException e) { | |
127 | Activator.getDefault().logError("Error deleting experiment element", e); | |
128 | } | |
87644443 GB |
129 | } |
130 | } | |
131 | ||
132 | /* Delete traces */ | |
f537c959 PT |
133 | ITmfProjectModelElement[] traces = project.getTracesFolder().getChildren().toArray(new ITmfProjectModelElement[0]); |
134 | for (ITmfProjectModelElement element : traces) { | |
87644443 GB |
135 | if (element instanceof TmfTraceElement) { |
136 | TmfTraceElement trace = (TmfTraceElement) element; | |
137 | IResource resource = trace.getResource(); | |
138 | ||
139 | /* Close the trace if open */ | |
140 | trace.closeEditors(); | |
141 | ||
142 | IPath path = resource.getLocation(); | |
143 | if (path != null) { | |
144 | /* Delete supplementary files */ | |
145 | trace.deleteSupplementaryFolder(); | |
146 | } | |
147 | ||
148 | /* Finally, delete the trace */ | |
c068a752 GB |
149 | try { |
150 | resource.delete(true, new NullProgressMonitor()); | |
151 | } catch (CoreException e) { | |
152 | Activator.getDefault().logError("Error deleting trace element", e); | |
153 | } | |
87644443 GB |
154 | } |
155 | } | |
156 | ||
157 | /* Delete the project itself */ | |
c068a752 GB |
158 | try { |
159 | project.getResource().delete(true, null); | |
160 | } catch (CoreException e) { | |
161 | Activator.getDefault().logError("Error deleting project", e); | |
162 | } | |
87644443 GB |
163 | } |
164 | ||
165 | /** | |
3d2e4ad3 GB |
166 | * Makes the main display thread sleep, so it gives a chance to other |
167 | * threads needing the main display to execute | |
87644443 GB |
168 | * |
169 | * @param waitTimeMillis | |
170 | * time to wait in millisecond | |
171 | */ | |
172 | public static void delayThread(final long waitTimeMillis) { | |
173 | final Display display = Display.getCurrent(); | |
174 | if (display != null) { | |
175 | final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis; | |
176 | while (System.currentTimeMillis() < endTimeMillis) { | |
177 | if (!display.readAndDispatch()) { | |
178 | display.sleep(); | |
179 | } | |
180 | display.update(); | |
181 | } | |
182 | } else { | |
183 | try { | |
184 | Thread.sleep(waitTimeMillis); | |
185 | } catch (final InterruptedException e) { | |
186 | // Ignored | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
bc5f2035 GB |
191 | /** |
192 | * Makes the main display thread sleep to give a chance to other threads to | |
193 | * execute. It sleeps until the a trace element's corresponding trace is | |
194 | * available (opened) or returns after a timeout. It allows to set short | |
195 | * delays, while still not failing tests when it randomly takes a bit more | |
196 | * time for the trace to open. | |
197 | * | |
198 | * If the project model element sent in parameter is not a trace element, | |
199 | * then the thread is delayed only once by the default delay time. For | |
200 | * longer delays in those cases, it is preferable to use the | |
201 | * {@link ProjectModelTestData#delayThread(long)} instead. | |
202 | * | |
203 | * Timeout is DELAY_COUNTER * DEFAULT_DELAY ms | |
204 | * | |
205 | * @param projectElement | |
206 | * The trace element we are waiting for. If the element if not of | |
207 | * type TmfTraceElement, the thread is delayed only once. | |
208 | * @throws TimeoutException | |
209 | * If after the maximum number of delays the trace is still | |
210 | * null, we throw a timeout exception, the trace has not opened. | |
211 | */ | |
212 | public static void delayUntilTraceOpened(final ITmfProjectModelElement projectElement) throws TimeoutException { | |
213 | if (projectElement instanceof TmfTraceElement) { | |
214 | TmfTraceElement traceElement = (TmfTraceElement) projectElement; | |
e44f940d | 215 | final long deadline = System.nanoTime() + (DELAY_COUNTER * DEFAULT_DELAY * 1000000L); |
bc5f2035 GB |
216 | do { |
217 | delayThread(DEFAULT_DELAY); | |
218 | if (traceElement.getTrace() != null) { | |
219 | return; | |
220 | } | |
221 | } while (System.nanoTime() < deadline); | |
222 | throw new TimeoutException("Timeout while waiting for " + traceElement); | |
223 | } | |
224 | delayThread(DEFAULT_DELAY); | |
225 | } | |
226 | ||
87644443 | 227 | } |