public void RegistryPlugin_WhenRegistrationContainsNullPluginDependency_Throws() { var registry = new Registry(); var pluginRegistration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\")); pluginRegistration.PluginDependencies.Add(null); var ex = Assert.Throws<ArgumentException>(() => registry.RegisterPlugin(pluginRegistration)); Assert.Contains(ex.Message, "The plugin dependencies must not contain a null reference."); }
public void DisabledReason_WhenServiceDisabled_DescribesTheDependency() { var registry = new Registry(); var plugin1 = registry.RegisterPlugin(new PluginRegistration("plugin1Id", new TypeName("Plugin1, Assembly"), new DirectoryInfo(@"C:\"))); var plugin2 = registry.RegisterPlugin(new PluginRegistration("plugin2Id", new TypeName("Plugin2, Assembly"), new DirectoryInfo(@"C:\")) { PluginDependencies = { plugin1 } }); var service = registry.RegisterService(new ServiceRegistration(plugin1, "serviceId", new TypeName("Service, Assembly"))); var component = registry.RegisterComponent(new ComponentRegistration(plugin2, service, "componentId", new TypeName("Component, Assembly"))); plugin1.Disable("The reason"); // Now that plugin dependencies have been implemented, we see a different message than before. // However if later we add a feature to disable individual services independently of a plugin // then we should see the following message: // Assert.AreEqual("The service implemented by this component was disabled. Reason: The plugin that provides this service was disabled. Reason: The reason", component.DisabledReason); Assert.AreEqual("The plugin that provides this component was disabled. Reason: The plugin depends on another disabled plugin: 'plugin1Id'.", component.DisabledReason); }
public void ResolvePlugin_WhenActivationFails_Throws() { var registry = new Registry(); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\")) { PluginHandlerFactory = handlerFactory, PluginProperties = { { "Name", "Value" } } }); var handler = MockRepository.GenerateMock<IHandler>(); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .Constraints(Is.NotNull(), Is.Equal(typeof(IPlugin)), Is.Equal(typeof(DummyPlugin)), Is.Equal(new PropertySet() { { "Name", "Value" } })) .Return(handler); handler.Expect(x => x.Activate()).Throw(new InvalidOperationException("Boom")); var ex = Assert.Throws<RuntimeException>(() => plugin.ResolvePlugin()); Assert.AreEqual("Could not resolve instance of plugin 'pluginId'.", ex.Message); handlerFactory.VerifyAllExpectations(); handler.VerifyAllExpectations(); }
public void ResolveTraitsHandler_WhenHandlerFactoryFails_Throws() { var registry = new Registry(); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\")) { TraitsProperties = { { "Name", "Value" } } }); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .Constraints(Is.NotNull(), Is.Equal(typeof(PluginTraits)), Is.Equal(typeof(PluginTraits)), Is.Equal(new PropertySet() { { "Name", "Value" } })) .Throw(new Exception("Boom")); PluginDescriptor.RunWithInjectedTraitsHandlerFactoryMock(handlerFactory, () => { var ex = Assert.Throws<RuntimeException>(() => plugin.ResolveTraitsHandler()); Assert.Contains(ex.Message, "Could not resolve the traits handler of plugin 'pluginId'."); }); handlerFactory.VerifyAllExpectations(); }
public void ResolvePluginType_WhenTypeIsValid_ReturnsType() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\"))); var pluginType = plugin.ResolvePluginType(); Assert.AreEqual(typeof(DummyPlugin), pluginType); }
public void FindByServiceTypeName_WhenComponentsNotRegistered_ReturnsEmptyList() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName(typeof(DummyService)))); var result = registry.Components.FindByServiceTypeName(new TypeName(typeof(DummyService))); Assert.IsEmpty(result); }
public void GetSearchPaths_WhenResourcePathIsRelative_ReturnsBinAndProbingPathsWithResourcePathAppended() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("InvalidPlugin, Assembly"), new DirectoryInfo(@"C:\")) { ProbingPaths = { "ProbeA", "ProbeB" } }); IList<string> result = plugin.GetSearchPaths("resource.txt").ToList(); Assert.AreElementsEqual(new[] { @"C:\resource.txt", @"C:\bin\resource.txt", @"C:\ProbeA\resource.txt", @"C:\bin\ProbeA\resource.txt", @"C:\ProbeB\resource.txt", @"C:\bin\ProbeB\resource.txt" }, result); }
public void RegisterService_WhenServiceTypeNameNotUnique_Throws() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName("Service, Assembly"))); var ex = Assert.Throws<ArgumentException>(() => { registry.RegisterService(new ServiceRegistration(plugin, "differentServiceId", new TypeName("Service, Assembly"))); // same service type }); Assert.Contains(ex.Message, "There is already a service registered with type name 'Service, Assembly'. This service has id 'differentServiceId' and the other service has id 'serviceId'."); }
public void RegisterComponent_WhenServiceBelongsToDifferentRegistry_Throws() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var foreignRegistry = new Registry(); var foreignPlugin = foreignRegistry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var foreignService = foreignRegistry.RegisterService(new ServiceRegistration(foreignPlugin, "serviceId", new TypeName("Service, Assembly"))); var ex = Assert.Throws<ArgumentException>(() => { registry.RegisterComponent(new ComponentRegistration(plugin, foreignService, "componentId", new TypeName("Component, Assembly"))); }); Assert.Contains(ex.Message, "The specified service descriptor does not belong to this registry."); }
public void RegisterPlugin_WhenPluginDependencyBelongsToDifferentRegistry_Throws() { var registry = new Registry(); var foreignRegistry = new Registry(); var foreignPlugin = foreignRegistry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var ex = Assert.Throws<ArgumentException>(() => { registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\")) { PluginDependencies = { foreignPlugin } }); }); Assert.Contains(ex.Message, "One of the plugin dependencies does not belong to this registry."); }
public void RegisterPlugin_WhenPluginDependencyListNotClosed_AutomaticallyAddsIndirectDependency() { var registry = new Registry(); var plugin1 = registry.RegisterPlugin(new PluginRegistration("plugin1Id", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var plugin2 = registry.RegisterPlugin(new PluginRegistration("plugin2Id", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\")) { PluginDependencies = { plugin1 } }); var plugin3 = registry.RegisterPlugin(new PluginRegistration("plugin3Id", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\")) { PluginDependencies = { plugin2 } }); Assert.AreElementsEqual(new[] { plugin2, plugin1 }, plugin3.PluginDependencies); }
public void RegisterPlugin_WhenPluginIdNotUnique_Throws() { var registry = new Registry(); registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var ex = Assert.Throws<ArgumentException>(() => { registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); }); Assert.Contains(ex.Message, "There is already a plugin registered with id 'pluginId'."); }
public void RegisterComponent_WhenArgumentsValidButNoComponentTypeNameProvided_UsesTheDefaultComponentTypeSepcifiedByTheService() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName("Service, Assembly")) { DefaultComponentTypeName = new TypeName("Component, Assembly") }); var handlerFactory = MockRepository.GenerateStub<IHandlerFactory>(); var component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", null)); Assert.Multiple(() => { Assert.AreSame(plugin, component.Plugin); Assert.AreSame(service, component.Service); Assert.AreEqual("componentId", component.ComponentId); Assert.AreEqual(new TypeName("Component, Assembly"), component.ComponentTypeName); }); }
public void RegisterComponent_WhenArgumentsValid_RegistersTheComponentAndReturnsItsDescriptor() { var registry = new Registry(); var plugin1 = registry.RegisterPlugin(new PluginRegistration("plugin1Id", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var plugin2 = registry.RegisterPlugin(new PluginRegistration("plugin2Id", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var service = registry.RegisterService(new ServiceRegistration(plugin2, "serviceId", new TypeName("Service, Assembly"))); var handlerFactory = MockRepository.GenerateStub<IHandlerFactory>(); var component = registry.RegisterComponent(new ComponentRegistration(plugin2, service, "componentId", new TypeName("Component, Assembly")) { ComponentProperties = { { "ConfigName", "Value" } }, TraitsProperties = { { "TraitName", "Value" } }, ComponentHandlerFactory = handlerFactory }); Assert.Multiple(() => { Assert.AreSame(plugin2, component.Plugin); Assert.AreSame(service, component.Service); Assert.AreEqual("componentId", component.ComponentId); Assert.AreEqual(new TypeName("Component, Assembly"), component.ComponentTypeName); Assert.AreEqual(new PropertySet() { { "ConfigName", "Value" } }, component.ComponentProperties); Assert.AreEqual(new PropertySet() { { "TraitName", "Value" } }, component.TraitsProperties); Assert.AreSame(handlerFactory, component.ComponentHandlerFactory); }); }
public void RegistryPlugin_WhenRegistrationNull_Throws() { var registry = new Registry(); Assert.Throws<ArgumentNullException>(() => registry.RegisterPlugin(null)); }
public void RegisterComponent_WhenServiceBelongsToPluginNotIndicatedAsAPluginDependency_Throws() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var foreignPlugin = registry.RegisterPlugin(new PluginRegistration("foreignPluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var foreignService = registry.RegisterService(new ServiceRegistration(foreignPlugin, "foreignServiceId", new TypeName("Service, Assembly"))); var ex = Assert.Throws<ArgumentException>(() => { registry.RegisterComponent(new ComponentRegistration(plugin, foreignService, "componentId", new TypeName("Component, Assembly"))); }); Assert.Contains(ex.Message, "The service belongs to a plugin that was not declared as a dependency of the plugin that provides this component. Plugin 'pluginId' should declare a dependency on plugin 'foreignPluginId'."); }
public void FindByServiceTypeName_WhenComponentsRegistered_ReturnsComponentList() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName(typeof(DummyService)))); var component1 = registry.RegisterComponent(new ComponentRegistration(plugin, service, "component1Id", new TypeName("Component1, Assembly"))); var component2 = registry.RegisterComponent(new ComponentRegistration(plugin, service, "component2Id", new TypeName("Component2, Assembly"))); var result = registry.Components.FindByServiceTypeName(new TypeName(typeof(DummyService))); Assert.AreElementsEqualIgnoringOrder(new[] { component1, component2 }, result); }
public void RegisterComponent_WhenComponentIdNotUnique_Throws() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName("Service, Assembly"))); registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName("Component, Assembly"))); var ex = Assert.Throws<ArgumentException>(() => { registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName("Component, Assembly"))); }); Assert.Contains(ex.Message, "There is already a component registered with id 'componentId'."); }
public void GetSearchPaths_WhenResourcePathIsEmpty_Throws() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("InvalidPlugin, Assembly"), new DirectoryInfo(@"C:\"))); Assert.Throws<ArgumentException>(() => plugin.GetSearchPaths("")); }
public void RegisterComponent_WhenComponentMissingComponentTypeAndTheServiceDoesNotHaveADefaultComponentType_Throws() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName("Service, Assembly"))); var ex = Assert.Throws<ArgumentException>(() => { registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", null)); }); Assert.Contains(ex.Message, "The specified service descriptor does not have a default component type name so the component registration must specify a component type name but it does not."); }
public void GetSearchPaths_WhenResourcePathIsAbsolute_ReturnsResourcePathItself() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("InvalidPlugin, Assembly"), new DirectoryInfo(@"C:\"))); IList<string> result = plugin.GetSearchPaths(@"C:\SomePath\resource.txt").ToList(); Assert.AreElementsEqual(new[] { @"C:\SomePath\resource.txt" }, result); }
public void Indexer_WhenPluginIdIsRegistered_ReturnsPlugin() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var result = registry.Plugins["pluginId"]; Assert.AreSame(plugin, result); }
public void ResolvePluginType_WhenTypeIsInvalid_Throws() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("InvalidPlugin, Assembly"), new DirectoryInfo(@"C:\"))); var ex = Assert.Throws<RuntimeException>(() => plugin.ResolvePluginType()); Assert.Contains(ex.Message, "Could not resolve the plugin type of plugin 'pluginId'."); }
public void Indexer_WhenServiceIdIsRegistered_ReturnsService() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName("Service, Assembly"))); var result = registry.Services["serviceId"]; Assert.AreSame(service, result); }
public void ResolvePluginHandler_WhenComponentHasDependencyOnPluginDescriptor_OwnPluginDescriptorIsResolved() { var registry = new Registry(); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\")) { PluginHandlerFactory = handlerFactory, PluginProperties = { { "Name", "Value" } }, TraitsProperties = { { "name", "pluginName" } } }); var handler = MockRepository.GenerateStub<IHandler>(); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .IgnoreArguments() .Return(handler); plugin.ResolvePluginHandler(); var args = handlerFactory.GetArgumentsForCallsMadeOn(x => x.CreateHandler(null, null, null, null), x => x.IgnoreArguments()); var suppliedDependencyResolver = (IObjectDependencyResolver)args[0][0]; var dependencyResolution = suppliedDependencyResolver.ResolveDependency("descriptor", typeof(IPluginDescriptor), null); Assert.AreSame(plugin, dependencyResolution.Value); }
public void GetByServiceTypeName_WhenServiceTypeNameIsRegistered_ReturnsService() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName(typeof(DummyService)))); var result = registry.Services.GetByServiceTypeName(new TypeName(typeof(DummyService))); Assert.AreSame(service, result); }
public void ResolvePlugin_WhenWellFormed_ReturnsPluginObject() { var registry = new Registry(); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\")) { PluginHandlerFactory = handlerFactory, PluginProperties = { { "Name", "Value" } } }); var handler = MockRepository.GenerateMock<IHandler>(); var pluginInstance = new DummyPlugin(); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .Constraints(Is.NotNull(), Is.Equal(typeof(IPlugin)), Is.Equal(typeof(DummyPlugin)), Is.Equal(new PropertySet() { { "Name", "Value" } })) .Return(handler); handler.Expect(x => x.Activate()).Return(pluginInstance); var result = (DummyPlugin) plugin.ResolvePlugin(); handlerFactory.VerifyAllExpectations(); handler.VerifyAllExpectations(); Assert.AreSame(pluginInstance, result); }
public void Indexer_WhenComponentIdIsRegistered_ReturnsComponent() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName("Service, Assembly"))); var component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName("Component, Assembly"))); var result = registry.Components["componentId"]; Assert.AreSame(component, result); }
public void ResolvePluginTraits_WhenWellFormed_ReturnsPluginTraitsObject() { var registry = new Registry(); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\")) { TraitsProperties = { { "Name", "Value" } } }); var handler = MockRepository.GenerateMock<IHandler>(); var traitsInstance = new PluginTraits("name"); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .Constraints(Is.NotNull(), Is.Equal(typeof(PluginTraits)), Is.Equal(typeof(PluginTraits)), Is.Equal(new PropertySet() { { "Name", "Value" } })) .Return(handler); handler.Expect(x => x.Activate()).Return(traitsInstance); PluginDescriptor.RunWithInjectedTraitsHandlerFactoryMock(handlerFactory, () => { var result = plugin.ResolveTraits(); Assert.AreSame(traitsInstance, result); }); handlerFactory.VerifyAllExpectations(); handler.VerifyAllExpectations(); }
public void DisabledReason_WhenPluginDisabled_ReturnsTheReason() { var registry = new Registry(); var plugin1 = registry.RegisterPlugin(new PluginRegistration("plugin1Id", new TypeName("Plugin1, Assembly"), new DirectoryInfo(@"C:\"))); var plugin2 = registry.RegisterPlugin(new PluginRegistration("plugin2Id", new TypeName("Plugin2, Assembly"), new DirectoryInfo(@"C:\")) { PluginDependencies = { plugin1 } }); var service = registry.RegisterService(new ServiceRegistration(plugin1, "serviceId", new TypeName("Service, Assembly"))); var component = registry.RegisterComponent(new ComponentRegistration(plugin2, service, "componentId", new TypeName("Component, Assembly"))); plugin2.Disable("The reason"); Assert.AreEqual("The plugin that provides this component was disabled. Reason: The reason", component.DisabledReason); }