예제 #1
0
 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);
     }
 }
예제 #2
0
        //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");
        }
예제 #3
0
 public void ConvertKnownFormatArgumentExceptionTest(ConvertedFormat inputFormat)
 {
     foreach (var outputFormat in SignificantFormatsTestData.TestCases)
     {
         FormatProcessorBase.ClearFormatProcessorsCache();
         var converter = new CommonFormatConverter();
         Assert.IsNotNull(converter);
         Assert.Throws <ArgumentNullException>(() => converter.ConvertProcessor(null, "outfile", outputFormat));
         Assert.Throws <ArgumentException>(() => converter.ConvertProcessor(converter.CreateFormatProcessor(inputFormat),
                                                                            null, outputFormat));
         Assert.Throws <ArgumentException>(() => converter.ConvertProcessor(converter.CreateFormatProcessor(inputFormat),
                                                                            string.Empty, outputFormat));
     }
 }
예제 #4
0
        public void ConvertValidKnownFormatsDataTest(ConvertedFormat inputFormat)
        {
            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();

            foreach (var outputFormat in SignificantFormatsTestData.TestCases)
            {
                FormatProcessorBase.ClearFormatProcessorsCache();
                using (var processor = converter.CreateFormatProcessor(inputFormat))
                {
                    processor.AddDataItem(item1);
                    processor.AddDataItem(item2);
                    Assert.IsTrue((FormatDataItem)processor[0] == item1);
                    Assert.IsTrue((FormatDataItem)processor[1] == item2);
                    converter.ConvertProcessor(processor, filePath1, outputFormat);
                }
                using (var processor = converter.CreateFormatProcessor(outputFormat))
                {
                    processor.ReadFromFile(filePath1);
                    Assert.IsTrue((FormatDataItem)processor[0] == item1);
                    Assert.IsTrue((FormatDataItem)processor[1] == item2);
                    converter.ConvertProcessor(processor, filePath2, inputFormat);
                }
                using (var processor = converter.CreateFormatProcessor(inputFormat))
                {
                    processor.ReadFromFile(filePath2);
                    Assert.IsTrue((FormatDataItem)processor[0] == item1);
                    Assert.IsTrue((FormatDataItem)processor[1] == item2);
                }
                File.Delete(filePath1);
                File.Delete(filePath2);
            }
        }
예제 #5
0
        public void TryGetSupportedFormatFromPathTrue(ConvertedFormat format)
        {
            FormatProcessorBase.ClearFormatProcessorsCache();
            var converter = new CommonFormatConverter();

            Assert.IsNotNull(converter);
            using (var processor = converter.CreateFormatProcessor(format))
            {
                Assert.IsNotNull(processor);
                Assert.IsTrue(converter.ConvertProcessor(processor, $"file.{format}", format));
            }
            ConvertedFormat supportedFormat;

            Assert.IsTrue(converter.TryGetSupportedFormatFromPath($"file.{format}", out supportedFormat));
            Assert.AreEqual(supportedFormat, format);
        }