/// <exception cref="System.IO.IOException"/> public virtual void TestWriteInNonExistentDirectory() { Org.Apache.Hadoop.FS.Path path = Path("/test/hadoop/file"); NUnit.Framework.Assert.IsFalse("Parent exists", fs.Exists(path.GetParent())); CreateFile(path); Assert.True("Exists", fs.Exists(path)); Assert.Equal("Length", data.Length, fs.GetFileStatus(path).GetLen ()); Assert.True("Parent exists", fs.Exists(path.GetParent())); }
/// <exception cref="System.IO.IOException"/> public virtual void TestOverwrite() { Org.Apache.Hadoop.FS.Path path = Path("/test/hadoop/file"); fs.Mkdirs(path.GetParent()); CreateFile(path); Assert.True("Exists", fs.Exists(path)); Assert.Equal("Length", data.Length, fs.GetFileStatus(path).GetLen ()); try { fs.Create(path, false).Close(); Fail("Should throw IOException."); } catch (IOException) { } // Expected FSDataOutputStream @out = fs.Create(path, true); @out.Write(data, 0, data.Length); @out.Close(); Assert.True("Exists", fs.Exists(path)); Assert.Equal("Length", data.Length, fs.GetFileStatus(path).GetLen ()); }
/// <exception cref="System.Exception"/> public virtual void TestRenameFileMoveToExistingDirectory() { if (!RenameSupported()) { return; } Org.Apache.Hadoop.FS.Path src = Path("/test/hadoop/file"); CreateFile(src); Org.Apache.Hadoop.FS.Path dst = Path("/test/new/newfile"); fs.Mkdirs(dst.GetParent()); Rename(src, dst, true, false, true); }
/// <exception cref="System.Exception"/> public virtual void TestWorkingDirectory() { Org.Apache.Hadoop.FS.Path workDir = Path(GetDefaultWorkingDirectory()); Assert.Equal(workDir, fs.GetWorkingDirectory()); fs.SetWorkingDirectory(Path(".")); Assert.Equal(workDir, fs.GetWorkingDirectory()); fs.SetWorkingDirectory(Path("..")); Assert.Equal(workDir.GetParent(), fs.GetWorkingDirectory()); Org.Apache.Hadoop.FS.Path relativeDir = Path("hadoop"); fs.SetWorkingDirectory(relativeDir); Assert.Equal(relativeDir, fs.GetWorkingDirectory()); Org.Apache.Hadoop.FS.Path absoluteDir = Path("/test/hadoop"); fs.SetWorkingDirectory(absoluteDir); Assert.Equal(absoluteDir, fs.GetWorkingDirectory()); }
/// <exception cref="System.Exception"/> public virtual void TestMkdirs() { Org.Apache.Hadoop.FS.Path testDir = Path("/test/hadoop"); NUnit.Framework.Assert.IsFalse(fs.Exists(testDir)); NUnit.Framework.Assert.IsFalse(fs.IsFile(testDir)); Assert.True(fs.Mkdirs(testDir)); Assert.True(fs.Exists(testDir)); NUnit.Framework.Assert.IsFalse(fs.IsFile(testDir)); Assert.True(fs.Mkdirs(testDir)); Assert.True(fs.Exists(testDir)); NUnit.Framework.Assert.IsFalse(fs.IsFile(testDir)); Org.Apache.Hadoop.FS.Path parentDir = testDir.GetParent(); Assert.True(fs.Exists(parentDir)); NUnit.Framework.Assert.IsFalse(fs.IsFile(parentDir)); Org.Apache.Hadoop.FS.Path grandparentDir = parentDir.GetParent(); Assert.True(fs.Exists(grandparentDir)); NUnit.Framework.Assert.IsFalse(fs.IsFile(grandparentDir)); }
/// <exception cref="System.Exception"/> public virtual void TestRenameDirectoryMoveToExistingDirectory() { if (!RenameSupported()) { return; } Org.Apache.Hadoop.FS.Path src = Path("/test/hadoop/dir"); fs.Mkdirs(src); CreateFile(Path("/test/hadoop/dir/file1")); CreateFile(Path("/test/hadoop/dir/subdir/file2")); Org.Apache.Hadoop.FS.Path dst = Path("/test/new/newdir"); fs.Mkdirs(dst.GetParent()); Rename(src, dst, true, false, true); NUnit.Framework.Assert.IsFalse("Nested file1 exists", fs.Exists(Path("/test/hadoop/dir/file1" ))); NUnit.Framework.Assert.IsFalse("Nested file2 exists", fs.Exists(Path("/test/hadoop/dir/subdir/file2" ))); Assert.True("Renamed nested file1 exists", fs.Exists(Path("/test/new/newdir/file1" ))); Assert.True("Renamed nested exists", fs.Exists(Path("/test/new/newdir/subdir/file2" ))); }
/// <summary>Write a file and read it in, validating the result.</summary> /// <remarks> /// Write a file and read it in, validating the result. Optional flags control /// whether file overwrite operations should be enabled, and whether the /// file should be deleted afterwards. /// If there is a mismatch between what was written and what was expected, /// a small range of bytes either side of the first error are logged to aid /// diagnosing what problem occurred -whether it was a previous file /// or a corrupting of the current file. This assumes that two /// sequential runs to the same path use datasets with different character /// moduli. /// </remarks> /// <param name="path">path to write to</param> /// <param name="len">length of data</param> /// <param name="overwrite">should the create option allow overwrites?</param> /// <param name="delete"> /// should the file be deleted afterwards? -with a verification /// that it worked. Deletion is not attempted if an assertion has failed /// earlier -it is not in a <code>finally{}</code> block. /// </param> /// <exception cref="System.IO.IOException">IO problems</exception> protected internal virtual void WriteAndRead(Org.Apache.Hadoop.FS.Path path, byte [] src, int len, bool overwrite, bool delete) { Assert.True("Not enough data in source array to write " + len + " bytes", src.Length >= len); fs.Mkdirs(path.GetParent()); FSDataOutputStream @out = fs.Create(path, overwrite, fs.GetConf().GetInt("io.file.buffer.size" , 4096), (short)1, GetBlockSize()); @out.Write(src, 0, len); @out.Close(); Assert.True("Exists", fs.Exists(path)); Assert.Equal("Length", len, fs.GetFileStatus(path).GetLen()); FSDataInputStream @in = fs.Open(path); byte[] buf = new byte[len]; @in.ReadFully(0, buf); @in.Close(); Assert.Equal(len, buf.Length); int errors = 0; int first_error_byte = -1; for (int i = 0; i < len; i++) { if (src[i] != buf[i]) { if (errors == 0) { first_error_byte = i; } errors++; } } if (errors > 0) { string message = string.Format(" %d errors in file of length %d", errors, len); Log.Warn(message); // the range either side of the first error to print // this is a purely arbitrary number, to aid user debugging int overlap = 10; for (int i_1 = Math.Max(0, first_error_byte - overlap); i_1 < Math.Min(first_error_byte + overlap, len); i_1++) { byte actual = buf[i_1]; byte expected = src[i_1]; string letter = ToChar(actual); string line = string.Format("[%04d] %2x %s\n", i_1, actual, letter); if (expected != actual) { line = string.Format("[%04d] %2x %s -expected %2x %s\n", i_1, actual, letter, expected , ToChar(expected)); } Log.Warn(line); } Fail(message); } if (delete) { bool deleted = fs.Delete(path, false); Assert.True("Deleted", deleted); NUnit.Framework.Assert.IsFalse("No longer exists", fs.Exists(path)); } }