Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab/3.3/Tests/PluginDependenciesTest.cs @ 6230

Last change on this file since 6230 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Reflection;
26using System.Text;
27using HeuristicLab.PluginInfrastructure;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab_33.Tests {
31  [TestClass]
32  public class PluginDependenciesTest {
33    private static Dictionary<Assembly, Type> loadedPlugins;
34    private static Dictionary<string, string> pluginNames;
35    private static HashSet<string> extLibPluginNames;
36
37    // Use ClassInitialize to run code before running the first test in the class
38    [ClassInitialize]
39    public static void MyClassInitialize(TestContext testContext) {
40      loadedPlugins = PluginLoader.Assemblies.Where(x => PluginLoader.IsPluginAssembly(x)).ToDictionary(a => a, GetPluginFromAssembly);
41      pluginNames = loadedPlugins.ToDictionary(a => a.Key.GetName().FullName, a => GetPluginName(a.Value));
42
43      extLibPluginNames = new HashSet<string>();
44      extLibPluginNames.Add("HeuristicLab.ALGLIB");
45      extLibPluginNames.Add("HeuristicLab.LibSVM");
46      extLibPluginNames.Add("HeuristicLab.log4net");
47      extLibPluginNames.Add("HeuristicLab.Netron");
48      extLibPluginNames.Add("HeuristicLab.ProtobufCS");
49      extLibPluginNames.Add("HeuristicLab.SharpDevelop");
50      extLibPluginNames.Add("HeuristicLab.WinFormsUI");
51    }
52
53    [TestMethod]
54    public void CheckReferenceAssembliesForPluginDependencies() {
55      StringBuilder errorMessage = new StringBuilder();
56      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
57        Type plugin = loadedPlugins[pluginAssembly];
58        Dictionary<string, PluginDependencyAttribute> pluginDependencies =
59          Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a.Dependency);
60        foreach (AssemblyName referencedPluginName in pluginAssembly.GetReferencedAssemblies())
61          if (pluginNames.ContainsKey(referencedPluginName.FullName)) {  //check if reference assembly is a plugin
62            if (!pluginDependencies.ContainsKey(pluginNames[referencedPluginName.FullName]))
63              errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to referenced plugin " + pluginNames[referencedPluginName.FullName] + ".");
64          }
65      }
66
67      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
68    }
69
70    [TestMethod]
71    public void CheckPluginDependenciesForReferencedAssemblies() {
72      StringBuilder errorMessage = new StringBuilder();
73      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
74        Type plugin = loadedPlugins[pluginAssembly];
75        Dictionary<PluginDependencyAttribute, string> pluginDependencies =
76          Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a, a => a.Dependency);
77
78        foreach (PluginDependencyAttribute attribute in pluginDependencies.Keys) {
79          string pluginDependencyName = pluginDependencies[attribute];
80          //do not check extlib plugins, because the transport assemblies are never referenced in the assemblies
81          if (extLibPluginNames.Contains(pluginDependencyName)) continue;
82          if (pluginAssembly.GetReferencedAssemblies().Where(a => pluginNames.ContainsKey(a.FullName))
83            .All(a => pluginNames[a.FullName] != pluginDependencyName)) {
84            errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + ".");
85          }
86        }
87      }
88
89      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
90    }
91
92    private static Type GetPluginFromAssembly(Assembly assembly) {
93      return assembly.GetExportedTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).FirstOrDefault();
94    }
95    private static string GetPluginName(Type plugin) {
96      string name = string.Empty;
97      PluginAttribute pluginAttribute = (PluginAttribute)Attribute.GetCustomAttribute(plugin, typeof(PluginAttribute));
98      if (pluginAttribute != null)
99        name = pluginAttribute.Name;
100      return name;
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.