zaaReloaded2/Tests/SerializationTest.cs

170 lines
6.6 KiB
C#
Raw Normal View History

/* SelectFirstDayTest.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using zaaReloaded2.Controller.Elements;
using NUnit.Framework;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using zaaReloaded2.Controller;
namespace Tests.Controller
{
[TestFixture]
class SerializationTest
{
[Test]
public void SerializeControlElement()
{
SelectFirstDay element = new SelectFirstDay(
new List<FormatElementBase>()
{
new Items("hello"),
new Items("world")
}
);
MemoryStream stream = new MemoryStream();
// XmlSerializer does not work with interface properties...
// XmlSerializer serializer = new XmlSerializer(typeof(SelectFirstDay));
// BinaryFormatter serializer = new BinaryFormatter();
SoapFormatter serializer = new SoapFormatter();
serializer.Serialize(stream, element);
stream.Position = 0;
StreamReader sr = new StreamReader(stream);
// Console.WriteLine(sr.ReadToEnd());
stream.Position = 0;
SelectFirstDay deserialized = serializer.Deserialize(stream) as SelectFirstDay;
Assert.IsNotNull(deserialized);
Assert.AreEqual(element.Children.Count, deserialized.Children.Count);
Assert.AreEqual(element.Children[0].Content, deserialized.Children[0].Content);
Assert.AreEqual(element.Children[1].Content, deserialized.Children[1].Content);
}
[Test]
public void SerializeFormatElement()
{
string testString = "Hello World";
CustomText element = new CustomText();
element.Content = testString;
MemoryStream stream = new MemoryStream();
SoapFormatter serializer = new SoapFormatter();
serializer.Serialize(stream, element);
stream.Position = 0;
CustomText deserialized = serializer.Deserialize(stream) as CustomText;
Assert.IsNotNull(deserialized);
Assert.AreEqual(element.Content, deserialized.Content);
}
[Test]
public void SerializeSettings()
{
Settings settings = CreateTestSettings1();
MemoryStream stream = new MemoryStream();
SoapFormatter serializer = new SoapFormatter();
serializer.Serialize(stream, settings);
stream.Position = 0;
Settings deserialized = serializer.Deserialize(stream) as Settings;
Assert.IsNotNull(deserialized);
Assert.AreEqual(settings.Name, deserialized.Name);
Assert.AreEqual(settings.ReferenceStyle, deserialized.ReferenceStyle);
Assert.AreEqual(settings.Elements.Count, deserialized.Elements.Count);
Assert.AreEqual(settings.Elements[0].GetType(), deserialized.Elements[0].GetType());
Assert.AreEqual(settings.Elements[1].GetType(), deserialized.Elements[1].GetType());
}
[Test]
public void SerializeSettingsRepository()
{
SettingsRepository repository = new SettingsRepository();
repository.SettingsList.Add(CreateTestSettings1());
repository.SettingsList.Add(CreateTestSettings2());
MemoryStream stream = new MemoryStream();
SoapFormatter serializer = new SoapFormatter();
serializer.Serialize(stream, repository);
stream.Position = 0;
SettingsRepository deserialized = serializer.Deserialize(stream) as SettingsRepository;
Assert.IsNotNull(deserialized);
Assert.AreEqual(repository.SettingsList.Count, deserialized.SettingsList.Count);
Assert.AreEqual(
repository.SettingsList[1].Elements.Count,
deserialized.SettingsList[1].Elements.Count
);
Assert.AreEqual(
repository.SettingsList[0].Name,
deserialized.SettingsList[0].Name
);
Assert.AreEqual(
repository.SettingsList[0].ReferenceStyle,
deserialized.SettingsList[0].ReferenceStyle
);
}
Settings CreateTestSettings1()
{
string testName = "test name...";
string testString = "Hello World";
SelectFirstDay controlElement = new SelectFirstDay(
new List<FormatElementBase>()
{
new Items("hello"),
new Items("world")
}
);
CustomText formatElement = new CustomText();
formatElement.Content = testString;
Settings settings = new Settings(testName, new List<ElementBase>()
{
controlElement,
formatElement
}
);
settings.ReferenceStyle = zaaReloaded2.Formatter.ReferenceStyle.IfSpecialItem;
return settings;
}
Settings CreateTestSettings2()
{
string testName = "another test name";
SelectFirstDay controlElement1 = new SelectFirstDay(
new List<FormatElementBase>()
{
new CustomText(),
}
);
SelectLastDay controlElement2 = new SelectLastDay(
new List<FormatElementBase>()
{
new Items("Items content"),
}
);
Settings settings = new Settings(testName, new List<ElementBase>()
{
controlElement1,
controlElement2
}
);
settings.ReferenceStyle = zaaReloaded2.Formatter.ReferenceStyle.IfSpecialOrAbnormal;
return settings;
}
}
}