Esempio n. 1
0
        public void UniqueFile_GivenExistingDirectoryOnSecondCall_AppendixGetsContinued()
        {
            const string filename            = @"C:\test";
            const string firstUniquifiedFile = @"C:\test_2";
            const string expectedFilename    = @"C:\test_3";

            var fileWrap = MockRepository.GenerateStub <IFile>();

            fileWrap.Stub(x => x.Exists(filename)).Return(false).Repeat.Once();
            fileWrap.Stub(x => x.Exists(firstUniquifiedFile)).Return(false).Repeat.Once();
            var directoryWrap = MockRepository.GenerateStub <IDirectory>();

            directoryWrap.Stub(x => x.Exists(filename)).Return(true).Repeat.Once();
            directoryWrap.Stub(x => x.Exists(firstUniquifiedFile)).Return(false).Repeat.Once();

            var uniqueFilename = new UniqueFilename(filename, directoryWrap, fileWrap);

            uniqueFilename.CreateUniqueFileName();

            fileWrap.Stub(x => x.Exists(firstUniquifiedFile)).Return(false).Repeat.Once();
            fileWrap.Stub(x => x.Exists(expectedFilename)).Return(false).Repeat.Once();
            directoryWrap.Stub(x => x.Exists(firstUniquifiedFile)).Return(true).Repeat.Once();
            directoryWrap.Stub(x => x.Exists(expectedFilename)).Return(false).Repeat.Once();

            Assert.AreEqual(expectedFilename, uniqueFilename.CreateUniqueFileName());
        }
Esempio n. 2
0
        public string MoveSingleOutputFile(string tempFile, string outputFilenameTemplate)
        {
            CreateOutputDirectory(outputFilenameTemplate);

            lock (_lockObject)
            {
                var uniqueFilename = new UniqueFilename(outputFilenameTemplate, DirectoryWrap, FileWrap);

                if (UniqueFileNameEnabled)
                {
                    outputFilenameTemplate = uniqueFilename.CreateUniqueFileName();
                }

                var success = CopyFile(tempFile, outputFilenameTemplate);

                if (!success)
                {
                    success = CopyFile(tempFile, uniqueFilename.CreateUniqueFileName());
                    if (!success)
                    {
                        throw new IOException("copy not successful on second try");
                    }
                }
            }

            DeleteFile(tempFile);
            return(outputFilenameTemplate);
        }
