Free cookie consent management tool by TermsFeed Policy Generator

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

#2520: fixed unit tests checking for exceptions

Location:
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core
Files:
5 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  }
Note: See TracChangeset for help on using the changeset viewer.