| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014, 2015 Ericsson |
| 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | * Marc-Andre Laperle - Added tests for extracting archives during import |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.tracecompass.tmf.ctf.ui.swtbot.tests; |
| 15 | |
| 16 | import static org.junit.Assert.assertEquals; |
| 17 | import static org.junit.Assert.assertFalse; |
| 18 | import static org.junit.Assert.assertNotNull; |
| 19 | import static org.junit.Assert.assertTrue; |
| 20 | |
| 21 | import java.io.ByteArrayInputStream; |
| 22 | import java.io.File; |
| 23 | import java.io.IOException; |
| 24 | import java.net.URISyntaxException; |
| 25 | import java.util.Collections; |
| 26 | import java.util.Comparator; |
| 27 | import java.util.List; |
| 28 | |
| 29 | import org.eclipse.core.resources.IFile; |
| 30 | import org.eclipse.core.resources.IFolder; |
| 31 | import org.eclipse.core.resources.IProject; |
| 32 | import org.eclipse.core.resources.IResource; |
| 33 | import org.eclipse.core.resources.ResourcesPlugin; |
| 34 | import org.eclipse.core.runtime.CoreException; |
| 35 | import org.eclipse.core.runtime.IPath; |
| 36 | import org.eclipse.core.runtime.Path; |
| 37 | import org.eclipse.core.runtime.URIUtil; |
| 38 | import org.eclipse.jface.viewers.StructuredSelection; |
| 39 | import org.eclipse.jface.wizard.WizardDialog; |
| 40 | import org.eclipse.swt.widgets.Shell; |
| 41 | import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; |
| 42 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; |
| 43 | import org.eclipse.swtbot.swt.finder.results.VoidResult; |
| 44 | import org.eclipse.swtbot.swt.finder.waits.Conditions; |
| 45 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; |
| 46 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo; |
| 47 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio; |
| 48 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; |
| 49 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; |
| 50 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTableItem; |
| 51 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotText; |
| 52 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
| 53 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; |
| 54 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizard; |
| 55 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.ImportTraceWizardPage; |
| 56 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Messages; |
| 57 | import org.eclipse.tracecompass.tmf.core.TmfCommonConstants; |
| 58 | import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor; |
| 59 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectElement; |
| 60 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectRegistry; |
| 61 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement; |
| 62 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; |
| 63 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTracesFolder; |
| 64 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.ConditionHelpers; |
| 65 | import org.eclipse.tracecompass.tmf.ui.swtbot.tests.shared.SWTBotUtils; |
| 66 | import org.eclipse.ui.IWorkbench; |
| 67 | import org.eclipse.ui.IWorkbenchWindow; |
| 68 | import org.eclipse.ui.PlatformUI; |
| 69 | import org.junit.Test; |
| 70 | import org.junit.runner.RunWith; |
| 71 | |
| 72 | /** |
| 73 | * SWTBot Smoke test using ImportTraceWizard. |
| 74 | * |
| 75 | * @author Bernd Hufmann |
| 76 | */ |
| 77 | @RunWith(SWTBotJunit4ClassRunner.class) |
| 78 | public class StandardImportAndReadSmokeTest extends AbstractImportAndReadSmokeTest { |
| 79 | |
| 80 | private static final String TRACE_FOLDER_PARENT_PATH = fTrace.getPath() + File.separator + ".." + File.separator + ".." + File.separator; |
| 81 | private static final String ARCHIVE_FILE_NAME = "synctraces.tar.gz"; |
| 82 | private static final String EMPTY_ARCHIVE_FOLDER = "emptyArchiveFolder"; |
| 83 | private static final String EMPTY_FILE_NAME = "emptyFile"; |
| 84 | private static final String TRACE_ARCHIVE_PATH = TRACE_FOLDER_PARENT_PATH + ARCHIVE_FILE_NAME; |
| 85 | private static final String TRACE_FOLDER_PARENT_NAME = "traces"; |
| 86 | private static final String TRACE_PROJECT_NAME = "Tracing"; |
| 87 | |
| 88 | private static final String ARCHIVE_ROOT_ELEMENT_NAME = "/"; |
| 89 | private static final String GENERATED_ARCHIVE_NAME = "testtraces.zip"; |
| 90 | private static final String URI_SEPARATOR = "/"; |
| 91 | private static final String URI_FILE_SCHEME = "file:"; |
| 92 | private static final String URI_JAR_FILE_SCHEME = "jar:file:"; |
| 93 | private static final boolean IS_WIN32 = System.getProperty("os.name").startsWith("Windows"); //$NON-NLS-1$//$NON-NLS-2$ |
| 94 | private static final String URI_DEVICE_SEPARATOR = IS_WIN32 ? URI_SEPARATOR : ""; |
| 95 | |
| 96 | /** |
| 97 | * Test import from directory |
| 98 | * |
| 99 | * @throws Exception |
| 100 | * on error |
| 101 | */ |
| 102 | @Test |
| 103 | public void testImportFromDirectory() throws Exception { |
| 104 | testImport(0, false, false); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Test import from directory, create links |
| 109 | * |
| 110 | * @throws Exception |
| 111 | * on error |
| 112 | */ |
| 113 | @Test |
| 114 | public void testImportFromDirectoryLinks() throws Exception { |
| 115 | testImport(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE, false, false); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Test import from directory, preserve folder structure |
| 120 | * |
| 121 | * @throws Exception |
| 122 | * on error |
| 123 | */ |
| 124 | @Test |
| 125 | public void testImportFromDirectoryPreserveFolder() throws Exception { |
| 126 | testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, false); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Test import from directory, create links, preserve folder structure |
| 131 | * |
| 132 | * @throws Exception |
| 133 | * on error |
| 134 | */ |
| 135 | @Test |
| 136 | public void testImportFromDirectoryLinksPreserveFolder() throws Exception { |
| 137 | int options = ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE; |
| 138 | testImport(options, false, false); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Test import from directory, overwrite all |
| 143 | * |
| 144 | * @throws Exception |
| 145 | * on error |
| 146 | */ |
| 147 | @Test |
| 148 | public void testImportFromDirectoryOverwrite() throws Exception { |
| 149 | testImport(0, false, false); |
| 150 | testImport(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, false); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Test import from archive |
| 155 | * |
| 156 | * @throws Exception |
| 157 | * on error |
| 158 | */ |
| 159 | @Test |
| 160 | public void testImportFromArchive() throws Exception { |
| 161 | testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true, true); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Test import from directory, preserve folder structure |
| 166 | * @throws Exception on error |
| 167 | */ |
| 168 | @Test |
| 169 | public void testImportFromArchivePreserveFolder() throws Exception { |
| 170 | testImport(ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false, true); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Test import from directory, overwrite all |
| 175 | * |
| 176 | * @throws Exception |
| 177 | * on error |
| 178 | */ |
| 179 | @Test |
| 180 | public void testImportFromArchiveOverwrite() throws Exception { |
| 181 | testImport(0, false, true); |
| 182 | testImport(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false, true); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Test import from directory containing archives |
| 187 | * |
| 188 | * @throws Exception |
| 189 | * on error |
| 190 | */ |
| 191 | @Test |
| 192 | public void testExtractArchivesFromDirectory() throws Exception { |
| 193 | testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Test import from directory containing archives, create links |
| 198 | * @throws Exception on error |
| 199 | */ |
| 200 | @Test |
| 201 | public void testExtractArchivesFromDirectoryLinks() throws Exception { |
| 202 | testImportAndExtractArchives(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, false); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Test import from directory containing archives, create links, preserve folder structure |
| 207 | * @throws Exception on error |
| 208 | */ |
| 209 | @Test |
| 210 | public void testExtractArchivesFromDirectoryLinksPreserveStruture() throws Exception { |
| 211 | testImportAndExtractArchives(ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE | ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, false); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Test import from archive containing archives |
| 216 | * |
| 217 | * @throws Exception |
| 218 | * on error |
| 219 | */ |
| 220 | @Test |
| 221 | public void testExtractArchivesFromArchive() throws Exception { |
| 222 | testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES, true); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Test import from archive containing archives, preserve folder structure |
| 227 | * |
| 228 | * @throws Exception |
| 229 | * on error |
| 230 | */ |
| 231 | @Test |
| 232 | public void testExtractArchivesFromArchivePreserveFolder() throws Exception { |
| 233 | testImportAndExtractArchives(ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES | ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE, true); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Test import from an empty archive. This should not import anything. |
| 238 | * |
| 239 | * @throws Exception |
| 240 | * on error |
| 241 | */ |
| 242 | @Test |
| 243 | public void testEmptyArchive() throws Exception { |
| 244 | createProject(); |
| 245 | String testArchivePath = createEmptyArchive(); |
| 246 | |
| 247 | openImportWizard(); |
| 248 | selectImportFromArchive(testArchivePath); |
| 249 | selectFolder(ARCHIVE_ROOT_ELEMENT_NAME); |
| 250 | setOptions(0, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 251 | importFinish(); |
| 252 | |
| 253 | assertNoTraces(); |
| 254 | |
| 255 | SWTBotUtils.deleteProject(getProjectName(), fBot); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Test import from an empty directory. This should not import anything. |
| 260 | * |
| 261 | * @throws Exception |
| 262 | * on error |
| 263 | */ |
| 264 | @Test |
| 265 | public void testEmptyDirectory() throws Exception { |
| 266 | createProject(); |
| 267 | String testDirectoryPath = createEmptyDirectory().getLocation().toOSString(); |
| 268 | |
| 269 | openImportWizard(); |
| 270 | selectImportFromDirectory(testDirectoryPath); |
| 271 | selectFolder(EMPTY_ARCHIVE_FOLDER); |
| 272 | setOptions(0, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 273 | importFinish(); |
| 274 | |
| 275 | assertNoTraces(); |
| 276 | |
| 277 | SWTBotUtils.deleteProject(getProjectName(), fBot); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Test import from an directory with an empty file. This should not import anything. |
| 282 | * |
| 283 | * @throws Exception |
| 284 | * on error |
| 285 | */ |
| 286 | @Test |
| 287 | public void testEmptyFile() throws Exception { |
| 288 | createProject(); |
| 289 | IFolder folder = createEmptyDirectory(); |
| 290 | createEmptyFile(folder); |
| 291 | String testDirectoryPath = folder.getLocation().toOSString(); |
| 292 | openImportWizard(); |
| 293 | selectImportFromDirectory(testDirectoryPath); |
| 294 | selectFile(EMPTY_FILE_NAME, EMPTY_ARCHIVE_FOLDER); |
| 295 | setOptions(ImportTraceWizardPage.OPTION_IMPORT_UNRECOGNIZED_TRACES, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 296 | importFinish(); |
| 297 | |
| 298 | assertNoTraces(); |
| 299 | |
| 300 | SWTBotUtils.deleteProject(getProjectName(), fBot); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Test import from a directory containing an empty archive. This should not import anything. |
| 305 | * |
| 306 | * @throws Exception |
| 307 | * on error |
| 308 | */ |
| 309 | @Test |
| 310 | public void testDirectoryWithEmptyArchive() throws Exception { |
| 311 | createProject(); |
| 312 | createEmptyArchive(); |
| 313 | |
| 314 | openImportWizard(); |
| 315 | selectImportFromDirectory(getProjectResource().getLocation().toOSString()); |
| 316 | selectFile(GENERATED_ARCHIVE_NAME, getProjectName()); |
| 317 | setOptions(0, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 318 | importFinish(); |
| 319 | |
| 320 | assertNoTraces(); |
| 321 | |
| 322 | SWTBotUtils.deleteProject(getProjectName(), fBot); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Test import from a nested empty archive. This should not import anything. |
| 327 | * |
| 328 | * @throws Exception |
| 329 | * on error |
| 330 | */ |
| 331 | @Test |
| 332 | public void testNestedEmptyArchive() throws Exception { |
| 333 | createProject(); |
| 334 | IProject project = getProjectResource(); |
| 335 | |
| 336 | // Create the empty archive from an empty folder |
| 337 | String testArchivePath = createEmptyArchive(); |
| 338 | |
| 339 | // Rename archive so that we can create a new one with the same name |
| 340 | project.refreshLocal(IResource.DEPTH_ONE, null); |
| 341 | IFile[] files = project.getWorkspace().getRoot().findFilesForLocationURI(new File(testArchivePath).toURI()); |
| 342 | IFile archiveFile = files[0]; |
| 343 | String newEmptyArchiveName = "nested" + archiveFile.getName(); |
| 344 | IPath dest = archiveFile.getFullPath().removeLastSegments(1).append(newEmptyArchiveName); |
| 345 | archiveFile.move(dest, true, null); |
| 346 | IFile renamedArchiveFile = archiveFile.getWorkspace().getRoot().getFile(dest); |
| 347 | |
| 348 | createArchive(renamedArchiveFile); |
| 349 | |
| 350 | openImportWizard(); |
| 351 | selectImportFromArchive(testArchivePath); |
| 352 | selectFolder(ARCHIVE_ROOT_ELEMENT_NAME); |
| 353 | setOptions(0, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 354 | importFinish(); |
| 355 | |
| 356 | assertNoTraces(); |
| 357 | |
| 358 | SWTBotUtils.deleteProject(getProjectName(), fBot); |
| 359 | } |
| 360 | |
| 361 | private void assertNoTraces() { |
| 362 | TmfProjectElement tmfProject = TmfProjectRegistry.getProject(getProjectResource(), true); |
| 363 | assertNotNull(tmfProject); |
| 364 | TmfTraceFolder tracesFolder = tmfProject.getTracesFolder(); |
| 365 | assertNotNull(tracesFolder); |
| 366 | List<TmfTraceElement> traces = tracesFolder.getTraces(); |
| 367 | assertTrue(traces.isEmpty()); |
| 368 | } |
| 369 | |
| 370 | private void testImport(int options, boolean testViews, boolean fromArchive) throws Exception { |
| 371 | createProject(); |
| 372 | String expectedSourceLocation = null; |
| 373 | openImportWizard(); |
| 374 | if (fromArchive) { |
| 375 | expectedSourceLocation = URI_JAR_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(TRACE_ARCHIVE_PATH).getCanonicalPath()) + "!" + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR; |
| 376 | selectImportFromArchive(TRACE_ARCHIVE_PATH); |
| 377 | selectFolder(ARCHIVE_ROOT_ELEMENT_NAME); |
| 378 | SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace); |
| 379 | assertFalse(checkBox.isEnabled()); |
| 380 | } else { |
| 381 | String sourcePath = TRACE_FOLDER_PARENT_PATH + File.separator + TRACE_FOLDER + File.separator + TRACE_NAME; |
| 382 | expectedSourceLocation = URI_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(sourcePath).getCanonicalPath()) + URI_SEPARATOR; |
| 383 | selectImportFromDirectory(TRACE_FOLDER_PARENT_PATH); |
| 384 | selectFolder(new String [] {TRACE_FOLDER_PARENT_NAME, TRACE_FOLDER }); |
| 385 | } |
| 386 | |
| 387 | setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 388 | importFinish(); |
| 389 | |
| 390 | IPath expectedElementPath = new Path(TRACE_NAME); |
| 391 | if ((options & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) { |
| 392 | expectedElementPath = new Path(TRACE_FOLDER).append(expectedElementPath); |
| 393 | } |
| 394 | |
| 395 | checkOptions(options, expectedSourceLocation, expectedElementPath); |
| 396 | TmfEventsEditor tmfEd = SWTBotUtils.openEditor(fBot, getProjectName(), expectedElementPath); |
| 397 | if (testViews) { |
| 398 | testViews(tmfEd); |
| 399 | } |
| 400 | |
| 401 | fBot.closeAllEditors(); |
| 402 | |
| 403 | SWTBotUtils.deleteProject(getProjectName(), fBot); |
| 404 | } |
| 405 | |
| 406 | private void testImportAndExtractArchives(int options, boolean fromArchive) throws Exception { |
| 407 | createProject(); |
| 408 | |
| 409 | String expectedSourceLocation; |
| 410 | IPath expectedElementPath; |
| 411 | if (fromArchive) { |
| 412 | String testArchivePath = createNestedArchive(); |
| 413 | openImportWizard(); |
| 414 | selectImportFromArchive(testArchivePath); |
| 415 | selectFile(ARCHIVE_FILE_NAME, ARCHIVE_ROOT_ELEMENT_NAME, TRACE_PROJECT_NAME, TRACE_FOLDER_PARENT_NAME); |
| 416 | |
| 417 | expectedSourceLocation = URI_JAR_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(testArchivePath).getCanonicalPath()) + "!" + URI_SEPARATOR + TRACE_PROJECT_NAME + URI_SEPARATOR + TRACE_FOLDER_PARENT_NAME + URI_SEPARATOR + ARCHIVE_FILE_NAME |
| 418 | + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR; |
| 419 | expectedElementPath = new Path(TRACE_PROJECT_NAME).append(TRACE_FOLDER_PARENT_NAME).append(ARCHIVE_FILE_NAME).append(TRACE_FOLDER).append(TRACE_NAME); |
| 420 | } else { |
| 421 | openImportWizard(); |
| 422 | selectImportFromDirectory(TRACE_FOLDER_PARENT_PATH); |
| 423 | selectFile(ARCHIVE_FILE_NAME, TRACE_FOLDER_PARENT_NAME); |
| 424 | expectedElementPath = new Path(ARCHIVE_FILE_NAME).append(TRACE_FOLDER).append(TRACE_NAME); |
| 425 | expectedSourceLocation = URI_FILE_SCHEME + URI_DEVICE_SEPARATOR + new Path(new File(TRACE_FOLDER_PARENT_PATH).getCanonicalPath()) + URI_SEPARATOR + ARCHIVE_FILE_NAME + URI_SEPARATOR + TRACE_FOLDER + URI_SEPARATOR + TRACE_NAME + URI_SEPARATOR; |
| 426 | } |
| 427 | |
| 428 | if ((options & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) == 0) { |
| 429 | expectedElementPath = new Path(TRACE_NAME); |
| 430 | } |
| 431 | |
| 432 | setOptions(options, ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 433 | importFinish(); |
| 434 | // Archives should never be imported as links |
| 435 | int expectedOptions = options & ~ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE; |
| 436 | checkOptions(expectedOptions, expectedSourceLocation, expectedElementPath); |
| 437 | |
| 438 | TmfEventsEditor editor = SWTBotUtils.openEditor(fBot, TRACE_PROJECT_NAME, expectedElementPath); |
| 439 | testViews(editor); |
| 440 | |
| 441 | SWTBotUtils.deleteProject(getProjectName(), fBot); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Create a temporary archive containing a nested archive. For example, |
| 446 | * testtraces.zip/synctraces.tar.gz can be used to test a nested archive. |
| 447 | */ |
| 448 | private String createNestedArchive() throws IOException, CoreException, URISyntaxException { |
| 449 | // Link to the test traces folder. We use a link so that we can safely |
| 450 | // delete the entire project when we are done. |
| 451 | IProject project = getProjectResource(); |
| 452 | String canonicalPath = new File(TRACE_FOLDER_PARENT_PATH).getCanonicalPath(); |
| 453 | IFolder folder = project.getFolder(TRACE_FOLDER_PARENT_NAME); |
| 454 | folder.createLink(new Path(canonicalPath), IResource.REPLACE, null); |
| 455 | IFile file = folder.getFile(ARCHIVE_FILE_NAME); |
| 456 | return createArchive(file); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Create the empty archive from an empty folder |
| 461 | */ |
| 462 | private String createEmptyArchive() throws CoreException, URISyntaxException { |
| 463 | return createArchive(createEmptyDirectory()); |
| 464 | } |
| 465 | |
| 466 | private IFolder createEmptyDirectory() throws CoreException { |
| 467 | IProject project = getProjectResource(); |
| 468 | IFolder folder = project.getFolder(EMPTY_ARCHIVE_FOLDER); |
| 469 | folder.create(true, true, null); |
| 470 | return folder; |
| 471 | } |
| 472 | |
| 473 | private static void createEmptyFile(IFolder folder) throws CoreException { |
| 474 | // Create empty file |
| 475 | IFile file = folder.getFile(EMPTY_FILE_NAME); |
| 476 | file.create(new ByteArrayInputStream(new byte[0]), true, null); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Create a temporary archive from the specified resource. |
| 481 | */ |
| 482 | private static String createArchive(IResource sourceResource) throws URISyntaxException { |
| 483 | IPath exportedPath = sourceResource.getFullPath(); |
| 484 | |
| 485 | SWTBotTreeItem traceFilesProject = SWTBotUtils.selectProject(fBot, TRACE_PROJECT_NAME); |
| 486 | traceFilesProject.contextMenu("Export...").click(); |
| 487 | |
| 488 | fBot.waitUntil(Conditions.shellIsActive("Export")); |
| 489 | SWTBotShell activeShell = fBot.activeShell(); |
| 490 | SWTBotTree exportWizardsTree = fBot.tree(); |
| 491 | SWTBotTreeItem treeItem = SWTBotUtils.getTreeItem(fBot, exportWizardsTree, "General", "Archive File"); |
| 492 | treeItem.select(); |
| 493 | fBot.button("Next >").click(); |
| 494 | fBot.button("&Deselect All").click(); |
| 495 | |
| 496 | if (sourceResource instanceof IFile) { |
| 497 | String[] folderPath = exportedPath.removeLastSegments(1).segments(); |
| 498 | String fileName = exportedPath.lastSegment(); |
| 499 | selectFile(fileName, folderPath); |
| 500 | } else { |
| 501 | selectFolder(exportedPath.segments()); |
| 502 | } |
| 503 | |
| 504 | String workspacePath = URIUtil.toFile(URIUtil.fromString(System.getProperty("osgi.instance.area"))).getAbsolutePath(); |
| 505 | final String archiveDestinationPath = workspacePath + File.separator + TRACE_PROJECT_NAME + File.separator + GENERATED_ARCHIVE_NAME; |
| 506 | fBot.comboBox().setText(archiveDestinationPath); |
| 507 | fBot.button("&Finish").click(); |
| 508 | fBot.waitUntil(Conditions.shellCloses(activeShell)); |
| 509 | return archiveDestinationPath; |
| 510 | } |
| 511 | |
| 512 | private void testViews(TmfEventsEditor editor) { |
| 513 | testHistogramView(getViewPart("Histogram"), editor); |
| 514 | testPropertyView(getViewPart("Properties")); |
| 515 | testStatisticsView(getViewPart("Statistics")); |
| 516 | } |
| 517 | |
| 518 | private static void openImportWizard() { |
| 519 | fWizard = new ImportTraceWizard(); |
| 520 | |
| 521 | UIThreadRunnable.asyncExec(new VoidResult() { |
| 522 | @Override |
| 523 | public void run() { |
| 524 | final IWorkbench workbench = PlatformUI.getWorkbench(); |
| 525 | // Fire the Import Trace Wizard |
| 526 | if (workbench != null) { |
| 527 | final IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow(); |
| 528 | Shell shell = activeWorkbenchWindow.getShell(); |
| 529 | assertNotNull(shell); |
| 530 | ((ImportTraceWizard) fWizard).init(PlatformUI.getWorkbench(), StructuredSelection.EMPTY); |
| 531 | WizardDialog dialog = new WizardDialog(shell, fWizard); |
| 532 | dialog.open(); |
| 533 | } |
| 534 | } |
| 535 | }); |
| 536 | |
| 537 | fBot.waitUntil(ConditionHelpers.isWizardReady(fWizard)); |
| 538 | } |
| 539 | |
| 540 | private static void selectImportFromDirectory(String directoryPath) { |
| 541 | SWTBotRadio button = fBot.radio("Select roo&t directory:"); |
| 542 | button.click(); |
| 543 | |
| 544 | SWTBotCombo sourceCombo = fBot.comboBox(); |
| 545 | File traceFolderParent = new File(directoryPath); |
| 546 | sourceCombo.setText(traceFolderParent.getAbsolutePath()); |
| 547 | |
| 548 | SWTBotText text = fBot.text(); |
| 549 | text.setFocus(); |
| 550 | } |
| 551 | |
| 552 | private static void selectImportFromArchive(String archivePath) { |
| 553 | SWTBotRadio button = fBot.radio("Select &archive file:"); |
| 554 | button.click(); |
| 555 | |
| 556 | SWTBotCombo sourceCombo = fBot.comboBox(1); |
| 557 | |
| 558 | sourceCombo.setText(new File(archivePath).getAbsolutePath()); |
| 559 | |
| 560 | SWTBotText text = fBot.text(); |
| 561 | text.setFocus(); |
| 562 | } |
| 563 | |
| 564 | private static void selectFolder(String... treePath) { |
| 565 | selectFolder(true, treePath); |
| 566 | } |
| 567 | |
| 568 | private static void selectFolder(boolean check, String... treePath) { |
| 569 | SWTBotTree tree = fBot.tree(); |
| 570 | fBot.waitUntil(Conditions.widgetIsEnabled(tree)); |
| 571 | SWTBotTreeItem folderNode = SWTBotUtils.getTreeItem(fBot, tree, treePath); |
| 572 | if (check) { |
| 573 | folderNode.check(); |
| 574 | } else { |
| 575 | folderNode.select(); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | private static void selectFile(String fileName, String... folderTreePath) { |
| 580 | selectFolder(false, folderTreePath); |
| 581 | |
| 582 | SWTBotTable fileTable = fBot.table(); |
| 583 | fBot.waitUntil(Conditions.widgetIsEnabled(fileTable)); |
| 584 | fBot.waitUntil(ConditionHelpers.isTableItemAvailable(fileName, fileTable)); |
| 585 | SWTBotTableItem tableItem = fileTable.getTableItem(fileName); |
| 586 | tableItem.check(); |
| 587 | } |
| 588 | |
| 589 | private static void setOptions(int optionFlags, String traceTypeName) { |
| 590 | SWTBotCheckBox checkBox = fBot.checkBox(Messages.ImportTraceWizard_CreateLinksInWorkspace); |
| 591 | if (checkBox.isEnabled()) { |
| 592 | if ((optionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0) { |
| 593 | checkBox.select(); |
| 594 | } else { |
| 595 | checkBox.deselect(); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | checkBox = fBot.checkBox(Messages.ImportTraceWizard_PreserveFolderStructure); |
| 600 | if ((optionFlags & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) { |
| 601 | checkBox.select(); |
| 602 | } else { |
| 603 | checkBox.deselect(); |
| 604 | } |
| 605 | |
| 606 | checkBox = fBot.checkBox(Messages.ImportTraceWizard_ImportUnrecognized); |
| 607 | if ((optionFlags & ImportTraceWizardPage.OPTION_IMPORT_UNRECOGNIZED_TRACES) != 0) { |
| 608 | checkBox.select(); |
| 609 | } else { |
| 610 | checkBox.deselect(); |
| 611 | } |
| 612 | |
| 613 | checkBox = fBot.checkBox(Messages.ImportTraceWizard_OverwriteExistingTrace); |
| 614 | if ((optionFlags & ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES) != 0) { |
| 615 | checkBox.select(); |
| 616 | } else { |
| 617 | checkBox.deselect(); |
| 618 | } |
| 619 | |
| 620 | SWTBotCombo comboBox = fBot.comboBoxWithLabel(Messages.ImportTraceWizard_TraceType); |
| 621 | if (traceTypeName != null && !traceTypeName.isEmpty()) { |
| 622 | comboBox.setSelection(traceTypeName); |
| 623 | } else { |
| 624 | comboBox.setSelection(ImportTraceWizardPage.TRACE_TYPE_AUTO_DETECT); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | private void checkOptions(int optionFlags, String expectedSourceLocation, IPath expectedElementPath) throws CoreException { |
| 629 | IProject project = getProjectResource(); |
| 630 | assertTrue(project.exists()); |
| 631 | TmfProjectElement tmfProject = TmfProjectRegistry.getProject(project, true); |
| 632 | assertNotNull(tmfProject); |
| 633 | TmfTraceFolder tracesFolder = tmfProject.getTracesFolder(); |
| 634 | assertNotNull(tracesFolder); |
| 635 | List<TmfTraceElement> traces = tracesFolder.getTraces(); |
| 636 | assertFalse(traces.isEmpty()); |
| 637 | Collections.sort(traces, new Comparator<TmfTraceElement>() { |
| 638 | @Override |
| 639 | public int compare(TmfTraceElement arg0, TmfTraceElement arg1) { |
| 640 | return arg0.getElementPath().compareTo(arg1.getElementPath()); |
| 641 | } |
| 642 | }); |
| 643 | |
| 644 | TmfTraceElement tmfTraceElement = traces.get(0); |
| 645 | IResource traceResource = tmfTraceElement.getResource(); |
| 646 | |
| 647 | assertEquals((optionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0, traceResource.isLinked()); |
| 648 | |
| 649 | // i.e. /Tracing/Traces |
| 650 | IPath expectedPath = Path.ROOT.append(new Path(TRACE_PROJECT_NAME)).append(TmfTracesFolder.TRACES_FOLDER_NAME).append(expectedElementPath); |
| 651 | assertEquals(expectedPath, traceResource.getFullPath()); |
| 652 | |
| 653 | String sourceLocation = traceResource.getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION); |
| 654 | assertNotNull(sourceLocation); |
| 655 | assertEquals(expectedSourceLocation, sourceLocation); |
| 656 | } |
| 657 | |
| 658 | @Override |
| 659 | protected String getProjectName() { |
| 660 | return TRACE_PROJECT_NAME; |
| 661 | } |
| 662 | |
| 663 | private IProject getProjectResource() { |
| 664 | return ResourcesPlugin.getWorkspace().getRoot().getProject(getProjectName()); |
| 665 | } |
| 666 | } |