示例#1
0
        public void WriteTest()
        {
            StatisticsWriter target            = new StatisticsWriter();
            string           referenceFilename = "Resources/statistics/statistics.xml";
            string           filename          = "Resources/statistics/statistics-neu.xml";

            Statistics expected = new Statistics();

            var date = new StatisticsDate(2012, 12, 15);
            var item = new StatisticsItem
            {
                Type      = StatisticsItemType.Song,
                Title     = "You are so faithful",
                Copyright = "Musik & Copyright unbekannt",
                CcliId    = "",
                Count     = 1
            };

            date.Items.Add(item.Identifier, item);
            expected.Dates.Add(date.Identifier, date);

            date = new StatisticsDate(2013, 1, 6);

            item = new StatisticsItem
            {
                Type      = StatisticsItemType.Song,
                Title     = "A Mighty Fortress Is Our God",
                Copyright = "Public Domain",
                CcliId    = "42964",
                Count     = 2
            };
            date.Items.Add(item.Identifier, item);

            item = new StatisticsItem
            {
                Type      = StatisticsItemType.Song,
                Title     = "You are so faithful",
                Copyright = "Musik & Copyright unbekannt",
                CcliId    = "",
                Count     = 1
            };
            date.Items.Add(item.Identifier, item);

            expected.Dates.Add(date.Identifier, date);

            target.Write(filename, expected);

            Assert.IsTrue(FileUtils.FileEquals(filename, referenceFilename));
        }
        public Statistics read(string filename)
        {
            var xmlDoc = new XmlDocument();

            xmlDoc.Load(filename);
            var xmlRoot = xmlDoc.DocumentElement;

            if (xmlRoot.Name != "statistics" ||
                (xmlRoot.Attributes["version"] == null || xmlRoot.Attributes["version"].Value != "1.0"))
            {
                throw new Exception("Ungültige Statistikdatei!");
            }
            var sl = new Statistics();

            for (var i = 0; i < xmlRoot.ChildNodes.Count; i++)
            {
                var node = xmlRoot.ChildNodes[i];
                if (node.Name == "date")
                {
                    var year  = int.Parse(node.Attributes["year"].Value);
                    var month = int.Parse(node.Attributes["month"].Value);
                    var day   = int.Parse(node.Attributes["day"].Value);
                    var date  = new StatisticsDate(year, month, day);
                    sl.Dates.Add(date.Identifier, date);

                    for (var j = 0; j < node.ChildNodes.Count; j++)
                    {
                        var cnode = node.ChildNodes[j];
                        if (cnode.Name == "song")
                        {
                            var item = new StatisticsItem
                            {
                                Type      = StatisticsItemType.Song,
                                Title     = cnode.Attributes["title"].Value,
                                CcliId    = cnode.Attributes["ccli"].Value,
                                Copyright = cnode.Attributes["copyright"].Value,
                                Count     = int.Parse(cnode.Attributes["count"].Value)
                            };
                            date.Items.Add(item.Identifier, item);
                        }
                    }
                }
            }
            return(sl);
        }
        public void ReadTest()
        {
            StatisticsReader target   = new StatisticsReader();
            string           filename = "Resources/statistics/statistics.xml";

            Statistics expected = new Statistics();

            var date = new StatisticsDate(2012, 12, 15);
            var item = new StatisticsItem
            {
                Type      = StatisticsItemType.Song,
                Title     = "You are so faithful",
                Copyright = "Musik & Copyright unbekannt",
                CcliId    = "",
                Count     = 1
            };

            date.Items.Add(item.Identifier, item);
            expected.Dates.Add(date.Identifier, date);

            date = new StatisticsDate(2013, 1, 6);

            item = new StatisticsItem
            {
                Type      = StatisticsItemType.Song,
                Title     = "A Mighty Fortress Is Our God",
                Copyright = "Public Domain",
                CcliId    = "42964",
                Count     = 2
            };
            date.Items.Add(item.Identifier, item);

            item = new StatisticsItem
            {
                Type      = StatisticsItemType.Song,
                Title     = "You are so faithful",
                Copyright = "Musik & Copyright unbekannt",
                CcliId    = "",
                Count     = 1
            };
            date.Items.Add(item.Identifier, item);

            expected.Dates.Add(date.Identifier, date);

            Statistics actual = target.read(filename);

            foreach (var edate in expected.Dates)
            {
                Assert.AreEqual(edate.Value.Identifier, actual.Dates[edate.Key].Identifier);
                Assert.AreEqual(edate.Value.Year, actual.Dates[edate.Key].Year);
                Assert.AreEqual(edate.Value.Month, actual.Dates[edate.Key].Month);
                Assert.AreEqual(edate.Value.Day, actual.Dates[edate.Key].Day);
                Assert.AreEqual(edate.Value.Items.Count, actual.Dates[edate.Key].Items.Count);
                foreach (var eitem in edate.Value.Items)
                {
                    Assert.AreEqual(eitem.Value.CcliId, actual.Dates[edate.Key].Items[eitem.Key].CcliId);
                    Assert.AreEqual(eitem.Value.Title, actual.Dates[edate.Key].Items[eitem.Key].Title);
                    Assert.AreEqual(eitem.Value.Copyright, actual.Dates[edate.Key].Items[eitem.Key].Copyright);
                    Assert.AreEqual(eitem.Value.Count, actual.Dates[edate.Key].Items[eitem.Key].Count);
                    Assert.AreEqual(eitem.Value.Type, actual.Dates[edate.Key].Items[eitem.Key].Type);
                }
            }
        }