Esempio n. 3
0
        /// <summary>
        ///     Ensure unique filename.
        /// </summary>
        /// <param name="uniqueFilename">The UniqueFilename object that should be used</param>
        /// <returns>unique outputfilename</returns>
        protected string EnsureUniqueFilename(UniqueFilename uniqueFilename)
        {
            Logger.Debug("Ensuring unique filename for: " + uniqueFilename.OriginalFilename);
            var newFilename = uniqueFilename.CreateUniqueFileName();

            Logger.Debug("Unique filename result: " + newFilename);

            return(newFilename);
        }
        public void UniqueFile_GivenExistingFilenameOnSecondCall_AppendixGetsContinued()
        {
            const string filename            = @"C:\test.jpg";
            const string firstUniquifiedFile = @"C:\test_2.jpg";
            const string expectedFilename    = @"C:\test_3.jpg";

            _file.Exists(filename).Returns(true);
            _file.Exists(expectedFilename).Returns(false);
            _directory.Exists("").ReturnsForAnyArgs(false);

            var uniqueFilename = new UniqueFilename(filename, _directory, _file, _pathUtil);

            uniqueFilename.CreateUniqueFileName();

            _file.Exists(firstUniquifiedFile).Returns(true);
            _file.Exists(expectedFilename).Returns(false);

            Assert.AreEqual(expectedFilename, uniqueFilename.CreateUniqueFileName());
        }
        public void UniqueFile_GivenNonexistingFile_ReturnsSameFile()
        {
            const string filename = @"C:\test.txt";

            _file.Exists(filename).Returns(false);
            _directory.Exists("").ReturnsForAnyArgs(false);

            var uniqueFilename = new UniqueFilename(filename, _directory, _file, _pathUtil);

            Assert.AreEqual(filename, uniqueFilename.CreateUniqueFileName());
        }
        public void UniqueFile_GivenExistingFileWithoutExtension_ReturnsUniquifiedFile()
        {
            const string filename         = @"C:\test";
            const string expectedFilename = @"C:\test_2";

            _file.Exists(filename).Returns(true);
            _file.Exists(expectedFilename).Returns(false);
            _directory.Exists("").ReturnsForAnyArgs(false);

            var uniqueFilename = new UniqueFilename(filename, _directory, _file, _pathUtil);

            Assert.AreEqual(expectedFilename, uniqueFilename.CreateUniqueFileName());
        }
        public void UniqueFile_GivenExistingFile_LastUniqueFilenamePropertyIsUniquifiedFile()
        {
            const string filename         = @"C:\test.txt";
            const string expectedFilename = @"C:\test_2.txt";

            _file.Exists(filename).Returns(true);
            _file.Exists(expectedFilename).Returns(false);
            _directory.Exists("").ReturnsForAnyArgs(false);

            var uniqueFilename = new UniqueFilename(filename, _directory, _file, _pathUtil);

            uniqueFilename.CreateUniqueFileName();
            Assert.AreEqual(expectedFilename, uniqueFilename.LastUniqueFilename);
        }
        public void UniqueFile_ExistingFilePathWithMaximumPathLength_UniqueFilenameCallsEllpipsisForTooLongPath()
        {
            const string dir245Chars     = @"C:\ThisIsAVeryLongFileNameBecauseItHasMoreThan150CharactersAndToReachThatIHaveToWriteALotMoreTextThanICanThinkAboutRightNowIfYouReadThisUpToHereIOwnYouALittleSnackAndIStillNeedAFewMoreCharactersLetsSimplyCountOneTwoThreeFourFiveSixSevenEightNine";
            const string fileName13Chars = "File12345.pdf";
            //Combine adds a "\" so the result is the max path lengh of 260
            var tooLongPath = PathSafe.Combine(dir245Chars, fileName13Chars);

            _file.Exists("").ReturnsForAnyArgs(x => true, x => false);
            _directory.Exists("").ReturnsForAnyArgs(false);

            var uniqueFilename = new UniqueFilename(tooLongPath, _directory, _file, _pathUtil);

            Assert.Throws <PathTooLongException>(() => uniqueFilename.CreateUniqueFileName());
        }
 /// <summary>
 ///     Ensure unique filename.
 /// </summary>
 /// <param name="uniqueFilename">The UniqueFilename object that should be used</param>
 /// <returns>unique outputfilename</returns>
 private string EnsureUniqueFilename(UniqueFilename uniqueFilename)
 {
     try
     {
         Logger.Debug("Ensuring unique filename for: " + uniqueFilename.OriginalFilename);
         var newFilename = uniqueFilename.CreateUniqueFileName();
         Logger.Debug("Unique filename result: " + newFilename);
         return(newFilename);
     }
     catch (PathTooLongException ex)
     {
         throw new ProcessingException(ex.Message, ErrorCode.Conversion_PathTooLong);
     }
 }
Esempio n. 10
0
        public void UniqueFile_GivenNonexistingFile_ReturnsSameFile()
        {
            const string filename = @"C:\test.txt";

            var fileWrap = MockRepository.GenerateStub <IFile>();

            fileWrap.Stub(x => x.Exists(filename)).Return(false);
            var directoryWrap = MockRepository.GenerateStub <IDirectory>();

            directoryWrap.Stub(x => x.Exists("")).IgnoreArguments().Return(false);

            var uniqueFilename = new UniqueFilename(filename, directoryWrap, fileWrap);

            Assert.AreEqual(filename, uniqueFilename.CreateUniqueFileName());
        }
Esempio n. 11
0
        public void UniqueFile_GivenExistingFileWithoutExtension_ReturnsUniquifiedFile()
        {
            const string filename         = @"C:\test";
            const string expectedFilename = @"C:\test_2";

            var fileWrap = MockRepository.GenerateStub <IFile>();

            fileWrap.Stub(x => x.Exists(filename)).Return(true);
            fileWrap.Stub(x => x.Exists(expectedFilename)).Return(false);
            var directoryWrap = MockRepository.GenerateStub <IDirectory>();

            directoryWrap.Stub(x => x.Exists("")).IgnoreArguments().Return(false);

            var uniqueFilename = new UniqueFilename(filename, directoryWrap, fileWrap);

            Assert.AreEqual(expectedFilename, uniqueFilename.CreateUniqueFileName());
        }