예제 #1
0
        public static Configuration UseHandlerDiscovery(
            this Configuration configuration,
            IEnumerable <Assembly> withinAssemblies = null)
        {
            var commandHandlerDescriptions =
                (withinAssemblies?.Types() ??
                 Discover.ConcreteTypes())
                .SelectMany(
                    concreteType =>
                    concreteType.GetInterfaces()
                    .Where(
                        i => i.IsConstructedGenericType &&
                        i.GetGenericTypeDefinition() == typeof(ICommandHandler <>))
                    .Select(
                        handlerInterface => new CommandHandlerDescription(handlerInterface, concreteType)))
                .ToArray();

            configuration.CommandHandlerDescriptions
            .AddRange(commandHandlerDescriptions);

            foreach (var handlerDescription in commandHandlerDescriptions)
            {
                configuration.Container.Register(
                    handlerDescription.HandlerInterface,
                    c => c.Resolve(handlerDescription.ConcreteHandlerType));
            }

            return(configuration);
        }
예제 #2
0
 public void DerivedFrom_returns_classes_implementing_the_specified_interface()
 {
     Discover.ConcreteTypes()
     .DerivedFrom(typeof(ICommand))
     .Should()
     .Contain(typeof(StringCommand));
 }
예제 #3
0
        public TestDefinitionRegistry(IEnumerable <Type> testTypes = null)
        {
            testTypes = testTypes ??
                        Discover.ConcreteTypes()
                        .DerivedFrom(typeof(IPeakyTest));

            tests = GetTestDefinitions(testTypes);
        }
예제 #4
0
            /// <summary>
            ///     Initializes a new instance of the <see cref="For{T}" /> class.
            /// </summary>
            public For()
            {
                var configSetting = GetSerializedSetting(Key);

                var targetType = typeof(T);

                if (typeof(T).IsAbstract)
                {
                    targetType = Discover.ConcreteTypes().Single(t => String.Equals(t.Name, Key, StringComparison.OrdinalIgnoreCase));
                }

                if (!String.IsNullOrWhiteSpace(configSetting))
                {
                    Value = (T)Deserialize(targetType, configSetting);
                }
                else
                {
                    try
                    {
                        Value = (T)Activator.CreateInstance(targetType);
                        LogCreation(BuildKey(), " new instance. No configuration found.");
                    }
                    catch (MissingMethodException)
                    {
                        if (typeof(T).IsAbstract)
                        {
                            throw new ConfigurationErrorsException(
                                      String.Format(
                                          "Settings.For is unable to create an instance of {0} because it is abstract. You should either change the class definition to be concrete or redirect settings resolution to another class by adding an appSetting entry, for example: \n    <add key=\"{1}\" value=\"{{NAME_OF_CONCRETE_TYPE}}\" />",
                                          typeof(T).AssemblyQualifiedName,
                                          typeof(T).Name));
                        }

                        throw new ConfigurationErrorsException(
                                  String.Format(
                                      "Type {0} cannot be instantiated without some additional setup because it does not have a parameterless constructor. You can fix this by setting Settings<{1}>.Deserialize with a delegate that can instantiate this type.",
                                      typeof(T).AssemblyQualifiedName,
                                      typeof(T).Name));
                    }
                }
            }
        /// <summary>
        /// Discovers tests in the application and maps routes to allow them to be called over HTTP.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="configureTargets">A delegate to configure the test targets.</param>
        /// <param name="baseUri">The base URI segment that the tests will be routed under.</param>
        /// <param name="testTypes">The Types to map routes for. If omitted, all discovered implementations of <see cref="IMonitoringTest" /> will be routed.</param>
        /// <param name="handler">A message handler to handle test requests, if you would like to provide authentication, logging, or other functionality during calls to monitoring tests.</param>
        /// <param name="testUiScriptUrl">The location of the test UI script.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">configuration</exception>
        public static HttpConfiguration MapTestRoutes(
            this HttpConfiguration configuration,
            Action <TestTargetRegistry> configureTargets = null,
            string baseUri = "tests",
            IEnumerable <Type> testTypes = null,
            HttpMessageHandler handler   = null,
            string testUiScriptUrl       = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            if (!Trace.Listeners.OfType <TracingFilter.TraceListener>().Any())
            {
                var traceListener = new TracingFilter.TraceListener();
                Trace.Listeners.Add(traceListener);
                Instrumentation.Log.EntryPosted += (_, args) => traceListener.WriteLine(args.LogEntry.ToLogString());
            }

            // set up specialized handling on the specified routes
            configuration.Filters.Add(new TestErrorFilter(baseUri));
            if (!string.IsNullOrEmpty(testUiScriptUrl) && Uri.IsWellFormedUriString(testUiScriptUrl, UriKind.RelativeOrAbsolute))
            {
                configuration.TestUiUriIs(testUiScriptUrl);
            }

            var testRootRouteTemplate = baseUri.AppendSegment("{environment}/{application}");

            configuration.RootTestUriIs(baseUri);

            // set up test discovery routes
            configuration.Routes.MapHttpRoute(
                TestRootRouteName,
                testRootRouteTemplate,
                defaults: new
            {
                controller  = "MonitoringTest",
                action      = "tests",
                application = RouteParameter.Optional,
                environment = RouteParameter.Optional
            },
                constraints: null,
                handler: handler);

            // set up test execution routes
            var targetRegistry = new TestTargetRegistry(configuration);

            configuration.TestTargetsAre(targetRegistry);
            if (configureTargets != null)
            {
                configureTargets(targetRegistry);
            }

            testTypes = testTypes ?? Discover.ConcreteTypes()
                        .DerivedFrom(typeof(IMonitoringTest));

            var testDefinitions = testTypes.GetTestDefinitions();

            configuration.TestDefinitionsAre(testDefinitions);

            testDefinitions.Select(p => p.Value)
            .ForEach(test =>
            {
                var targetConstraint = new TargetConstraint(test);

                targetRegistry.ForEach(target =>
                {
                    Task.Run(() => targetConstraint.Match(target));
                });

                configuration.Routes.MapHttpRoute(
                    test.RouteName,
                    testRootRouteTemplate.AppendSegment(test.TestName),
                    defaults: new
                {
                    controller = "MonitoringTest",
                    action     = "run",
                    testName   = test.TestName
                },
                    constraints: new
                {
                    tag         = new TagConstraint(test),
                    application = new ApplicationConstraint(test),
                    environment = new EnvironmentConstraint(test),
                    target      = targetConstraint
                },
                    handler: handler
                    );
            });

            return(configuration);
        }
예제 #6
0
 public void ConcreteTypes_contains_non_abstract_non_generic_classes()
 {
     Discover.ConcreteTypes().Should().Contain(typeof(ConcreteHandlerOfMultipleCommands));
 }
예제 #7
0
 public void Concrete_types_does_not_include_enums()
 {
     Discover.ConcreteTypes().Should().NotContain(t => t == typeof(Enum));
 }
예제 #8
0
 public void Concrete_types_does_not_include_delegates()
 {
     Discover.ConcreteTypes().Should().NotContain(t => t == typeof(ADelegate));
 }
예제 #9
0
 public void Concrete_types_does_not_include_static_classes()
 {
     Discover.ConcreteTypes().Should().NotContain(t => t == typeof(StaticClass));
 }
예제 #10
0
 public void Concrete_types_does_not_include_abstract_classes()
 {
     Discover.ConcreteTypes().Should().NotContain(t => t.IsAbstract);
 }
예제 #11
0
 public void Concrete_types_does_not_include_interfaces()
 {
     Discover.ConcreteTypes().Should().NotContain(t => t.IsInterface);
 }