Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab-3.3/PluginDependenciesTest.cs

Last change on this file was 17940, checked in by gkronber, 3 years ago

#3117: changed plugin dependency test case to allow plugin dependencies to the same plugin in different versions.

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