コード例 #1
0
        public void SerializeToXML_GeneratesXMLString()
        {
            //Arrange
            bool                 actualResult   = false;
            bool                 expectedResult = true;
            PrivateType          pt             = new PrivateType(typeof(Program));
            RUBIObjectCollection collection     = (RUBIObjectCollection)pt.InvokeStatic("DummyData", null);

            //Act
            string serializedXml = collection.SerializeToXML();

            try
            {
                System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Parse(serializedXml);

                actualResult = true;
            }
            catch
            {
                actualResult = false;
            }

            //Assert
            Assert.AreEqual(actualResult, expectedResult);
        }
コード例 #2
0
        public static string SerializeToXML(this RUBIObjectCollection source)
        {
            //Create a string writer in order to output to console as opposed to file
            using (var sw = new StringWriter())
            {
                //Settings to configure the way the XML will be output to the console. Really, only Indent = true; is needed.
                var settings = new XmlWriterSettings();
                settings.NewLineChars    = Environment.NewLine;
                settings.IndentChars     = "  ";
                settings.NewLineHandling = NewLineHandling.Replace;
                settings.Indent          = true;

                //Create writer that writes the xml to the string writer object
                using (var xw = XmlWriter.Create(sw, settings))
                {
                    //Create serializer that can serialize a collection of RUBIObjects
                    XmlSerializer serializer =
                        new XmlSerializer(typeof(RUBIObjectCollection));

                    //Serialize this instance of a RUBICollection object, into XML and write to the string writer output
                    serializer.Serialize(xw, source);

                    //Flush the xmlwriter stream as it isn't needed any longer
                    xw.Flush();
                }

                //Return the XML as a formatted string
                return(sw.ToString());
            }
        }
コード例 #3
0
        public void DummyData_TestDataCreated()
        {
            //Arrange
            PrivateType pt = new PrivateType(typeof(Program));

            //Act
            RUBIObjectCollection collection = (RUBIObjectCollection)pt.InvokeStatic("DummyData", null);
            int actualResult   = collection.Objects.Count;
            int expectedResult = 10;

            //Assert
            Assert.AreEqual(actualResult, expectedResult);
        }
コード例 #4
0
        public static RUBIObjectCollection DeserializeToCollection(this string source)
        {
            RUBIObjectCollection collection = null;
            XmlSerializer        serializer = null;

            //Read the XML string into a stream.
            using (var sr = new StringReader(source))
            {
                //Instantiate an XML Serializer to expect a collection of RUBI Objects
                serializer = new XmlSerializer(typeof(RUBIObjectCollection));

                //Deserialize the XML stream to a collection
                collection = (RUBIObjectCollection)serializer.Deserialize(sr);
            }

            return(collection);
        }
コード例 #5
0
    /// <summary>
    /// Generates dummy data for testing purposes
    /// </summary>
    /// <returns>A collection of RUBIObjects</returns>
    private static RUBIObjectCollection DummyData()
    {
        Random random     = new Random();
        var    collection = new RUBIObjectCollection();

        //Build a collection of RUBIObjects and instantiate them with semi-random data.
        for (int i = 0; i < 10; i++)
        {
            int month = random.Next(1, 12);             //Random month as an integer
            int year  = random.Next(2010, 2015);        //Random year as an integer

            //Create object and add to collection.
            collection.Objects.Add(new RUBIObject()
            {
                ID          = Guid.NewGuid(),
                Name        = string.Format("Object{0}", i),
                Description = "Description",
                CreatedOn   = new DateTime(year, month, 1)
            });
        }

        return(collection);
    }