public void MustThrowIfDuplicateQueryHandlers()
        {
            var assembly = New.Common().Assembly.WithTypes(typeof(FakeHandler), typeof(FakeHandler)).Creation;
            var sc       = new ServiceCollection();

            Assert.ThrowsAny <Exception>(() => sc.AddQueryHandlers(new Assembly[] { assembly }));
        }
Beispiel #2
0
        public void MustReturnFalseWhenTypeIsNotGeneric()
        {
            var nonGenericType = New.Common().Type.Creation;
            var result         = TypeExtensions.IsGenericTypeWithNonGenericTypeParameters(nonGenericType);

            Assert.False(result);
        }
Beispiel #3
0
 public WhenDispatching()
 {
     _serviceProvider.Setup(p => p.GetService(typeof(IEnumerable <IMessageHandler <IMessage> >)))
     .Returns(new[] { _handler.Object }.TakeWhile(_ => true));
     _logger = New.Common().Logger <EventDispatcher>();
     _sut    = new EventDispatcher(_serviceProvider.Object, _logger.Creation);
 }
Beispiel #4
0
        public void MustReturnEmptyListIfOpenGenericTypeToMatchIsNull()
        {
            var type = New.Common().Type;

            var result = TypeExtensions.GetImplementedInterfacesThatMatchOpenGenericType(type, null);

            Assert.Empty(result);
        }
Beispiel #5
0
        public void MustReturnEmptyListIfTypeHasNotImplementedInterfaces()
        {
            var type = New.Common().Type;

            var result = TypeExtensions.GetImplementedInterfacesThatMatchOpenGenericType(type, typeof(IEnumerable <>));

            Assert.Empty(result);
        }
Beispiel #6
0
 public WhenDispatchingGenericQuery()
 {
     _query = It.IsAny <IQuery <string> >();
     _serviceProvider.Setup(p => p.GetService(typeof(IQueryHandler <IQuery <string>, string>)))
     .Returns(_handler.Object);
     _logger = New.Common().Logger <QueryDispatcher>();
     _sut    = new QueryDispatcher(_serviceProvider.Object, _logger.Creation);
 }
Beispiel #7
0
        public void MustReturnEMptyIfTypeHasNoGenericArguments()
        {
            var type = New.Common().Type.Creation;

            var result = TypeExtensions.GetImplementedInterfacesOfSingleGenericTypeArgument(type);

            Assert.Empty(result);
        }
Beispiel #8
0
 public WhenDispatchingStrongTypedQuery()
 {
     _query = new TestQuery();
     _serviceProvider.Setup(p => p.GetService(typeof(IQueryHandler <TestQuery, string>)))
     .Returns(_handler.Object);
     _logger = New.Common().Logger <QueryDispatcher>();
     _sut    = new QueryDispatcher(_serviceProvider.Object, _logger.Creation);
 }
Beispiel #9
0
 public WhenDispatching()
 {
     _serviceProvider.Setup(p => p.GetService(typeof(IMessageHandler <IMessage>)))
     .Returns(_handler.Object);
     _serviceProvider.Setup(p => p.GetService(typeof(ICommandBehaviorPipeline <IMessage>)))
     .Returns(_pipeline.Object);
     _logger = New.Common().Logger <CommandDispatcher>();
     _sut    = new CommandDispatcher(_serviceProvider.Object, _logger.Creation);
 }
Beispiel #10
0
        public void MustThrowIfTypeHasMoreThanOneGenericArgument()
        {
            var implementedInterfaceType = typeof(ICommand);
            var type = New.Common().Type
                       .WithGenericArguments(implementedInterfaceType, implementedInterfaceType)
                       .Creation;

            Assert.Throws <InvalidOperationException>(() => TypeExtensions.GetImplementedInterfacesOfSingleGenericTypeArgument(type));
        }
Beispiel #11
0
        public void MustReturnTrueWhenTypeIsGenericAndNoTypeParametersAreGeneric()
        {
            var nonGenericType = New.Common().Type.Creation;
            var genericType    = New.Common().Type
                                 .AsGeneric()
                                 .WithGenericArguments(nonGenericType).Creation;

            var result = TypeExtensions.IsGenericTypeWithNonGenericTypeParameters(genericType);

            Assert.True(result);
        }
