Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab.Persistence.Attic/PersistenceConsistencyChecks.cs

Last change on this file was 17865, checked in by gkronber, 3 years ago

#3089: merged r17781 from trunk to stable

File size: 1.8 KB
RevLine 
[16908]1using System;
2using System.Collections.Generic;
3using System.Linq;
[17865]4using System.Reflection;
[16908]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
[17865]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            }
[16908]33          }
34        }
[17865]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);
[16908]40      }
41
42      foreach (var kvp in duplicates) {
43        Console.WriteLine($"{kvp.Key} has same GUID as {kvp.Value}");
44      }
[17165]45
[16908]46      if (duplicates.Any()) Assert.Fail("Duplicate GUIDs found.");
47    }
48  }
49}
Note: See TracBrowser for help on using the repository browser.