/// <summary>
        /// Initializes the runtime.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Loads plugins and initalizes the runtime component model.  The
        /// specifics of the system can be configured by editing the appropriate
        /// *.plugin files to register new components and facilities as required.
        /// </para>
        /// </remarks>
        /// <param name="setup">The runtime setup parameters.</param>
        /// <param name="logger">The logger to attach, or null if none.</param>
        /// <returns>An object that when disposed automatically calls <see cref="Shutdown" />.
        /// This is particularly useful in combination with the C# "using" statement
        /// or its equivalent.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="setup"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the runtime has already been initialized.</exception>
        public static IDisposable Initialize(RuntimeSetup setup, ILogger logger)
        {
            if (setup == null)
                throw new ArgumentNullException("setup");

            if (RuntimeAccessor.IsInitialized)
                throw new InvalidOperationException("The runtime has already been initialized.");

            var registry = new Registry();
            var assemblyLoader = new DefaultAssemblyLoader();
            var pluginLoader = new CachingPluginLoader();
            IRuntime runtime = new DefaultRuntime(registry, pluginLoader, assemblyLoader, setup); // TODO: make me configurable via setup
            if (logger != null)
                runtime.AddLogListener(logger);

            try
            {
                RuntimeAccessor.SetRuntime(runtime);

                runtime.Initialize();

                if (!UnhandledExceptionPolicy.HasReportUnhandledExceptionHandler)
                    UnhandledExceptionPolicy.ReportUnhandledException += HandleUnhandledExceptionNotification;
            }
            catch (Exception)
            {
                RuntimeAccessor.SetRuntime(null);
                throw;
            }

            return new AutoShutdown();
        }
        public void PopulateCatalog_WhenPluginXmlContainsPreprocessorInstructions_AppliesThem()
        {
            string pluginContents = "<plugin pluginId=\"pluginId\" xmlns=\"http://www.gallio.org/\"><traits><?ifdef A?><name>A</name><?endif?><?ifdef B?><property>B</property><?endif?></traits></plugin>";

            PluginLoaderTest.RunWithTemporaryPluginFile((pluginDir, pluginFile) =>
            {
                Guid installationId = Guid.NewGuid();
                var loader = new CachingPluginLoader();
                loader.InstallationId = installationId;
                loader.AddPluginPath(pluginFile);
                loader.DefinePreprocessorConstant("A");

                Hash64 hash = new Hash64().Add(pluginFile).Add("A").Add(installationId.ToString());
                var cacheDir = CachingPluginLoader.GetCurrentUserPluginCacheDir();
                string cacheFilePath = Path.Combine(cacheDir, hash + ".xml");

                if (System.IO.File.Exists(cacheFilePath))
                    System.IO.File.Delete(cacheFilePath);

                // First pass.
                {
                    Plugin plugin = null;
                    var catalog = MockRepository.GenerateMock<IPluginCatalog>();
                    catalog.Expect(x => x.AddPlugin(null, null)).IgnoreArguments()
                        .Do((Action<Plugin, DirectoryInfo>)delegate(Plugin pluginArg, DirectoryInfo baseDirectoryArg)
                        {
                            plugin = pluginArg;
                        });

                    loader.PopulateCatalog(catalog, NullProgressMonitor.CreateInstance());

                    catalog.VerifyAllExpectations(); // added one plugin

                    Assert.AreEqual(new PropertySet() { { "name", "A" } }, plugin.Traits.PropertySet);
                }

                // Check cache file.
                {
                    Assert.IsTrue(File.Exists(cacheFilePath));

                    Cache cache = Assert.XmlDeserialize<Cache>(File.ReadAllText(cacheFilePath));

                    Assert.AreEqual(installationId.ToString(), cache.InstallationId);
                    Assert.AreEqual(1, cache.PluginInfos.Count);
                    Assert.AreEqual(pluginDir, cache.PluginInfos[0].BaseDirectory);
                    Assert.AreEqual("pluginId", cache.PluginInfos[0].Plugin.PluginId);
                    Assert.AreEqual(pluginFile, cache.PluginInfos[0].PluginFile);
                    Assert.AreEqual(File.GetLastWriteTimeUtc(pluginFile), cache.PluginInfos[0].PluginFileModificationTime);
                }

                // Second pass should restore from cache.
                {
                    Plugin plugin = null;
                    var catalog = MockRepository.GenerateMock<IPluginCatalog>();
                    catalog.Expect(x => x.AddPlugin(null, null)).IgnoreArguments()
                        .Do((Action<Plugin, DirectoryInfo>)delegate(Plugin pluginArg, DirectoryInfo baseDirectoryArg)
                        {
                            plugin = pluginArg;
                        });

                    loader.PopulateCatalog(catalog, NullProgressMonitor.CreateInstance());

                    catalog.VerifyAllExpectations(); // added one plugin

                    Assert.AreEqual(new PropertySet() { { "name", "A" } }, plugin.Traits.PropertySet);
                }
            }, pluginContents);
        }