Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/01/17 15:15:25 (8 years ago)
Author:
gkronber
Message:

#2520: fixed unit tests checking for exceptions

Location:
branches/PersistenceReintegration
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs

    r14771 r15020  
    134134
    135135    public object CreateInstance(Type type) {
    136       return StaticCache.GetTypeInfo(type).GetConstructor()();
     136      try {
     137        return StaticCache.GetTypeInfo(type).GetConstructor()();
     138      } catch (Exception e) {
     139        throw new PersistenceException("Deserialization failed.", e);
     140      }
    137141    }
    138142
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Serializer.cs

    r13347 r15020  
    2020#endregion
    2121
     22using System;
    2223using System.IO;
    2324using System.IO.Compression;
     
    2930    }
    3031    public virtual void Serialize(object o, string path) {
    31       using (var fileStream = new FileStream(path, FileMode.Create)) {
    32         using (var zipStream = new DeflateStream(fileStream, CompressionMode.Compress)) {
     32      string tempfile = Path.GetTempFileName();
     33
     34      using (FileStream stream = File.Create(tempfile)) {
     35        using (var zipStream = new DeflateStream(stream, CompressionMode.Compress)) {
    3336          Serialize(o, zipStream);
    3437        }
    3538      }
     39
     40      File.Copy(tempfile, path, true);
     41      File.Delete(tempfile);
    3642    }
    3743    public virtual byte[] Serialize(object o) {
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StaticCache.cs

    r14927 r15020  
    244244        TypeInfo typeInfo;
    245245        if (!typeInfos.TryGetValue(type, out typeInfo)) {
    246           var transformer = guid2Transformer.Values.OrderBy(x => x.Priority).First(x => x.CanTransformType(type));
     246          var transformer = guid2Transformer.Values.OrderBy(x => x.Priority).FirstOrDefault(x => x.CanTransformType(type));
     247          if(transformer == null) throw new PersistenceException("No transformer found for type " + type.AssemblyQualifiedName);
    247248          typeInfo = new TypeInfo(type, transformer);
    248249          typeInfos.Add(type, typeInfo);
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableConstructorFlag.cs

    r15018 r15020  
    2525
    2626namespace HeuristicLab.Persistence {
    27   // the enum type which is used for the definition of storable constructors
    28   public struct StorableConstructorFlag {
     27  // the type which is used for the definition of storable constructors
     28  public struct StorableConstructorFlag  {
    2929    // empty
    3030  };
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs

    r15018 r15020  
    5858      StorableTypeAttribute = StorableTypeAttribute.GetStorableTypeAttribute(type);
    5959      if (StorableTypeAttribute != null) {
    60         // traverse type hierarchy from base type to sub types
    61         Stack<Type> types = new Stack<Type>();
     60        // check constructors (
     61        if (!type.IsValueType && !type.IsEnum && !type.IsInterface &&
     62          GetStorableConstructor() == null && GetDefaultConstructor() == null)
     63          throw new PersistenceException("No storable constructor or parameterless constructor found.");
     64
     65          // traverse type hierarchy from base type to sub types
     66          Stack<Type> types = new Stack<Type>();
    6267        while (type != null) {
    6368          types.Push(type);
     
    9095            foreach (var property in propertyInfos) {
    9196              var attrib = StorableAttribute.GetStorableAttribute(property);
     97              if (!attrib.AllowOneWay && (!property.CanRead || !property.CanWrite))
     98                throw new PersistenceException("Properties must be readable and writable or explicity enable one way serialization.");
     99
    92100              var name = attrib == null || string.IsNullOrEmpty(attrib.Name) ? property.Name : attrib.Name;
    93101              properties.Add(new ComponentInfo(type.Name + '.' + name, property, attrib, property.CanRead, property.CanWrite));
     
    118126
    119127      // get storable constructor
    120       var ctor = Type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
    121                      .Where(x => StorableConstructorAttribute.IsStorableConstructor(x))
    122                      .Where(x => (x.GetParameters().Length == 1) && (x.GetParameters()[0].ParameterType == typeof(StorableConstructorFlag)))
    123                      .FirstOrDefault();
     128      var ctor = GetStorableConstructor();
    124129      if (ctor != null) {
    125130        DynamicMethod dm = new DynamicMethod("", typeof(object), null, Type, true);
     
    136141
    137142      // get default constructor
    138       ctor = Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
    139                                  null, Type.EmptyTypes, null);
     143      ctor = GetDefaultConstructor();
    140144      if (ctor != null) {
    141145        DynamicMethod dm = new DynamicMethod("", typeof(object), null, Type, true);
     
    147151      }
    148152
    149       throw new Exception("No storable constructor or parameterless constructor found.");
     153      throw new PersistenceException("No storable constructor or parameterless constructor found.");
     154    }
     155
     156    private ConstructorInfo GetStorableConstructor() {
     157      return Type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
     158               .Where(x => StorableConstructorAttribute.IsStorableConstructor(x))
     159               .Where(x => (x.GetParameters().Length == 1) && (x.GetParameters()[0].ParameterType == typeof(StorableConstructorFlag)))
     160               .FirstOrDefault();
     161    }
     162
     163    private ConstructorInfo GetDefaultConstructor() {
     164      return Type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
     165                             null, Type.EmptyTypes, null);
    150166    }
    151167  }
  • branches/PersistenceReintegration/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs

    r15018 r15020  
    268268  }
    269269
    270   [StorableType("6494ee1d-877b-4e78-83df-f3f55a77b9b4")]
    271270  public class NonSerializable {
    272271    int x = 0;
     
    13821381      int value;
    13831382      int PropertyValue { get; set; }
     1383
     1384      /*
     1385      [StorableConstructor]
     1386      public TestStruct(StorableConstructorFlag deserializing) {
     1387        value = 0;
     1388        PropertyValue = 0;
     1389      }
     1390      */
     1391
    13841392      public TestStruct(int value)
    13851393        : this() {
     
    15841592      public int Value4 { get; private set; }
    15851593      [StorableConstructor]
    1586       public AllFieldsStorable(bool isDeserializing) {
    1587         if (!isDeserializing) {
    1588           Value1 = 12;
    1589           Value2 = 23;
    1590           Value3 = 34;
    1591           Value4 = 56;
    1592         }
     1594      public AllFieldsStorable(StorableConstructorFlag isdeserializing) {
     1595      }
     1596
     1597      public void InitValues() {
     1598        Value1 = 12;
     1599        Value2 = 23;
     1600        Value3 = 34;
     1601        Value4 = 56;
    15931602      }
    15941603    }
     
    15991608    public void TestStorableTypeDiscoveryAllFields() {
    16001609      ProtoBufSerializer serializer = new ProtoBufSerializer();
    1601       AllFieldsStorable afs = new AllFieldsStorable(false);
     1610      AllFieldsStorable afs = new AllFieldsStorable(default(StorableConstructorFlag));
     1611      afs.InitValues();
    16021612      serializer.Serialize(afs, tempFile);
    16031613      AllFieldsStorable newAfs = (AllFieldsStorable)serializer.Deserialize(tempFile);
     
    16151625      public int Value3 { get; private set; }
    16161626      public int Value4 { get; private set; }
    1617       [StorableConstructor]
    1618       public AllPropertiesStorable(bool isDeserializing) {
    1619         if (!isDeserializing) {
    1620           Value1 = 12;
    1621           Value2 = 23;
    1622           Value3 = 34;
    1623           Value4 = 56;
    1624         }
     1627
     1628      public AllPropertiesStorable() {
     1629      }
     1630
     1631      public void InitValues() {
     1632        Value1 = 12;
     1633        Value2 = 23;
     1634        Value3 = 34;
     1635        Value4 = 56;
    16251636      }
    16261637    }
     
    16311642    public void TestStorableTypeDiscoveryAllProperties() {
    16321643      ProtoBufSerializer serializer = new ProtoBufSerializer();
    1633       AllPropertiesStorable afs = new AllPropertiesStorable(false);
     1644      AllPropertiesStorable afs = new AllPropertiesStorable();
     1645      afs.InitValues();
    16341646      serializer.Serialize(afs, tempFile);
    16351647      AllPropertiesStorable newAfs = (AllPropertiesStorable)serializer.Deserialize(tempFile);
     
    16481660      public int Value3 { get; private set; }
    16491661      public int Value4 { get; private set; }
    1650       [StorableConstructor]
    1651       public AllFieldsAndAllPropertiesStorable(bool isDeserializing) {
    1652         if (!isDeserializing) {
    1653           Value1 = 12;
    1654           Value2 = 23;
    1655           Value3 = 34;
    1656           Value4 = 56;
    1657         }
     1662
     1663      public AllFieldsAndAllPropertiesStorable() {
     1664      }
     1665
     1666      public void InitValues() {
     1667        Value1 = 12;
     1668        Value2 = 23;
     1669        Value3 = 34;
     1670        Value4 = 56;
     1671
    16581672      }
    16591673    }
     
    16641678    public void TestStorableTypeDiscoveryAllFieldsAndAllProperties() {
    16651679      ProtoBufSerializer serializer = new ProtoBufSerializer();
    1666       AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable(false);
     1680      AllFieldsAndAllPropertiesStorable afs = new AllFieldsAndAllPropertiesStorable();
     1681      afs.InitValues();
    16671682      serializer.Serialize(afs, tempFile);
    16681683      AllFieldsAndAllPropertiesStorable newAfs = (AllFieldsAndAllPropertiesStorable)serializer.Deserialize(tempFile);
     
    16801695      public int Value3 { get; private set; }
    16811696      public int Value4 { get; private set; }
    1682       [StorableConstructor]
    1683       public MarkedOnlyStorable(bool isDeserializing) {
    1684         if (!isDeserializing) {
    1685           Value1 = 12;
    1686           Value2 = 23;
    1687           Value3 = 34;
    1688           Value4 = 56;
    1689         }
     1697      public MarkedOnlyStorable() {
     1698      }
     1699
     1700      public void InitValues() {
     1701        Value1 = 12;
     1702        Value2 = 23;
     1703        Value3 = 34;
     1704        Value4 = 56;
    16901705      }
    16911706    }
     
    16961711    public void TestStorableTypeDiscoveryMarkedOnly() {
    16971712      ProtoBufSerializer serializer = new ProtoBufSerializer();
    1698       MarkedOnlyStorable afs = new MarkedOnlyStorable(false);
     1713      MarkedOnlyStorable afs = new MarkedOnlyStorable();
     1714      afs.InitValues();
    16991715      serializer.Serialize(afs, tempFile);
    17001716      MarkedOnlyStorable newAfs = (MarkedOnlyStorable)serializer.Deserialize(tempFile);
Note: See TracChangeset for help on using the changeset viewer.