Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/PluginDependenciesTest.cs @ 7849

Last change on this file since 7849 was 7450, checked in by gkronber, 13 years ago

#1773 removed reference to plugin HeuristicLab.MathJax-1.1 but added mathjax to the list of extlibs to fix the plugin-dependency checker unit test.

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.MathJax");
48      extLibPluginNames.Add("HeuristicLab.Netron");
49      extLibPluginNames.Add("HeuristicLab.ProtobufCS");
50      extLibPluginNames.Add("HeuristicLab.SharpDevelop");
51      extLibPluginNames.Add("HeuristicLab.WinFormsUI");
52    }
53
54    [TestMethod]
55    public void CheckReferenceAssembliesForPluginDependencies() {
56      StringBuilder errorMessage = new StringBuilder();
57      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
58        Type plugin = loadedPlugins[pluginAssembly];
59        Dictionary<string, PluginDependencyAttribute> pluginDependencies =
60          Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a.Dependency);
61        foreach (AssemblyName referencedPluginName in pluginAssembly.GetReferencedAssemblies())
62          if (pluginNames.ContainsKey(referencedPluginName.FullName)) {  //check if reference assembly is a plugin
63            if (!pluginDependencies.ContainsKey(pluginNames[referencedPluginName.FullName]))
64              errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to referenced plugin " + pluginNames[referencedPluginName.FullName] + ".");
65          }
66      }
67
68      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
69    }
70
71    [TestMethod]
72    public void CheckPluginDependenciesForReferencedAssemblies() {
73      StringBuilder errorMessage = new StringBuilder();
74      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
75        Type plugin = loadedPlugins[pluginAssembly];
76        Dictionary<PluginDependencyAttribute, string> pluginDependencies =
77          Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a, a => a.Dependency);
78
79        foreach (PluginDependencyAttribute attribute in pluginDependencies.Keys) {
80          string pluginDependencyName = pluginDependencies[attribute];
81          //do not check extlib plugins, because the transport assemblies are never referenced in the assemblies
82          if (extLibPluginNames.Contains(pluginDependencyName)) continue;
83          if (pluginAssembly.GetReferencedAssemblies().Where(a => pluginNames.ContainsKey(a.FullName))
84            .All(a => pluginNames[a.FullName] != pluginDependencyName)) {
85            errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + ".");
86          }
87        }
88      }
89
90      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
91    }
92
93    private static Type GetPluginFromAssembly(Assembly assembly) {
94      return assembly.GetExportedTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).FirstOrDefault();
95    }
96    private static string GetPluginName(Type plugin) {
97      string name = string.Empty;
98      PluginAttribute pluginAttribute = (PluginAttribute)Attribute.GetCustomAttribute(plugin, typeof(PluginAttribute));
99      if (pluginAttribute != null)
100        name = pluginAttribute.Name;
101      return name;
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.