mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

kenneth_suter
11.16.2007 cb7c69ea6b7701d5e691a04fce50cef885dc65f3
tests for FileManager rename methods
1 files modified
67 ■■■■■ changed files
opends/tests/unit-tests-testng/src/server/org/opends/quicksetup/util/FileManagerTest.java 67 ●●●●● patch | view | raw | blame | history
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
   */