Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12371 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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