Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1733


Ignore:
Timestamp:
05/04/09 16:43:46 (16 years ago)
Author:
epitzer
Message:

Added Serialization to stream. Do not overwrite file if serialization fails. (#603)

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

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs

    r1704 r1733  
    161161
    162162    public static void Serialize(object obj, string filename, Configuration config) {
     163      string tempfile = Path.GetTempFileName();
     164      try {
     165        Serialize(obj, File.Create(tempfile), config);
     166        File.Copy(tempfile, filename, true);
     167        File.Delete(tempfile);
     168      } catch (Exception x) {
     169        Logger.Warn("Exception caught, no data has been written.");
     170        throw;
     171      }
     172    }
     173
     174    public static void Serialize(object obj, Stream stream, Configuration config) {
    163175      try {
    164176        Serializer serializer = new Serializer(obj, config);
    165177        XmlGenerator generator = new XmlGenerator();
    166178        ILog logger = Logger.GetDefaultLogger();
    167         using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(filename))) {
     179        using (ZipOutputStream zipStream = new ZipOutputStream(stream)) {
    168180          zipStream.SetLevel(9);
    169181          zipStream.PutNextEntry(new ZipEntry("data.xml"));
  • trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs

    r1705 r1733  
    172172    [Storable]
    173173    public KeyValuePair<List<C>, C> kvpList;
     174  }
     175
     176  public class NonSerializable {
     177    int x;
    174178  }
    175179
     
    504508    }
    505509
     510    [TestMethod]
     511    public void TestSavingException() {     
     512      List<int> list = new List<int> { 1, 2, 3 };
     513      XmlGenerator.Serialize(list, tempFile);
     514      NonSerializable s = new NonSerializable();
     515      try {
     516        XmlGenerator.Serialize(s, tempFile);
     517        Assert.Fail("Exception expected");
     518      } catch (PersistenceException) { }
     519      List<int> newList = (List<int>)XmlParser.DeSerialize(tempFile);
     520      Assert.AreEqual(list[0], newList[0]);
     521      Assert.AreEqual(list[1], newList[1]);
     522    }
     523
    506524    [ClassInitialize]
    507525    public static void Initialize(TestContext testContext) {
Note: See TracChangeset for help on using the changeset viewer.