Beispiel #12
0
        public AssemblyCreator(INewContext newContext, Assembly assembly = null)
            : base(newContext, assembly)
        {
            WithFullName(Guid.NewGuid().ToString());
            WithTypes(New.Common().Type.Creation);
            _assemblyMock.Setup(a => a.Equals(It.IsAny <Assembly>()))
            .Returns <Assembly>(x => x.FullName == _assemblyMock.Object.FullName &&
                                x.GetTypes() == _assemblyMock.Object.GetTypes());

            Creation = _assemblyMock.Object;
        }
Beispiel #13
0
        public void MustReturnFalseIfGenericImplementedInterfaceDoesNotHaveMatchingType()
        {
            var genericArgumentType = New.Common().Type;
            var type = New.Common().Type
                       .WithGenericArguments(genericArgumentType)
                       .Creation;
            var types = new Type[] { type };

            var result = TypeExtensions.HasMatchingGenericArgumentType(types, typeof(ICommand));

            Assert.False(result);
        }
Beispiel #14
0
        public void MustNotReturnImplementedInterfacesThatAreNotGeneric()
        {
            var genericTypeDef = typeof(IMessageHandler <>);
            var interfaceType  = New.Common().Type;
            var type           = New.Common().Type
                                 .WithImplementedInterfaces(interfaceType).Creation;

            var result = TypeExtensions.GetImplementedInterfacesThatMatchOpenGenericType(type, genericTypeDef);

            Assert.DoesNotContain(interfaceType, result);
            Assert.Empty(result);
        }
Beispiel #15
0
        public void MustReturnFalseIfTypeParameterTypeToMatchIsNull()
        {
            var genericArgumentType = New.Common().Type
                                      .WithImplementedInterfaces(typeof(ICommand)).Creation;
            var type = New.Common().Type
                       .WithGenericArguments(genericArgumentType)
                       .Creation;
            var types = new Type[] { type };

            var result = TypeExtensions.HasMatchingGenericArgumentType(types, null);

            Assert.False(result);
        }
Beispiel #16
0
        public void MustGetImplementedInterfacesIfTypeHasExactlyOneGenericArgument()
        {
            var implementedInterfaceType = typeof(ICommand);
            var genericArgumentType      = New.Common().Type
                                           .WithImplementedInterfaces(implementedInterfaceType).Creation;
            var type = New.Common().Type
                       .WithGenericArguments(genericArgumentType)
                       .Creation;

            var result = TypeExtensions.GetImplementedInterfacesOfSingleGenericTypeArgument(type);

            Assert.Contains(implementedInterfaceType, result);
        }
Beispiel #17
0
        public void MustNotFilterAnyAssembliesWhenNamespaceSelectorIsEmptyAndNoExplicitAssemblies()
        {
            var assembly = New.Common().Assembly.Creation;
            var assemblyFilterSourceProvider = New.Cqrs().AssemblyFilterSourceProvider
                                               .WithSourceAssemblies(assembly)
                                               .Creation;

            var filter = new AssemblySourceFilter(assemblyFilterSourceProvider, string.Empty, null);
            var result = filter.Apply();

            result.Should().HaveCount(1);
            result.Should().Contain(assembly);
        }
        public void MustRegisterHandlersForGenericEvent()
        {
            var assembly = New.Common().Assembly.WithTypes(typeof(FakeEventHandler3)).Creation;
            var sc       = new ServiceCollection();

            sc.AddEventHandlers(new Assembly[] { assembly });

            var sd = sc.GetServiceDescriptorByImplementationType(typeof(FakeEventHandler3));

            sd.Lifetime.Should().Be(ServiceLifetime.Transient);
            sd.ServiceType.Should().Be(typeof(IMessageHandler <FakeGenericEvent <string> >));
            sd.ImplementationType.Should().Be(typeof(FakeEventHandler3));
        }
