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 GB |
17 | import java.io.File; |
18 | ||
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; | |
c068a752 | 26 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
87644443 GB |
27 | import org.eclipse.linuxtools.internal.tmf.ui.project.model.TmfImportHelper; |
28 | import org.eclipse.linuxtools.tmf.core.TmfCommonConstants; | |
9ac63b5b | 29 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace; |
87644443 GB |
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; | |
37 | ||
38 | /** | |
39 | * Creates objects used for this package's testing purposes | |
c068a752 GB |
40 | * |
41 | * @author Geneviève Bastien | |
87644443 GB |
42 | */ |
43 | public class ProjectModelTestData { | |
44 | ||
45 | /** Default test project name */ | |
46 | public static final String PROJECT_NAME = "Test_Project"; | |
47 | ||
9ac63b5b | 48 | private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL; |
87644443 GB |
49 | |
50 | /** | |
51 | * Gets a project element with traces all initialized | |
52 | * | |
53 | * @return A project stub element | |
54 | * @throws CoreException | |
55 | * If something happened with the project creation | |
56 | */ | |
57 | public static TmfProjectElement getFilledProject() throws CoreException { | |
58 | ||
c068a752 GB |
59 | assumeTrue(CtfTmfTestTrace.KERNEL.exists()); |
60 | ||
87644443 GB |
61 | IProject project = TmfProjectRegistry.createProject(PROJECT_NAME, null, null); |
62 | IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME); | |
63 | ||
64 | /* Create a trace, if it exist, it will be replaced */ | |
9ac63b5b | 65 | File file = new File(testTrace.getPath()); |
87644443 GB |
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())) { | |
70 | return null; | |
71 | } | |
72 | linkedTrace.setPersistentProperty(TmfCommonConstants.TRACETYPE, | |
c068a752 | 73 | "org.eclipse.linuxtools.tmf.tests.ctf.tracetype"); |
87644443 GB |
74 | |
75 | final TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true); | |
76 | TmfTraceElement traceElement = projectElement.getTracesFolder().getTraces().get(0); | |
77 | traceElement.refreshTraceType(); | |
78 | ||
79 | return projectElement; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Get the name of the test trace element | |
84 | * | |
85 | * @return The trace name | |
86 | */ | |
87 | public static String getTraceName() { | |
9ac63b5b | 88 | File file = new File(testTrace.getPath()); |
87644443 GB |
89 | String path = file.getAbsolutePath(); |
90 | final IPath pathString = Path.fromOSString(path); | |
91 | return pathString.lastSegment(); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Deletes a project | |
96 | * | |
97 | * @param project | |
98 | * Project to delete | |
87644443 | 99 | */ |
c068a752 | 100 | public static void deleteProject(TmfProjectElement project) { |
87644443 GB |
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(); | |
106 | ||
107 | /* Close the experiment if open */ | |
108 | experiment.closeEditors(); | |
109 | ||
110 | IPath path = resource.getLocation(); | |
111 | if (path != null) { | |
112 | /* Delete supplementary files */ | |
113 | experiment.deleteSupplementaryFolder(); | |
114 | } | |
115 | ||
116 | /* Finally, delete the experiment */ | |
c068a752 GB |
117 | try { |
118 | resource.delete(true, null); | |
119 | } catch (CoreException e) { | |
120 | Activator.getDefault().logError("Error deleting experiment element", e); | |
121 | } | |
87644443 GB |
122 | } |
123 | } | |
124 | ||
125 | /* Delete traces */ | |
126 | for (ITmfProjectModelElement element : project.getTracesFolder().getChildren()) { | |
127 | if (element instanceof TmfTraceElement) { | |
128 | TmfTraceElement trace = (TmfTraceElement) element; | |
129 | IResource resource = trace.getResource(); | |
130 | ||
131 | /* Close the trace if open */ | |
132 | trace.closeEditors(); | |
133 | ||
134 | IPath path = resource.getLocation(); | |
135 | if (path != null) { | |
136 | /* Delete supplementary files */ | |
137 | trace.deleteSupplementaryFolder(); | |
138 | } | |
139 | ||
140 | /* Finally, delete the trace */ | |
c068a752 GB |
141 | try { |
142 | resource.delete(true, new NullProgressMonitor()); | |
143 | } catch (CoreException e) { | |
144 | Activator.getDefault().logError("Error deleting trace element", e); | |
145 | } | |
87644443 GB |
146 | } |
147 | } | |
148 | ||
149 | /* Delete the project itself */ | |
c068a752 GB |
150 | try { |
151 | project.getResource().delete(true, null); | |
152 | } catch (CoreException e) { | |
153 | Activator.getDefault().logError("Error deleting project", e); | |
154 | } | |
87644443 GB |
155 | } |
156 | ||
157 | /** | |
3d2e4ad3 GB |
158 | * Makes the main display thread sleep, so it gives a chance to other |
159 | * threads needing the main display to execute | |
87644443 GB |
160 | * |
161 | * @param waitTimeMillis | |
162 | * time to wait in millisecond | |
163 | */ | |
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()) { | |
170 | display.sleep(); | |
171 | } | |
172 | display.update(); | |
173 | } | |
174 | } else { | |
175 | try { | |
176 | Thread.sleep(waitTimeMillis); | |
177 | } catch (final InterruptedException e) { | |
178 | // Ignored | |
179 | } | |
180 | } | |
181 | } | |
182 | ||
183 | } |