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