Beispiel #1
0
        public void ValidateReturnsExpectedResultWhenEntryStreamIsEmpty()
        {
            var sut = new ZipArchiveDbaseEntryValidator <FakeDbaseRecord>(
                Encoding.Default,
                new FakeDbaseSchema(),
                new FakeDbaseRecordValidator());

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    archive.CreateEntry("entry");
                }
                stream.Flush();
                stream.Position = 0;

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("entry");

                    var result = sut.Validate(entry);

                    Assert.Equal(
                        ZipArchiveProblems.Single(entry.HasDbaseHeaderFormatError(
                                                      new EndOfStreamException("Unable to read beyond the end of the stream."))
                                                  ),
                        result,
                        new FileProblemComparer());
                }
            }
        }
Beispiel #2
0
        public void ValidateEntryCanNotBeNull()
        {
            var sut = new ZipArchiveDbaseEntryValidator <FakeDbaseRecord>(
                Encoding.Default,
                new FakeDbaseSchema(),
                new FakeDbaseRecordValidator());

            Assert.Throws <ArgumentNullException>(() => sut.Validate(null));
        }
Beispiel #3
0
        public void ValidatePassesExpectedDbaseRecordsToDbaseRecordValidator()
        {
            var schema    = new FakeDbaseSchema();
            var validator = new CollectDbaseRecordValidator();
            var sut       = new ZipArchiveDbaseEntryValidator <FakeDbaseRecord>(
                Encoding.UTF8,
                schema,
                validator);
            var records = new []
            {
                new FakeDbaseRecord {
                    Field = { Value = 1 }
                },
                new FakeDbaseRecord {
                    Field = { Value = 2 }
                }
            };
            var date   = DateTime.Today;
            var header = new DbaseFileHeader(
                date,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(records.Length),
                schema);

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    var entry = archive.CreateEntry("entry");
                    using (var entryStream = entry.Open())
                        using (var writer = new BinaryWriter(entryStream, Encoding.UTF8))
                        {
                            header.Write(writer);
                            foreach (var record in records)
                            {
                                record.Write(writer);
                            }
                            writer.Write(DbaseRecord.EndOfFile);
                            entryStream.Flush();
                        }
                }
                stream.Flush();
                stream.Position = 0;

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("entry");

                    var result = sut.Validate(entry);

                    Assert.Equal(ZipArchiveProblems.None, result);
                    Assert.Equal(records, validator.Collected);
                }
            }
        }
Beispiel #4
0
        public void ValidateReturnsExpectedResultWhenDbaseRecordValidatorReturnsErrors()
        {
            var problems = new FileProblem[]
            {
                new FileError("file1", "error1", new ProblemParameter("parameter1", "value1")),
                new FileWarning("file2", "error2", new ProblemParameter("parameter2", "value2"))
            };
            var schema = new FakeDbaseSchema();
            var sut    = new ZipArchiveDbaseEntryValidator <FakeDbaseRecord>(
                Encoding.UTF8,
                schema,
                new FakeDbaseRecordValidator(problems));
            var date   = DateTime.Today;
            var header = new DbaseFileHeader(
                date,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(0),
                schema);

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    var entry = archive.CreateEntry("entry");
                    using (var entryStream = entry.Open())
                        using (var writer = new BinaryWriter(entryStream, Encoding.UTF8))
                        {
                            header.Write(writer);
                            entryStream.Flush();
                        }
                }
                stream.Flush();
                stream.Position = 0;

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("entry");

                    var result = sut.Validate(entry);

                    Assert.Equal(
                        ZipArchiveProblems.None.AddRange(problems),
                        result);
                }
            }
        }
Beispiel #5
0
        public void ValidateReturnsExpectedResultWhenDbaseFileHeaderDoesNotMatch()
        {
            var sut = new ZipArchiveDbaseEntryValidator <FakeDbaseRecord>(
                Encoding.UTF8,
                new FakeDbaseSchema(),
                new FakeDbaseRecordValidator());
            var date   = DateTime.Today;
            var header = new DbaseFileHeader(
                date,
                DbaseCodePage.Western_European_ANSI,
                new DbaseRecordCount(0),
                new AnonymousDbaseSchema(new DbaseField[0]));

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    var entry = archive.CreateEntry("entry");
                    using (var entryStream = entry.Open())
                        using (var writer = new BinaryWriter(entryStream, Encoding.UTF8))
                        {
                            header.Write(writer);
                            entryStream.Flush();
                        }
                }
                stream.Flush();
                stream.Position = 0;

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("entry");

                    var result = sut.Validate(entry);

                    Assert.Equal(
                        ZipArchiveProblems.Single(entry.HasDbaseSchemaMismatch(
                                                      new FakeDbaseSchema(),
                                                      new AnonymousDbaseSchema(new DbaseField[0]))
                                                  ),
                        result);
                }
            }
        }
Beispiel #6
0
        public void ValidateReturnsExpectedResultWhenEntryStreamContainsMalformedDbaseHeader()
        {
            var sut = new ZipArchiveDbaseEntryValidator <FakeDbaseRecord>(
                Encoding.Default,
                new FakeDbaseSchema(),
                new FakeDbaseRecordValidator());

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    var entry = archive.CreateEntry("entry");
                    using (var entryStream = entry.Open())
                    {
                        entryStream.WriteByte(0);
                        entryStream.Flush();
                    }
                }
                stream.Flush();
                stream.Position = 0;

                using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                {
                    var entry = archive.GetEntry("entry");

                    var result = sut.Validate(entry);

                    Assert.Equal(
                        ZipArchiveProblems.Single(entry.HasDbaseHeaderFormatError(
                                                      new DbaseFileHeaderException("The database file type must be 3 (dBase III)."))
                                                  ),
                        result,
                        new FileProblemComparer());
                }
            }
        }