Ejemplo n.º 1
0
 ResolverImpl(ITestHelperConfiguration config)
 {
     _container = new SimpleServiceContainer();
     _container.Add(config);
     _config       = config;
     TransientMode = config.GetBoolean("TestHelper/TransientMode") ?? false;
     string[] assemblies = config.Get("TestHelper/PreLoadedAssemblies", String.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
     if (assemblies.Length > 0)
     {
         using (WeakAssemblyNameResolver.TemporaryInstall())
         {
             var types = new List <Type>();
             foreach (var n in assemblies)
             {
                 var a = Assembly.Load(n);
                 types.AddRange(a.GetExportedTypes().Where(t => t.IsInterface && typeof(IMixinTestHelper).IsAssignableFrom(t)));
             }
             _preLoadedTypes = types;
             if (!TransientMode)
             {
                 var ctx = new Context(_container);
                 foreach (var preLoad in _preLoadedTypes)
                 {
                     Resolve(ctx, preLoad);
                 }
             }
         }
     }
     else
     {
         _preLoadedTypes = Type.EmptyTypes;
     }
 }
Ejemplo n.º 2
0
        Type MapType(Type t, bool throwOnError)
        {
            Debug.Assert(t != typeof(ITestHelperResolvedCallback) && t != typeof(IMixinTestHelper));
            string typeName = _config.Get("TestHelper/" + t.FullName);

            if (typeName != null)
            {
                // Always throw when config is used.
                Type fromConfig = SimpleTypeFinder.WeakResolver(typeName, true);
                if (typeof(IMixinTestHelper).IsAssignableFrom(fromConfig))
                {
                    throw new Exception($"Mapped type '{fromConfig.FullName}' is a Mixin. It can not be explicitely implemented.");
                }
                return(fromConfig);
            }
            if (t.IsInterface && t.Name[0] == 'I')
            {
                var    cName    = t.Name.Substring(1);
                string fullName = $"{t.Namespace}.{cName}, {t.Assembly.FullName}";
                Type   found    = SimpleTypeFinder.WeakResolver(fullName, false);
                if (found == null && cName.EndsWith("Core"))
                {
                    var nameNoCore = cName.Remove(cName.Length - 4);
                    var ns         = t.Namespace.Split('.').ToList();
                    while (ns.Count > 0)
                    {
                        fullName = $"{String.Join(".", ns)}.{nameNoCore}, {t.Assembly.FullName}";
                        found    = SimpleTypeFinder.WeakResolver(fullName, false);
                        if (found != null)
                        {
                            break;
                        }
                        ns.RemoveAt(ns.Count - 1);
                    }
                }
                if (found != null && t.IsAssignableFrom(found))
                {
                    return(found);
                }
                if (typeof(IMixinTestHelper).IsAssignableFrom(t))
                {
                    if (t.GetMembers().Length > 0)
                    {
                        throw new Exception($"Interface '{t.FullName}' is a Mixin. It can not have members of its own.");
                    }
                    return(MixinType.Create(t));
                }
            }
            if (!throwOnError)
            {
                return(null);
            }
            throw new Exception($"Unable to locate an implementation for {t.AssemblyQualifiedName}.");
        }
        /// <summary>
        /// Gets an integer configuration value.
        /// </summary>
        /// <param name="this">This configuration.</param>
        /// <param name="key">The configuration entry key.</param>
        /// <returns>The value or null if not found.</returns>
        public static int?GetInt32(this ITestHelperConfiguration @this, NormalizedPath key)
        {
            var s = @this.Get(key);

            return(s == null ? (int?)null : Int32.Parse(s));
        }
        /// <summary>
        /// Gets a boolean configuration value.
        /// </summary>
        /// <param name="this">This configuration.</param>
        /// <param name="key">The configuration entry key.</param>
        /// <returns>The value or null if not found.</returns>
        public static bool?GetBoolean(this ITestHelperConfiguration @this, NormalizedPath key)
        {
            var s = @this.Get(key);

            return(s == null ? (bool?)null : StringComparer.OrdinalIgnoreCase.Equals(s, "true"));
        }
Ejemplo n.º 5
0
        internal MonitorTestHelper(ITestHelperConfiguration config, IBasicTestHelper basic)
        {
            _config = config;
            _basic  = basic;

            basic.OnlyOnce(() =>
            {
                _logToBinFile = _config.GetBoolean("Monitor/LogToBinFile")
                                ?? _config.GetBoolean("Monitor/LogToBinFiles")
                                ?? false;
                _logToTextFile = _config.GetBoolean("Monitor/LogToTextFile")
                                 ?? _config.GetBoolean("Monitor/LogToTextFiles")
                                 ?? false;

                string logLevel = _config.Get("Monitor/LogLevel");
                if (logLevel != null)
                {
                    var lf = LogFilter.Parse(logLevel);
                    ActivityMonitor.DefaultFilter = lf;
                }
                LogFile.RootLogPath = basic.LogFolder;
                var conf            = new GrandOutputConfiguration();
                if (_logToBinFile)
                {
                    var binConf = new BinaryFileConfiguration
                    {
                        UseGzipCompression = true,
                        Path = FileUtil.CreateUniqueTimedFolder(LogFile.RootLogPath + "CKMon/", null, DateTime.UtcNow)
                    };
                    conf.AddHandler(binConf);
                }
                if (_logToTextFile)
                {
                    var txtConf = new TextFileConfiguration
                    {
                        Path = FileUtil.CreateUniqueTimedFolder(LogFile.RootLogPath + "Text/", null, DateTime.UtcNow)
                    };
                    conf.AddHandler(txtConf);
                }
                GrandOutput.EnsureActiveDefault(conf, clearExistingTraceListeners: false);
                var monitorListener = Trace.Listeners.OfType <MonitorTraceListener>().FirstOrDefault(m => m.GrandOutput == GrandOutput.Default);
                // (Defensive programming) There is no real reason for this listener to not be in the listeners, but it can be.
                if (monitorListener != null)
                {
                    // If our standard MonitorTraceListener has been injected, then we remove the StaticBasicTestHelper.SafeTraceListener
                    // that throws Exceptions instead of callinf FailFast.
                    Trace.Listeners.Remove("CK.Testing.SafeTraceListener");
                }
            });
            _monitor               = new ActivityMonitor("MonitorTestHelper");
            _console               = new ActivityMonitorConsoleClient();
            LogToConsole           = _config.GetBoolean("Monitor/LogToConsole") ?? false;
            basic.OnCleanupFolder += OnCleanupFolder;
            basic.OnlyOnce(() =>
            {
                var basePath = LogFile.RootLogPath + "Text" + FileUtil.DirectorySeparatorString;
                if (Directory.Exists(basePath))
                {
                    CleanupTimedFolders(_monitor, _basic, basePath, MaxCurrentLogFolderCount, MaxArchivedLogFolderCount);
                }
                basePath = LogFile.RootLogPath + "CKMon" + FileUtil.DirectorySeparatorString;
                if (Directory.Exists(basePath))
                {
                    CleanupTimedFolders(_monitor, _basic, basePath, MaxCurrentLogFolderCount, MaxArchivedLogFolderCount);
                }
            });
        }