From abab8cd511d83174155f7be5dec054ff12ab0bcd Mon Sep 17 00:00:00 2001
From: kenneth_suter <kenneth_suter@localhost>
Date: Wed, 11 Jul 2007 21:16:32 +0000
Subject: [PATCH] tests for FileManager rename methods

---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/quicksetup/util/FileManagerTest.java |   67 +++++++++++++++++++++++++++++++++
 1 files changed, 67 insertions(+), 0 deletions(-)

diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/quicksetup/util/FileManagerTest.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/quicksetup/util/FileManagerTest.java
index 13f9665..0756c0f 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/quicksetup/util/FileManagerTest.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/quicksetup/util/FileManagerTest.java
@@ -33,6 +33,7 @@
 import org.opends.quicksetup.TestUtilities;
 import org.opends.quicksetup.Constants;
 import org.opends.quicksetup.ApplicationException;
+import org.opends.server.util.StaticUtils;
 
 import java.io.File;
 import java.io.IOException;
@@ -40,6 +41,9 @@
 import java.io.FileReader;
 import java.io.BufferedReader;
 import java.io.FileFilter;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
 
 /**
  * FileManager Tester.
@@ -127,6 +131,69 @@
   }
 
   /**
+   * Tests the rename.
+   * @throws Exception
+   */
+  public void testRenameNonExistentTarget() throws Exception {
+    File src = File.createTempFile("src", null);
+    File target = new File(src.getParentFile(), "target");
+    try {
+      if (target.exists()) {
+        target.delete();
+        assert(!target.exists());
+      }
+      fileManager.rename(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 basic move.
    * @throws Exception if something unexpected
    */

--
Gitblit v1.10.0