示例#1
0
        /// <summary>
        /// Parses the supplied XML into an instance of
        /// <see cref="XmlAtomContent" />.
        /// </summary>
        /// <param name="xml">
        /// A string of characters containing the XML representation of an Atom
        /// Content element.
        /// </param>
        /// <param name="serializer">
        /// The <see cref="IContentSerializer" /> used to deserialize the XML
        /// into an instance of the correct object type.
        /// </param>
        /// <returns>
        /// A new instance of <see cref="XmlAtomContent" /> containing the data
        /// from the supplied <paramref name="xml" />.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="serializer" /> is <see langword="null" />.
        /// </exception>
        public static XmlAtomContent Parse(
            string xml,
            IContentSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            var sr = new StringReader(xml);

            try
            {
                using (var r = XmlReader.Create(sr))
                {
                    sr = null;
                    return(XmlAtomContent.ReadFrom(r, serializer));
                }
            }
            finally
            {
                if (sr != null)
                {
                    sr.Dispose();
                }
            }
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AtomEntry"/> class.
        /// </summary>
        /// <param name="id">The ID of the entry.</param>
        /// <param name="title">The title of the entry.</param>
        /// <param name="published">The date and time of publication.</param>
        /// <param name="updated">
        /// The date and time the entry was last updated.
        /// </param>
        /// <param name="author">The author of the entry.</param>
        /// <param name="content">The content of the entry.</param>
        /// <param name="links">The links of the entry.</param>
        /// <remarks>
        /// <para>
        /// The constructor arguments are subsequently available on the object
        /// as properties.
        /// </para>
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="title" />
        /// or
        /// <paramref name="author" />
        /// or
        /// <paramref name="content" />
        /// or
        /// <paramref name="content" /> is <see langword="null" />.
        /// </exception>
        public AtomEntry(
            UuidIri id,
            string title,
            DateTimeOffset published,
            DateTimeOffset updated,
            AtomAuthor author,
            XmlAtomContent content,
            IEnumerable <AtomLink> links)
        {
            if (title == null)
            {
                throw new ArgumentNullException("title");
            }
            if (author == null)
            {
                throw new ArgumentNullException("author");
            }
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            if (links == null)
            {
                throw new ArgumentNullException("links");
            }

            this.id        = id;
            this.title     = title;
            this.published = published;
            this.updated   = updated;
            this.author    = author;
            this.content   = content;
            this.links     = links;
        }
 public void ItemIsCorrect(
     [Frozen]object expected,
     XmlAtomContent sut)
 {
     var actual = sut.Item;
     Assert.Equal(expected, actual);
 }
        public void GetHashCodeReturnsCorrectResult(XmlAtomContent sut)
        {
            var actual = sut.GetHashCode();

            var expected = sut.Item.GetHashCode();
            Assert.Equal(expected, actual);
        }
示例#5
0
 /// <summary>
 /// Returns a new instance of <see cref="AtomEntry" /> with the
 /// supplied content, but all other values held constant.
 /// </summary>
 /// <param name="newContent">The new content.</param>
 /// <returns>
 /// A new instance of <see cref="AtomEntry" /> with the supplied
 /// content, but all other values held constant.
 /// </returns>
 /// <seealso cref="Content" />
 public AtomEntry WithContent(XmlAtomContent newContent)
 {
     return(new AtomEntry(
                this.id,
                this.title,
                this.published,
                this.updated,
                this.author,
                newContent,
                this.links));
 }
 public void ReadFromReturnsCorrectResult(
     XmlContentSerializer serializer,
     XmlAtomContent seed,
     XmlAttributedTestEventX tex)
 {
     var expected = seed.WithItem(tex);
     using (var sr = new StringReader(expected.ToXmlString(serializer)))
     using (var r = XmlReader.Create(sr))
     {
         XmlAtomContent actual = XmlAtomContent.ReadFrom(r, serializer);
         Assert.Equal(expected, actual);
     }
 }
示例#7
0
        /// <summary>
        /// Creates an <see cref="AtomEntry" /> instance from XML.
        /// </summary>
        /// <param name="xmlReader">
        /// The <see cref="XmlReader" /> containing the XML representation of
        /// the Atom Entry.
        /// </param>
        /// <param name="serializer">
        /// The <see cref="IContentSerializer" /> used to serialize custom XML
        /// content.
        /// </param>
        /// <returns>
        /// A new instance of <see cref="AtomEntry" /> containing the data from
        /// the XML representation of the Atom Entry contained in
        /// <paramref name="xmlReader" />.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="serializer" /> is <see langword="null" />.
        /// </exception>
        public static AtomEntry ReadFrom(
            XmlReader xmlReader,
            IContentSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            var navigator = new XPathDocument(xmlReader).CreateNavigator();

            var resolver = new XmlNamespaceManager(new NameTable());

            resolver.AddNamespace("atom", "http://www.w3.org/2005/Atom");

            var id = navigator
                     .Select("/atom:entry/atom:id", resolver).Cast <XPathNavigator>()
                     .Single().Value;
            var title = navigator
                        .Select("/atom:entry/atom:title[@type = 'text']", resolver).Cast <XPathNavigator>()
                        .Single().Value;
            var published = navigator
                            .Select("/atom:entry/atom:published", resolver).Cast <XPathNavigator>()
                            .Single().Value;
            var updated = navigator
                          .Select("/atom:entry/atom:updated", resolver).Cast <XPathNavigator>()
                          .Single().Value;
            var author = navigator
                         .Select("/atom:entry/atom:author", resolver).Cast <XPathNavigator>()
                         .Single().ReadSubtree();
            var content = navigator
                          .Select("/atom:entry/atom:content[@type = 'application/xml']", resolver).Cast <XPathNavigator>()
                          .Single().ReadSubtree();
            var links = navigator
                        .Select("/atom:entry/atom:link", resolver).Cast <XPathNavigator>();

            return(new AtomEntry(
                       UuidIri.Parse(id),
                       title,
                       DateTimeOffset.Parse(published, CultureInfo.InvariantCulture),
                       DateTimeOffset.Parse(updated, CultureInfo.InvariantCulture),
                       AtomAuthor.ReadFrom(author),
                       XmlAtomContent.ReadFrom(content, serializer),
                       links.Select(x => AtomLink.ReadFrom(x.ReadSubtree()))));
        }
        public void WithContentReturnsCorrectResult(
            AtomEntry sut,
            XmlAtomContent newContent)
        {
            AtomEntry actual = sut.WithContent(newContent);

            var expected = sut.AsSource().OfLikeness<AtomEntry>()
                .With(x => x.Content).EqualsWhen(
                    (s, d) => object.Equals(newContent, d.Content));
            expected.ShouldEqual(actual);
        }
        public void SutCanRoundTripToString(
            XmlContentSerializer serializer,
            XmlAtomContent seed,
            XmlAttributedTestEventX tex)
        {
            var expected = seed.WithItem(tex);
            var xml = expected.ToXmlString(serializer);

            XmlAtomContent actual = XmlAtomContent.Parse(xml, serializer);

            Assert.Equal(expected, actual);
        }
        public void WriteToXmlWriterWritesCorrectXml(
            XmlContentSerializer serializer,
            XmlAtomContent content,
            AtomEntry entry,
            XmlAttributedTestEventX tex)
        {
            // Fixture setup
            var sb = new StringBuilder();
            using (var w = XmlWriter.Create(sb))
            {
                var sut = content.WithItem(tex);

                // Exercise system
                sut.WriteTo(w, serializer);

                // Verify outcome
                w.Flush();

                var expected = XDocument.Parse(
                    "<content type=\"application/xml\" xmlns=\"http://www.w3.org/2005/Atom\">" +
                    "  <test-event-x xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://grean:rocks\">" +
                    "    <number>" + tex.Number + "</number>" +
                    "    <text>" + tex.Text + "</text>" +
                    "  </test-event-x>" +
                    "</content>");

                var actual = XDocument.Parse(sb.ToString());
                Assert.Equal(expected, actual, new XNodeEqualityComparer());

                // Teardown
            }
        }
        public void WithItemReturnsCorrectResult(
            XmlAtomContent sut,
            object newItem)
        {
            XmlAtomContent actual = sut.WithItem(newItem);

            var expected = actual.AsSource().OfLikeness<XmlAtomContent>()
                .With(x => x.Item).EqualsWhen(
                    (s, d) => object.Equals(newItem, s.Item));
            expected.ShouldEqual(actual);
        }
 public void SutIsXmlWritable(XmlAtomContent sut)
 {
     Assert.IsAssignableFrom<IXmlWritable>(sut);
 }
 public void SutIsNotEqualToAnonymousObject(
     XmlAtomContent sut,
     object anonymous)
 {
     var actual = sut.Equals(anonymous);
     Assert.False(actual);
 }
 public void SutEqualsIdenticalOther(XmlAtomContent sut)
 {
     var other = sut.WithItem(sut.Item);
     var actual = sut.Equals(other);
     Assert.True(actual);
 }
 public void SutDoesNotEqualDifferentOther(
     XmlAtomContent sut,
     XmlAtomContent other)
 {
     var actual = sut.Equals(other);
     Assert.False(actual);
 }