|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Ericsson |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License 2.0 which |
| 6 | + * accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | + |
| 12 | +package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.module; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertFalse; |
| 15 | +import static org.junit.Assert.assertNotNull; |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | +import static org.junit.Assert.fail; |
| 18 | + |
| 19 | +import org.eclipse.core.resources.IFolder; |
| 20 | +import org.eclipse.core.resources.IProject; |
| 21 | +import org.eclipse.core.resources.IProjectDescription; |
| 22 | +import org.eclipse.core.resources.IResource; |
| 23 | +import org.eclipse.core.resources.IWorkspaceRoot; |
| 24 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 25 | +import org.eclipse.core.runtime.CoreException; |
| 26 | +import org.eclipse.core.runtime.IPath; |
| 27 | +import org.eclipse.core.runtime.IStatus; |
| 28 | +import org.eclipse.core.runtime.Path; |
| 29 | +import org.eclipse.jdt.annotation.NonNull; |
| 30 | +import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlAnalysisModuleSource; |
| 31 | +import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils; |
| 32 | +import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator; |
| 33 | +import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles; |
| 34 | +import org.eclipse.tracecompass.tmf.core.TmfCommonConstants; |
| 35 | +import org.eclipse.tracecompass.tmf.core.TmfProjectNature; |
| 36 | +import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
| 37 | +import org.eclipse.tracecompass.tmf.core.event.TmfEvent; |
| 38 | +import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
| 39 | +import org.eclipse.tracecompass.tmf.core.io.ResourceUtil; |
| 40 | +import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal; |
| 41 | +import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; |
| 42 | +import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 43 | +import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
| 44 | +import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStubNs; |
| 45 | +import org.junit.After; |
| 46 | +import org.junit.AfterClass; |
| 47 | +import org.junit.Before; |
| 48 | +import org.junit.BeforeClass; |
| 49 | +import org.junit.Test; |
| 50 | + |
| 51 | +import com.google.common.collect.ImmutableList; |
| 52 | + |
| 53 | +/** |
| 54 | + * Test the XML Utils delete supplementary directory |
| 55 | + * |
| 56 | + * @author Geneviève Bastien |
| 57 | + */ |
| 58 | +public class XmlUtilsWithTraceTest { |
| 59 | + |
| 60 | + private static final @NonNull String TEST_TRACE_NAME = "testTrace4.xml"; |
| 61 | + |
| 62 | + private static final @NonNull String TEST_TRACE = "test_traces/" + TEST_TRACE_NAME; |
| 63 | + |
| 64 | + private static final @NonNull String ANALYSIS_ID = "xml.core.tests.simple.pattern"; |
| 65 | + |
| 66 | + private static IProject sfProject; |
| 67 | + |
| 68 | + /** |
| 69 | + * Class setup |
| 70 | + * |
| 71 | + * @throws CoreException |
| 72 | + * if an error occurs |
| 73 | + */ |
| 74 | + @BeforeClass |
| 75 | + public static void startUp() throws CoreException { |
| 76 | + IProject project = ResourcesPlugin.getWorkspace().getRoot() |
| 77 | + .getProject(TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME); |
| 78 | + if (!project.exists()) { |
| 79 | + project.create(null); |
| 80 | + if (!project.isOpen()) { |
| 81 | + project.open(null); |
| 82 | + } |
| 83 | + IProjectDescription description = project.getDescription(); |
| 84 | + description.setNatureIds(new String[] { TmfProjectNature.ID }); |
| 85 | + project.setDescription(description, null); |
| 86 | + } |
| 87 | + if (!project.isOpen()) { |
| 88 | + project.open(null); |
| 89 | + } |
| 90 | + |
| 91 | + IFolder tracesFolder = project.getFolder("Traces"); //$NON-NLS-1$ |
| 92 | + if (!tracesFolder.exists()) { |
| 93 | + tracesFolder.create(true, true, null); |
| 94 | + } |
| 95 | + |
| 96 | + IFolder supplRootFolder = project.getFolder(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME); |
| 97 | + if (!supplRootFolder.exists()) { |
| 98 | + supplRootFolder.create(true, true, null); |
| 99 | + } |
| 100 | + sfProject = project; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Class tear down |
| 105 | + * |
| 106 | + * @throws CoreException |
| 107 | + * if an error occurs |
| 108 | + */ |
| 109 | + @AfterClass |
| 110 | + public static void tearDown() throws CoreException { |
| 111 | + if (sfProject != null) { |
| 112 | + sfProject.delete(true, null); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Load the XML files for the current test |
| 118 | + */ |
| 119 | + @Before |
| 120 | + public void setUp() { |
| 121 | + XmlUtils.addXmlFile(TmfXmlTestFiles.VALID_PATTERN_SIMPLE_FILE.getFile()); |
| 122 | + XmlAnalysisModuleSource.notifyModuleChange(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Clean |
| 127 | + */ |
| 128 | + @After |
| 129 | + public void cleanUp() { |
| 130 | + XmlUtils.deleteFiles(ImmutableList.of( |
| 131 | + TmfXmlTestFiles.VALID_PATTERN_SIMPLE_FILE.getFile().getName())); |
| 132 | + XmlAnalysisModuleSource.notifyModuleChange(); |
| 133 | + } |
| 134 | + |
| 135 | + private ITmfTrace getTrace() throws CoreException { |
| 136 | + TmfXmlTraceStubNs trace = new TmfXmlTraceStubNs(); |
| 137 | + String absolutePath = Activator.getAbsolutePath(new Path(TEST_TRACE)).toOSString(); |
| 138 | + IStatus status = trace.validate(null, absolutePath); |
| 139 | + if (!status.isOK()) { |
| 140 | + fail(status.getException().getMessage()); |
| 141 | + } |
| 142 | + |
| 143 | + IResource resource = createResource(absolutePath); |
| 144 | + try { |
| 145 | + trace.initTrace(resource, absolutePath, TmfEvent.class); |
| 146 | + } catch (TmfTraceException e) { |
| 147 | + trace.dispose(); |
| 148 | + fail(e.getMessage()); |
| 149 | + } |
| 150 | + |
| 151 | + // Initialize the trace and module |
| 152 | + TmfTraceOpenedSignal signal = new TmfTraceOpenedSignal(this, trace, null); |
| 153 | + trace.traceOpened(signal); |
| 154 | + // The data provider manager uses opened traces from the manager |
| 155 | + TmfTraceManager.getInstance().traceOpened(signal); |
| 156 | + return trace; |
| 157 | + } |
| 158 | + |
| 159 | + @SuppressWarnings("null") |
| 160 | + private static IResource createResource(String path) throws CoreException { |
| 161 | + IPath targetLocation = new Path(path); |
| 162 | + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); |
| 163 | + IProject project = root.getProject(TmfCommonConstants.DEFAULT_TRACE_PROJECT_NAME); |
| 164 | + project.refreshLocal(IResource.DEPTH_INFINITE, null); |
| 165 | + IFolder tracesFolder = project.getFolder("Traces"); |
| 166 | + IResource resource = tracesFolder.getFile(TEST_TRACE_NAME); |
| 167 | + ResourceUtil.createSymbolicLink(resource, targetLocation, true, null); |
| 168 | + |
| 169 | + IFolder supplRootFolder = project.getFolder(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER_NAME); |
| 170 | + IFolder supplFolder = supplRootFolder.getFolder(resource.getProjectRelativePath().removeFirstSegments(1)); |
| 171 | + if (!supplFolder.exists()) { |
| 172 | + supplFolder.create(true, true, null); |
| 173 | + } |
| 174 | + resource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, supplFolder.getLocation().toOSString()); |
| 175 | + |
| 176 | + return resource; |
| 177 | + } |
| 178 | + |
| 179 | + private static void runModule(ITmfTrace trace) { |
| 180 | + IAnalysisModule module = trace.getAnalysisModule(ANALYSIS_ID); |
| 181 | + assertNotNull(module); |
| 182 | + module.schedule(); |
| 183 | + assertTrue(module.waitForCompletion()); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Test delete supplementary dir when trace is open |
| 188 | + * |
| 189 | + * @throws CoreException |
| 190 | + * if an error occurs |
| 191 | + */ |
| 192 | + @Test |
| 193 | + public void testDeleteSupplDirWithTraceOpen() throws CoreException { |
| 194 | + ITmfTrace trace = getTrace(); |
| 195 | + assertNotNull(trace); |
| 196 | + try { |
| 197 | + runModule(trace); |
| 198 | + String supplPathString = TmfTraceManager.getSupplementaryFileDir(trace); |
| 199 | + IPath path = new Path(supplPathString); |
| 200 | + IPath ssFile = path.addTrailingSeparator().append(ANALYSIS_ID).addFileExtension("ht"); |
| 201 | + IPath segFile = path.addTrailingSeparator().append(ANALYSIS_ID).addFileExtension("dat"); |
| 202 | + assertTrue(ssFile.toFile().exists()); |
| 203 | + assertTrue(segFile.toFile().exists()); |
| 204 | + XmlUtils.deleteSupplementaryResources(TmfXmlTestFiles.VALID_PATTERN_SIMPLE_FILE.getFile().getName(), null); |
| 205 | + assertFalse(ssFile.toFile().exists()); |
| 206 | + assertFalse(segFile.toFile().exists()); |
| 207 | + } finally { |
| 208 | + trace.dispose(); |
| 209 | + TmfTraceManager.getInstance().traceClosed(new TmfTraceClosedSignal(this, trace)); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Test delete supplementary dir when trace is open |
| 215 | + * |
| 216 | + * @throws CoreException |
| 217 | + * Exception thrown by analyses |
| 218 | + */ |
| 219 | + @Test |
| 220 | + public void testDeleteSupplDirWithTraceClosed() throws CoreException { |
| 221 | + ITmfTrace trace = getTrace(); |
| 222 | + assertNotNull(trace); |
| 223 | + try { |
| 224 | + runModule(trace); |
| 225 | + String supplPathString = TmfTraceManager.getSupplementaryFileDir(trace); |
| 226 | + IPath path = new Path(supplPathString); |
| 227 | + IPath ssFile = path.addTrailingSeparator().append(ANALYSIS_ID).addFileExtension("ht"); |
| 228 | + IPath segFile = path.addTrailingSeparator().append(ANALYSIS_ID).addFileExtension("dat"); |
| 229 | + assertTrue(ssFile.toFile().exists()); |
| 230 | + assertTrue(segFile.toFile().exists()); |
| 231 | + |
| 232 | + TmfTraceClosedSignal signal = new TmfTraceClosedSignal(this, trace); |
| 233 | + TmfTraceManager.getInstance().traceClosed(signal); |
| 234 | + trace.dispose(); |
| 235 | + |
| 236 | + XmlUtils.deleteSupplementaryResources(TmfXmlTestFiles.VALID_PATTERN_SIMPLE_FILE.getFile().getName(), null); |
| 237 | + assertFalse(ssFile.toFile().exists()); |
| 238 | + assertFalse(segFile.toFile().exists()); |
| 239 | + } finally { |
| 240 | + trace.dispose(); |
| 241 | + TmfTraceManager.getInstance().traceClosed(new TmfTraceClosedSignal(this, trace)); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | +} |
0 commit comments