Example #1
0
        public void Delete(string[] files, DeleteFileBindingModel deleteFileBindingModel)
        {
            //Uses string[] files to get all the files from a specific directory.
            //Uses DeleteFileBindingModel to get specific information from the binding model and set some properties.

            //isDeleted checks if the work has been done corretly and succesfully
            bool isDeleted = false;

            //Throws exeption if a file that should be deleted is opened while the service is trying to delete the files.
            try
            {
                foreach (var file in files)
                {
                    File.Delete(file);
                    //deleteFileBindingModel.DeletedFiles property is used to add the files that are deleted in it successfully
                    deleteFileBindingModel.DeletedFiles.Add(file);
                    isDeleted = true;
                }
            }
            catch (IOException)
            {
                isDeleted = false;
                MessageBox.Show("File opened! \nPlease, close the file to proceed!", "Running process",
                                MessageBoxButtons.OK, MessageBoxIcon.Error, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }

            //If everything is completed successfully, the user gets this text.
            if (isDeleted == true)
            {
                MessageBox.Show("Files deleted", "Files Deleted", MessageBoxButtons.OK, MessageBoxIcon.None, 0,
                                MessageBoxOptions.DefaultDesktopOnly);
            }
        }
        public void DeleteMethodDeletesFilesSuccesfully(string sourcePath)
        {
            CurrentUser.user.username = "******";
            DeleteFileBindingModel bindingModel = new DeleteFileBindingModel();

            bindingModel.FileSourcePath = sourcePath;

            DeleterFileService deleteFileService = new DeleterFileService();

            string[] files = new string[] { sourcePath + "\\aaaa.pptx", sourcePath + "\\banan.docx" };


            deleteFileService.Delete(files, bindingModel);


            FileAssert.DoesNotExist(sourcePath + @"\aaaa.pptx");
            FileAssert.DoesNotExist(sourcePath + @"\banan.docx");
        }
        public void DeleteMethodFailed(string sourcePath)
        {
            CurrentUser.user.username = "******";
            DeleteFileBindingModel bindingModel = new DeleteFileBindingModel();

            bindingModel.FileSourcePath = sourcePath;

            DeleterFileService deleteFileService = new DeleterFileService();

            FieldInfo[] fields    = typeof(DeleterFileService).GetFields(BindingFlags.Instance | BindingFlags.NonPublic).ToArray();
            FieldInfo   isDeleted = fields.FirstOrDefault(x => x.Name == "isDeleted");

            isDeleted.SetValue(deleteFileService, false);

            string[] files = new string[] { sourcePath + "\\aaaa.pptx", sourcePath + "\\banan.docx" };

            deleteFileService.Delete(files, bindingModel);

            Assert.IsFalse((bool)isDeleted.GetValue(deleteFileService));
        }