[4490] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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;
|
---|
[8086] | 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 |
|
---|
[4512] | 31 | namespace HeuristicLab_33.Tests {
|
---|
[4490] | 32 | [TestClass]
|
---|
| 33 | public class PluginDependenciesTest {
|
---|
| 34 | private static Dictionary<Assembly, Type> loadedPlugins;
|
---|
| 35 | private static Dictionary<string, string> pluginNames;
|
---|
[8086] | 36 | private static Dictionary<string, Assembly> pluginFilesToPluginLookup = new Dictionary<string, Assembly>();
|
---|
| 37 | //private static Dictionary<string, string> pluginToPluginFilesLookup = new Dictionary<string, string>();
|
---|
[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) {
|
---|
[8086] | 42 | loadedPlugins = PluginLoader.Assemblies.Where(PluginLoader.IsPluginAssembly).ToDictionary(a => a, GetPluginFromAssembly);
|
---|
[4632] | 43 | pluginNames = loadedPlugins.ToDictionary(a => a.Key.GetName().FullName, a => GetPluginName(a.Value));
|
---|
| 44 |
|
---|
[8086] | 45 | foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
|
---|
| 46 | Type pluginType = GetPluginFromAssembly(pluginAssembly);
|
---|
| 47 | var pluginFileAttributes = Attribute.GetCustomAttributes(pluginType, false).OfType<PluginFileAttribute>();
|
---|
| 48 | foreach (var pluginFileAttribute in pluginFileAttributes) {
|
---|
| 49 | string fillNameWithoutExtension = Path.GetFileNameWithoutExtension(pluginFileAttribute.FileName);
|
---|
| 50 | pluginFilesToPluginLookup.Add(fillNameWithoutExtension, pluginAssembly);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
[4490] | 53 | }
|
---|
| 54 |
|
---|
| 55 | [TestMethod]
|
---|
| 56 | public void CheckReferenceAssembliesForPluginDependencies() {
|
---|
| 57 | StringBuilder errorMessage = new StringBuilder();
|
---|
| 58 | foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
|
---|
| 59 | Type plugin = loadedPlugins[pluginAssembly];
|
---|
[8086] | 60 | Dictionary<string, PluginDependencyAttribute> pluginDependencies = Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a.Dependency);
|
---|
| 61 |
|
---|
| 62 | foreach (AssemblyName referencedAssemblyName in pluginAssembly.GetReferencedAssemblies()) {
|
---|
| 63 | if (IsPluginAssemblyName(referencedAssemblyName)) {
|
---|
| 64 | if (!pluginDependencies.ContainsKey(pluginNames[referencedAssemblyName.FullName]))
|
---|
| 65 | errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to referenced plugin " + pluginNames[referencedAssemblyName.FullName] + ".");
|
---|
| 66 | } else { //no plugin assembly => test if the assembly is delivered by another plugin
|
---|
| 67 | if (pluginFilesToPluginLookup.ContainsKey(referencedAssemblyName.Name)) {
|
---|
| 68 | string containingPluginFullName = pluginFilesToPluginLookup[referencedAssemblyName.Name].FullName;
|
---|
| 69 | if (containingPluginFullName != pluginAssembly.FullName && !pluginDependencies.ContainsKey(pluginNames[containingPluginFullName]))
|
---|
| 70 | errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to plugin " + pluginNames[containingPluginFullName] + " due to a reference to " + referencedAssemblyName.FullName + ".");
|
---|
| 71 | }
|
---|
[4490] | 72 | }
|
---|
[8086] | 73 | }
|
---|
[4490] | 74 | }
|
---|
| 75 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | [TestMethod]
|
---|
| 79 | public void CheckPluginDependenciesForReferencedAssemblies() {
|
---|
| 80 | StringBuilder errorMessage = new StringBuilder();
|
---|
| 81 | foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
|
---|
| 82 | Type plugin = loadedPlugins[pluginAssembly];
|
---|
[8086] | 83 | Dictionary<PluginDependencyAttribute, string> pluginDependencies = Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a, a => a.Dependency);
|
---|
[4490] | 84 |
|
---|
| 85 | foreach (PluginDependencyAttribute attribute in pluginDependencies.Keys) {
|
---|
| 86 | string pluginDependencyName = pluginDependencies[attribute];
|
---|
[8086] | 87 |
|
---|
| 88 | if (pluginDependencyName == "HeuristicLab.MathJax") continue; //is never referenced as this plugin contains HTML files
|
---|
| 89 | var referencedPluginAssemblies = pluginAssembly.GetReferencedAssemblies().Where(IsPluginAssemblyName);
|
---|
| 90 | if (referencedPluginAssemblies.Any(a => pluginNames[a.FullName] == pluginDependencyName)) continue;
|
---|
| 91 |
|
---|
| 92 | var referencedNonPluginAssemblies = pluginAssembly.GetReferencedAssemblies().Where(a => !IsPluginAssemblyName(a));
|
---|
| 93 | bool found = (from referencedNonPluginAssemblie in referencedNonPluginAssemblies
|
---|
| 94 | select referencedNonPluginAssemblie.Name into assemblyName
|
---|
| 95 | where pluginFilesToPluginLookup.ContainsKey(assemblyName)
|
---|
| 96 | select GetPluginFromAssembly(pluginFilesToPluginLookup[assemblyName]) into pluginType
|
---|
| 97 | select GetPluginName(pluginType)).Any(pluginName => pluginName == pluginDependencyName);
|
---|
| 98 |
|
---|
| 99 | if (!found) errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + ".");
|
---|
[4490] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | private static Type GetPluginFromAssembly(Assembly assembly) {
|
---|
| 106 | return assembly.GetExportedTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).FirstOrDefault();
|
---|
| 107 | }
|
---|
[8086] | 108 |
|
---|
[4490] | 109 | private static string GetPluginName(Type plugin) {
|
---|
| 110 | string name = string.Empty;
|
---|
| 111 | PluginAttribute pluginAttribute = (PluginAttribute)Attribute.GetCustomAttribute(plugin, typeof(PluginAttribute));
|
---|
| 112 | if (pluginAttribute != null)
|
---|
| 113 | name = pluginAttribute.Name;
|
---|
| 114 | return name;
|
---|
| 115 | }
|
---|
[8086] | 116 |
|
---|
| 117 | private static bool IsPluginAssemblyName(AssemblyName assemblyName) {
|
---|
| 118 | return pluginNames.ContainsKey(assemblyName.FullName);
|
---|
| 119 | }
|
---|
[4490] | 120 | }
|
---|
| 121 | }
|
---|