From e2975aec3b5810cb198a31c6422477186818e7fe Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Sun, 23 Nov 2014 18:41:57 -0500 Subject: [PATCH] Cleanup temporary test files A properly defined test should correctly cleanup after itself when the tests have completed execution. Change-Id: I9887558e698a2ee823a9f91727cde5eccc2348f2 Signed-off-by: Thanh Ha --- .../persist/storage/file/xml/FileStorageAdapterTest.java | 2 ++ .../controller/netconf/it/NetconfITSecureTest.java | 6 +++++- .../org/opendaylight/controller/netconf/netty/SSHTest.java | 5 ++++- .../netconf/ssh/authentication/SSHServerTest.java | 7 ++++++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java b/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java index 3ea9a48a5d..16ea1b8431 100644 --- a/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java +++ b/opendaylight/config/config-persister-file-xml-adapter/src/test/java/org/opendaylight/controller/config/persist/storage/file/xml/FileStorageAdapterTest.java @@ -38,6 +38,7 @@ public class FileStorageAdapterTest { @Before public void setUp() throws Exception { file = Files.createTempFile("testFilePersist", ".txt").toFile(); + file.deleteOnExit(); if (!file.exists()) { return; } @@ -185,6 +186,7 @@ public class FileStorageAdapterTest { @Test public void testNoLastConfig() throws Exception { File file = Files.createTempFile("testFilePersist", ".txt").toFile(); + file.deleteOnExit(); if (!file.exists()) { return; } diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java index 6e265a44a5..fdc9a02006 100644 --- a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java @@ -25,6 +25,7 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.local.LocalAddress; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.util.concurrent.GlobalEventExecutor; +import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.file.Files; @@ -77,6 +78,7 @@ public class NetconfITSecureTest extends AbstractNetconfConfigTest { public static final String USERNAME = "user"; public static final String PASSWORD = "pwd"; + private File sshKeyPair; private SshProxyServer sshProxyServer; private ExecutorService nioExec; @@ -85,6 +87,8 @@ public class NetconfITSecureTest extends AbstractNetconfConfigTest { @Before public void setUp() throws Exception { + sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile(); + sshKeyPair.deleteOnExit(); nioExec = Executors.newFixedThreadPool(1); clientGroup = new NioEventLoopGroup(); minaTimerEx = Executors.newScheduledThreadPool(1); @@ -99,7 +103,7 @@ public class NetconfITSecureTest extends AbstractNetconfConfigTest { return true; } }) - .setKeyPairProvider(new PEMGeneratorHostKeyProvider(Files.createTempFile("prefix", "suffix").toAbsolutePath().toString())) + .setKeyPairProvider(new PEMGeneratorHostKeyProvider(sshKeyPair.toPath().toAbsolutePath().toString())) .setIdleTimeout(Integer.MAX_VALUE) .createSshProxyServerConfiguration()); } diff --git a/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java b/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java index 34b236b461..b682099595 100644 --- a/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java +++ b/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/netty/SSHTest.java @@ -19,6 +19,7 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.util.HashedWheelTimer; +import java.io.File; import java.net.InetSocketAddress; import java.nio.file.Files; import java.util.concurrent.ExecutorService; @@ -67,6 +68,8 @@ public class SSHTest { @Test public void test() throws Exception { + File sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile(); + sshKeyPair.deleteOnExit(); new Thread(new EchoServer(), "EchoServer").start(); final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 10831); @@ -77,7 +80,7 @@ public class SSHTest { public boolean authenticate(final String username, final String password, final ServerSession session) { return true; } - }).setKeyPairProvider(new PEMGeneratorHostKeyProvider(Files.createTempFile("prefix", "suffix").toAbsolutePath().toString())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration()); + }).setKeyPairProvider(new PEMGeneratorHostKeyProvider(sshKeyPair.toPath().toAbsolutePath().toString())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration()); final EchoClientHandler echoClientHandler = connectClient(addr); diff --git a/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/ssh/authentication/SSHServerTest.java b/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/ssh/authentication/SSHServerTest.java index 38aa2e71ac..297cf896da 100644 --- a/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/ssh/authentication/SSHServerTest.java +++ b/opendaylight/netconf/netconf-ssh/src/test/java/org/opendaylight/controller/netconf/ssh/authentication/SSHServerTest.java @@ -15,6 +15,7 @@ import static org.mockito.Mockito.doReturn; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import java.net.InetSocketAddress; +import java.io.File; import java.nio.file.Files; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -49,6 +50,7 @@ public class SSHServerTest { private static final int PORT = 1830; private static final Logger logger = LoggerFactory.getLogger(SSHServerTest.class); + private File sshKeyPair; private SshProxyServer server; @Mock @@ -59,6 +61,9 @@ public class SSHServerTest { @Before public void setUp() throws Exception { + sshKeyPair = Files.createTempFile("sshKeyPair", ".pem").toFile(); + sshKeyPair.deleteOnExit(); + MockitoAnnotations.initMocks(this); doReturn(null).when(mockedContext).createFilter(anyString()); doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString()); @@ -74,7 +79,7 @@ public class SSHServerTest { public boolean authenticate(final String username, final String password, final ServerSession session) { return true; } - }).setKeyPairProvider(new PEMGeneratorHostKeyProvider(Files.createTempFile("prefix", "suffix").toAbsolutePath().toString())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration()); + }).setKeyPairProvider(new PEMGeneratorHostKeyProvider(sshKeyPair.toPath().toAbsolutePath().toString())).setIdleTimeout(Integer.MAX_VALUE).createSshProxyServerConfiguration()); logger.info("SSH server started on " + PORT); } -- 2.36.6