Changeset 2106 for trunk/sources/HeuristicLab.Persistence
- Timestamp:
- 06/25/09 16:45:09 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/PersistenceException.cs
r1823 r2106 4 4 using HeuristicLab.Persistence.Core.Tokens; 5 5 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 6 using System.Text; 6 7 7 8 namespace HeuristicLab.Persistence.Core { … … 12 13 public PersistenceException(string message) : base(message) { } 13 14 public PersistenceException(string message, Exception innerException) : base(message, innerException) { } 15 public PersistenceException(string message, IEnumerable<Exception> innerExceptions) 16 : base(message) { 17 int i = 0; 18 foreach (var x in innerExceptions) { 19 i += 1; 20 this.Data.Add("Inner Exception " + i, x); 21 } 22 } 23 public override string ToString() { 24 var sb = new StringBuilder() 25 .Append(base.ToString()) 26 .Append('\n'); 27 foreach (Exception x in Data.Values) { 28 sb.Append(x.ToString()).Append('\n'); 29 } 30 return sb.ToString(); 31 } 14 32 } 15 33 -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r1823 r2106 34 34 private readonly Dictionary<Type, int> typeCache; 35 35 private readonly Configuration configuration; 36 private readonly bool isTestRun; 37 private readonly List<Exception> exceptions; 36 38 37 39 public List<TypeMapping> TypeCache { … … 78 80 this(obj, configuration, "ROOT") { } 79 81 80 public Serializer(object obj, Configuration configuration, string rootName) { 82 public Serializer(object obj, Configuration configuration, string rootName) 83 : this(obj, configuration, rootName, false) { } 84 85 public Serializer(object obj, Configuration configuration, string rootName, bool isTestRun) { 86 81 87 this.obj = obj; 82 88 this.rootName = rootName; … … 84 90 obj2id = new Dictionary<object, int>(new ReferenceEqualityComparer()) { { new object(), 0 } }; 85 91 typeCache = new Dictionary<Type, int>(); 92 this.isTestRun = isTestRun; 93 this.exceptions = new List<Exception>(); 86 94 } 87 95 … … 91 99 92 100 public IEnumerator<ISerializationToken> GetEnumerator() { 93 return Serialize(new DataMemberAccessor(rootName, null, () => obj, null)); 101 var enumerator = Serialize(new DataMemberAccessor(rootName, null, () => obj, null)); 102 if (isTestRun) { 103 return AddExceptionCompiler(enumerator); 104 } else { 105 return enumerator; 106 } 107 } 108 109 public IEnumerator<ISerializationToken> AddExceptionCompiler(IEnumerator<ISerializationToken> enumerator) { 110 while (enumerator.MoveNext()) 111 yield return enumerator.Current; 112 if (exceptions.Count == 1) 113 throw exceptions[0]; 114 if (exceptions.Count > 1) 115 throw new PersistenceException("Multiple exceptions during serialization", exceptions); 94 116 } 95 117 … … 109 131 obj2id.Add(value, (int)id); 110 132 } 111 IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type); 112 if (primitiveSerializer != null) 113 return PrimitiveEnumerator(accessor.Name, typeId, primitiveSerializer.Format(value), id); 114 ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type); 115 if (compositeSerializer != null) 116 return CompositeEnumerator(accessor.Name, compositeSerializer.Decompose(value), id, typeId, compositeSerializer.CreateMetaInfo(value)); 117 throw new PersistenceException( 118 String.Format( 119 "No suitable method for serializing values of type \"{0}\" found\r\n" + 120 "primitive serializers:\r\n{1}\r\n" + 121 "composite serializers:\r\n{2}", 122 value.GetType().VersionInvariantName(), 123 string.Join("\r\n", configuration.PrimitiveSerializers.Select(f => f.GetType().VersionInvariantName()).ToArray()), 124 string.Join("\r\n", configuration.CompositeSerializers.Select(d => d.GetType().VersionInvariantName()).ToArray()) 125 )); 126 133 try { 134 IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type); 135 if (primitiveSerializer != null) 136 return PrimitiveEnumerator(accessor.Name, typeId, primitiveSerializer.Format(value), id); 137 ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type); 138 if (compositeSerializer != null) 139 return CompositeEnumerator(accessor.Name, compositeSerializer.Decompose(value), id, typeId, compositeSerializer.CreateMetaInfo(value)); 140 throw new PersistenceException( 141 String.Format( 142 "No suitable method for serializing values of type \"{0}\" found\r\n" + 143 "primitive serializers:\r\n{1}\r\n" + 144 "composite serializers:\r\n{2}", 145 value.GetType().VersionInvariantName(), 146 string.Join("\r\n", configuration.PrimitiveSerializers.Select(f => f.GetType().VersionInvariantName()).ToArray()), 147 string.Join("\r\n", configuration.CompositeSerializers.Select(d => d.GetType().VersionInvariantName()).ToArray()) 148 )); 149 } catch (Exception x) { 150 if (isTestRun) { 151 exceptions.Add(x); 152 return new List<ISerializationToken>().GetEnumerator(); 153 } else { 154 throw x; 155 } 156 } 127 157 } 128 158 -
trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs
r1938 r2106 506 506 507 507 [TestMethod] 508 public void TestMultipleFailure() { 509 List<NonSerializable> l = new List<NonSerializable>(); 510 l.Add(new NonSerializable()); 511 l.Add(new NonSerializable()); 512 l.Add(new NonSerializable()); 513 try { 514 Serializer s = new Serializer(l, 515 ConfigurationService.Instance.GetConfiguration(new XmlFormat()), 516 "ROOT", true); 517 StringBuilder tokens = new StringBuilder(); 518 foreach (var token in s) { 519 tokens.Append(token.ToString()); 520 } 521 Assert.Fail("Exception expected"); 522 } catch (PersistenceException px) { 523 Assert.AreEqual(3, px.Data.Count); 524 } 525 } 526 527 [TestMethod] 508 528 public void TestAssemblyVersionCheck() { 509 529 IntWrapper i = new IntWrapper(1);
Note: See TracChangeset
for help on using the changeset viewer.