Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4490 was 4490, checked in by mkommend, 14 years ago

Created PluginDependenciesTest and corrected all projects (ticket #1202).

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
25using System.Reflection;
26using System.Text;
27using HeuristicLab.PluginInfrastructure;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab_3._3.Tests {
31  [TestClass]
32  public class PluginDependenciesTest {
33    private static Dictionary<Assembly, Type> loadedPlugins;
34    private static Dictionary<string, string> pluginNames;
35
36    // Use ClassInitialize to run code before running the first test in the class
37    [ClassInitialize]
38    public static void MyClassInitialize(TestContext testContext) {
39      loadedPlugins = PluginLoader.pluginAssemblies.ToDictionary(a => a, GetPluginFromAssembly);
40      pluginNames = loadedPlugins.ToDictionary(a => a.Key.GetName().FullName,
41                                                                          a => GetPluginName(a.Value));
42    }
43
44    [TestMethod]
45    public void CheckReferenceAssembliesForPluginDependencies() {
46      StringBuilder errorMessage = new StringBuilder();
47      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
48        Type plugin = loadedPlugins[pluginAssembly];
49        Dictionary<string, PluginDependencyAttribute> pluginDependencies =
50          Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a.Dependency);
51        foreach (AssemblyName referencedPluginName in pluginAssembly.GetReferencedAssemblies())
52          if (pluginNames.ContainsKey(referencedPluginName.FullName)) {  //check if reference assembly is a plugin
53            if (!pluginDependencies.ContainsKey(pluginNames[referencedPluginName.FullName]))
54              errorMessage.AppendLine("Missing dependency in plugin " + plugin + " to referenced plugin " + pluginNames[referencedPluginName.FullName] + ".");
55          }
56      }
57
58      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
59    }
60
61    [TestMethod]
62    public void CheckPluginDependenciesForReferencedAssemblies() {
63      StringBuilder errorMessage = new StringBuilder();
64      foreach (Assembly pluginAssembly in loadedPlugins.Keys) {
65        Type plugin = loadedPlugins[pluginAssembly];
66        Dictionary<PluginDependencyAttribute, string> pluginDependencies =
67          Attribute.GetCustomAttributes(plugin, false).OfType<PluginDependencyAttribute>().ToDictionary(a => a, a => a.Dependency);
68
69        foreach (PluginDependencyAttribute attribute in pluginDependencies.Keys) {
70          string pluginDependencyName = pluginDependencies[attribute];
71          if (pluginAssembly.GetReferencedAssemblies()
72            .All(a => pluginNames.ContainsKey(a.FullName) && pluginNames[a.FullName] != pluginDependencyName)) {
73            errorMessage.AppendLine("Unnecessary plugin dependency in " + GetPluginName(plugin) + " to " + pluginDependencyName + ".");
74          }
75        }
76      }
77
78      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
79    }
80
81    private static Type GetPluginFromAssembly(Assembly assembly) {
82      return assembly.GetExportedTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).FirstOrDefault();
83    }
84    private static string GetPluginName(Type plugin) {
85      string name = string.Empty;
86      PluginAttribute pluginAttribute = (PluginAttribute)Attribute.GetCustomAttribute(plugin, typeof(PluginAttribute));
87      if (pluginAttribute != null)
88        name = pluginAttribute.Name;
89      return name;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.