From c1041d73b620b8f13619ce5e110bd33784045b6c Mon Sep 17 00:00:00 2001
From: kenneth_suter <kenneth_suter@localhost>
Date: Wed, 27 Jun 2007 13:48:58 +0000
Subject: [PATCH] This commit includes tests for the recently committed StaticUtils.renameFile method. In addition it includes a mechanism for running Windows specific tests
---
opends/tests/unit-tests-testng/src/server/org/opends/server/util/TestStaticUtils.java | 69 ++++++++++++++++++++++++++++++++++
1 files changed, 69 insertions(+), 0 deletions(-)
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/util/TestStaticUtils.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/util/TestStaticUtils.java
index 60ad4b8..ee52012 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/util/TestStaticUtils.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/util/TestStaticUtils.java
@@ -33,7 +33,10 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
+import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -850,6 +853,72 @@
}
/**
+ * Tests the {@link StaticUtils#renameFile(java.io.File, java.io.File)}
+ * method.
+ *
+ * @throws Exception If the test failed unexpectedly.
+ */
+ @Test
+ public void testRenameFileNonExistentTarget() throws Exception {
+ File src = File.createTempFile("src", null);
+ File target = new File(src.getParentFile(), "target");
+ try {
+ if (target.exists()) {
+ target.delete();
+ assert(!target.exists());
+ }
+ StaticUtils.renameFile(src, target);
+ assert(!src.exists());
+ assert(target.exists());
+ } finally {
+ src.delete();
+ target.delete();
+ }
+ }
+
+ /**
+ * Tests the {@link StaticUtils#renameFile(java.io.File, java.io.File)}
+ * method.
+ *
+ * @throws Exception If the test failed unexpectedly.
+ */
+ @Test
+ public void testRenameFileExistentTarget() throws Exception {
+ File src = File.createTempFile("src", null);
+ File target = File.createTempFile("target", null);
+ try {
+ StaticUtils.renameFile(src, target);
+ assert(!src.exists());
+ assert(target.exists());
+ } finally {
+ src.delete();
+ target.delete();
+ }
+ }
+
+ /**
+ * Tests the {@link StaticUtils#renameFile(java.io.File, java.io.File)}
+ * method. Renaming locked files is a problem on Windows but not so
+ * much on other platforms.
+ *
+ * @throws Exception If the test failed unexpectedly.
+ */
+ @Test(groups = {"windows"}, expectedExceptions = IOException.class)
+ public void testRenameFileLockedTarget() throws Exception {
+ File src = File.createTempFile("src", null);
+ File target = File.createTempFile("target", null);
+ FileChannel c = new RandomAccessFile(target, "rw").getChannel();
+ FileLock lock = c.lock();
+ try {
+ StaticUtils.renameFile(src, target);
+ } finally {
+ lock.release();
+ src.delete();
+ target.delete();
+ }
+ }
+
+ /**
* Tests the {@link StaticUtils#recursiveDelete(File)} method.
*
* @throws Exception
--
Gitblit v1.10.0