Exemplo n.º 1
0
        private Document(string id, MetadataStack metadata, FilePath source, Stream stream, object streamLock, IEnumerable <KeyValuePair <string, object> > items, bool disposeStream)
        {
            if (source?.IsAbsolute == false)
            {
                throw new ArgumentException("Document sources must be absolute", nameof(source));
            }

            Id        = id ?? throw new ArgumentNullException(nameof(id));
            Source    = source;
            _metadata = items == null ? metadata : metadata.Clone(items);

            if (stream == null)
            {
                _stream = Stream.Null;
            }
            else
            {
                if (!stream.CanRead)
                {
                    throw new ArgumentException("Document stream must support reading.", nameof(stream));
                }

                if (!stream.CanSeek)
                {
                    _stream        = new SeekableStream(stream, disposeStream);
                    _disposeStream = true;
                }
                else
                {
                    _stream        = stream;
                    _disposeStream = disposeStream;
                }
            }
            _streamLock = streamLock ?? new object();
        }
Exemplo n.º 2
0
            public void CanCloneWithNewValues()
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary();
                MetadataStack      metadata        = new MetadataStack(initialMetadata);

                // When
                metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", "a") });

                // Then
                Assert.AreEqual("a", metadata["A"]);
            }
Exemplo n.º 3
0
            public void ConvertStringToIntArray()
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary();
                MetadataStack      metadata        = new MetadataStack(initialMetadata);

                // When
                metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", "1") });
                IMetadata <int[]> metadataAs = metadata.MetadataAs <int[]>();

                // Then
                Assert.AreEqual("1", metadata["A"]);
                CollectionAssert.AreEqual(new int[] { 1 }, (IEnumerable)metadataAs["A"]);
            }
Exemplo n.º 4
0
            public void ConvertIntArrayToStringEnumerable()
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary();
                MetadataStack      metadata        = new MetadataStack(initialMetadata);

                // When
                metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", new int[] { 1, 2, 3 }) });
                IMetadata <IEnumerable <string> > metadataAs = metadata.MetadataAs <IEnumerable <string> >();

                // Then
                CollectionAssert.AreEqual(new int[] { 1, 2, 3 }, (IEnumerable)metadata["A"]);
                CollectionAssert.AreEqual(new string[] { "1", "2", "3" }, metadataAs["A"]);
            }
Exemplo n.º 5
0
            public void ConvertIntToString()
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary();
                MetadataStack      metadata        = new MetadataStack(initialMetadata);

                // When
                metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", 1) });
                IMetadata <string> metadataAs = metadata.MetadataAs <string>();

                // Then
                Assert.AreEqual(1, metadata["A"]);
                Assert.AreEqual("1", metadataAs["A"]);
            }
Exemplo n.º 6
0
            public void ReturnsCorrectDirectoryPathForDirectoryPath(string path, string expected)
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary();
                MetadataStack      metadata        = new MetadataStack(initialMetadata);

                // When
                metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", new DirectoryPath(path)) });
                object result = metadata.DirectoryPath("A");

                // Then
                Assert.IsInstanceOf <DirectoryPath>(result);
                Assert.AreEqual(expected, ((DirectoryPath)result).FullPath);
            }
Exemplo n.º 7
0
            public void ReturnsCorrectStringForFilePath(string path, string expected)
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary();
                MetadataStack      metadata        = new MetadataStack(initialMetadata);

                // When
                metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", new FilePath(path)) });
                object result = metadata.String("A");

                // Then
                Assert.IsInstanceOf <string>(result);
                Assert.AreEqual(expected, result);
            }
Exemplo n.º 8
0
            public void ContainsNewValues()
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary {
                    ["A"] = "a"
                };
                MetadataStack metadata = new MetadataStack(initialMetadata);

                // When
                MetadataStack clone = metadata.Clone(new Dictionary <string, object> {
                    { "B", "b" }
                });

                // Then
                Assert.AreEqual("b", clone["B"]);
            }
Exemplo n.º 9
0
            public void ClonedMetadataDoesNotContainNewValues()
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary {
                    ["A"] = "a"
                };
                MetadataStack metadata = new MetadataStack(initialMetadata);

                // When
                MetadataStack clone = metadata.Clone(new Dictionary <string, object> {
                    { "B", "b" }
                });

                // Then
                Assert.IsFalse(metadata.ContainsKey("B"));
            }
Exemplo n.º 10
0
            public void ReplacesValue()
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary {
                    ["A"] = "a"
                };
                MetadataStack metadata = new MetadataStack(initialMetadata);

                // When
                MetadataStack clone = metadata.Clone(new Dictionary <string, object> {
                    { "A", "b" }
                });

                // Then
                Assert.AreEqual("a", metadata["A"]);
                Assert.AreEqual("b", clone["A"]);
            }
Exemplo n.º 11
0
            public void ReturnsCorrectFilePathForString(string path, string expected)
            {
                // Given
                MetadataDictionary initialMetadata = new MetadataDictionary();
                MetadataStack      metadata        = new MetadataStack(initialMetadata);

                // When
                metadata = metadata.Clone(new[] { new KeyValuePair <string, object>("A", path) });
                object result = metadata.FilePath("A");

                // Then
                if (expected == null)
                {
                    Assert.IsNull(result);
                }
                else
                {
                    Assert.IsInstanceOf <FilePath>(result);
                    Assert.AreEqual(expected, ((FilePath)result).FullPath);
                }
            }