Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/PluginDependenciesTest.cs @ 18242

Last change on this file since 18242 was 17865, checked in by gkronber, 4 years ago

#3089: merged r17781 from trunk to stable

File size: 7.8 KB
RevLine 
[4490]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4490]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;
[7947]24using System.IO;
[4490]25using System.Linq;
26using System.Reflection;
27using System.Text;
28using HeuristicLab.PluginInfrastructure;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
[9885]31namespace HeuristicLab.Tests {
[4490]32  [TestClass]
33  public class PluginDependenciesTest {
34    private static Dictionary<Assembly, Type> loadedPlugins;
35    private static Dictionary<string, string> pluginNames;
[7947]36    private static Dictionary<string, Assembly> pluginFilesToPluginLookup = new Dictionary<string, Assembly>();
[4490]37
38    // Use ClassInitialize to run code before running the first test in the class
39    [ClassInitialize]
40    public static void MyClassInitialize(TestContext testContext) {
[17865]41      try {
42        loadedPlugins = PluginLoader.Assemblies.Where(PluginLoader.IsPluginAssembly).ToDictionary(a => a, GetPluginFromAssembly);
43      } catch (BadImageFormatException e) {
44        var message = string.Join(Environment.NewLine, "Could not load all types. Check if test process architecture is set to x64.", string.Empty, "Exception:", e);
45        Assert.Fail(message);
46      }
47
[4632]48      pluginNames = loadedPlugins.ToDictionary(a => a.Key.GetName().FullName, a => GetPluginName(a.Value));
49
[7947]50      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
51        Type pluginType = GetPluginFromAssembly(pluginAssembly);
52        var pluginFileAttributes = Attribute.GetCustomAttributes(pluginType, false).OfType<PluginFileAttribute>();
53        foreach (var pluginFileAttribute in pluginFileAttributes) {
54          string fillNameWithoutExtension = Path.GetFileNameWithoutExtension(pluginFileAttribute.FileName);
55          pluginFilesToPluginLookup.Add(fillNameWithoutExtension, pluginAssembly);
56        }
57      }
[4490]58    }
59
60    [TestMethod]
[9885]61    [TestCategory("General")]
62    [TestCategory("Essential")]
63    [TestProperty("Time", "short")]
[4490]64    public void CheckReferenceAssembliesForPluginDependencies() {
65      StringBuilder errorMessage = new StringBuilder();
66      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
67        Type plugin = loadedPlugins[pluginAssembly];
[12608]68        var pluginFiles = new HashSet<string>(Attribute.GetCustomAttributes(plugin, false)
69            .OfType<PluginFileAttribute>().Where(pf => pf.FileType == PluginFileType.Assembly).Select(pf => pf.FileName));
70        var pluginAssemblies = PluginLoader.Assemblies.Where(a => pluginFiles.Contains(Path.GetFileName(a.Location))).ToList();
71        var referencedAssemblies = pluginAssemblies.SelectMany(a => a.GetReferencedAssemblies()).ToList();
72
[7947]73        Dictionary<string, PluginDependencyAttribute> pluginDependencies = Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a.Dependency);
74
[12608]75        foreach (AssemblyName referencedAssemblyName in referencedAssemblies) {
[7947]76          if (IsPluginAssemblyName(referencedAssemblyName)) {
77            if (!pluginDependencies.ContainsKey(pluginNames[referencedAssemblyName.FullName]))
78              errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to referenced plugin " + pluginNames[referencedAssemblyName.FullName] + ".");
79          } else { //no plugin assembly => test if the assembly is delivered by another plugin
80            if (pluginFilesToPluginLookup.ContainsKey(referencedAssemblyName.Name)) {
81              string containingPluginFullName = pluginFilesToPluginLookup[referencedAssemblyName.Name].FullName;
82              if (containingPluginFullName != pluginAssembly.FullName && !pluginDependencies.ContainsKey(pluginNames[containingPluginFullName]))
83                errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to plugin " + pluginNames[containingPluginFullName] + " due to a reference to " + referencedAssemblyName.FullName + ".");
84            }
[4490]85          }
[7947]86        }
[4490]87      }
88      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
89    }
90
91    [TestMethod]
[9885]92    [TestCategory("General")]
93    [TestCategory("Essential")]
94    [TestProperty("Time", "short")]
[4490]95    public void CheckPluginDependenciesForReferencedAssemblies() {
96      StringBuilder errorMessage = new StringBuilder();
97      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
98        Type plugin = loadedPlugins[pluginAssembly];
[7947]99        Dictionary<PluginDependencyAttribute, string> pluginDependencies = Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a, a => a.Dependency);
[12608]100        var pluginFiles = new HashSet<string>(Attribute.GetCustomAttributes(plugin, false)
101          .OfType<PluginFileAttribute>().Where(pf => pf.FileType == PluginFileType.Assembly).Select(pf => pf.FileName));
102        var pluginAssemblies = PluginLoader.Assemblies.Where(a => pluginFiles.Contains(Path.GetFileName(a.Location))).ToList();
103        var referencedAssemblies = pluginAssemblies.SelectMany(a => a.GetReferencedAssemblies()).ToList();
[4490]104
105        foreach (PluginDependencyAttribute attribute in pluginDependencies.Keys) {
106          string pluginDependencyName = pluginDependencies[attribute];
[7948]107
108          if (pluginDependencyName == "HeuristicLab.MathJax") continue; //is never referenced as this plugin contains HTML files
[11155]109          if (pluginDependencyName == "HeuristicLab.MatlabConnector") continue; //the matlab connector is loaded dynamically and hence not referenced by the dll
[12608]110          var referencedPluginAssemblies = referencedAssemblies.Where(IsPluginAssemblyName);
[7947]111          if (referencedPluginAssemblies.Any(a => pluginNames[a.FullName] == pluginDependencyName)) continue;
112
[12608]113          var referencedNonPluginAssemblies = referencedAssemblies.Where(a => !IsPluginAssemblyName(a));
[8164]114          bool found = (from referencedNonPluginAssembly in referencedNonPluginAssemblies
115                        select referencedNonPluginAssembly.Name into assemblyName
[7947]116                        where pluginFilesToPluginLookup.ContainsKey(assemblyName)
117                        select GetPluginFromAssembly(pluginFilesToPluginLookup[assemblyName]) into pluginType
118                        select GetPluginName(pluginType)).Any(pluginName => pluginName == pluginDependencyName);
119
120          if (!found) errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + ".");
[4490]121        }
122      }
123      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
124    }
125
126    private static Type GetPluginFromAssembly(Assembly assembly) {
[9885]127      return assembly.GetExportedTypes().FirstOrDefault(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
[4490]128    }
[7947]129
[4490]130    private static string GetPluginName(Type plugin) {
131      string name = string.Empty;
132      PluginAttribute pluginAttribute = (PluginAttribute)Attribute.GetCustomAttribute(plugin, typeof(PluginAttribute));
133      if (pluginAttribute != null)
134        name = pluginAttribute.Name;
135      return name;
136    }
[7947]137
138    private static bool IsPluginAssemblyName(AssemblyName assemblyName) {
139      return pluginNames.ContainsKey(assemblyName.FullName);
140    }
[4490]141  }
142}
Note: See TracBrowser for help on using the repository browser.