Commit | Line | Data |
---|---|---|
87644443 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
87644443 GB |
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; |
6a6adab9 | 18 | import java.lang.reflect.InvocationTargetException; |
bc5f2035 | 19 | import java.util.concurrent.TimeoutException; |
87644443 GB |
20 | |
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; | |
6a6adab9 | 26 | import org.eclipse.core.runtime.IProgressMonitor; |
87644443 GB |
27 | import org.eclipse.core.runtime.NullProgressMonitor; |
28 | import org.eclipse.core.runtime.Path; | |
c068a752 | 29 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
87644443 GB |
30 | import org.eclipse.linuxtools.internal.tmf.ui.project.model.TmfImportHelper; |
31 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; | |
9ac63b5b | 32 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; |
87644443 | 33 | import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement; |
6a6adab9 | 34 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfCommonProjectElement; |
87644443 GB |
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; | |
6a6adab9 GB |
41 | import org.eclipse.ui.PlatformUI; |
42 | import org.eclipse.ui.actions.WorkspaceModifyOperation; | |
87644443 GB |
43 | |
44 | /** | |
45 | * Creates objects used for this package's testing purposes | |
c068a752 GB |
46 | * |
47 | * @author Geneviève Bastien | |
87644443 GB |
48 | */ |
49 | public class ProjectModelTestData { | |
50 | ||
bc5f2035 | 51 | /* Maximum number of thread delays the main thread will do before timing out */ |
96e1c302 | 52 | private static final int DELAY_COUNTER = 1000; |
bc5f2035 | 53 | /* Default delay time when having the main thread sleep. */ |
e44f940d | 54 | private static final long DEFAULT_DELAY = 500; |
bc5f2035 | 55 | |
87644443 GB |
56 | /** Default test project name */ |
57 | public static final String PROJECT_NAME = "Test_Project"; | |
58 | ||
9ac63b5b | 59 | private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL; |
87644443 GB |
60 | |
61 | /** | |
62 | * Gets a project element with traces all initialized | |
63 | * | |
64 | * @return A project stub element | |
65 | * @throws CoreException | |
66 | * If something happened with the project creation | |
67 | */ | |
68 | public static TmfProjectElement getFilledProject() throws CoreException { | |
69 | ||
c068a752 GB |
70 | assumeTrue(CtfTmfTestTrace.KERNEL.exists()); |
71 | ||
87644443 GB |
72 | IProject project = TmfProjectRegistry.createProject(PROJECT_NAME, null, null); |
73 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); | |
74 | ||
75 | /* Create a trace, if it exist, it will be replaced */ | |
9ac63b5b | 76 | File file = new File(testTrace.getPath()); |
87644443 GB |
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())) { | |
81 | return null; | |
82 | } | |
83 | linkedTrace.setPersistentProperty(TmfCommonConstants.TRACETYPE, | |
a926c25c | 84 | "org.eclipse.linuxtools.tmf.core.tests.ctf.tracetype"); |
87644443 GB |
85 | |
86 | final TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true); | |
87 | TmfTraceElement traceElement = projectElement.getTracesFolder().getTraces().get(0); | |
88 | traceElement.refreshTraceType(); | |
89 | ||
94227c30 GB |
90 | projectElement.refresh(); |
91 | ||
87644443 GB |
92 | return projectElement; |
93 | } | |
94 | ||
6a6adab9 GB |
95 | /** |
96 | * Adds a new experiment to the project | |
97 | * | |
98 | * @param projectElement | |
99 | * The project to add to | |
100 | * @param experimentName | |
101 | * Name of the experiment | |
102 | * @return The newly created experiment | |
103 | */ | |
104 | public static TmfExperimentElement addExperiment(TmfProjectElement projectElement, String experimentName) { | |
105 | IFolder experimentFolder = projectElement.getExperimentsFolder().getResource(); | |
106 | final IFolder folder = experimentFolder.getFolder(experimentName); | |
107 | ||
108 | WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { | |
109 | @Override | |
110 | public void execute(IProgressMonitor monitor) throws CoreException { | |
111 | monitor.beginTask("", 1000); | |
112 | folder.create(false, true, monitor); | |
113 | monitor.done(); | |
114 | } | |
115 | }; | |
116 | try { | |
117 | PlatformUI.getWorkbench().getProgressService().busyCursorWhile(operation); | |
118 | } catch (InterruptedException | InvocationTargetException | RuntimeException exception) { | |
119 | ||
120 | } | |
121 | ||
122 | for (ITmfProjectModelElement el : projectElement.getExperimentsFolder().getChildren()) { | |
123 | if (el.getName().equals(experimentName) && (el instanceof TmfExperimentElement)) { | |
124 | return (TmfExperimentElement) el; | |
125 | } | |
126 | } | |
127 | return null; | |
128 | } | |
129 | ||
87644443 GB |
130 | /** |
131 | * Get the name of the test trace element | |
132 | * | |
133 | * @return The trace name | |
134 | */ | |
135 | public static String getTraceName() { | |
9ac63b5b | 136 | File file = new File(testTrace.getPath()); |
87644443 GB |
137 | String path = file.getAbsolutePath(); |
138 | final IPath pathString = Path.fromOSString(path); | |
139 | return pathString.lastSegment(); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Deletes a project | |
144 | * | |
145 | * @param project | |
146 | * Project to delete | |
87644443 | 147 | */ |
c068a752 | 148 | public static void deleteProject(TmfProjectElement project) { |
87644443 | 149 | /* Delete experiments */ |
f537c959 PT |
150 | ITmfProjectModelElement[] experiments = project.getExperimentsFolder().getChildren().toArray(new ITmfProjectModelElement[0]); |
151 | for (ITmfProjectModelElement element : experiments) { | |
87644443 GB |
152 | if (element instanceof TmfExperimentElement) { |
153 | TmfExperimentElement experiment = (TmfExperimentElement) element; | |
154 | IResource resource = experiment.getResource(); | |
155 | ||
156 | /* Close the experiment if open */ | |
157 | experiment.closeEditors(); | |
158 | ||
159 | IPath path = resource.getLocation(); | |
160 | if (path != null) { | |
161 | /* Delete supplementary files */ | |
162 | experiment.deleteSupplementaryFolder(); | |
163 | } | |
164 | ||
165 | /* Finally, delete the experiment */ | |
c068a752 GB |
166 | try { |
167 | resource.delete(true, null); | |
168 | } catch (CoreException e) { | |
169 | Activator.getDefault().logError("Error deleting experiment element", e); | |
170 | } | |
87644443 GB |
171 | } |
172 | } | |
173 | ||
174 | /* Delete traces */ | |
f537c959 PT |
175 | ITmfProjectModelElement[] traces = project.getTracesFolder().getChildren().toArray(new ITmfProjectModelElement[0]); |
176 | for (ITmfProjectModelElement element : traces) { | |
87644443 GB |
177 | if (element instanceof TmfTraceElement) { |
178 | TmfTraceElement trace = (TmfTraceElement) element; | |
179 | IResource resource = trace.getResource(); | |
180 | ||
181 | /* Close the trace if open */ | |
182 | trace.closeEditors(); | |
183 | ||
184 | IPath path = resource.getLocation(); | |
185 | if (path != null) { | |
186 | /* Delete supplementary files */ | |
187 | trace.deleteSupplementaryFolder(); | |
188 | } | |
189 | ||
190 | /* Finally, delete the trace */ | |
c068a752 GB |
191 | try { |
192 | resource.delete(true, new NullProgressMonitor()); | |
193 | } catch (CoreException e) { | |
194 | Activator.getDefault().logError("Error deleting trace element", e); | |
195 | } | |
87644443 GB |
196 | } |
197 | } | |
198 | ||
199 | /* Delete the project itself */ | |
c068a752 GB |
200 | try { |
201 | project.getResource().delete(true, null); | |
202 | } catch (CoreException e) { | |
203 | Activator.getDefault().logError("Error deleting project", e); | |
204 | } | |
87644443 GB |
205 | } |
206 | ||
207 | /** | |
3d2e4ad3 GB |
208 | * Makes the main display thread sleep, so it gives a chance to other |
209 | * threads needing the main display to execute | |
87644443 GB |
210 | * |
211 | * @param waitTimeMillis | |
212 | * time to wait in millisecond | |
213 | */ | |
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()) { | |
220 | display.sleep(); | |
221 | } | |
222 | display.update(); | |
223 | } | |
224 | } else { | |
225 | try { | |
226 | Thread.sleep(waitTimeMillis); | |
227 | } catch (final InterruptedException e) { | |
228 | // Ignored | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
bc5f2035 GB |
233 | /** |
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. | |
239 | * | |
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. | |
244 | * | |
245 | * Timeout is DELAY_COUNTER * DEFAULT_DELAY ms | |
246 | * | |
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. | |
253 | */ | |
254 | public static void delayUntilTraceOpened(final ITmfProjectModelElement projectElement) throws TimeoutException { | |
6a6adab9 GB |
255 | if (projectElement instanceof TmfCommonProjectElement) { |
256 | TmfCommonProjectElement traceElement = (TmfCommonProjectElement) projectElement; | |
e44f940d | 257 | final long deadline = System.nanoTime() + (DELAY_COUNTER * DEFAULT_DELAY * 1000000L); |
bc5f2035 GB |
258 | do { |
259 | delayThread(DEFAULT_DELAY); | |
260 | if (traceElement.getTrace() != null) { | |
261 | return; | |
262 | } | |
263 | } while (System.nanoTime() < deadline); | |
264 | throw new TimeoutException("Timeout while waiting for " + traceElement); | |
265 | } | |
266 | delayThread(DEFAULT_DELAY); | |
267 | } | |
268 | ||
87644443 | 269 | } |