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); } }
public void CreateUnknownFormatProcessor(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); var converter = new CommonFormatConverter(); Assert.IsNotNull(converter); Assert.Throws <ArgumentException>(() => converter.CreateFormatProcessor(format)); }
/// <summary> /// Checks if specified format is supported by any of format processors and returns a new instance of the corresponding one /// </summary> /// <param name="format"></param> /// <returns>New format processor instance if specified format is supported, otherwise null</returns> public IFormatProcessor CreateFormatProcessor(ConvertedFormat format) { var processor = _formatProcessors.Find(p => p.Format == format); if (processor == null) { throw new ArgumentException($"Format {format} is not supported"); } return(processor?.GetFormatProcessor()); }
public void Create(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); var converter = new CommonFormatConverter(); var processor = converter.CreateFormatProcessor(format) as FormatProcessorBase; Assert.IsNotNull(processor); Assert.IsNotNull(processor.Data); Assert.AreNotEqual(ConvertedFormat.UNKNOWN, processor.Format); Assert.DoesNotThrow(() => processor.GetFormatProcessor()); }
/// <summary> /// Tries to convert an input file without explicit format specification /// </summary> /// <param name="inputFilePath"></param> /// <param name="inputFormat"></param> /// <param name="outputFilePath"></param> /// <param name="outputFormat"></param> /// <returns>If conversion was successful</returns> public bool Convert(string inputFilePath, ConvertedFormat inputFormat, string outputFilePath, ConvertedFormat outputFormat) { bool result = false; using (var inputFormatProcessor = CreateFormatProcessor(inputFormat)) { result = inputFormatProcessor.ReadFromFile(inputFilePath) && ConvertProcessor(inputFormatProcessor, outputFilePath, outputFormat); } return(result); }
public void Dispose(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); var converter = new CommonFormatConverter(); var processor = converter.CreateFormatProcessor(format) as FormatProcessorBase; processor.AddDataCollectionChangedHandler((sender, args) => { }); processor.AddDataCollectionChangedHandler((sender, args) => { }); processor.AddNewDataItem(new DateTime(), string.Empty, 100); processor.AddNewDataItem(new DateTime(2010, 02, 10), "VS", 200); Assert.DoesNotThrow(() => processor.Dispose()); }
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)); } }
public void AddRemoveDataItem(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); var converter = new CommonFormatConverter(); var processor = converter.CreateFormatProcessor(format) as FormatProcessorBase; IFormatDataItem dataItem = new FormatDataItem(); Assert.Throws <ArgumentNullException>(() => processor.AddDataItem(null)); Assert.DoesNotThrow(() => processor.AddDataItem(dataItem, false)); Assert.Throws <InvalidOperationException>(() => processor.AddDataItem(dataItem, false)); Assert.IsFalse(processor.RemoveDataItem(null)); Assert.IsFalse(processor.RemoveDataItem(dataItem.Clone() as IFormatDataItem)); Assert.IsTrue(processor.RemoveDataItem(dataItem)); }
/// <summary> /// Tries to convert an input file with explicit format specification /// </summary> /// <param name="inputFilePath"></param> /// <param name="outputFilePath"></param> /// <param name="outputFormat"></param> /// <returns>If conversion was successful</returns> public bool Convert(string inputFilePath, string outputFilePath, ConvertedFormat outputFormat) { bool result = false; ConvertedFormat inputFormat; if (TryGetSupportedFormatFromPath(inputFilePath, out inputFormat)) { using (var inputProcessor = CreateFormatProcessor(inputFormat)) { result = inputProcessor.ReadFromFile(inputFilePath) && ConvertProcessor(inputProcessor, outputFilePath, outputFormat); } } return(result); }
public void CreateKnownFormatProcessor(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); var converter = new CommonFormatConverter(); Assert.IsNotNull(converter); var firstProcessor = converter.CreateFormatProcessor(format); Assert.IsNotNull(firstProcessor); var secondProcessor = firstProcessor.GetFormatProcessor(); Assert.IsNotNull(secondProcessor); Assert.AreNotSame(firstProcessor, secondProcessor); secondProcessor.Dispose(); Assert.AreSame(secondProcessor.GetFormatProcessor(), secondProcessor); }
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); }
public void Cache(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); var converter = new CommonFormatConverter(); var processor1 = converter.CreateFormatProcessor(format) as FormatProcessorBase; var handler = new NotifyCollectionChangedEventHandler((sender, args) => { }); var dataItem = new FormatDataItem(); processor1.Dispose(); string filePath = "file"; Assert.IsTrue(processor1.IsCached); Assert.Throws <InvalidOperationException>(() => processor1.Dispose()); Assert.Throws <InvalidOperationException>(() => { var someVar = processor1.Data; }); Assert.Throws <InvalidOperationException>(() => { processor1.AddDataCollectionChangedHandler(handler); }); Assert.Throws <InvalidOperationException>(() => { processor1.RemoveDataCollectionChangedHandler(handler); }); Assert.Throws <InvalidOperationException>(() => { processor1.AddDataItem(dataItem); }); Assert.Throws <InvalidOperationException>(() => { processor1.RemoveDataItem(dataItem); }); Assert.Throws <InvalidOperationException>(() => { processor1.ClearData(); }); Assert.Throws <InvalidOperationException>(() => { processor1.GetEnumerator(); }); Assert.Throws <InvalidOperationException>(() => { processor1.ReadFromFile(filePath); }); Assert.Throws <InvalidOperationException>(() => { processor1.SaveToFile(filePath); }); Assert.Throws <InvalidOperationException>(() => { processor1.SetData(null); }); Assert.DoesNotThrow(() => { processor1.SupportsFormat(format.ToString()); }); var processor2 = converter.CreateFormatProcessor(format); Assert.AreSame(processor1, processor2); Assert.False(processor1.IsCached); Assert.DoesNotThrow(() => { var someVar = processor1.Data; }); Assert.DoesNotThrow(() => { processor1.AddDataCollectionChangedHandler(handler); }); Assert.DoesNotThrow(() => { processor1.RemoveDataCollectionChangedHandler(handler); }); Assert.DoesNotThrow(() => { processor1.AddDataItem(dataItem); }); Assert.DoesNotThrow(() => { processor1.RemoveDataItem(dataItem); }); Assert.DoesNotThrow(() => { processor1.ClearData(); }); Assert.DoesNotThrow(() => { processor1.GetEnumerator(); }); Assert.DoesNotThrow(() => { processor1.SaveToFile(filePath); }); Assert.DoesNotThrow(() => { processor1.ReadFromFile(filePath); }); Assert.DoesNotThrow(() => { processor1.SetData(new[] { dataItem }); }); File.Delete(filePath); processor1.Dispose(); Assert.IsTrue(processor1.IsCached); processor2 = processor1.GetFormatProcessor(); Assert.AreSame(processor1, processor2); Assert.False(processor1.IsCached); }
public void ManipulateCollectionHandlers(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); var converter = new CommonFormatConverter(); var processor = converter.CreateFormatProcessor(format) as FormatProcessorBase; Assert.Throws <ArgumentNullException>(() => processor.AddDataCollectionChangedHandler(null)); NotifyCollectionChangedEventHandler handler1 = (sender, args) => { }; NotifyCollectionChangedEventHandler handler2 = (sender, args) => { }; Assert.DoesNotThrow(() => processor.AddDataCollectionChangedHandler(handler1)); Assert.Throws <InvalidOperationException>(() => processor.AddDataCollectionChangedHandler(handler1)); Assert.Throws <ArgumentNullException>(() => processor.RemoveDataCollectionChangedHandler(null)); Assert.Throws <InvalidOperationException>(() => processor.RemoveDataCollectionChangedHandler(handler2)); Assert.DoesNotThrow(() => processor.AddDataCollectionChangedHandler(handler2)); Assert.DoesNotThrow(() => processor.RemoveDataCollectionChangedHandler(handler2)); Assert.DoesNotThrow(() => processor.RemoveDataCollectionChangedHandler(handler1)); }
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); } }
public void SupportsFormat(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); var converter = new CommonFormatConverter(); var processor = converter.CreateFormatProcessor(format) as FormatProcessorBase; Assert.IsTrue(processor.Format == format); Assert.IsTrue(processor.SupportsFormat(format.ToString())); Assert.IsTrue(processor.SupportsFormat("." + format.ToString())); foreach (ConvertedFormat frmt in SignificantFormatsTestData.TestCases) { if (frmt != format) { var strFormat = frmt.ToString(); Assert.IsFalse(processor.SupportsFormat(strFormat)); Assert.IsFalse(processor.SupportsFormat("." + strFormat)); } } }
/// <summary> /// Tries to get supported file format from provided file path by file's extension /// </summary> /// <param name="filePath"></param> /// <param name="format"></param> /// <returns>If supported format was revealed</returns> public bool TryGetSupportedFormatFromPath(string filePath, out ConvertedFormat format) { if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath)) { throw new ArgumentException("Extension can't be retrieved from path leading to non-existing file"); } bool result = false; format = ConvertedFormat.UNKNOWN; if (_fullPathFormatRegex.IsMatch(filePath)) { string extension = _fullPathFormatRegex.Match(filePath).Value; var supportedFormatProcessor = _formatProcessors.Find(p => p.SupportsFormat(extension)); if (supportedFormatProcessor != null) { format = supportedFormatProcessor.Format; result = format != ConvertedFormat.UNKNOWN; } } return(result); }
public void ReadWriteFiles(ConvertedFormat format) { FormatProcessorBase.ClearFormatProcessorsCache(); string filePath1 = "file1"; string filePath2 = "file2"; var converter = new CommonFormatConverter(); var processor = converter.CreateFormatProcessor(format) as FormatProcessorBase; Assert.Throws <ArgumentException>(() => processor.ReadFromFile(filePath1)); Assert.Throws <ArgumentException>(() => processor.ReadFromFile(null)); processor.AddNewDataItem(new DateTime(), string.Empty, 100); processor.AddNewDataItem(new DateTime(2010, 02, 10), "VS", 200); Assert.DoesNotThrow(() => processor.SaveToFile(filePath1)); Assert.IsTrue(processor.SaveToFile(filePath1)); Assert.DoesNotThrow(() => processor.ReadFromFile(filePath1)); Assert.IsTrue(processor.ReadFromFile(filePath1)); Assert.DoesNotThrow(() => processor.SaveToFile(filePath2)); Assert.IsTrue(processor.SaveToFile(filePath2)); Assert.DoesNotThrow(() => processor.ReadFromFile(filePath2)); Assert.IsTrue(processor.ReadFromFile(filePath2)); File.Delete(filePath1); File.Delete(filePath2); }
/// <summary> /// Tries to convert data from provided format processor /// </summary> /// <param name="inputFormatProcessor"></param> /// <param name="outputFilePath"></param> /// <param name="outputFormat"></param> /// <returns>If conversion was successful</returns> public bool ConvertProcessor(IFormatProcessor inputFormatProcessor, string outputFilePath, ConvertedFormat outputFormat) { if (inputFormatProcessor == null) { throw new ArgumentNullException("Can't convert data from null-valued input format processor"); } if (string.IsNullOrEmpty(outputFilePath)) { throw new ArgumentException("Can't save data at null or empty file path"); } bool result = false; using (var outputProcessor = CreateFormatProcessor(outputFormat)) { outputProcessor.SetData(inputFormatProcessor.Data.AsEnumerable(), true); result = outputProcessor.SaveToFile(outputFilePath); } return(result); }