101 lines
2.9 KiB
C#
101 lines
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using JD.Core.ProjectManaging;
|
|
using JD.Core.Tests;
|
|
using NUnit.Framework;
|
|
|
|
namespace JD.Serialization
|
|
{
|
|
[TestFixture]
|
|
public class XmlSerializerTests : SerializerTestsBase
|
|
{
|
|
private IDisposable _restoreProject;
|
|
|
|
[SetUp]
|
|
public void Setup()
|
|
{
|
|
_restoreProject = ProjectManager.Instance.SetDataRoot(System.IO.Path.Combine(System.IO.Path.GetPathRoot(Environment.CurrentDirectory), @"JD_DATA\main\data"));
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
_restoreProject.Dispose();
|
|
}
|
|
|
|
protected override T ReadWrite<T>(T model)
|
|
{
|
|
string content = Write(model);
|
|
return Read<T>(content);
|
|
}
|
|
|
|
private string Write<T>(T model)
|
|
{
|
|
CleanDictionaries(model);
|
|
|
|
var serializer = new XmlSerializer();
|
|
serializer.RethrowExceptions = true;
|
|
LuaSerializer.AssembliesToSearchForType.Add(GetType().Assembly);
|
|
var stream = new MemoryStream();
|
|
|
|
serializer.Serialize(model, stream);
|
|
return ReadStream(stream);
|
|
}
|
|
|
|
private T Read<T>(string content)
|
|
where T : new()
|
|
{
|
|
var serializer = new XmlSerializer();
|
|
serializer.RethrowExceptions = true;
|
|
LuaSerializer.AssembliesToSearchForType.Add(GetType().Assembly);
|
|
|
|
using (var stream = new MemoryStream())
|
|
{
|
|
using (var streamWriter = new StreamWriter(stream, Encoding.Default, 1024, leaveOpen: true))
|
|
{
|
|
streamWriter.Write(content);
|
|
}
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
return (T)serializer.Deserialize(stream);
|
|
}
|
|
}
|
|
|
|
private string ReadStream(Stream stream)
|
|
{
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
using (var reader = new StreamReader(stream))
|
|
{
|
|
return reader.ReadToEnd();
|
|
}
|
|
}
|
|
|
|
#region Not Implemented
|
|
|
|
// TODO: Fix those features:
|
|
|
|
/// <summary>
|
|
/// The serializer does not support dictionaries for now ... this removes them.
|
|
/// </summary>
|
|
private static void CleanDictionaries<T>(T model)
|
|
{
|
|
MyModel myModel = model as MyModel;
|
|
|
|
if (myModel != null)
|
|
{
|
|
myModel.Dictionary = null;
|
|
myModel.List?.ForEach(CleanDictionaries);
|
|
}
|
|
}
|
|
|
|
public override void Test_a_list_can_contain_a_child_class_of_the_list_type() { }
|
|
|
|
public override void Test_can_read_write_a_list_as_the_root_value() { }
|
|
|
|
public override void Test_can_read_write_an_UAF_Dictionary() { base.Test_can_read_write_an_UAF_Dictionary(); }
|
|
|
|
#endregion
|
|
}
|
|
}
|