[4490] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[7947] | 24 | using System.IO;
|
---|
[4490] | 25 | using System.Linq;
|
---|
| 26 | using System.Reflection;
|
---|
| 27 | using System.Text;
|
---|
| 28 | using HeuristicLab.PluginInfrastructure;
|
---|
| 29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 30 |
|
---|
[9764] | 31 | namespace 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) {
|
---|
[7947] | 41 | loadedPlugins = PluginLoader.Assemblies.Where(PluginLoader.IsPluginAssembly).ToDictionary(a => a, GetPluginFromAssembly);
|
---|
[4632] | 42 | pluginNames = loadedPlugins.ToDictionary(a => a.Key.GetName().FullName, a => GetPluginName(a.Value));
|
---|
| 43 |
|
---|
[7947] | 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 | }
|
---|
[4490] | 52 | }
|
---|
| 53 |
|
---|
| 54 | [TestMethod]
|
---|
[9783] | 55 | [TestCategory("General")]
|
---|
| 56 | [TestCategory("Essential")]
|
---|
| 57 | [TestProperty("Time", "short")]
|
---|
[4490] | 58 | public void CheckReferenceAssembliesForPluginDependencies() {
|
---|
| 59 | StringBuilder errorMessage = new StringBuilder();
|
---|
| 60 | foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
|
---|
| 61 | Type plugin = loadedPlugins[pluginAssembly];
|
---|
[12522] | 62 | var pluginFiles = new HashSet<string>(Attribute.GetCustomAttributes(plugin, false)
|
---|
| 63 | .OfType<PluginFileAttribute>().Where(pf => pf.FileType == PluginFileType.Assembly).Select(pf => pf.FileName));
|
---|
| 64 | var pluginAssemblies = PluginLoader.Assemblies.Where(a => pluginFiles.Contains(Path.GetFileName(a.Location))).ToList();
|
---|
| 65 | var referencedAssemblies = pluginAssemblies.SelectMany(a => a.GetReferencedAssemblies()).ToList();
|
---|
| 66 |
|
---|
[7947] | 67 | Dictionary<string, PluginDependencyAttribute> pluginDependencies = Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a.Dependency);
|
---|
| 68 |
|
---|
[12522] | 69 | foreach (AssemblyName referencedAssemblyName in referencedAssemblies) {
|
---|
[7947] | 70 | if (IsPluginAssemblyName(referencedAssemblyName)) {
|
---|
| 71 | if (!pluginDependencies.ContainsKey(pluginNames[referencedAssemblyName.FullName]))
|
---|
| 72 | errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to referenced plugin " + pluginNames[referencedAssemblyName.FullName] + ".");
|
---|
| 73 | } else { //no plugin assembly => test if the assembly is delivered by another plugin
|
---|
| 74 | if (pluginFilesToPluginLookup.ContainsKey(referencedAssemblyName.Name)) {
|
---|
| 75 | string containingPluginFullName = pluginFilesToPluginLookup[referencedAssemblyName.Name].FullName;
|
---|
| 76 | if (containingPluginFullName != pluginAssembly.FullName && !pluginDependencies.ContainsKey(pluginNames[containingPluginFullName]))
|
---|
| 77 | errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to plugin " + pluginNames[containingPluginFullName] + " due to a reference to " + referencedAssemblyName.FullName + ".");
|
---|
| 78 | }
|
---|
[4490] | 79 | }
|
---|
[7947] | 80 | }
|
---|
[4490] | 81 | }
|
---|
| 82 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | [TestMethod]
|
---|
[9783] | 86 | [TestCategory("General")]
|
---|
| 87 | [TestCategory("Essential")]
|
---|
| 88 | [TestProperty("Time", "short")]
|
---|
[4490] | 89 | public void CheckPluginDependenciesForReferencedAssemblies() {
|
---|
| 90 | StringBuilder errorMessage = new StringBuilder();
|
---|
| 91 | foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
|
---|
| 92 | Type plugin = loadedPlugins[pluginAssembly];
|
---|
[7947] | 93 | Dictionary<PluginDependencyAttribute, string> pluginDependencies = Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a, a => a.Dependency);
|
---|
[12522] | 94 | var pluginFiles = new HashSet<string>(Attribute.GetCustomAttributes(plugin, false)
|
---|
| 95 | .OfType<PluginFileAttribute>().Where(pf => pf.FileType == PluginFileType.Assembly).Select(pf => pf.FileName));
|
---|
| 96 | var pluginAssemblies = PluginLoader.Assemblies.Where(a => pluginFiles.Contains(Path.GetFileName(a.Location))).ToList();
|
---|
| 97 | var referencedAssemblies = pluginAssemblies.SelectMany(a => a.GetReferencedAssemblies()).ToList();
|
---|
[4490] | 98 |
|
---|
| 99 | foreach (PluginDependencyAttribute attribute in pluginDependencies.Keys) {
|
---|
| 100 | string pluginDependencyName = pluginDependencies[attribute];
|
---|
[7948] | 101 |
|
---|
| 102 | if (pluginDependencyName == "HeuristicLab.MathJax") continue; //is never referenced as this plugin contains HTML files
|
---|
[11127] | 103 | if (pluginDependencyName == "HeuristicLab.MatlabConnector") continue; //the matlab connector is loaded dynamically and hence not referenced by the dll
|
---|
[12522] | 104 | var referencedPluginAssemblies = referencedAssemblies.Where(IsPluginAssemblyName);
|
---|
[7947] | 105 | if (referencedPluginAssemblies.Any(a => pluginNames[a.FullName] == pluginDependencyName)) continue;
|
---|
| 106 |
|
---|
[12522] | 107 | var referencedNonPluginAssemblies = referencedAssemblies.Where(a => !IsPluginAssemblyName(a));
|
---|
[8164] | 108 | bool found = (from referencedNonPluginAssembly in referencedNonPluginAssemblies
|
---|
| 109 | select referencedNonPluginAssembly.Name into assemblyName
|
---|
[7947] | 110 | where pluginFilesToPluginLookup.ContainsKey(assemblyName)
|
---|
| 111 | select GetPluginFromAssembly(pluginFilesToPluginLookup[assemblyName]) into pluginType
|
---|
| 112 | select GetPluginName(pluginType)).Any(pluginName => pluginName == pluginDependencyName);
|
---|
| 113 |
|
---|
| 114 | if (!found) errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + ".");
|
---|
[4490] | 115 | }
|
---|
| 116 | }
|
---|
| 117 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private static Type GetPluginFromAssembly(Assembly assembly) {
|
---|
[9783] | 121 | return assembly.GetExportedTypes().FirstOrDefault(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
|
---|
[4490] | 122 | }
|
---|
[7947] | 123 |
|
---|
[4490] | 124 | private static string GetPluginName(Type plugin) {
|
---|
| 125 | string name = string.Empty;
|
---|
| 126 | PluginAttribute pluginAttribute = (PluginAttribute)Attribute.GetCustomAttribute(plugin, typeof(PluginAttribute));
|
---|
| 127 | if (pluginAttribute != null)
|
---|
| 128 | name = pluginAttribute.Name;
|
---|
| 129 | return name;
|
---|
| 130 | }
|
---|
[7947] | 131 |
|
---|
| 132 | private static bool IsPluginAssemblyName(AssemblyName assemblyName) {
|
---|
| 133 | return pluginNames.ContainsKey(assemblyName.FullName);
|
---|
| 134 | }
|
---|
[4490] | 135 | }
|
---|
| 136 | }
|
---|