Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab/3.3/Tests/StorableConstructorTest.cs @ 6231

Last change on this file since 6231 was 6231, checked in by epitzer, 12 years ago

fix StorableConstructorTest (#1530)

File size: 2.1 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Reflection;
6using System.Text;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.PluginInfrastructure;
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10
11namespace HeuristicLab_33.Tests {
12  [TestClass]
13  public class StorableConstructorTest {
14
15    // Use ClassInitialize to run code before running the first test in the class
16    [ClassInitialize]
17    public static void MyClassInitialize(TestContext testContext) {
18      PluginLoader.Assemblies.Any();
19    }
20
21    [TestMethod]
22    public void TestStorableConstructor() {
23      StringBuilder errorMessage = new StringBuilder();
24
25      foreach (Type storableType in ApplicationManager.Manager.GetTypes(typeof(object))) {
26        //test only storable types contained in HL plugin assemblies
27        if (!PluginLoader.Assemblies.Contains(storableType.Assembly) ||
28          !storableType.GetCustomAttributes(typeof(StorableClassAttribute), false).Any())
29          continue;
30
31        IEnumerable<ConstructorInfo> ctors = storableType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
32        ConstructorInfo storableConstructor = ctors.Where(c => c.GetParameters().Count() == 1 && c.GetParameters().First().ParameterType == typeof(bool)).FirstOrDefault();
33        if (storableConstructor == null) errorMessage.Append(Environment.NewLine + storableType.ToString() + ": No storable constructor is defined.");
34        else {
35          if (storableType.IsSealed && !storableConstructor.IsPrivate)
36            errorMessage.Append(Environment.NewLine + storableType.ToString() + ": Storable constructor must be private in sealed classes.");
37          else if (!storableType.IsSealed && !(storableConstructor.IsFamily || storableConstructor.IsPublic))
38            errorMessage.Append(Environment.NewLine + storableType.ToString() + ": Storable constructor must be protected (can be public in rare cases).");
39        }
40      }
41      Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
42    }
43  }
44}
Note: See TracBrowser for help on using the repository browser.