tmf: Move statevalue unboxing method implementations to the base classes
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui.tests / src / org / eclipse / linuxtools / tmf / ui / tests / project / model / ProjectModelTestData.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.ui.tests.project.model;
14
15import java.io.File;
16
17import org.eclipse.core.resources.IFolder;
18import org.eclipse.core.resources.IProject;
19import org.eclipse.core.resources.IResource;
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.IPath;
22import org.eclipse.core.runtime.NullProgressMonitor;
23import org.eclipse.core.runtime.Path;
24import org.eclipse.linuxtools.internal.tmf.ui.project.model.TmfImportHelper;
25import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
26import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTraces;
27import org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement;
28import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
29import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectElement;
30import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectRegistry;
31import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
32import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceFolder;
33import org.eclipse.swt.widgets.Display;
34
35/**
36 * Creates objects used for this package's testing purposes
37 */
38public class ProjectModelTestData {
39
40 /** Default test project name */
41 public static final String PROJECT_NAME = "Test_Project";
42
43 private static final int TRACE_INDEX = 0;
44 private static final String PATH = CtfTmfTestTraces.getTestTracePath(TRACE_INDEX);
45
46 /**
47 * Gets a project element with traces all initialized
48 *
49 * @return A project stub element
50 * @throws CoreException
51 * If something happened with the project creation
52 */
53 public static TmfProjectElement getFilledProject() throws CoreException {
54
55 IProject project = TmfProjectRegistry.createProject(PROJECT_NAME, null, null);
56 IFolder traceFolder = project.getFolder(TmfTraceFolder.TRACE_FOLDER_NAME);
57
58 /* Create a trace, if it exist, it will be replaced */
59 File file = new File(PATH);
60 String path = file.getAbsolutePath();
61 final IPath pathString = Path.fromOSString(path);
62 IResource linkedTrace = TmfImportHelper.createLink(traceFolder, pathString, pathString.lastSegment());
63 if (!(linkedTrace != null && linkedTrace.exists())) {
64 return null;
65 }
66 linkedTrace.setPersistentProperty(TmfCommonConstants.TRACETYPE,
67 "org.eclipse.linuxtools.tmf.ui.type.ctf");
68
69 final TmfProjectElement projectElement = TmfProjectRegistry.getProject(project, true);
70 TmfTraceElement traceElement = projectElement.getTracesFolder().getTraces().get(0);
71 traceElement.refreshTraceType();
72
73 return projectElement;
74 }
75
76 /**
77 * Get the name of the test trace element
78 *
79 * @return The trace name
80 */
81 public static String getTraceName() {
82 File file = new File(PATH);
83 String path = file.getAbsolutePath();
84 final IPath pathString = Path.fromOSString(path);
85 return pathString.lastSegment();
86 }
87
88 /**
89 * Deletes a project
90 *
91 * @param project
92 * Project to delete
93 * @throws CoreException
94 * Thrown by the resource deletion
95 */
96 public static void deleteProject(TmfProjectElement project) throws CoreException {
97 /* Delete experiments */
98 for (ITmfProjectModelElement element : project.getExperimentsFolder().getChildren()) {
99 if (element instanceof TmfExperimentElement) {
100 TmfExperimentElement experiment = (TmfExperimentElement) element;
101 IResource resource = experiment.getResource();
102
103 /* Close the experiment if open */
104 experiment.closeEditors();
105
106 IPath path = resource.getLocation();
107 if (path != null) {
108 /* Delete supplementary files */
109 experiment.deleteSupplementaryFolder();
110 }
111
112 /* Finally, delete the experiment */
113 resource.delete(true, null);
114 }
115 }
116
117 /* Delete traces */
118 for (ITmfProjectModelElement element : project.getTracesFolder().getChildren()) {
119 if (element instanceof TmfTraceElement) {
120 TmfTraceElement trace = (TmfTraceElement) element;
121 IResource resource = trace.getResource();
122
123 /* Close the trace if open */
124 trace.closeEditors();
125
126 IPath path = resource.getLocation();
127 if (path != null) {
128 /* Delete supplementary files */
129 trace.deleteSupplementaryFolder();
130 }
131
132 /* Finally, delete the trace */
133 resource.delete(true, new NullProgressMonitor());
134 }
135 }
136
137 /* Delete the project itself */
138 project.getResource().delete(true, null);
139 }
140
141 /**
142 * Makes the main display thread sleep, so it gives a chance to other thread
143 * needing the main display to execute
144 *
145 * @param waitTimeMillis
146 * time to wait in millisecond
147 */
148 public static void delayThread(final long waitTimeMillis) {
149 final Display display = Display.getCurrent();
150 if (display != null) {
151 final long endTimeMillis = System.currentTimeMillis() + waitTimeMillis;
152 while (System.currentTimeMillis() < endTimeMillis) {
153 if (!display.readAndDispatch()) {
154 display.sleep();
155 }
156 display.update();
157 }
158 } else {
159 try {
160 Thread.sleep(waitTimeMillis);
161 } catch (final InterruptedException e) {
162 // Ignored
163 }
164 }
165 }
166
167}
This page took 0.036186 seconds and 5 git commands to generate.