public void ConvertValidKnownFormatsSuccessTest(ConvertedFormat inputFormat) { foreach (var outputFormat in SignificantFormatsTestData.TestCases) { FormatProcessorBase.ClearFormatProcessorsCache(); string filePath1 = "file1"; string filePath2 = "file2"; FormatDataItem item1 = new FormatDataItem(); item1.SetDate("01.01.2001"); item1.SetBrandName("brand1"); item1.SetPrice(1111); FormatDataItem item2 = new FormatDataItem(); item2.SetDate("02.02.2002"); item2.SetBrandName("brand2"); item2.SetPrice(2222); var converter = new CommonFormatConverter(); Assert.IsNotNull(converter); using (var processor = converter.CreateFormatProcessor(inputFormat)) { processor.AddDataItem(item1); processor.AddDataItem(item2); Assert.IsNotNull(processor); Assert.IsTrue(converter.ConvertProcessor(processor, filePath1, outputFormat)); Assert.DoesNotThrow(() => converter.ConvertProcessor(processor, filePath1, outputFormat)); } Assert.IsTrue(converter.Convert(filePath1, outputFormat, filePath2, inputFormat)); Assert.DoesNotThrow(() => converter.Convert(filePath1, outputFormat, filePath2, inputFormat)); Assert.IsTrue(converter.Convert(filePath2, inputFormat, filePath1, outputFormat)); Assert.DoesNotThrow(() => converter.Convert(filePath2, inputFormat, filePath1, outputFormat)); Assert.IsTrue(File.Exists(filePath1)); Assert.IsTrue(File.Exists(filePath2)); File.Delete(filePath1); File.Delete(filePath2); } }
//simple usage private static void Main(string[] args) { //create format converter CommonFormatConverter converter = new CommonFormatConverter(); //populate bin format processor, add some data, save to file and convert using (var binProcessor = converter.CreateFormatProcessor(ConvertedFormat.BIN)) { binProcessor.AddNewDataItem(10, 2, 1000, "Car", 100500); binProcessor[0].SetMonth(3); binProcessor.SaveToFile("Car.bin"); converter.ConvertProcessor(binProcessor, "Car.xml", ConvertedFormat.XML); foreach (var dataItem in binProcessor) { Console.WriteLine(dataItem.ToString()); } } //populate xml format processor, add new data, modify existing data, save to file and convert using (var xmlProcessor = converter.CreateFormatProcessor(ConvertedFormat.XML)) { xmlProcessor.ReadFromFile("Car.xml"); xmlProcessor.AddNewDataItem(15, 5, 2000, "BlaCar", 90); xmlProcessor[1].SetDay(20); xmlProcessor.SaveToFile("BlaCar.xml"); converter.Convert("BlaCar.xml", "BlaCar.bin", ConvertedFormat.BIN); foreach (var dataItem in xmlProcessor) { Console.WriteLine(dataItem.ToString()); } } //populate bin format processor, add new data, modify existing data, save to file and convert using (var binProcessor = converter.CreateFormatProcessor(ConvertedFormat.BIN)) { binProcessor.ReadFromFile("BlaCar.bin"); binProcessor.AddNewDataItem(5, 12, 2005, "BlaBlaCar", 999); binProcessor[2].SetPrice(888); converter.ConvertProcessor(binProcessor, "BlaBlaCar.xml", ConvertedFormat.XML); foreach (var dataItem in binProcessor) { Console.WriteLine(dataItem.ToString()); } } //validate data //press enter key to exit program Console.ReadLine(); //removing test files File.Delete("Car.bin"); File.Delete("Car.xml"); File.Delete("BlaCar.bin"); File.Delete("BlaCar.xml"); File.Delete("BlaBlaCar.xml"); }