Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit c11dcd60 authored by Harald Martini's avatar Harald Martini
Browse files

Added Tests for updating Title and datasource when a new completed input file is saved

parent 7181483f
No related branches found
No related tags found
No related merge requests found
......@@ -70,7 +70,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
}
}
public interface IMultistageVehicleViewModel : IVehicleViewModel
public interface IMultistageVehicleViewModel : IVehicleViewModel, INotifyPropertyChanged
{
bool HasErrors { get; }
Dictionary<string, string> Errors { get; }
......
......@@ -247,10 +247,10 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
public string VehicleInputDataFilePath
{
get => ManufacturingStageViewModel.VehicleInputDataFilePath;
get => ManufacturingStageViewModel.InputDataFilePath;
set
{
ManufacturingStageViewModel.VehicleInputDataFilePath = value;
ManufacturingStageViewModel.InputDataFilePath = value;
OnPropertyChanged();
}
}
......
using System.Diagnostics;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using Castle.Core.Smtp;
using TUGraz.VectoCommon.InputData;
using TUGraz.VectoCore.Models.Declaration;
using TUGraz.VectoCore.Utils;
......@@ -17,7 +20,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
public class StageInputViewModel : StageViewModelBase, IDocumentViewModel, IJobEditViewModel
{
private bool _canBeEdited;
private readonly DataSource _dataSource;
private DataSource _dataSource;
private readonly XmlDocumentType _documentType;
private readonly string _documentName;
private bool _selected;
......@@ -30,10 +33,10 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
: InterimStageBusVehicleViewModel_v2_8.VERSION) as IMultistageVehicleViewModel;
Debug.Assert(_vehicleViewModel != null);
Title = "Edit Stage Input - New File";
Init();
}
public StageInputViewModel(IDeclarationInputDataProvider inputData, IMultiStageViewModelFactory multiStageViewModelFactory) : base(multiStageViewModelFactory)
{
_documentName = inputData.JobInputData.JobName;
......@@ -42,13 +45,32 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
(_vehicleViewModel as InterimStageBusVehicleViewModel_v2_8).ShowConsolidatedData = false;
_dataSource = inputData.DataSource;
_documentType = XmlDocumentType.DeclarationJobData;
Title = $"Edit Stage Input - {Path.GetFileNameWithoutExtension(_dataSource.SourceFile)}";
Title = $"Edit Stage Input - {Path.GetFileName(_dataSource.SourceFile)}";
Init();
}
#region Overrides of StageViewModelBase
protected override bool LoadStageInputData(IDeclarationInputDataProvider inputData)
{
base.LoadStageInputData(inputData);
DataSource = inputData.DataSource;
return true;
}
#endregion
private void SetTitle()
{
Title = "Edit Stage Input - " + ((_dataSource?.SourceFile != null)
? Path.GetFileName(_dataSource.SourceFile)
: "New File");
}
private void Init()
{
SetTitle();
Components.Add("vehicle", VehicleViewModel as IViewModelBase);
Components.Add("auxiliaries", VehicleViewModel.MultistageAuxiliariesViewModel as IViewModelBase);
Components.Add("airdrag", VehicleViewModel.MultistageAirdragViewModel as IViewModelBase);
......@@ -63,7 +85,15 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
public XmlDocumentType DocumentType => _documentType;
public DataSource DataSource => _dataSource;
public DataSource DataSource
{
get => _dataSource;
set
{
SetProperty(ref _dataSource, value);
SetTitle();
}
}
public IEditViewModel EditViewModel => this;
......
......@@ -24,7 +24,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
{
public interface IStageViewModelBase
{
string VehicleInputDataFilePath { get; set; }
string InputDataFilePath { get; set; }
IMultistageVehicleViewModel VehicleViewModel { get; set; }
ICommand SwitchComponentViewCommand { get; }
ICommand SaveInputDataCommand { get; }
......@@ -36,7 +36,9 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
public class StageViewModelBase : ViewModelBase, IStageViewModelBase
{
protected Dictionary<string, IViewModelBase> Components = new Dictionary<string, IViewModelBase>(StringComparer.CurrentCultureIgnoreCase);
protected Dictionary<string, IViewModelBase> Components =
new Dictionary<string, IViewModelBase>(StringComparer.CurrentCultureIgnoreCase);
protected IMultistageVehicleViewModel _vehicleViewModel;
protected IMultiStageViewModelFactory _viewModelFactory;
private IViewModelBase _currentview;
......@@ -72,7 +74,9 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
get => _vehicleViewModel;
set => SetProperty(ref _vehicleViewModel, value);
}
private bool _showSaveAndCloseButtons = false;
public bool ShowSaveAndCloseButtons
{
get => _showSaveAndCloseButtons;
......@@ -80,17 +84,20 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
}
#region Commands
public ICommand SwitchComponentViewCommand
{
get {
return _switchComponentViewCommand ?? new RelayCommand<string>(SwitchViewExecute, (string s) => SwitchViewCanExecute(s));
get
{
return _switchComponentViewCommand ??
new RelayCommand<string>(SwitchViewExecute, (string s) => SwitchViewCanExecute(s));
}
}
private void SwitchViewExecute(string viewToShow)
{
IViewModelBase newView;
var success= Components.TryGetValue(viewToShow, out newView);
var success = Components.TryGetValue(viewToShow, out newView);
if (success) {
CurrentView = newView;
}
......@@ -107,14 +114,12 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
private IXMLInputDataReader _inputDataReader;
public ICommand SaveInputDataCommand =>
_saveInputDataCommand ?? new RelayCommand(() => {
SaveInputDataExecute(filename: _vehicleInputDataFilePath);
}, () => _vehicleInputDataFilePath != null);
_saveInputDataCommand ??
new RelayCommand(() => { SaveInputDataExecute(filename: _vehicleInputDataFilePath); },
() => _vehicleInputDataFilePath != null);
public ICommand SaveInputDataAsCommand =>
_saveInputDataAsCommand ?? new RelayCommand(() => {
SaveInputDataExecute(filename: null);
}, () => true);
_saveInputDataAsCommand ?? new RelayCommand(() => { SaveInputDataExecute(filename: null); }, () => true);
private ICommand _loadVehicleDataCommand;
......@@ -123,23 +128,21 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
public ICommand LoadVehicleDataCommand
{
get
{
return _loadVehicleDataCommand ?? new RelayCommand(LoadVehicleDataExecute, () => true);
}
get { return _loadVehicleDataCommand ?? new RelayCommand(LoadVehicleDataExecute, () => true); }
}
#endregion Commands
#region File I/O
private void LoadVehicleDataExecute()
{
var fileName = _multistageDependencies.DialogHelper.OpenXMLFileDialog();
if (fileName == null)
{
if (fileName == null) {
return;
}
LoadVehicleData(fileName);
LoadStageInputData(fileName);
return;
}
......@@ -168,11 +171,9 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
}
if (filename == null)
{
if (filename == null) {
filename = dialogHelper.SaveToXMLDialog(Settings.Default.DefaultFilePath);
if (filename == null)
{
if (filename == null) {
return;
}
}
......@@ -189,65 +190,64 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
var valid = false;
var validationError = "";
try
{
try {
var validator = new XMLValidator(xDoc.ToXmlDocument());
valid = validator.ValidateXML(XmlDocumentType.DeclarationJobData);
validationError = validator.ValidationError;
} catch (Exception e) {
dialogHelper.ShowMessageBox(messageBoxText: (e.Message + "\n" + e.InnerException),
caption: "Error saving File");
}
catch (Exception e)
{
dialogHelper.ShowMessageBox(messageBoxText: (e.Message + "\n" + e.InnerException), caption: "Error saving File");
}
if (!valid)
{
if (!valid) {
dialogHelper.ShowMessageBox($"Invalid Document: {validationError}", "Error");
var tempFile = Path.GetTempFileName();
try
{
try {
xDoc.Save(tempFile, SaveOptions.OmitDuplicateNamespaces);
LoadVehicleData(tempFile);
File.Delete(tempFile);
}
catch (Exception e)
{
LoadStageInputData(tempFile);
} catch (Exception e) {
dialogHelper.ShowMessageBox(e.Message, "Error");
throw;
} finally {
if (File.Exists(tempFile)) {
File.Delete(tempFile);
}
;
}
}
else
{
} else {
xDoc.Save(filename, SaveOptions.OmitDuplicateNamespaces);
LoadVehicleData(filename);
LoadStageInputData(filename);
}
}
protected virtual bool LoadStageInputData(IDeclarationInputDataProvider inputData)
{
private bool LoadVehicleData(string fileName)
var vehicleInputData = inputData.JobInputData.Vehicle;
VehicleViewModel.SetVehicleInputData(vehicleInputData);
InputDataFilePath = inputData.DataSource.SourceFile;
return true;
}
protected bool LoadStageInputData(string fileName)
{
try
{
var inputData = (IDeclarationInputDataProvider)_inputDataReader.Create(fileName);
var vehicleInputData = inputData.JobInputData.Vehicle;
VehicleViewModel.SetVehicleInputData(vehicleInputData);
VehicleInputDataFilePath = fileName;
return LoadStageInputData(inputData);
}
catch (Exception e)
{
_multistageDependencies.DialogHelper.ShowMessageBox(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
return true;
}
public string VehicleInputDataFilePath
public string InputDataFilePath
{
get => _vehicleInputDataFilePath;
set => SetProperty(ref _vehicleInputDataFilePath, value);
......
using System.CodeDom;
using System;
using System.CodeDom;
using System.Runtime.CompilerServices;
using TUGraz.VectoCommon.InputData;
using TUGraz.VectoCore.InputData.FileIO.XML;
......@@ -20,5 +22,13 @@ namespace Vecto3GUI2020Test
{
return _inputDataReader.Create(fileName);
}
public static string GetMethodName([CallerMemberName] string name = null)
{
if (name == null) {
throw new ArgumentException();
}
return name;
}
}
}
\ No newline at end of file
using System.Runtime.InteropServices;
using Ninject;
using NUnit.Framework;
using TUGraz.VectoCore.Configuration;
using VECTO3GUI2020.ViewModel.Interfaces.Document;
using VECTO3GUI2020.ViewModel.Interfaces.JobEdit;
using VECTO3GUI2020.ViewModel.MultiStage.Implementation;
using VECTO3GUI2020.ViewModel.MultiStage.Interfaces;
namespace Vecto3GUI2020Test.ViewModelTests
{
[TestFixture]
public class StageViewModelTests : ViewModelTestBase
{
[TestCase(true, TestName="Exempted")]
[TestCase(false, TestName="NotExempted")]
public void updateFilePathsWhenSavedAs_non_exempted(bool exempted)
{
IMultiStageViewModelFactory vmFactory = _kernel.Get<IMultiStageViewModelFactory>();
var StageInput = vmFactory.GetStageInputViewModel(exempted) as StageInputViewModel;
var vehicleVm = StageInput.VehicleViewModel as InterimStageBusVehicleViewModel_v2_8;
vehicleVm.Manufacturer = "adsf";
vehicleVm.ManufacturerAddress = "asdf 123";
vehicleVm.VIN = "1234567890";
var fileName = TestHelper.GetMethodName() + ".xml";
StageInput.SaveInputDataExecute(GetFullPath(fileName));
Assert.True(checkFileNameExists(fileName));
Assert.AreEqual(GetFullPath(fileName), StageInput.InputDataFilePath);
//Check if title is updated
StringAssert.Contains(fileName, StageInput.Title);
//Check datasource
Assert.NotNull(StageInput.DataSource);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment