// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using Core.Common.Utils;
namespace Core.Common.TestUtil
{
///
/// This class can be used to set temporary files while testing.
/// Disposing an instance of this class will delete the files.
///
///
/// The following is an example for how to use this class:
///
/// using(new FileDisposeHelper(new[]{"pathToFile"})) {
/// // Perform tests with files
/// }
///
///
public class FileDisposeHelper : IDisposable
{
private readonly IEnumerable files;
///
/// Creates a new instance of .
///
/// Paths of the files that will be created, if the path is valid.
/// Thrown when one of the files in could
/// not be created by the system.
public FileDisposeHelper(IEnumerable filePaths)
{
files = filePaths;
Create();
}
///
/// Creates a new instance of .
///
/// Path of the file that will be created, if valid.
/// Thrown when the file could not be created by the system.
public FileDisposeHelper(string filePath) : this(new[]
{
filePath
}) {}
public void Dispose()
{
foreach (var file in files)
{
try
{
DeleteFile(file);
}
catch
{
// ignored
}
}
}
///
/// Creates the temporary files.
///
/// Thrown when the file could not be created by the system.
private void Create()
{
foreach (var file in files)
{
CreateFile(file);
}
}
///
/// Creates a file at at the given file path. If the is
/// invalid, no file is created.
///
/// Path of the new file.
/// Thrown when either:
///
/// - The caller does not have the required permission.-or- path specified a file that is read-only.
/// - The specified path, file name, or both exceed the system-defined maximum length. For example, on
/// Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260
/// characters.
/// - The specified path is invalid (for example, it is on an unmapped drive).
/// - An I/O error occurred while creating the file.
///
///
private static void CreateFile(string filePath)
{
if (FileUtils.IsValidFilePath(filePath))
{
try
{
using (File.Create(filePath)) {}
}
catch (Exception e)
{
if (e is DirectoryNotFoundException || e is IOException || e is NotSupportedException || e is UnauthorizedAccessException)
{
throw new ArgumentException(e.Message);
}
throw;
}
}
}
///
/// Deletes a file at at the given file path. If the is
/// invalid, no file is deleted (obviously).
///
/// Path of the file to delete.
/// Thrown when either:
///
/// - The specified is invalid (for example, it is on an unmapped drive).
/// - The specified is in use. -or-There is an open handle on the file,
/// and the operating system is Windows XP or earlier. This open handle can result from enumerating
/// directories and files. For more information, see How to: Enumerate Directories and Files.
/// - is in an invalid format.
/// - The specified exceed the system-defined
/// maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and
/// file names must be less than 260 characters.
/// - The caller does not have the required permission.-or-
/// is a directory.-or- specified a read-only file.
///
///
private static void DeleteFile(string filePath)
{
if (FileUtils.IsValidFilePath(filePath))
{
try
{
File.Delete(filePath);
}
catch (Exception e)
{
if (e is DirectoryNotFoundException || e is IOException || e is NotSupportedException || e is UnauthorizedAccessException)
{
throw new ArgumentException(e.Message);
}
throw;
}
}
}
}
}