public void TestSerializeZoo() { var serialaizer = new ZooSerializer(); //Успешная попытка сериализации пустого зоопарка var zoo = new Zoo("name", "address"); serialaizer.SerializeZoo(zoo); Assert.AreEqual(true, File.Exists(serialaizer.FileName)); var zoo2 = serialaizer.DeserializeZoo(); Assert.AreEqual(zoo.Name, zoo2.Name); Assert.AreEqual(zoo.Address, zoo2.Address); Assert.AreEqual(0, zoo2.GetListOfAviaries().Count); Assert.AreEqual(0, zoo2.GetListOfAnimals().Count); //Успешная попытка сериализации непустого зоопарка zoo.AddAviary(new Cage(CageType.WithRocks)); zoo.AddAviary(new Pool(PoolType.IndoorsPool)); zoo.AddAviary(new Yard(YardType.Plain)); zoo.SettleAnimal(new Mammal(MammalDetachment.Carnivora, "family1", "genus1", "species1"), zoo.GetListOfAviaries(AviaryType.Cage).First()); zoo.SettleAnimal(new Bird(BirdDetachment.Anseriformes, "family2", "genus2", "species2"), zoo.GetListOfAviaries(AviaryType.Pool).First()); zoo.SettleAnimal(new Mammal(MammalDetachment.Artiodactyla, "family3", "genus3", "species3"), zoo.GetListOfAviaries(AviaryType.Yard).First()); serialaizer.SerializeZoo(zoo); zoo2 = serialaizer.DeserializeZoo(); Assert.AreEqual(3, zoo2.GetListOfAviaries().Count); Assert.AreEqual(3, zoo2.GetListOfAnimals().Count); Assert.AreEqual(true, zoo2.GetListOfAviaries(AviaryType.Cage).All(aviary => (aviary as Cage).Kind == CageType.WithRocks)); Assert.AreEqual(true, zoo2.GetListOfAviaries(AviaryType.Pool).All(aviary => (aviary as Pool).Kind == PoolType.IndoorsPool)); Assert.AreEqual(true, zoo2.GetListOfAviaries(AviaryType.Yard).All(aviary => (aviary as Yard).Kind == YardType.Plain)); Assert.AreEqual(true, zoo2.GetListOfAnimals(AnimalClass.Mammal).Any(animal => (animal as Mammal).Detachment == MammalDetachment.Carnivora)); Assert.AreEqual(true, zoo2.GetListOfAnimals(AnimalClass.Bird).Any(animal => (animal as Bird).Detachment == BirdDetachment.Anseriformes)); Assert.AreEqual(true, zoo2.GetListOfAnimals(AnimalClass.Mammal).Any(animal => (animal as Mammal).Detachment == MammalDetachment.Artiodactyla)); //Неуспешная попытка сериализации несуществующего зоопарка try { serialaizer.SerializeZoo(null); Assert.Fail(); } catch (ArgumentException) { } //Неуспешная попытка сериализации зоопарка в файл с некорректным путем try { serialaizer.FileName = @"??"; serialaizer.SerializeZoo(zoo); Assert.Fail(); } catch (ArgumentException) { } }
private void SerializeZoo() { Console.Clear(); var serializer = new ZooSerializer(); try { serializer.SerializeZoo(_zoo); Console.WriteLine("Зоопарк успешно сериализован в файл {0}", serializer.FileName); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("Зоопарк не был сериализован"); } Console.WriteLine("------------------------------------------------"); Console.WriteLine("Нажмите любую клавишу для возврата в меню..."); Console.ReadKey(); }