Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1705


Ignore:
Timestamp:
04/29/09 15:32:59 (16 years ago)
Author:
epitzer
Message:

Check for default constructor in all decomposers to ensure failure during serialization instead of deserialization. (#606)

Location:
trunk/sources/HeuristicLab.Persistence
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/DictionaryDecomposer.cs

    r1625 r1705  
    55using System.Collections.Generic;
    66using HeuristicLab.Persistence.Default.Decomposers.Storable;
     7using HeuristicLab.Persistence.Auxiliary;
    78
    89namespace HeuristicLab.Persistence.Default.Decomposers {
     
    1718
    1819    public bool CanDecompose(Type type) {
    19       return type.GetInterface(typeof(IDictionary).FullName) != null;
     20      return ReflectionTools.HasDefaultConstructor(type) &&
     21        type.GetInterface(typeof(IDictionary).FullName) != null;
    2022    }
    2123
     
    5456      } catch (ArgumentException e) {
    5557        throw new PersistenceException("Duplicate dictionary key.", e);
    56       }     
     58      }
    5759    }
    5860  }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/EnumerableDecomposer.cs

    r1683 r1705  
    66using System.Collections.Generic;
    77using HeuristicLab.Persistence.Default.Decomposers.Storable;
     8using HeuristicLab.Persistence.Auxiliary;
    89
    910namespace HeuristicLab.Persistence.Default.Decomposers {
     
    1920    public bool CanDecompose(Type type) {
    2021      return
     22        ReflectionTools.HasDefaultConstructor(type) &&
    2123        type.GetInterface(typeof(IEnumerable).FullName) != null &&
    2224        type.GetMethod("Add") != null &&
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/NumberEnumerable2StringDecomposer.cs

    r1644 r1705  
    77using System.Text;
    88using HeuristicLab.Persistence.Default.Decomposers.Storable;
     9using HeuristicLab.Persistence.Auxiliary;
    910
    1011namespace HeuristicLab.Persistence.Default.Decomposers {
     
    5455    public bool CanDecompose(Type type) {
    5556      return
     57        ReflectionTools.HasDefaultConstructor(type) &&
    5658        ImplementsGenericEnumerable(type) &&
    5759        HasAddMethod(type);
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/Storable/StorableDecomposer.cs

    r1679 r1705  
    44using HeuristicLab.Persistence.Interfaces;
    55using HeuristicLab.Persistence.Core;
     6using System.Reflection;
     7using HeuristicLab.Persistence.Auxiliary;
    68
    79namespace HeuristicLab.Persistence.Default.Decomposers.Storable {
     
    1517
    1618    public bool CanDecompose(Type type) {
    17       return StorableAttribute.GetStorableMembers(type, false).Count() > 0 ||
    18         EmptyStorableClassAttribute.IsEmptyStorable(type);
     19      return ReflectionTools.HasDefaultConstructor(type) &&
     20        (StorableAttribute.GetStorableMembers(type, false).Count() > 0 ||
     21          EmptyStorableClassAttribute.IsEmptyStorable(type));
    1922
    2023    }
     
    2629    public IEnumerable<Tag> Decompose(object obj) {
    2730      foreach (var mapping in StorableAttribute.GetStorableAccessors(obj)) {
    28         yield return new Tag(mapping.Value.Name ?? mapping.Key, mapping.Value.Get());       
     31        yield return new Tag(mapping.Value.Name ?? mapping.Key, mapping.Value.Get());
    2932      }
    3033    }
  • trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs

    r1701 r1705  
    3838  }
    3939
     40  public class NonDefaultConstructorClass {
     41    [Storable]
     42    int value;
     43    public NonDefaultConstructorClass(int value) {
     44      this.value = value;
     45    }
     46  }
     47
    4048  public class IntWrapper {
    4149
     
    100108    [Storable]
    101109    public int[] i = new[] { 3, 4, 5, 6 };
    102     [Storable(Name="Test String")]
     110    [Storable(Name = "Test String")]
    103111    public string s;
    104112    [Storable]
     
    178186
    179187    [TestCleanup()]
    180     public void ClearTempFile() {     
     188    public void ClearTempFile() {
    181189      StreamReader reader = new StreamReader(tempFile);
    182190      string s = reader.ReadToEnd();
     
    210218      Assert.AreEqual(
    211219        DebugStringGenerator.Serialize(r),
    212         DebugStringGenerator.Serialize(newR));     
     220        DebugStringGenerator.Serialize(newR));
    213221      Assert.AreSame(newR, newR.selfReferences[0]);
    214222      Assert.AreNotSame(r, newR);
     
    223231      Assert.AreEqual(r.intList[0], 9);
    224232      Assert.AreEqual(r.intList[1], 8);
    225       Assert.AreEqual(r.intList[2], 7);     
     233      Assert.AreEqual(r.intList[2], 7);
    226234      Assert.AreEqual(r.multiDimArray[0, 0], 5);
    227235      Assert.AreEqual(r.multiDimArray[0, 1], 4);
     
    231239      Assert.AreEqual(r.multiDimArray[1, 2], 6);
    232240      Assert.IsFalse(r.boolean);
    233       Assert.IsTrue((DateTime.Now - r.dateTime).TotalSeconds < 10);     
     241      Assert.IsTrue((DateTime.Now - r.dateTime).TotalSeconds < 10);
    234242      Assert.AreEqual(r.kvp.Key, "string key");
    235243      Assert.AreEqual(r.kvp.Value, 321);
     
    486494    }
    487495
     496    [TestMethod]
     497    public void NonDefaultConstructorTest() {
     498      NonDefaultConstructorClass c = new NonDefaultConstructorClass(1);
     499      try {
     500        XmlGenerator.Serialize(c, tempFile);
     501        Assert.Fail("Exception not thrown");
     502      } catch (PersistenceException) {
     503      }
     504    }
     505
    488506    [ClassInitialize]
    489507    public static void Initialize(TestContext testContext) {
Note: See TracChangeset for help on using the changeset viewer.