tmf: Add utility method to delete supplementary files of a trace
authorAlexandre Montplaisir <alexmonthy@efficios.com>
Fri, 7 Oct 2016 21:04:51 +0000 (17:04 -0400)
committerAlexandre Montplaisir <alexmonthy@efficios.com>
Tue, 18 Oct 2016 17:54:24 +0000 (13:54 -0400)
Add the new (and move an existing) test into a separate file, because
the existing test suite uses the same static traces for all the tests.
If we are to delete a trace's supplementary files, the trace should
be regenerated for other tests!

Change-Id: I0727422dd190d9012e6b8ccce0d7d175711e149b
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-on: https://git.eclipse.org/r/82778
Reviewed-by: Hudson CI
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Tested-by: Genevieve Bastien <gbastien+lttng@versatic.net>
ctf/org.eclipse.tracecompass.tmf.ctf.core.tests/src/org/eclipse/tracecompass/tmf/ctf/core/tests/temp/tracemanager/TmfTraceManagerTest.java
ctf/org.eclipse.tracecompass.tmf.ctf.core.tests/src/org/eclipse/tracecompass/tmf/ctf/core/tests/temp/tracemanager/TmfTraceManagerUtilityTest.java [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/trace/TmfTraceManager.java

index 7c7721916e2ca917276de053baa6aca1b730034e..cdbec2d75baaa23b19e95618ab92bac2bfe78c43 100644 (file)
@@ -17,7 +17,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 
-import java.io.File;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.concurrent.TimeUnit;
@@ -229,26 +228,6 @@ public class TmfTraceManagerTest {
         assertEquals(expected, actual);
     }
 
-    /**
-     * Test the {@link TmfTraceManager#getSupplementaryFileDir} method.
-     */
-    @Test
-    public void testSupplementaryFileDir() {
-        final ITmfTrace localTrace1 = trace1;
-        final ITmfTrace localTrace2 = trace2;
-        assertNotNull(localTrace1);
-        assertNotNull(localTrace2);
-        String name1 = localTrace1.getName();
-        String name2 = localTrace2.getName();
-        String basePath = TmfTraceManager.getTemporaryDirPath() + File.separator;
-
-        String expected1 = basePath + name1 + File.separator;
-        String expected2 = basePath + name2 + File.separator;
-
-        assertEquals(expected1, TmfTraceManager.getSupplementaryFileDir(localTrace1));
-        assertEquals(expected2, TmfTraceManager.getSupplementaryFileDir(localTrace2));
-    }
-
     // ------------------------------------------------------------------------
     // Test a single trace
     // ------------------------------------------------------------------------
diff --git a/ctf/org.eclipse.tracecompass.tmf.ctf.core.tests/src/org/eclipse/tracecompass/tmf/ctf/core/tests/temp/tracemanager/TmfTraceManagerUtilityTest.java b/ctf/org.eclipse.tracecompass.tmf.ctf.core.tests/src/org/eclipse/tracecompass/tmf/ctf/core/tests/temp/tracemanager/TmfTraceManagerUtilityTest.java
new file mode 100644 (file)
index 0000000..37b3a61
--- /dev/null
@@ -0,0 +1,103 @@
+/*******************************************************************************
+ * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.tmf.ctf.core.tests.temp.tracemanager;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
+import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests for the utility methods in {@link TmfTraceManager}.
+ *
+ * @author Alexandre Montplaisir
+ */
+public class TmfTraceManagerUtilityTest {
+
+    private ITmfTrace fTrace;
+
+    /**
+     * Test initialization
+     */
+    @Before
+    public void setup() {
+        fTrace = CtfTmfTestTraceUtils.getTrace(CtfTestTrace.TRACE2);
+        fTrace.indexTrace(true);
+    }
+
+    /**
+     * Test clean-up
+     */
+    @AfterClass
+    public static void teardown() {
+        CtfTmfTestTraceUtils.dispose(CtfTestTrace.TRACE2);
+    }
+
+    /**
+     * Test the {@link TmfTraceManager#getSupplementaryFileDir} method.
+     */
+    @Test
+    public void testSupplementaryFileDir() {
+        final ITmfTrace trace = fTrace;
+        assertNotNull(trace);
+        String name1 = trace.getName();
+        String basePath = TmfTraceManager.getTemporaryDirPath() + File.separator;
+
+        String expected = basePath + name1 + File.separator;
+        assertEquals(expected, TmfTraceManager.getSupplementaryFileDir(trace));
+    }
+
+    /**
+     * Test the {@link TmfTraceManager#deleteSupplementaryFiles} method.
+     */
+    @Test
+    public void testDeleteSupplementaryFiles() {
+        ITmfTrace trace = fTrace;
+        assertNotNull(trace);
+
+        String suppFilesPath = TmfTraceManager.getSupplementaryFileDir(trace);
+        try {
+            /*
+             * Initializing/indexing the trace should have produced some
+             * supplementary files already.
+             */
+            assertFalse(isDirectoryEmpty(suppFilesPath));
+
+            TmfTraceManager.deleteSupplementaryFiles(trace);
+            assertTrue(isDirectoryEmpty(suppFilesPath));
+
+        } catch (IOException e) {
+            fail(e.getMessage());
+        }
+    }
+
+    private static boolean isDirectoryEmpty(String dirPath) throws IOException {
+        Path path = Paths.get(dirPath);
+        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
+            return !stream.iterator().hasNext();
+        }
+    }
+}
index eb28f3e157ea961e0193ecf62740b364aeb22976..642f426a6e3a0678f9f287dc3e95a5b178281435 100644 (file)
@@ -17,6 +17,7 @@ package org.eclipse.tracecompass.tmf.core.trace;
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
 import java.io.File;
+import java.io.IOException;
 import java.net.URISyntaxException;
 import java.util.Collection;
 import java.util.Collections;
@@ -26,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.commons.io.FileUtils;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
@@ -266,6 +268,22 @@ public final class TmfTraceManager {
         }
     }
 
+    /**
+     * Delete the supplementary files of a given trace.
+     *
+     * @param trace
+     *            The trace for which the supplementary files are to be deleted
+     * @since 2.2
+     */
+    public static void deleteSupplementaryFiles(ITmfTrace trace) {
+        try {
+            FileUtils.cleanDirectory(new File(TmfTraceManager.getSupplementaryFileDir(trace)));
+        } catch (IOException e) {
+            Activator.logError("Error deleting supplementary files for trace " + trace.getName(), e); //$NON-NLS-1$
+        }
+        refreshSupplementaryFiles(trace);
+    }
+
     // ------------------------------------------------------------------------
     // Signal handlers
     // ------------------------------------------------------------------------
This page took 0.027953 seconds and 5 git commands to generate.