Beispiel #19
0
        public void MustNotContainDuplicateAssemblies()
        {
            var assembly           = New.Common().Assembly.Creation;
            var explicitAssemblies = new Assembly[] { assembly };

            var assemblyFilterSourceProvider = New.Cqrs().AssemblyFilterSourceProvider.WithSourceAssemblies(assembly).Creation;

            var filter = new AssemblySourceFilter(assemblyFilterSourceProvider, string.Empty, explicitAssemblies);
            var result = filter.Apply();

            result.Should().HaveCount(1);
            Assert.Contains(assembly, result);
        }
        public void MustRegisterAllQueryHandlers()
        {
            var assembly = New.Common().Assembly.WithTypes(typeof(FakeHandler)).Creation;
            var sc       = new ServiceCollection();

            sc.AddQueryHandlers(new Assembly[] { assembly });

            var sd = sc.GetServiceDescriptorByImplementationType(typeof(FakeHandler));

            sd.Lifetime.Should().Be(ServiceLifetime.Transient);
            sd.ServiceType.Should().Be(typeof(IQueryHandler <FakeQuery, string>));
            sd.ImplementationType.Should().Be(typeof(FakeHandler));
        }
Beispiel #21
0
        public void MustReturnImplementedInterfacesThatAreGenericAndHaveMatchingGenericTypeDef()
        {
            var genericTypeDef = typeof(IMessageHandler <>);
            var interfaceType  = New.Common().Type
                                 .AsGeneric()
                                 .WithGenericTypeDef(genericTypeDef);
            var type = New.Common().Type
                       .WithImplementedInterfaces(interfaceType).Creation;

            var result = TypeExtensions.GetImplementedInterfacesThatMatchOpenGenericType(type, genericTypeDef);

            Assert.Contains(interfaceType, result);
        }
Beispiel #22
0
        public void MustContainExplicitAssemblies()
        {
            var assembly           = New.Common().Assembly.Creation;
            var explicitAssemblies = new Assembly[] { assembly };

            var assemblyFilterSourceProvider = New.Cqrs().AssemblyFilterSourceProvider
                                               .Creation;

            var filter = new AssemblySourceFilter(assemblyFilterSourceProvider, string.Empty, explicitAssemblies);
            var result = filter.Apply();

            Assert.Equal(explicitAssemblies, filter.ExplictAssemblies);
            Assert.Contains(assembly, result);
        }
Beispiel #23
0
        public void MustReturnFalseWhenAtLeastOneTypeParameterIsGeneric()
        {
            var genericTypeParameter = New.Common().Type
                                       .AsGeneric()
                                       .AsGenericTypeParameter()
                                       .Creation;
            var genericType = New.Common().Type
                              .AsGeneric()
                              .WithGenericArguments(genericTypeParameter).Creation;

            var result = TypeExtensions.IsGenericTypeWithNonGenericTypeParameters(genericType);

            Assert.False(result);
        }
        public void MustOnlyRegisterEventHandlers()
        {
            var assembly = New.Common().Assembly.WithTypes(typeof(FakeCommandHandler), typeof(FakeEventHandler)).Creation;
            var sc       = new ServiceCollection();

            sc.AddEventHandlers(new Assembly[] { assembly });

            var sd = sc.GetServiceDescriptorByImplementationType(typeof(FakeEventHandler));

            sc.Should().HaveCount(1);

            sd.Lifetime.Should().Be(ServiceLifetime.Transient);
            sd.ServiceType.Should().Be(typeof(IMessageHandler <FakeEvent>));
            sd.ImplementationType.Should().Be(typeof(FakeEventHandler));
        }
Beispiel #25
0
        public void MustNotFilterAnyAssembliesWhenNameselectorIsNullWithExplicitAssemblies()
        {
            var assembly           = New.Common().Assembly.Creation;
            var assembly2          = New.Common().Assembly.Creation;
            var explicitAssemblies = new Assembly[] { assembly };

            var assemblyFilterSourceProvider = New.Cqrs().AssemblyFilterSourceProvider
                                               .WithSourceAssemblies(assembly2)
                                               .Creation;

            var filter = new AssemblySourceFilter(assemblyFilterSourceProvider, null, explicitAssemblies);
            var result = filter.Apply();

            result.Should().HaveCount(2);
            result.Should().Contain(assembly);
            result.Should().Contain(assembly2);
        }
