From 2bd866f590b37d5c289054d220bec1bdf4d0726e Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 22 Dec 2012 17:18:05 -0800 Subject: [PATCH] Better handling of xml errors on tvrage --- .../SortHelperFixture.cs} | 2 +- .../ConvertToDayOfWeekFixture.cs} | 14 ++-- .../XElementHelperTests/ConvertToTFixture.cs | 70 +++++++++++++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 3 +- NzbDrone.Core/Helpers/XElementHelper.cs | 51 ++++++++++++++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + NzbDrone.Core/Providers/TvRageProvider.cs | 66 +++++------------ 7 files changed, 152 insertions(+), 55 deletions(-) rename NzbDrone.Core.Test/{SortHelperTest.cs => HelperTests/SortHelperFixture.cs} (96%) rename NzbDrone.Core.Test/{ProviderTests/TvRageProviderTests/ParseDayOfWeekFixture.cs => HelperTests/XElementHelperTests/ConvertToDayOfWeekFixture.cs} (72%) create mode 100644 NzbDrone.Core.Test/HelperTests/XElementHelperTests/ConvertToTFixture.cs create mode 100644 NzbDrone.Core/Helpers/XElementHelper.cs diff --git a/NzbDrone.Core.Test/SortHelperTest.cs b/NzbDrone.Core.Test/HelperTests/SortHelperFixture.cs similarity index 96% rename from NzbDrone.Core.Test/SortHelperTest.cs rename to NzbDrone.Core.Test/HelperTests/SortHelperFixture.cs index e85045c46..19f6d028a 100644 --- a/NzbDrone.Core.Test/SortHelperTest.cs +++ b/NzbDrone.Core.Test/HelperTests/SortHelperFixture.cs @@ -12,7 +12,7 @@ using NzbDrone.Core.Providers; using NzbDrone.Core.Repository; using NzbDrone.Core.Test.Framework; -namespace NzbDrone.Core.Test +namespace NzbDrone.Core.Test.HelperTests { [TestFixture] // ReSharper disable InconsistentNaming diff --git a/NzbDrone.Core.Test/ProviderTests/TvRageProviderTests/ParseDayOfWeekFixture.cs b/NzbDrone.Core.Test/HelperTests/XElementHelperTests/ConvertToDayOfWeekFixture.cs similarity index 72% rename from NzbDrone.Core.Test/ProviderTests/TvRageProviderTests/ParseDayOfWeekFixture.cs rename to NzbDrone.Core.Test/HelperTests/XElementHelperTests/ConvertToDayOfWeekFixture.cs index 95cf3699d..3fe30360a 100644 --- a/NzbDrone.Core.Test/ProviderTests/TvRageProviderTests/ParseDayOfWeekFixture.cs +++ b/NzbDrone.Core.Test/HelperTests/XElementHelperTests/ConvertToDayOfWeekFixture.cs @@ -8,13 +8,14 @@ using FluentAssertions; using NUnit.Framework; using Ninject; using NzbDrone.Common; +using NzbDrone.Core.Helpers; using NzbDrone.Core.Providers; using NzbDrone.Core.Test.Framework; using NzbDrone.Test.Common; using TvdbLib.Data; using TvdbLib.Exceptions; -namespace NzbDrone.Core.Test.ProviderTests.TvRageProviderTests +namespace NzbDrone.Core.Test.HelperTests.XElementHelperTests { [TestFixture] // ReSharper disable InconsistentNaming @@ -23,25 +24,26 @@ namespace NzbDrone.Core.Test.ProviderTests.TvRageProviderTests [Test] public void should_return_null_if_xelement_is_null() { - Mocker.Resolve().ParseDayOfWeek(null).Should().Be(null); + XElement test = null; + test.ConvertToDayOfWeek().Should().Be(null); } [Test] public void should_return_null_if_value_is_null() { - Mocker.Resolve().ParseDayOfWeek(new XElement("airday", null)).Should().Be(null); + new XElement("airday", null).ConvertToDayOfWeek().Should().Be(null); } [Test] public void should_return_null_if_value_is_empty() { - Mocker.Resolve().ParseDayOfWeek(new XElement("airday", "")).Should().Be(null); + new XElement("airday", "").ConvertToDayOfWeek().Should().Be(null); } [Test] public void should_return_null_if_value_is_daily() { - Mocker.Resolve().ParseDayOfWeek(new XElement("airday", "Daily")).Should().Be(null); + new XElement("airday", "Daily").ConvertToDayOfWeek().Should().Be(null); } [Test] @@ -59,7 +61,7 @@ namespace NzbDrone.Core.Test.ProviderTests.TvRageProviderTests [TestCase("Saturday", DayOfWeek.Saturday)] public void should_return_dayOfWeek_when_it_is_valid(string value, DayOfWeek expected) { - Mocker.Resolve().ParseDayOfWeek(new XElement("airday", value)).Should().Be(expected); + new XElement("airday", value).ConvertToDayOfWeek().Should().Be(expected); } } } \ No newline at end of file diff --git a/NzbDrone.Core.Test/HelperTests/XElementHelperTests/ConvertToTFixture.cs b/NzbDrone.Core.Test/HelperTests/XElementHelperTests/ConvertToTFixture.cs new file mode 100644 index 000000000..71256c40d --- /dev/null +++ b/NzbDrone.Core.Test/HelperTests/XElementHelperTests/ConvertToTFixture.cs @@ -0,0 +1,70 @@ +// ReSharper disable RedundantUsingDirective +using System; +using System.Collections.Generic; +using System.Xml.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.Helpers; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers; +using NzbDrone.Core.Repository; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.HelperTests.XElementHelperTests +{ + [TestFixture] + // ReSharper disable InconsistentNaming + public class XElementHelperTest : CoreTest + { + [Test] + public void Int32_should_return_zero_when_xelement_is_null() + { + XElement test = null; + + test.ConvertTo().Should().Be(0); + } + + [Test] + public void Int32_should_return_zero_when_value_is_null() + { + new XElement("test", null).ConvertTo().Should().Be(0); + } + + [Test] + public void Int32_should_return_value_when_value_is_an_int() + { + new XElement("test", 10).ConvertTo().Should().Be(10); + } + + [Test] + public void Nullable_Int32_should_return_null_when_xelement_is_null() + { + XElement test = null; + + test.ConvertTo>().Should().Be(null); + } + + [Test] + public void DateTime_should_return_zero_when_xelement_is_null() + { + XElement test = null; + + test.ConvertTo().Should().Be(DateTime.MinValue); + } + + [Test] + public void DateTime_should_return_zero_when_value_is_null() + { + new XElement("test", null).ConvertTo().Should().Be(DateTime.MinValue); + } + + [Test] + public void DateTime_should_return_value_when_value_is_a_date() + { + var date = DateTime.Today; + new XElement("test", date.ToString()).ConvertTo().Should().Be(date); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index f3aa10b36..4c8b55320 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -139,6 +139,7 @@ + @@ -207,7 +208,7 @@ - + diff --git a/NzbDrone.Core/Helpers/XElementHelper.cs b/NzbDrone.Core/Helpers/XElementHelper.cs new file mode 100644 index 000000000..d455474dd --- /dev/null +++ b/NzbDrone.Core/Helpers/XElementHelper.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Xml.Linq; + +namespace NzbDrone.Core.Helpers +{ + public static class XElementHelper + { + public static T ConvertTo(this XElement element) + { + if (element == null) + return default(T); + + if (String.IsNullOrEmpty(element.Value)) + return default(T); + + var converter = TypeDescriptor.GetConverter(typeof(T)); + try + { + return (T)converter.ConvertFromString(element.Value); + } + + catch + { + return default(T); + } + } + + public static DayOfWeek? ConvertToDayOfWeek(this XElement element) + { + if (element == null) + return null; + + if (String.IsNullOrWhiteSpace(element.Value)) + return null; + + try + { + return (DayOfWeek)Enum.Parse(typeof(DayOfWeek), element.Value); + } + catch (Exception) + { + } + + return null; + } + } +} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 402f146d9..406132b38 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -262,6 +262,7 @@ + diff --git a/NzbDrone.Core/Providers/TvRageProvider.cs b/NzbDrone.Core/Providers/TvRageProvider.cs index 6347afdae..3570de1d2 100644 --- a/NzbDrone.Core/Providers/TvRageProvider.cs +++ b/NzbDrone.Core/Providers/TvRageProvider.cs @@ -6,6 +6,7 @@ using System.Xml.Linq; using NLog; using Ninject; using NzbDrone.Common; +using NzbDrone.Core.Helpers; using NzbDrone.Core.Model.TvRage; namespace NzbDrone.Core.Providers @@ -40,27 +41,22 @@ namespace NzbDrone.Core.Providers try { var show = new TvRageSearchResult(); - show.ShowId = Int32.Parse(s.Element("showid").Value); + show.ShowId = s.Element("showid").ConvertTo(); show.Name = s.Element("name").Value; show.Link = s.Element("link").Value; show.Country = s.Element("country").Value; - DateTime started; - if (DateTime.TryParse(s.Element("started").Value, out started)) ; - show.Started = started; - - DateTime ended; - if (DateTime.TryParse(s.Element("ended").Value, out ended)) ; - show.Ended = ended; + show.Started = s.Element("started").ConvertTo(); + show.Ended = s.Element("ended").ConvertTo(); if (show.Ended < new DateTime(1900, 1, 1)) show.Ended = null; - show.Seasons = Int32.Parse(s.Element("seasons").Value); + show.Seasons = s.Element("seasons").ConvertTo(); show.Status = s.Element("status").Value; - show.RunTime = Int32.Parse(s.Element("runtime").Value); - show.AirTime = DateTime.Parse(s.Element("airtime").Value); - show.AirDay = ParseDayOfWeek(s.Element("airday")); + show.RunTime = s.Element("seasons").ConvertTo(); + show.AirTime = s.Element("seasons").ConvertTo(); + show.AirDay = s.Element("airday").ConvertToDayOfWeek(); searchResults.Add(show); } @@ -89,26 +85,21 @@ namespace NzbDrone.Core.Providers } var show = new TvRageSeries(); - show.ShowId = Int32.Parse(s.Element("showid").Value); + show.ShowId = s.Element("showid").ConvertTo(); show.Name = s.Element("showname").Value; show.Link = s.Element("showlink").Value; - show.Seasons = Int32.Parse(s.Element("seasons").Value); - show.Started = Int32.Parse(s.Element("started").Value); + show.Seasons = s.Element("seasons").ConvertTo(); + show.Started = s.Element("started").ConvertTo(); - DateTime startDate; - if (DateTime.TryParse(s.Element("startdate").Value, out startDate)) ; - show.StartDate = startDate; - - DateTime ended; - if (DateTime.TryParse(s.Element("ended").Value, out ended)) ; - show.Ended = ended; + show.StartDate = s.Element("startdate").ConvertTo(); + show.Ended = s.Element("ended").ConvertTo(); show.OriginCountry = s.Element("origin_country").Value; show.Status = s.Element("status").Value; - show.RunTime = Int32.Parse(s.Element("runtime").Value); + show.RunTime = s.Element("runtime").ConvertTo(); show.Network = s.Element("network").Value; - show.AirTime = DateTime.Parse(s.Element("airtime").Value); - show.AirDay = ParseDayOfWeek(s.Element("airday")); + show.AirTime = s.Element("airtime").ConvertTo(); + show.AirDay = s.Element("airday").ConvertToDayOfWeek(); show.UtcOffset = GetUtcOffset(s.Element("timezone").Value); return show; } @@ -139,10 +130,10 @@ namespace NzbDrone.Core.Providers try { var episode = new TvRageEpisode(); - episode.EpisodeNumber = Int32.Parse(e.Element("epnum").Value); - episode.SeasonNumber = Int32.Parse(e.Element("seasonnum").Value); + episode.EpisodeNumber = e.Element("epnum").ConvertTo(); + episode.SeasonNumber = e.Element("seasonnum").ConvertTo(); episode.ProductionCode = e.Element("prodnum").Value; - episode.AirDate = DateTime.Parse(e.Element("airdate").Value); + episode.AirDate = e.Element("airdate").ConvertTo(); episode.Link = e.Element("link").Value; episode.Title = e.Element("title").Value; episodes.Add(episode); @@ -174,24 +165,5 @@ namespace NzbDrone.Core.Providers return offset; } - - internal DayOfWeek? ParseDayOfWeek(XElement element) - { - if(element == null) - return null; - - if(String.IsNullOrWhiteSpace(element.Value)) - return null; - - try - { - return (DayOfWeek)Enum.Parse(typeof(DayOfWeek), element.Value); - } - catch(Exception) - { - } - - return null; - } } }