Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Tests/HeuristicLab.Persistence.Attic/PersistenceConsistencyChecks.cs @ 18190

Last change on this file since 18190 was 17781, checked in by jkarder, 4 years ago

#3089: explicitly fail on ReflectionTypeLoadException and BadImageFormatException

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using HEAL.Attic;
6using Microsoft.VisualStudio.TestTools.UnitTesting;
7
8namespace HeuristicLab.Tests.Persistence.Attic {
9  [TestClass]
10  public class PersistenceConsistencyChecks {
11    [TestCategory("Persistence.Attic")]
12    [TestCategory("Essential")]
13    [TestProperty("Time", "short")]
14    [TestMethod]
15    public void CheckDuplicateGUIDs() {
16      // easy to produce duplicate GUIDs with copy&paste
17      var dict = new Dictionary<Guid, string>();
18      var duplicates = new Dictionary<string, string>();
19
20      try {
21        // using AppDomain instead of ApplicationManager so that NonDiscoverableTypes are also checked
22        foreach (Type type in AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())) {
23          var attr = StorableTypeAttribute.GetStorableTypeAttribute(type);
24          if (attr == null)
25            continue;
26
27          foreach (var guid in attr.Guids) {
28            if (!dict.ContainsKey(guid)) {
29              dict.Add(guid, type.FullName);
30            } else {
31              duplicates.Add(type.FullName, dict[guid]);
32            }
33          }
34        }
35      } catch (ReflectionTypeLoadException e) {
36        var loaderExeptions = string.Join("-----" + Environment.NewLine, e.LoaderExceptions.Select(x => x.ToString()));
37        var message = string.Join(Environment.NewLine, "Could not load all types. Check if test process architecture is set to x64.",
38          string.Empty, "Exception:", e, string.Empty, "LoaderExceptions:", loaderExeptions);
39        Assert.Fail(message);
40      }
41
42      foreach (var kvp in duplicates) {
43        Console.WriteLine($"{kvp.Key} has same GUID as {kvp.Value}");
44      }
45
46      if (duplicates.Any()) Assert.Fail("Duplicate GUIDs found.");
47    }
48  }
49}
Note: See TracBrowser for help on using the repository browser.