1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Reflection;
|
---|
5 | using HEAL.Attic;
|
---|
6 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|