Beispiel #26
0
        public void MustMatchNamespaceSelectorWithSingleSymbolWildcardToAssemblyFullName()
        {
            var fullName = "Chatter.Cqrs";
            var assembly = New.Common().Assembly
                           .WithFullName(fullName)
                           .Creation;

            var assemblyFilterSourceProvider = New.Cqrs().AssemblyFilterSourceProvider
                                               .WithSourceAssemblies(assembly)
                                               .Creation;

            var filter = new AssemblySourceFilter(assemblyFilterSourceProvider, "Chatter?Cqrs", null);
            var result = filter.Apply();

            result.Should().HaveCount(1);
            result.First().FullName.Should().Be(fullName);
            result.First().Should().BeSameAs(assembly);
        }
Beispiel #27
0
        public void MustIgnoreCaseWhenApplyingNamespaceSelectorFilterToAssemblyFullName()
        {
            var fullName = "Chatter.Cqrs";
            var assembly = New.Common().Assembly
                           .WithFullName(fullName)
                           .Creation;

            var assemblyFilterSourceProvider = New.Cqrs().AssemblyFilterSourceProvider
                                               .WithSourceAssemblies(assembly)
                                               .Creation;

            var filter = new AssemblySourceFilter(assemblyFilterSourceProvider, fullName.ToUpper(), null);
            var result = filter.Apply();

            result.Should().HaveCount(1);
            result.First().FullName.Should().Be(fullName);
            result.First().Should().BeSameAs(assembly);
        }
Beispiel #28
0
        public void MustReturnFalseIfTypeDoesNotImplementIMessageHandler()
        {
            var typeToMatch = typeof(ICommand);

            var argType = New.Common().Type
                          .WithImplementedInterfaces(typeToMatch)
                          .Creation;
            var closedGenericWithArgType = New.Common().Type
                                           .WithGenericTypeDef(typeof(IEnumerable <>))
                                           .WithGenericArguments(argType)
                                           .Creation;
            var type = New.Common().Type
                       .WithImplementedInterfaces(closedGenericWithArgType)
                       .Creation;

            var result = CqrsExtensions.IsValidMessageHandler(type, typeToMatch);

            Assert.False(result);
        }
Beispiel #29
0
        public void MustReturnTrueIfNotGenericTypeAndImplementsIMessageHandlerWithMatchingTypeParam()
        {
            var typeToMatch = typeof(ICommand);

            var argType = New.Common().Type
                          .WithImplementedInterfaces(typeToMatch)
                          .Creation;
            var closedGenericWithArgType = New.Common().Type
                                           .WithGenericTypeDef(typeof(IMessageHandler <>))
                                           .WithGenericArguments(argType)
                                           .Creation;
            var type = New.Common().Type
                       .WithImplementedInterfaces(closedGenericWithArgType)
                       .Creation;

            var result = CqrsExtensions.IsValidMessageHandler(type, typeToMatch);

            Assert.True(result);
        }
        public void MustNotReplaceDuplicateRegistrations()
        {
            var assembly = New.Common().Assembly
                           .WithTypes(typeof(FakeEventHandler), typeof(FakeEventHandler2))
                           .Creation;
            var sc = new ServiceCollection();

            sc.AddEventHandlers(new Assembly[] { assembly });

            sc.Should().HaveCount(2);
            sc.Should().OnlyHaveUniqueItems();

            sc[0].Lifetime.Should().Be(ServiceLifetime.Transient);
            sc[0].ServiceType.Should().Be(typeof(IMessageHandler <FakeEvent>));
            sc[0].ImplementationType.Should().Be(typeof(FakeEventHandler));

            sc[1].Lifetime.Should().Be(ServiceLifetime.Transient);
            sc[1].ServiceType.Should().Be(typeof(IMessageHandler <FakeEvent>));
            sc[1].ImplementationType.Should().Be(typeof(FakeEventHandler2));
        }