Пример #1
0
        public override void AppendAllText(string path, string contents, Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

            if (!mockFileDataAccessor.FileExists(path))
            {
                var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
                if (!mockFileDataAccessor.Directory.Exists(dir))
                {
                    throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, StringResources.Manager.GetString("COULD_NOT_FIND_PART_OF_PATH_EXCEPTION"), path));
                }

                mockFileDataAccessor.AddFile(path, new MockFileData(contents, encoding));
            }
            else
            {
                var file          = mockFileDataAccessor.GetFile(path);
                var bytesToAppend = encoding.GetBytes(contents);
                file.Contents = file.Contents.Concat(bytesToAppend).ToArray();
            }
        }
 private void InternalFlush()
 {
     if (mockFileDataAccessor.FileExists(path))
     {
         var mockFileData = mockFileDataAccessor.GetFile(path);
         /* reset back to the beginning .. */
         Seek(0, SeekOrigin.Begin);
         /* .. read everything out */
         var data = new byte[Length];
         Read(data, 0, (int)Length);
         /* .. put it in the mock system */
         mockFileData.Contents = data;
     }
 }
Пример #3
0
        public override void AppendAllText(string path, string contents, Encoding encoding)
        {
            ValidateParameter(path, "path");

            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            if (!mockFileDataAccessor.FileExists(path))
            {
                var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
                if (!mockFileDataAccessor.Directory.Exists(dir))
                {
                    throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path));
                }

                mockFileDataAccessor.AddFile(path, new MockFileData(contents, encoding));
            }
            else
            {
                var file          = mockFileDataAccessor.GetFile(path);
                var bytesToAppend = encoding.GetBytes(contents);
                file.Contents = file.Contents.Concat(bytesToAppend).ToArray();
            }
        }
Пример #4
0
        public override void AppendAllText(string path, string contents, Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            _mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, nameof(path));

            if (!_mockFileDataAccessor.FileExists(path))
            {
                string dir = _mockFileDataAccessor.Path.GetDirectoryName(path);
                if (!_mockFileDataAccessor.Directory.Exists(dir))
                {
                    throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.COULD_NOT_FIND_PART_OF_PATH_EXCEPTION, path));
                }

                _mockFileDataAccessor.AddFile(path, new MockFileData(contents, encoding));
            }
            else
            {
                MockFileData file          = _mockFileDataAccessor.GetFile(path);
                byte[]       bytesToAppend = encoding.GetBytes(contents);
                file.Contents = file.Contents.Concat(bytesToAppend).ToArray();
            }
        }
Пример #5
0
        public override DirectoryInfoBase CreateDirectory(string path, DirectorySecurity directorySecurity)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Length == 0)
            {
                throw new ArgumentException("Path cannot be the empty string or all whitespace.", "path");
            }

            if (mockFileDataAccessor.FileExists(path))
            {
                var message = string.Format(CultureInfo.InvariantCulture, @"Cannot create ""{0}"" because a file or directory with the same name already exists.", path);
                var ex      = new IOException(message);
                ex.Data.Add("Path", path);
                throw ex;
            }

            path = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(path));

            if (!Exists(path))
            {
                mockFileDataAccessor.AddDirectory(path);
            }

            var created = new MockDirectoryInfo(mockFileDataAccessor, path);

            return(created);
        }
Пример #6
0
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, StreamType streamType)
        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                if (data != null && data.Length > 0 && streamType != StreamType.TRUNCATE)
                {
                    Write(data, 0, data.Length);
                    Seek(0, StreamType.APPEND.Equals(streamType)
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                if (StreamType.READ.Equals(streamType))
                {
                    throw new FileNotFoundException("File not found.", path);
                }
                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }

            canWrite = streamType != StreamType.READ;
        }
        public override DirectoryInfoBase CreateDirectory(string path, DirectorySecurity directorySecurity)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Length == 0)
            {
                throw new ArgumentException(Properties.Resources.PATH_CANNOT_BE_THE_EMPTY_STRING_OR_ALL_WHITESPACE, "path");
            }

            if (mockFileDataAccessor.FileExists(path))
            {
                var message = string.Format(CultureInfo.InvariantCulture, @"Cannot create ""{0}"" because a file or directory with the same name already exists.", path);
                var ex      = new IOException(message);
                ex.Data.Add("Path", path);
                throw ex;
            }

            path = EnsurePathEndsWithDirectorySeparator(mockFileDataAccessor.Path.GetFullPath(path));

            if (!Exists(path))
            {
                mockFileDataAccessor.AddDirectory(path);
            }

            var created = new MockDirectoryInfo(mockFileDataAccessor, path);

            return(created);
        }
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                if (data != null && data.Length > 0)
                {
                    Write(data, 0, data.Length);
                    Seek(0, forAppend
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }
        }
