예제 #1
0
        public void GetDirectoryNameTest()
        {
            var testData = new List <Tuple <string, string> >
            {
                new Tuple <string, string>(@"c:\path1\path2\file1.txt", "file1.txt"),
                new Tuple <string, string>(@"path1\path2\file1.txt", "file1.txt"),
                new Tuple <string, string>(@"\path1\path2\file1.txt", "file1.txt"),
                new Tuple <string, string>(@"\\server:\path1\path2\file1.txt", "file1.txt"),
                new Tuple <string, string>(@"file1.txt", "file1.txt"),
                new Tuple <string, string>("", ""),
                new Tuple <string, string>(@"c:\path1\path2\file1.txt", "file1.txt"),
                new Tuple <string, string>(@"c:\0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\path2\file1.txt", "file1.txt"),
                new Tuple <string, string>(@"c:\path2\012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt", "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt"),
                new Tuple <string, string>(@"0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\path2\file1.txt", "file1.txt"),
                new Tuple <string, string>(@"path2\012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt", "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt"),
                new Tuple <string, string>(@"file1", "file1"),
                new Tuple <string, string>(@"\", ""),
            };

            var fs = new FileSystemStorage();

            foreach (var data in testData)
            {
                var result = fs.GetFileName(data.Item1);
                if (String.Compare(result, data.Item2, true) != 0)
                {
                    Trace.WriteLine($"Input path: {data.Item1}");
                    Trace.WriteLine($"Output file: {result}, Should be: {data.Item2}");
                    Assert.Fail();
                }
            }
        }
예제 #2
0
        public void MoveFileTest()
        {
            var tempFolder = GetTempFolder();
            var fs         = new FileSystemStorage();

            var testData = new List <Tuple <string, string> >
            {
                new Tuple <string, string>(@"file1.txt", @"folder2\file2.txt"),
            };

            foreach (var data in testData)
            {
                var path1 = fs.Combine(tempFolder, data.Item1);
                var path2 = fs.Combine(tempFolder, data.Item2);

                //Delete leftovers
                try { fs.DeleteFile(path1); } catch { }
                try { fs.DeleteFile(path2); } catch { }
                try { fs.DeleteDirectory(fs.GetDirectoryName(path2)); } catch { }

                System.IO.File.WriteAllText(path1, "test file");

                //simple move target folder no exists
                try
                {
                    fs.MoveFile(path1, path2);
                    Trace.WriteLine($"move to {path2} shuold fail to move file - path error");
                    Assert.Fail();
                }
                catch { }

                try { fs.CreateDirectory(fs.GetDirectoryName(path2)); } catch { }

                //simple move
                fs.MoveFile(path1, path2);
                if (!fs.FileExists(path2))
                {
                    Trace.WriteLine($"failed to move file {path2}");
                    Assert.Fail();
                }

                //file already exist
                fs.CopyFile(path2, path1);
                try
                {
                    fs.MoveFile(path1, path2);
                    Trace.WriteLine($"Moveto {path2} shuold fail- file exist");
                    Assert.Fail();
                }
                catch { }

                if (!fs.FileExists(path2))
                {
                    Trace.WriteLine($"File was deleted{path1}");
                    Assert.Fail();
                }

                if (!fs.FileExists(path2))
                {
                    Trace.WriteLine($"File not copied - override {path2}");
                    Assert.Fail();
                }

                fs.DeleteFile(path1);
                fs.DeleteFile(path2);

                var targetFileName      = fs.GetFileName(path2);
                var targetDirectoryName = path2.Substring(0, path2.Length - targetFileName.Length);
                fs.DeleteDirectory(targetDirectoryName);
            }
        }