From e8b2d1fda0e679a4d2b875897438cc722014cd2d Mon Sep 17 00:00:00 2001 From: kayone Date: Tue, 24 Sep 2013 17:01:03 -0700 Subject: [PATCH] fixed broken test, cleaned up some code around config contracts. --- NzbDrone.Api/ClientSchema/SchemaBuilder.cs | 2 ++ NzbDrone.Common/Reflection/ReflectionExtensions.cs | 2 +- .../IndexerTests/IndexerServiceFixture.cs | 1 + .../Converters/ProviderSettingConverter.cs | 10 +++++++--- NzbDrone.Core/NzbDrone.Core.csproj | 1 + .../ConfigContractNotFoundException.cs | 13 +++++++++++++ NzbDrone.Core/ThingiProvider/ProviderService.cs | 2 +- 7 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 NzbDrone.Core/ThingiProvider/ConfigContractNotFoundException.cs diff --git a/NzbDrone.Api/ClientSchema/SchemaBuilder.cs b/NzbDrone.Api/ClientSchema/SchemaBuilder.cs index 1393387bc..c5c96c302 100644 --- a/NzbDrone.Api/ClientSchema/SchemaBuilder.cs +++ b/NzbDrone.Api/ClientSchema/SchemaBuilder.cs @@ -57,6 +57,8 @@ public static List ToSchema(object model) public static object ReadFormSchema(List fields, Type targetType) { + Ensure.That(() => targetType).IsNotNull(); + var properties = targetType.GetSimpleProperties(); var target = Activator.CreateInstance(targetType); diff --git a/NzbDrone.Common/Reflection/ReflectionExtensions.cs b/NzbDrone.Common/Reflection/ReflectionExtensions.cs index 0c70d0c42..898d45119 100644 --- a/NzbDrone.Common/Reflection/ReflectionExtensions.cs +++ b/NzbDrone.Common/Reflection/ReflectionExtensions.cs @@ -62,7 +62,7 @@ public static T GetAttribute(this MemberInfo member, bool isRequired = true) public static Type FindTypeByName(this Assembly assembly, string name) { - return assembly.GetTypes().Single(c => c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); + return assembly.GetTypes().SingleOrDefault(c => c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); } public static bool HasAttribute(this Type type) diff --git a/NzbDrone.Core.Test/IndexerTests/IndexerServiceFixture.cs b/NzbDrone.Core.Test/IndexerTests/IndexerServiceFixture.cs index 2012921f0..bbfb8404a 100644 --- a/NzbDrone.Core.Test/IndexerTests/IndexerServiceFixture.cs +++ b/NzbDrone.Core.Test/IndexerTests/IndexerServiceFixture.cs @@ -71,6 +71,7 @@ public void should_remove_missing_indexers_on_startup() var existingIndexers = Builder.CreateNew().BuildNew(); + existingIndexers.ConfigContract = typeof (NewznabSettings).Name; repo.Insert(existingIndexers); diff --git a/NzbDrone.Core/Datastore/Converters/ProviderSettingConverter.cs b/NzbDrone.Core/Datastore/Converters/ProviderSettingConverter.cs index 8e83938a9..ace64d6af 100644 --- a/NzbDrone.Core/Datastore/Converters/ProviderSettingConverter.cs +++ b/NzbDrone.Core/Datastore/Converters/ProviderSettingConverter.cs @@ -23,11 +23,15 @@ public override object FromDB(ConverterContext context) } var ordinal = context.DataRecord.GetOrdinal("ConfigContract"); - - var implementation = context.DataRecord.GetString(ordinal); + var contract = context.DataRecord.GetString(ordinal); - var impType = typeof (IProviderConfig).Assembly.FindTypeByName(implementation); + var impType = typeof (IProviderConfig).Assembly.FindTypeByName(contract); + + if (impType == null) + { + throw new ConfigContractNotFoundException(contract); + } return Json.Deserialize(stringValue, impType); } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index e5058b047..582096964 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -414,6 +414,7 @@ + diff --git a/NzbDrone.Core/ThingiProvider/ConfigContractNotFoundException.cs b/NzbDrone.Core/ThingiProvider/ConfigContractNotFoundException.cs new file mode 100644 index 000000000..47d6ecdf3 --- /dev/null +++ b/NzbDrone.Core/ThingiProvider/ConfigContractNotFoundException.cs @@ -0,0 +1,13 @@ +using NzbDrone.Common.Exceptions; + +namespace NzbDrone.Core.ThingiProvider +{ + public class ConfigContractNotFoundException : NzbDroneException + { + public ConfigContractNotFoundException(string contract) + : base("Couldn't find config contract " + contract) + { + } + + } +} diff --git a/NzbDrone.Core/ThingiProvider/ProviderService.cs b/NzbDrone.Core/ThingiProvider/ProviderService.cs index 9869f9679..4765de4bc 100644 --- a/NzbDrone.Core/ThingiProvider/ProviderService.cs +++ b/NzbDrone.Core/ThingiProvider/ProviderService.cs @@ -48,7 +48,7 @@ public List Templates() { ConfigContract = p.ConfigContract.Name, Implementation = p.GetType().Name, - Settings = (IProviderConfig)Activator.CreateInstance(ReflectionExtensions.CoreAssembly.FindTypeByName(p.ConfigContract.Name)) + Settings = (IProviderConfig)Activator.CreateInstance(p.ConfigContract) }).ToList(); }