public void op_ToString_whenRecordNullXml()
        {
            var key    = AlphaDecimal.Random();
            var record = new Record <string>
            {
                Cacheability = "public",
                Created      = new DateTime(1999, 12, 31, 01, 00, 00, 00),
                Etag         = "\"xyz\"",
                Expiration   = "P1D",
                Key          = key,
                Modified     = new DateTime(2001, 12, 31, 01, 00, 00, 00),
                Status       = 200,
                Urn          = "urn://example.com/abc",
                Value        = null
            };

            var obj = new RecordFile(record);

            var expected = new StringBuilder();

            expected.AppendLine("urn: urn://example.com/abc");
            expected.AppendLine("key: " + key);
            expected.AppendLine("etag: \"xyz\"");
            expected.AppendLine("created: 1999-12-31T01:00:00Z");
            expected.AppendLine("modified: 2001-12-31T01:00:00Z");
            expected.AppendLine("cacheability: public");
            expected.AppendLine("expiration: P1D");
            expected.AppendLine("status: 200");
            expected.AppendLine(string.Empty);

            var actual = obj.ToString();

            Assert.Equal(expected.ToString(), actual);
        }
        public void op_ToString()
        {
            var obj = new RecordFile
            {
                Body = "<root />"
            };

            obj.Headers["urn"]  = "urn://example.com/abc";
            obj.Headers["etag"] = "\"xyz\"";

            var expected = new StringBuilder();

            expected.AppendLine("urn: urn://example.com/abc");
            expected.AppendLine("key: ");
            expected.AppendLine("etag: \"xyz\"");
            expected.AppendLine("created: ");
            expected.AppendLine("modified: ");
            expected.AppendLine("cacheability: ");
            expected.AppendLine("expiration: ");
            expected.AppendLine("status: ");
            expected.AppendLine(string.Empty);
            expected.Append("<root />");

            var actual = obj.ToString();

            Assert.Equal(expected.ToString(), actual);
        }
        public void op_ToXml()
        {
            const string expected = "<root />";

            var obj = new RecordFile
            {
                Body = expected
            };

            var actual = obj.ToXml().CreateNavigator().OuterXml;

            Assert.Equal(expected, actual);
        }
        public void op_ToRecordOfT()
        {
            var expected = new Record <int>
            {
                Cacheability = "public",
                Created      = new DateTime(1999, 12, 31, 01, 00, 00, 00),
                Etag         = "\"xyz\"",
                Expiration   = "P1D",
                Key          = AlphaDecimal.Random(),
                Modified     = new DateTime(2001, 12, 31, 01, 00, 00, 00),
                Status       = 200,
                Urn          = "urn://example.com/abc",
                Value        = 123
            };

            var obj = new RecordFile(expected);

            var actual = obj.ToRecord <int>();

            Assert.Equal(expected, actual);
        }
        public void op_Save_FileSystemInfo()
        {
            var record = new Record <int>
            {
                Cacheability = "public",
                Created      = new DateTime(1999, 12, 31, 01, 00, 00, 00),
                Etag         = "\"xyz\"",
                Expiration   = "P1D",
                Key          = AlphaDecimal.Random(),
                Modified     = new DateTime(2001, 12, 31, 01, 00, 00, 00),
                Status       = 200,
                Urn          = "urn://example.com/abc",
                Value        = 123
            };

            var expected = new StringBuilder();

            expected.AppendLine("urn: urn://example.com/abc");
            expected.AppendLine("key: " + record.Key);
            expected.AppendLine("etag: \"xyz\"");
            expected.AppendLine("created: 1999-12-31T01:00:00Z");
            expected.AppendLine("modified: 2001-12-31T01:00:00Z");
            expected.AppendLine("cacheability: public");
            expected.AppendLine("expiration: P1D");
            expected.AppendLine("status: 200");
            expected.AppendLine(string.Empty);
            expected.Append("<int>123</int>");

            string actual;

            using (var root = new TempDirectory())
            {
                var obj = new RecordFile(record);
                obj.Save(root.Info);

                actual = new FileInfo(obj.Location.FullName).ReadToEnd();
            }

            Assert.Equal(expected.ToString(), actual);
        }
        public void op_Load_FileSystemInfo()
        {
            var key      = AlphaDecimal.Random();
            var expected = new Record <int>
            {
                Cacheability = "public",
                Created      = new DateTime(1999, 12, 31, 01, 00, 00, 00),
                Etag         = "\"xyz\"",
                Expiration   = "P1D",
                Key          = key,
                Modified     = new DateTime(2001, 12, 31, 01, 00, 00, 00),
                Status       = 200,
                Urn          = "urn://example.com/abc",
                Value        = 123
            };

            using (var file = new TempFile())
            {
                using (var stream = file.Info.Open(FileMode.Truncate, FileAccess.Write, FileShare.Read))
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.WriteLine("urn: urn://example.com/abc");
                        writer.WriteLine("key: " + key);
                        writer.WriteLine("etag: \"xyz\"");
                        writer.WriteLine("created: 1999-12-31T01:00:00Z");
                        writer.WriteLine("modified: 2001-12-31T01:00:00Z");
                        writer.WriteLine("cacheability: public");
                        writer.WriteLine("expiration: P1D");
                        writer.WriteLine("status: 200");
                        writer.WriteLine(string.Empty);
                        writer.Write("<int>123</int>");
                    }
                }

                var actual = RecordFile.Load(file.Info).ToRecord <int>();

                Assert.Equal(expected, actual);
            }
        }
        public static RecordFile Load(FileSystemInfo file)
        {
            if (null == file)
            {
                throw new ArgumentNullException("file");
            }

            var result = new RecordFile();

            using (var stream = new FileInfo(file.FullName)
                                .Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var reader = new StreamReader(stream))
                {
                    while (true)
                    {
                        var line = reader.ReadLine();
                        if (null == line)
                        {
                            break;
                        }

                        if (0 == line.Length)
                        {
                            result.Body = reader.ReadToEnd();
                            break;
                        }

                        var colon = line.IndexOf(':');
                        result.Headers[line.Substring(0, colon)] = line.Substring(colon + 1).Trim();
                    }
                }
            }

            return(result);
        }
 public void op_Load_FileSystemInfoNull()
 {
     Assert.Throws <ArgumentNullException>(() => RecordFile.Load(null));
 }