Пример #9
0
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false)
        {
            if (mockFileDataAccessor == null)
            {
                throw new ArgumentNullException("mockFileDataAccessor");
            }

            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                if (data != null && data.Length > 0)
                {
                    Write(data, 0, data.Length);
                    Seek(0, forAppend
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }
        }
        public override void Copy(string sourceFileName, string destFileName)
        {
            if (mockFileDataAccessor.FileExists(destFileName))
            {
                throw new IOException(string.Format("The file {0} already exists.", destFileName));
            }

            mockFileDataAccessor.AddFile(destFileName, mockFileDataAccessor.GetFile(sourceFileName));
        }
Пример #11
0
        public MockFileStream(
            IMockFileDataAccessor mockFileDataAccessor,
            string path,
            FileMode mode,
            FileAccess access   = FileAccess.ReadWrite,
            FileOptions options = FileOptions.None)

        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
            this.path    = path;
            this.options = options;

            if (mockFileDataAccessor.FileExists(path))
            {
                if (mode.Equals(FileMode.CreateNew))
                {
                    throw CommonExceptions.FileAlreadyExists(path);
                }

                var fileData = mockFileDataAccessor.GetFile(path);
                fileData.CheckFileAccess(path, access);

                var existingContents     = fileData.Contents;
                var keepExistingContents =
                    existingContents?.Length > 0 &&
                    mode != FileMode.Truncate && mode != FileMode.Create;
                if (keepExistingContents)
                {
                    Write(existingContents, 0, existingContents.Length);
                    Seek(0, mode == FileMode.Append
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                var directoryPath = mockFileDataAccessor.Path.GetDirectoryName(path);
                if (!string.IsNullOrEmpty(directoryPath) && !mockFileDataAccessor.Directory.Exists(directoryPath))
                {
                    throw CommonExceptions.CouldNotFindPartOfPath(path);
                }

                if (mode.Equals(FileMode.Open) || mode.Equals(FileMode.Truncate))
                {
                    throw CommonExceptions.FileNotFound(path);
                }

                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }

            this.access = access;
        }
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path)
        {
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                base.Write(data, 0, data.Length);
                base.Seek(0, SeekOrigin.Begin);
            }
        }
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path)
        {
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                base.Write(data, 0, data.Length);
                base.Seek(0, SeekOrigin.Begin);
            }
        }
Пример #14
0
        public MockFileStream(
            IMockFileDataAccessor mockFileDataAccessor,
            string path,
            StreamType streamType,
            FileOptions options,
            FileMode fileMode = FileMode.Append)
        {
            this.mockFileDataAccessor = mockFileDataAccessor ?? throw new ArgumentNullException(nameof(mockFileDataAccessor));
            this.path    = path;
            this.options = options;

            if (mockFileDataAccessor.FileExists(path))
            {
                if (fileMode.Equals(FileMode.CreateNew))
                {
                    throw CommonExceptions.FileAlreadyExists(path);
                }

                var fileData = mockFileDataAccessor.GetFile(path);
                fileData.CheckFileAccess(path, streamType != StreamType.READ ? FileAccess.Write : FileAccess.Read);

                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = fileData.Contents;
                if (data != null && data.Length > 0 && streamType != StreamType.TRUNCATE)
                {
                    Write(data, 0, data.Length);
                    Seek(0, StreamType.APPEND.Equals(streamType)
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
            else
            {
                if (!mockFileDataAccessor.Directory.Exists(mockFileDataAccessor.Path.GetDirectoryName(path)))
                {
                    throw CommonExceptions.CouldNotFindPartOfPath(path);
                }

                if (StreamType.READ.Equals(streamType) ||
                    fileMode.Equals(FileMode.Open) ||
                    fileMode.Equals(FileMode.Truncate))
                {
                    throw CommonExceptions.FileNotFound(path);
                }

                mockFileDataAccessor.AddFile(path, new MockFileData(new byte[] { }));
            }

            canWrite = streamType != StreamType.READ;
        }
Пример #15
0
        private void InternalFlush()
        {
            if (mockFileDataAccessor.FileExists(path))
            {
                mockFileDataAccessor.RemoveFile(path);
            }

            /* reset back to the beginning .. */
            base.Seek(0, SeekOrigin.Begin);
            /* .. read everything out */
            var data = new byte[base.Length];

            base.Read(data, 0, (int)base.Length);
            /* .. put it in the mock system */
            mockFileDataAccessor.AddFile(path, new MockFileData(data));
        }
Пример #16
0
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false)
        {
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                if (data != null && data.Length > 0)
                {
                    base.Write(data, 0, data.Length);
                    base.Seek(0, forAppend
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
        }
        public MockFileStream(IMockFileDataAccessor mockFileDataAccessor, string path, bool forAppend = false)
        {
            this.mockFileDataAccessor = mockFileDataAccessor;
            this.path = path;

            if (mockFileDataAccessor.FileExists(path))
            {
                /* only way to make an expandable MemoryStream that starts with a particular content */
                var data = mockFileDataAccessor.GetFile(path).Contents;
                if (data != null && data.Length > 0)
                {
                    base.Write(data, 0, data.Length);
                    base.Seek(0, forAppend
                        ? SeekOrigin.End
                        : SeekOrigin.Begin);
                }
            }
        }
        public override void AppendAllText(string path, string contents, Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            mockFileDataAccessor.PathVerifier.IsLegalAbsoluteOrRelative(path, "path");

            if (!mockFileDataAccessor.FileExists(path))
            {
                VerifyDirectoryExists(path);
                mockFileDataAccessor.AddFile(path, new MockFileData(contents, encoding));
            }
            else
            {
                var file = mockFileDataAccessor.GetFile(path);
                file.CheckFileAccess(path, FileAccess.Write);
                var bytesToAppend = encoding.GetBytes(contents);
                file.Contents = file.Contents.Concat(bytesToAppend).ToArray();
            }
        }
Пример #19
0
 public override bool Exists(string path)
 {
     return(mockFileDataAccessor.FileExists(path));
 }
Пример #20
0
 public override void AppendAllText(string path, string contents)
 {
     if (!mockFileDataAccessor.FileExists(path))
     {
         var dir = mockFileDataAccessor.Path.GetDirectoryName(path);
         if (!mockFileDataAccessor.Directory.Exists(dir))
         {
             throw new DirectoryNotFoundException(String.Format(CultureInfo.InvariantCulture, "Could not find a part of the path '{0}'.", path));
         }
         mockFileDataAccessor.AddFile(path, new MockFileData(contents));
     }
     else
     {
         mockFileDataAccessor
         .GetFile(path)
         .TextContents += contents;
     }
 }