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 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_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 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 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 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 ResolveComponentType_WhenTypeIsInvalid_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 component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName("InvalidComponent, Assembly"))); var ex = Assert.Throws<RuntimeException>(() => component.ResolveComponentType()); Assert.Contains(ex.Message, "Could not resolve the component type of component 'componentId'."); }
public void ResolveComponentTraits_WhenActivationFails_Throws() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName(typeof(DummyServiceWithTraits))) { TraitsHandlerFactory = handlerFactory, }); var component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName(typeof(DummyComponent))) { TraitsProperties = { { "Name", "Value" } } }); var handler = MockRepository.GenerateMock<IHandler>(); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .Constraints(Is.NotNull(), Is.Equal(typeof(Traits)), Is.Equal(typeof(DummyTraits)), Is.Equal(new PropertySet() { { "Name", "Value" } })) .Return(handler); handler.Expect(x => x.Activate()).Throw(new InvalidOperationException("Boom")); var ex = Assert.Throws<RuntimeException>(() => component.ResolveTraits()); Assert.AreEqual("Could not resolve traits of component 'componentId'.", ex.Message); handlerFactory.VerifyAllExpectations(); handler.VerifyAllExpectations(); }
public void IsDisabled_WhenServiceDisabled_ReturnsTrue() { 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"); Assert.IsTrue(component.IsDisabled); }
public void ResolveComponentHandler_WhenComponentHasDependencyOnComponentDescriptor_OwnComponentDescriptorIsResolved() { 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(DummyServiceWithTraits)))); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName(typeof(DummyComponentWithTraits))) { ComponentHandlerFactory = handlerFactory, ComponentProperties = { { "Name", "Value" } } }); var handler = MockRepository.GenerateStub<IHandler>(); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .IgnoreArguments() .Return(handler); component.ResolveComponentHandler(); 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(IComponentDescriptor), null); Assert.AreSame(component, dependencyResolution.Value); }
public void ResolveComponentTraits_WhenWellFormed_ReturnsComponentTraitsObject() { var registry = new Registry(); var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"))); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName(typeof(DummyServiceWithTraits))) { TraitsHandlerFactory = handlerFactory, }); var component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName(typeof(DummyComponent))) { TraitsProperties = { { "Name", "Value" } } }); var handler = MockRepository.GenerateMock<IHandler>(); var traitsInstance = new DummyTraits(); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .Constraints(Is.NotNull(), Is.Equal(typeof(Traits)), Is.Equal(typeof(DummyTraits)), Is.Equal(new PropertySet() { { "Name", "Value" } })) .Return(handler); handler.Expect(x => x.Activate()).Return(traitsInstance); var result = (DummyTraits)component.ResolveTraits(); Assert.AreSame(traitsInstance, result); handlerFactory.VerifyAllExpectations(); handler.VerifyAllExpectations(); }
public void ResolveComponentHandler_WhenHandlerFactorySucceeds_ReturnsHandler() { 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 handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName(typeof(DummyComponent))) { ComponentHandlerFactory = handlerFactory, ComponentProperties = { { "Name", "Value" } } }); var handler = MockRepository.GenerateStub<IHandler>(); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .Constraints(Is.NotNull(), Is.Equal(typeof(DummyService)), Is.Equal(typeof(DummyComponent)), Is.Equal(new PropertySet() { { "Name", "Value" } })) .Return(handler); var result = component.ResolveComponentHandler(); Assert.AreSame(handler, result); handlerFactory.VerifyAllExpectations(); }
public void ResolveComponentHandler_WhenHandlerFactoryFails_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(typeof(DummyService)))); var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>(); var component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName(typeof(DummyComponent))) { ComponentHandlerFactory = handlerFactory, ComponentProperties = { { "Name", "Value" } } }); handlerFactory.Expect(x => x.CreateHandler(null, null, null, null)) .Constraints(Is.NotNull(), Is.Equal(typeof(DummyService)), Is.Equal(typeof(DummyComponent)), Is.Equal(new PropertySet() { { "Name", "Value" } })) .Throw(new Exception("Boom")); var ex = Assert.Throws<RuntimeException>(() => component.ResolveComponentHandler()); Assert.Contains(ex.Message, "Could not resolve the component handler of component 'componentId'."); handlerFactory.VerifyAllExpectations(); }
public void ResolveComponentType_WhenTypeIsValid_ReturnsType() { 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(typeof(DummyComponent)))); var componentType = component.ResolveComponentType(); Assert.AreEqual(typeof(DummyComponent), componentType); }
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 DisabledReason_WhenPluginAndServiceNotDisabled_Throws() { 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"))); var ex = Assert.Throws<InvalidOperationException>(() => { var x = component.DisabledReason; }); Assert.AreEqual("The component has not been disabled.", ex.Message); }
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 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); }
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 RegistryComponent_WhenRegistrationNull_Throws() { var registry = new Registry(); Assert.Throws<ArgumentNullException>(() => registry.RegisterComponent(null)); }