public void ReadWriteTest() { const string filename = "../../../Musagetes/Collaterals/Testing/ReadWriteTest.xml"; var songDbWrite = new SongDb(null); songDbWrite.AddSong( new Song("title", "location", 1234, new Bpm(4321, false), songDbWrite, 321)); songDbWrite.AddSong( new Song("title2", "location2", 7890, new Bpm(987, true), songDbWrite, 654)); var writer = new SongDbWriter(filename, songDbWrite); writer.WriteDb().Wait(); Assert.IsTrue(writer.WriteSuccessful); var songDbRead = new SongDb(null); var reader = new SongDbReader(filename, songDbRead); reader.ReadDb().Wait(); Assert.IsTrue(reader.ReadSuccessful); Assert.AreEqual(songDbRead.Songs.Count, songDbWrite.Songs.Count, "Reader/Writer song counts don't match"); foreach (var song in songDbWrite.Songs) { var localSong = song; var match = songDbRead.Songs.First(s => s.SongTitle.Equals(localSong.SongTitle)); var matchTags = songDbRead.SongTagDictionary[match]; var tags = songDbRead.SongTagDictionary[localSong]; foreach (var tag in tags) { Assert.IsTrue(matchTags.Count(t => t.TagName.Equals(tag.TagName)) == 1, "Read/Write dictionary have different tags"); } Assert.AreEqual(localSong.Location, match.Location, "Locations don't match"); Assert.AreEqual(localSong.Milliseconds, match.Milliseconds, "Milliseconds don't match"); Assert.AreEqual(localSong.PlayCount, match.PlayCount, "Playcounts don't match"); Assert.AreEqual(localSong.Bpm.Guess, match.Bpm.Guess, "BPM Guesses don't match"); Assert.AreEqual(localSong.Bpm.Value, match.Bpm.Value, "BPM Values don't match"); } }
private async Task ReadSongsAsync(XmlReader reader) { await reader.ReadAsync(); while (reader.LocalName.Equals(Constants.Db.Song)) { Logger.Debug("Reading a song"); UInt32 id; if (!UInt32.TryParse(reader.GetAttribute(Constants.Db.Id), out id)) { Logger.Debug("Could not read song id"); await reader.ReadAsync(); continue; } if (reader.IsEmptyElement) { Logger.Debug("Song element is empty"); await reader.ReadAsync(); continue; } await reader.ReadAsync(); if (reader.IsEndElement(Constants.Db.Song)) { Logger.Debug("Song element is followed by end element"); reader.ReadEndElement(); continue; } reader.ConfirmElement(Constants.Db.SongTitle); var title = await reader.TryGetContentAsync(); reader.ConfirmElement(Constants.Db.Location); var location = await reader.TryGetContentAsync(); reader.ConfirmElement(Constants.Db.Milliseconds); int milliseconds; if (!int.TryParse(await reader.TryGetContentAsync(), out milliseconds)) { Logger.Error("Song {0} has a missing or unreadable millisecond value", title); } reader.ConfirmElement(Constants.Db.PlayCount); uint playCount; if (!uint.TryParse(await reader.TryGetContentAsync(), out playCount)) { Logger.Error("Song {0} has a missing or unreadable playcount", title); } reader.ConfirmElement(Constants.Db.Bpm); var guess = Convert.ToBoolean(reader.GetAttribute(Constants.Db.Guess)); int bpmValue; if (!int.TryParse(await reader.TryGetContentAsync(), out bpmValue)) { Logger.Error("Song {0} has a missing or unreadable BPM value", title); } var song = new Song(title, location, milliseconds, new Bpm(bpmValue, guess), playCount, id); SongDb.AddSong(song); reader.ConfirmElement(Constants.Db.Tags); if (reader.IsEmptyElement) { await reader.ReadAsync(); } else { await ReadSongTagsAsync(reader, song); reader.ReadEndElement(); } reader.ReadEndElement(); } }