Clean up tempDir after each test runs.

- Fix for issue with /tmp disk full issue on Jenkins.
- Using JUnit Rule TemporaryFolder where possible.

Change-Id: Ie91eba37581ba5bf6c32be7f614220e2098ce2f8
diff --git a/core/net/src/test/java/org/onosproject/cfg/impl/ComponentConfigLoaderTest.java b/core/net/src/test/java/org/onosproject/cfg/impl/ComponentConfigLoaderTest.java
index 0320cf7..1225959 100644
--- a/core/net/src/test/java/org/onosproject/cfg/impl/ComponentConfigLoaderTest.java
+++ b/core/net/src/test/java/org/onosproject/cfg/impl/ComponentConfigLoaderTest.java
@@ -17,9 +17,10 @@
 package org.onosproject.cfg.impl;
 
 import com.google.common.collect.ImmutableSet;
-import com.google.common.io.Files;
 import org.junit.Before;
+import org.junit.ClassRule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.onosproject.cfg.ComponentConfigAdapter;
 import org.slf4j.Logger;
 
@@ -38,7 +39,8 @@
  */
 public class ComponentConfigLoaderTest {
 
-    static final File TEST_DIR = Files.createTempDir();
+    @ClassRule
+    public static TemporaryFolder testFolder = new TemporaryFolder();
 
     private static final String FOO_COMPONENT = "fooComponent";
 
@@ -53,8 +55,8 @@
      * and assign it to the loader.configService for the test.
      */
     @Before
-    public void setUp() {
-        ComponentConfigLoader.cfgFile = new File(TEST_DIR, "test.json");
+    public void setUp() throws IOException {
+        ComponentConfigLoader.cfgFile = new File(testFolder.newFolder(), "test.json");
         loader = new ComponentConfigLoader();
         service = new TestConfigService();
         loader.configService = service;
diff --git a/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/ControllerTest.java b/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/ControllerTest.java
index 6d7be2d..f4e6191 100644
--- a/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/ControllerTest.java
+++ b/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/ControllerTest.java
@@ -23,7 +23,9 @@
 import java.util.stream.IntStream;
 
 import org.junit.Before;
+import org.junit.ClassRule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.onlab.junit.TestTools;
 import org.onlab.util.ItemNotFoundException;
 import org.onosproject.net.DeviceId;
@@ -36,8 +38,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.io.Files;
-
 import static com.google.common.io.ByteStreams.toByteArray;
 import static com.google.common.io.Files.write;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -53,16 +53,17 @@
  */
 public class ControllerTest {
 
+    @ClassRule
+    public static TemporaryFolder testFolder = new TemporaryFolder();
+
     Controller controller;
     protected static final Logger log = LoggerFactory.getLogger(ControllerTest.class);
 
-    static final File TEST_DIR = Files.createTempDir();
-
     /*
      * Writes the necessary file for the tests in the temporary directory
      */
-    static File stageTestResource(String name) throws IOException {
-        File file = new File(TEST_DIR, name);
+    private static File stageTestResource(String name) throws IOException {
+        File file = new File(testFolder.newFolder(), name);
         byte[] bytes = toByteArray(ControllerTest.class.getResourceAsStream(name));
         write(bytes, file);
         return file;
diff --git a/utils/stc/src/test/java/org/onlab/stc/CompilerTest.java b/utils/stc/src/test/java/org/onlab/stc/CompilerTest.java
index 59b5530..f30f8d3 100644
--- a/utils/stc/src/test/java/org/onlab/stc/CompilerTest.java
+++ b/utils/stc/src/test/java/org/onlab/stc/CompilerTest.java
@@ -15,9 +15,12 @@
  */
 package org.onlab.stc;
 
-import com.google.common.io.Files;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.onlab.util.Tools;
+
+import com.google.common.io.Files;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -35,10 +38,12 @@
  */
 public class CompilerTest {
 
-    static final File TEST_DIR = Files.createTempDir();
+
+    private static File testDir;
 
     @BeforeClass
     public static void setUpClass() throws IOException {
+        testDir = Files.createTempDir();
         stageTestResource("scenario.xml");
         stageTestResource("simple-scenario.xml");
         stageTestResource("one-scenario.xml");
@@ -49,16 +54,21 @@
         System.setProperty("TOC1", "1.2.3.1");
         System.setProperty("TOC2", "1.2.3.2");
         System.setProperty("TOC3", "1.2.3.3");
-        System.setProperty("test.dir", TEST_DIR.getAbsolutePath());
+        System.setProperty("test.dir", testDir.getAbsolutePath());
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws IOException {
+        Tools.removeDirectory(testDir.getPath());
     }
 
     static FileInputStream getStream(String name) throws FileNotFoundException {
-        return new FileInputStream(new File(TEST_DIR, name));
+        return new FileInputStream(new File(testDir, name));
     }
 
     static void stageTestResource(String name) throws IOException {
         byte[] bytes = toByteArray(CompilerTest.class.getResourceAsStream(name));
-        write(bytes, new File(TEST_DIR, name));
+        write(bytes, new File(testDir, name));
     }
 
     @Test
@@ -72,7 +82,7 @@
         assertEquals("incorrect step count", 33, flow.getVertexes().size());
         assertEquals("incorrect dependency count", 26, flow.getEdges().size());
         assertEquals("incorrect logDir",
-                     new File(TEST_DIR.getAbsolutePath(), "foo"), compiler.logDir());
+                     new File(testDir.getAbsolutePath(), "foo"), compiler.logDir());
 
         Step step = compiler.getStep("there");
         assertEquals("incorrect edge count", 2, flow.getEdgesFrom(step).size());
diff --git a/utils/stc/src/test/java/org/onlab/stc/CoordinatorTest.java b/utils/stc/src/test/java/org/onlab/stc/CoordinatorTest.java
index c6f057e..1e5580a 100644
--- a/utils/stc/src/test/java/org/onlab/stc/CoordinatorTest.java
+++ b/utils/stc/src/test/java/org/onlab/stc/CoordinatorTest.java
@@ -15,6 +15,7 @@
  */
 package org.onlab.stc;
 
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.onlab.util.Tools;
@@ -36,11 +37,15 @@
     @BeforeClass
     public static void setUpClass() throws IOException {
         CompilerTest.setUpClass();
-        Tools.removeDirectory(StepProcessorTest.DIR);
 
         StepProcessor.launcher = "true ";
     }
 
+    @AfterClass
+    public static void tearDownClass() throws IOException {
+        CompilerTest.tearDownClass();
+    }
+
     @Test
     public void simple() throws IOException, InterruptedException {
         executeTest("simple-scenario.xml");
diff --git a/utils/stc/src/test/java/org/onlab/stc/DependencyTest.java b/utils/stc/src/test/java/org/onlab/stc/DependencyTest.java
index cb989d8..b453305 100644
--- a/utils/stc/src/test/java/org/onlab/stc/DependencyTest.java
+++ b/utils/stc/src/test/java/org/onlab/stc/DependencyTest.java
@@ -29,6 +29,7 @@
 
     protected Step step1, step2;
 
+    @Override
     @Before
     public void setUp() throws ConfigurationException {
         super.setUp();
@@ -52,6 +53,7 @@
         assertTrue("incorrect isSoft", soft.isSoft());
     }
 
+    @Override
     @Test
     public void equality() {
         Dependency d1 = new Dependency(step1, step2, false);
diff --git a/utils/stc/src/test/java/org/onlab/stc/MonitorLayoutTest.java b/utils/stc/src/test/java/org/onlab/stc/MonitorLayoutTest.java
index 4b7f561..537ecbb 100644
--- a/utils/stc/src/test/java/org/onlab/stc/MonitorLayoutTest.java
+++ b/utils/stc/src/test/java/org/onlab/stc/MonitorLayoutTest.java
@@ -15,6 +15,8 @@
  */
 package org.onlab.stc;
 
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.onlab.stc.MonitorLayout.Box;
 
@@ -33,6 +35,16 @@
 
     private MonitorLayout layout;
 
+    @BeforeClass
+    public static void setUpClass() throws IOException {
+        CompilerTest.setUpClass();
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws IOException {
+        CompilerTest.tearDownClass();
+    }
+
     private Compiler getCompiler(String name) throws IOException {
         stageTestResource(name);
         Scenario scenario = loadScenario(getStream(name));
diff --git a/utils/stc/src/test/java/org/onlab/stc/StepProcessorTest.java b/utils/stc/src/test/java/org/onlab/stc/StepProcessorTest.java
index 6fd9ee1..a2f766f 100644
--- a/utils/stc/src/test/java/org/onlab/stc/StepProcessorTest.java
+++ b/utils/stc/src/test/java/org/onlab/stc/StepProcessorTest.java
@@ -15,15 +15,11 @@
  */
 package org.onlab.stc;
 
-import com.google.common.io.Files;
-import org.junit.AfterClass;
 import org.junit.BeforeClass;
+import org.junit.ClassRule;
 import org.junit.Test;
-import org.onlab.util.Tools;
-
+import org.junit.rules.TemporaryFolder;
 import java.io.File;
-import java.io.IOException;
-
 import static com.google.common.base.Preconditions.checkState;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
@@ -34,24 +30,23 @@
  */
 public class StepProcessorTest {
 
-    static final File DIR = Files.createTempDir();
+    @ClassRule
+    public static TemporaryFolder testFolder = new TemporaryFolder();
+
+    private static File dir;
     private final Listener delegate = new Listener();
 
     @BeforeClass
     public static void setUpClass() {
+        dir = testFolder.getRoot();
         StepProcessor.launcher = "echo";
-        checkState(DIR.exists() || DIR.mkdirs(), "Unable to create directory");
-    }
-
-    @AfterClass
-    public static void tearDownClass() throws IOException {
-        Tools.removeDirectory(DIR.getPath());
+        checkState(dir.exists() || dir.mkdirs(), "Unable to create directory");
     }
 
     @Test
     public void basics() {
-        Step step = new Step("foo", "ls " + DIR.getAbsolutePath(), null, null, null, 0);
-        StepProcessor processor = new StepProcessor(step, DIR, delegate, step.command());
+        Step step = new Step("foo", "ls " + dir.getAbsolutePath(), null, null, null, 0);
+        StepProcessor processor = new StepProcessor(step, dir, delegate, step.command());
         processor.run();
         assertTrue("should be started", delegate.started);
         assertTrue("should be stopped", delegate.stopped);