From 1e60f358cb42e92cfc66d3555800fff7b6efcee0 Mon Sep 17 00:00:00 2001 From: ArtjomP Date: Fri, 16 Feb 2018 00:18:34 +0300 Subject: [PATCH] UWP unit tests framework has been changed from NUnit to MSTest. Because for UWP only MSTests work well. Fixing issue #17 (#18) --- .../Extensions/StorageExtensions.cs | 4 +- .../StorageFileImplementation.cs | 79 +++-- .../StorageFolderImplementation.cs | 56 +++- .../FilesTest.cs | 243 ++++++++++++-- .../FoldersTest.cs | 301 ++++++++++++++++-- .../StandardFoldersTest.cs | 63 +++- .../TestFolder.cs | 2 +- .../PCLExt.FileStorage.UWP.Test.csproj | 7 +- 8 files changed, 655 insertions(+), 100 deletions(-) diff --git a/src/PCLExt.FileStorage.UWP/Extensions/StorageExtensions.cs b/src/PCLExt.FileStorage.UWP/Extensions/StorageExtensions.cs index a3a826c..eb784c7 100644 --- a/src/PCLExt.FileStorage.UWP/Extensions/StorageExtensions.cs +++ b/src/PCLExt.FileStorage.UWP/Extensions/StorageExtensions.cs @@ -15,7 +15,7 @@ public static Windows.Storage.NameCollisionOption ConvertToNameCollision( return Windows.Storage.NameCollisionOption.FailIfExists; if (collisionOption == NameCollisionOption.GenerateUniqueName) - return Windows.Storage.NameCollisionOption.GenerateUniqueName; + return Windows.Storage.NameCollisionOption.FailIfExists; if (collisionOption == NameCollisionOption.ReplaceExisting) return Windows.Storage.NameCollisionOption.ReplaceExisting; @@ -29,7 +29,7 @@ public static CreationCollisionOption ConvertToCreationCollision( if (collisionOption == NameCollisionOption.FailIfExists) return CreationCollisionOption.FailIfExists; if (collisionOption == NameCollisionOption.GenerateUniqueName) - return CreationCollisionOption.GenerateUniqueName; + return CreationCollisionOption.FailIfExists; if (collisionOption == NameCollisionOption.ReplaceExisting) return CreationCollisionOption.ReplaceExisting; diff --git a/src/PCLExt.FileStorage.UWP/StorageFileImplementation.cs b/src/PCLExt.FileStorage.UWP/StorageFileImplementation.cs index ea24618..8f29466 100644 --- a/src/PCLExt.FileStorage.UWP/StorageFileImplementation.cs +++ b/src/PCLExt.FileStorage.UWP/StorageFileImplementation.cs @@ -1,4 +1,6 @@ -using System; +using PCLExt.FileStorage.Extensions; +using PCLExt.FileStorage.UWP.Extensions; +using System; using System.IO; using System.Threading; using System.Threading.Tasks; @@ -7,8 +9,6 @@ using Windows.Storage.FileProperties; using PCLExt.FileStorage.Exceptions; -using PCLExt.FileStorage.Extensions; -using PCLExt.FileStorage.UWP.Extensions; namespace PCLExt.FileStorage.UWP { @@ -36,7 +36,7 @@ public bool Exists } } - public long Size => (long) GetStorageFileProperties().Size; + public long Size => (long)GetStorageFileProperties().Size; public DateTime CreationTime => _storageFile.DateCreated.ToLocalTime().DateTime; @@ -121,10 +121,32 @@ await _storageFile.CopyAsync(newFolder, newFile.Name). var newFolder = await StorageFolder.GetFolderFromPathAsync( System.IO.Path.GetDirectoryName(newPath)); - var newName = System.IO.Path.GetFileName(newPath); + var initialNewName = System.IO.Path.GetFileName(newPath); + var newName = initialNewName; var windowsNameCollision = StorageExtensions.ConvertToNameCollision(collisionOption); - var newFile = await _storageFile.CopyAsync( - newFolder, newName, windowsNameCollision); + var nameCollisionCounter = 2; + StorageFile newFile = null; + while (true) + { + try + { + newFile = await _storageFile.CopyAsync( + newFolder, newName, windowsNameCollision); + break; + } + catch (Exception ex) + { + if (collisionOption != NameCollisionOption.GenerateUniqueName) + { + if (ex.Message.Contains("HRESULT: 0x800700B7")) + throw new Exceptions.FileExistException(newPath, ex); + else + throw; + } + newName = $"{initialNewName} ({nameCollisionCounter++})"; + } + + } return new StorageFileImplementation(newFile.Path); } @@ -135,7 +157,14 @@ public void Delete() public async Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken)) { - await _storageFile.DeleteAsync().AsTask(cancellationToken); + try + { + await _storageFile.DeleteAsync().AsTask(cancellationToken); + } + catch (System.IO.FileNotFoundException ex) + { + throw new Exceptions.FileNotFoundException(_storageFile.Path, ex); + } } public void Move(IFile newFile) @@ -167,30 +196,26 @@ await MoveAsync( var newFolder = await StorageFolder.GetFolderFromPathAsync( System.IO.Path.GetDirectoryName(newPath)); - string newName; - if (collisionOption == NameCollisionOption.GenerateUniqueName) - { - newName = $"{Guid.NewGuid()}"; - var extension = PortablePath.GetExtension(newPath); - if (!string.IsNullOrEmpty(extension)) - newName = newName + "." + extension; - } - else - { - newName = System.IO.Path.GetFileName(newPath); - } - var windowsCollisionOption = StorageExtensions. ConvertToNameCollision(collisionOption); var oldFile = await StorageFile.GetFileFromPathAsync(_storageFile.Path); - try + var initialName = System.IO.Path.GetFileName(newPath); + var newName = initialName; + var nameCollisionCounter = 2; + while (true) { - await _storageFile.MoveAsync(newFolder, newName, windowsCollisionOption); - } - catch (Exception ex) - { - throw new FileExistException(newPath, ex); + try + { + await _storageFile.MoveAsync(newFolder, newName, windowsCollisionOption); + break; + } + catch (Exception ex) + { + if (collisionOption != NameCollisionOption.GenerateUniqueName) + throw new FileExistException(newPath, ex); + newName = $"{initialName} ({nameCollisionCounter++})"; + } } var result = new StorageFileImplementation(_storageFile); _storageFile = oldFile; diff --git a/src/PCLExt.FileStorage.UWP/StorageFolderImplementation.cs b/src/PCLExt.FileStorage.UWP/StorageFolderImplementation.cs index 11b788f..aa4ab21 100644 --- a/src/PCLExt.FileStorage.UWP/StorageFolderImplementation.cs +++ b/src/PCLExt.FileStorage.UWP/StorageFolderImplementation.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -90,7 +91,7 @@ public async Task CheckExistsAsync( return ExistenceCheckResult.NotFound; } - if (finded.Attributes == FileAttributes.Directory) + if (finded.Attributes == Windows.Storage.FileAttributes.Directory) { return ExistenceCheckResult.FolderExists; } @@ -124,7 +125,6 @@ private async Task CopyAsync( NameCollisionOption option = NameCollisionOption.ReplaceExisting, CancellationToken cancellationToken = default(CancellationToken)) { - var creationCollisionOption = StorageExtensions.ConvertToCreationCollision(option); // Get all files (shallow) from source var queryOptions = new QueryOptions @@ -151,9 +151,26 @@ private async Task CopyAsync( findedFile.Path == storageFile.Path) continue; } - var copiedFile = await storageFile.CopyAsync(targetStorageFolder, - storageFile.Name, - windowsOption); + + var nameCollisionCounter = 2; + var filename = storageFile.Name; + while (true) + { + try + { + var copiedFile = await storageFile.CopyAsync( + targetStorageFolder, + filename, + windowsOption); + break; + } + catch (Exception) + { + if(option != NameCollisionOption.GenerateUniqueName) + throw; + filename = $"{filename} ({nameCollisionCounter++})"; + } + } } catch (Exception ex) { @@ -165,11 +182,30 @@ private async Task CopyAsync( var queryFolders = source.CreateFolderQueryWithOptions(queryOptions); var folders = await queryFolders.GetFoldersAsync(); + var creationCollisionOption = StorageExtensions.ConvertToCreationCollision(option); + // For each folder call CopyAsync with new destination as destination foreach (var storageFolder in folders) { - var targetFolder = await folder.CreateFolderAsync(storageFolder.Name, - creationCollisionOption, cancellationToken) as StorageFolderImplementation; + var nameCollisionCounter = 2; + var subfolderName = storageFolder.Name; + StorageFolderImplementation targetFolder = null; + while (true) + { + try + { + + targetFolder = await folder.CreateFolderAsync(subfolderName, + creationCollisionOption, cancellationToken) as StorageFolderImplementation; + break; + } + catch (FolderExistException) + { + if (option != NameCollisionOption.GenerateUniqueName) + throw; + subfolderName = $"{storageFolder.Name} ({nameCollisionCounter++})"; + } + } if (targetFolder == null) throw new Exception("Can't create StorageFolder"); @@ -240,7 +276,7 @@ public void Delete() public async Task DeleteAsync( CancellationToken cancellationToken = default(CancellationToken)) { - if(Windows.ApplicationModel.Package.Current.InstalledLocation.Path == _storageFolder.Path) + if (Windows.ApplicationModel.Package.Current.InstalledLocation.Path == _storageFolder.Path) { throw new RootFolderDeletionException("Cannot delete root storage folder."); } @@ -249,7 +285,7 @@ public async Task DeleteAsync( { await _storageFolder.DeleteAsync().AsTask(cancellationToken); } - catch(Exception ex) + catch (Exception ex) { throw new FolderNotFoundException(_storageFolder.Path, ex); } @@ -272,7 +308,7 @@ public async Task GetFileAsync( } catch (Exception ex) { - throw new FileNotFoundException(name, ex); + throw new Exceptions.FileNotFoundException(name, ex); } } diff --git a/test/PCLExt.FileStorage.NetFX.Test/FilesTest.cs b/test/PCLExt.FileStorage.NetFX.Test/FilesTest.cs index 721514c..8b6cc1d 100644 --- a/test/PCLExt.FileStorage.NetFX.Test/FilesTest.cs +++ b/test/PCLExt.FileStorage.NetFX.Test/FilesTest.cs @@ -1,16 +1,23 @@ using System; using System.IO; +#if WINDOWS_UWP +using Microsoft.VisualStudio.TestTools.UnitTesting; +#else using NUnit.Framework; +#endif using PCLExt.FileStorage.Exceptions; -using PCLExt.FileStorage.Extensions; -using PCLExt.FileStorage.Files; +using PCLExt.FileStorage.Extensions; namespace PCLExt.FileStorage.Test { +#if WINDOWS_UWP + [TestClass] +#else [TestFixture] +#endif public class FilesTest { private const string FileName1 = "TestFile1"; @@ -19,13 +26,22 @@ public class FilesTest private static IFolder TestFolder => new TestFolder(); +#if WINDOWS_UWP + [TestCleanup] + [TestInitialize] +#else [SetUp] [TearDown] +#endif public void Clean() => TestFolder.Delete(); #region Size & Times +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void Size() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.OpenIfExists); @@ -39,47 +55,63 @@ public void Size() file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreationTime() { var timeBeforeFileCreaton = DateTime.Now.AddSeconds(-1); var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.OpenIfExists); - Assert.True(timeBeforeFileCreaton <= file.CreationTime); + Assert.IsTrue(timeBeforeFileCreaton <= file.CreationTime); file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreationTimeUTC() { var timeBeforeFileCreaton = DateTime.UtcNow.AddSeconds(-1); var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.OpenIfExists); - Assert.True(timeBeforeFileCreaton <= file.CreationTimeUTC, + Assert.IsTrue(timeBeforeFileCreaton <= file.CreationTimeUTC, $"{timeBeforeFileCreaton:yyyyMMddHHmmss.fff} <= {file.CreationTimeUTC:yyyyMMddHHmmss.fff}"); file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void LastAccessTime() { var timeBeforeFileCreaton = DateTime.Now.AddSeconds(-1); var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.OpenIfExists); - Assert.True(timeBeforeFileCreaton <= file.LastAccessTime); + Assert.IsTrue(timeBeforeFileCreaton <= file.LastAccessTime); file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void LastAccessTimeUTC() { var timeBeforeFileCreaton = DateTime.UtcNow.AddSeconds(-1); var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.OpenIfExists); - Assert.True(timeBeforeFileCreaton <= file.LastAccessTimeUTC); + Assert.IsTrue(timeBeforeFileCreaton <= file.LastAccessTimeUTC); file.Delete(); } @@ -88,7 +120,11 @@ public void LastAccessTimeUTC() #region Create +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateOpenIfExists() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.OpenIfExists); @@ -97,7 +133,11 @@ public void CreateOpenIfExists() file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateGenerateUniqueName() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -112,7 +152,11 @@ public void CreateGenerateUniqueName() file1.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateReplaceExisting() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -126,7 +170,11 @@ public void CreateReplaceExisting() file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFailIfExists() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -135,20 +183,33 @@ public void CreateFailIfExists() file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFailIfExistsTwice() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); Assert.IsTrue(file.Exists); + Action createFileAction = () => TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); - Assert.That(() => TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists), Throws.TypeOf()); +#if WINDOWS_UWP + Assert.ThrowsException(createFileAction); +#else + Assert.That(delegate { createFileAction.Invoke(); }, Throws.TypeOf()); +#endif } - #endregion +#endregion - #region Delete +#region Delete +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void Delete() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -157,22 +218,35 @@ public void Delete() Assert.IsFalse(file.Exists); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void DeleteTwice() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); file.Delete(); Assert.IsFalse(file.Exists); + Action deleteAction = () => file.Delete(); - Assert.That(() => file.Delete(), Throws.TypeOf()); +#if WINDOWS_UWP + Assert.ThrowsException(deleteAction); +#else + Assert.That(delegate { deleteAction.Invoke(); }, Throws.TypeOf()); +#endif } - #endregion +#endregion - #region Open +#region Open +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void OpenRead() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -190,16 +264,28 @@ public void OpenRead() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void OpenReadTryWrite() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); using (var stream = file.Open(FileAccess.Read)) +#if WINDOWS_UWP + Assert.ThrowsException(() => stream.WriteByte(0)); +#else Assert.That(() => stream.WriteByte(0), Throws.TypeOf()); +#endif } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void OpenReadWrite() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -220,19 +306,32 @@ public void OpenReadWrite() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void OpenUnknown() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); + Action openFileAction = () => file.Open((FileAccess)2); - Assert.That(() => file.Open((FileAccess) 2), Throws.ArgumentException); +#if WINDOWS_UWP + Assert.ThrowsException(openFileAction); +#else + Assert.That(delegate { openFileAction.Invoke(); }, Throws.ArgumentException); +#endif } - #endregion +#endregion - #region Rename +#region Rename +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void Rename() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -253,11 +352,15 @@ public void Rename() } } - #endregion +#endregion - #region Copy +#region Copy +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void Copy() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -283,7 +386,11 @@ public void Copy() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopySelf() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -305,7 +412,11 @@ public void CopySelf() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopyGenerateUniqueName() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -325,7 +436,11 @@ public void CopyGenerateUniqueName() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopyReplaceExisting() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -343,7 +458,11 @@ public void CopyReplaceExisting() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopySelfReplaceExisting() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -361,13 +480,23 @@ public void CopySelfReplaceExisting() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopyFailIfExists() { var file1 = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); var file2 = TestFolder.CreateFile(FileName2, CreationCollisionOption.FailIfExists); - Assert.That(() => file1.Copy(file2.Path, NameCollisionOption.FailIfExists), Throws.TypeOf()); + Action copyAction = () => file1.Copy(file2.Path, NameCollisionOption.FailIfExists); + +#if WINDOWS_UWP + Assert.ThrowsException(copyAction); +#else + Assert.That(delegate { copyAction.Invoke(); }, Throws.TypeOf()); +#endif Assert.IsTrue(file1.Exists); Assert.IsTrue(file2.Exists); @@ -376,33 +505,57 @@ public void CopyFailIfExists() file2.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopySelfFailIfExists() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); - Assert.That(() => file.Copy(file.Path, NameCollisionOption.FailIfExists), Throws.TypeOf()); + Action copyAction = () => file.Copy(file.Path, NameCollisionOption.FailIfExists); + +#if WINDOWS_UWP + Assert.ThrowsException(copyAction); +#else + Assert.That(delegate { copyAction.Invoke(); }, Throws.TypeOf()); +#endif Assert.IsTrue(file.Exists); file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopyUnknown() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); - Assert.That(() => file.Copy(file.Path, (NameCollisionOption)3), Throws.ArgumentException); + Action copyFileAction = () => file.Copy(file.Path, (NameCollisionOption)3); + +#if WINDOWS_UWP + Assert.ThrowsException(copyFileAction); +#else + Assert.That(delegate { copyFileAction.Invoke(); }, Throws.ArgumentException); +#endif file.Delete(); } - #endregion +#endregion - #region Move +#region Move +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void Move() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -427,7 +580,11 @@ public void Move() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveSelf() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -449,7 +606,11 @@ public void MoveSelf() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveGenerateUniqueName() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -468,7 +629,11 @@ public void MoveGenerateUniqueName() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveReplaceExisting() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -486,7 +651,11 @@ public void MoveReplaceExisting() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveSelfReplaceExisting() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -505,29 +674,53 @@ public void MoveSelfReplaceExisting() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveFailIfExists() { var file = TestFolder.CreateFile("TestFile", CreationCollisionOption.FailIfExists); - Assert.That(() => file.Move(file.Path, NameCollisionOption.FailIfExists), Throws.TypeOf()); + Action moveFileAction = () => file.Move(file.Path, NameCollisionOption.FailIfExists); + +#if WINDOWS_UWP + Assert.ThrowsException(moveFileAction); +#else + Assert.That(delegate { moveFileAction.Invoke(); }, Throws.TypeOf()); +#endif file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveUnknown() { var file = TestFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); - Assert.That(() => file.Move(file.Path, (NameCollisionOption) 3), Throws.ArgumentException); + Action moveAction = () => file.Move(file.Path, (NameCollisionOption)3); + +#if WINDOWS_UWP + Assert.ThrowsException(moveAction); +#else + Assert.That(delegate { moveAction.Invoke(); }, Throws.ArgumentException); +#endif file.Delete(); } - #endregion +#endregion +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void FileFromPath() { var file = new TestFolder().GetFileFromPath(Path.Combine("Folder1", FileName2)); diff --git a/test/PCLExt.FileStorage.NetFX.Test/FoldersTest.cs b/test/PCLExt.FileStorage.NetFX.Test/FoldersTest.cs index a14e310..ae7c341 100644 --- a/test/PCLExt.FileStorage.NetFX.Test/FoldersTest.cs +++ b/test/PCLExt.FileStorage.NetFX.Test/FoldersTest.cs @@ -1,6 +1,10 @@ -using System.IO; - +using System; +using System.IO; +#if WINDOWS_UWP +using Microsoft.VisualStudio.TestTools.UnitTesting; +#else using NUnit.Framework; +#endif using PCLExt.FileStorage.Exceptions; using PCLExt.FileStorage.Extensions; @@ -17,7 +21,11 @@ namespace PCLExt.FileStorage.Test { - [TestFixture] +#if WINDOWS_UWP + [TestClass] +#else + [TestFixture] +#endif public class FoldersTest { private const string FolderName1 = "TestFolder1"; @@ -25,16 +33,25 @@ public class FoldersTest private const string FileName1 = "TestFile1"; private const string FileName2 = "TestFile2"; +#if WINDOWS_UWP + [TestCleanup] + [TestInitialize] +#else [SetUp] [TearDown] +#endif public void Clean() { new TestFolder().Delete(); } - #region Create +#region Create +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFolder() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -43,7 +60,11 @@ public void CreateFolder() folder.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFolderTwiceOpenIfExists() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -53,7 +74,11 @@ public void CreateFolderTwiceOpenIfExists() folder.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFolderTwiceGenerateUniqueName() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -66,7 +91,11 @@ public void CreateFolderTwiceGenerateUniqueName() folder1.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFolderTwiceReplaceExisting() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -78,29 +107,51 @@ public void CreateFolderTwiceReplaceExisting() folder.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFolderTwiceFailIfExists() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); + Action createFolderAction = () => new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); - Assert.That(() => new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists), Throws.TypeOf()); +#if WINDOWS_UWP + Assert.ThrowsException(createFolderAction); +#else + Assert.That(delegate { createFolderAction.Invoke(); }, Throws.TypeOf()); +#endif Assert.IsTrue(folder.Exists); folder.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFolderTwiceUnknown() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); + Action createFolderAction = () => new TestFolder().CreateFolder(FolderName1, (CreationCollisionOption)4); - Assert.That(() => new TestFolder().CreateFolder(FolderName1, (CreationCollisionOption) 4), Throws.ArgumentException); +#if WINDOWS_UWP + Assert.ThrowsException(createFolderAction); +#else + Assert.That(delegate { createFolderAction.Invoke(); }, Throws.ArgumentException); +#endif Assert.IsTrue(folder.Exists); folder.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFile() { var file = new TestFolder().CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -109,7 +160,11 @@ public void CreateFile() file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFileTwiceOpenIfExists() { var file = new TestFolder().CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -119,7 +174,11 @@ public void CreateFileTwiceOpenIfExists() file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFileTwiceReplaceExisting() { var file = new TestFolder().CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -129,7 +188,11 @@ public void CreateFileTwiceReplaceExisting() file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFileTwiceGenerateUniqueName() { var file = new TestFolder().CreateFile(FileName1, CreationCollisionOption.FailIfExists); @@ -141,32 +204,56 @@ public void CreateFileTwiceGenerateUniqueName() file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFileTwiceFailIfExists() { var file = new TestFolder().CreateFile(FileName1, CreationCollisionOption.FailIfExists); - Assert.That(() => new TestFolder().CreateFile(FileName1, CreationCollisionOption.FailIfExists), Throws.TypeOf()); + Action createFileAction = () => new TestFolder().CreateFile(FileName1, CreationCollisionOption.FailIfExists); + +#if WINDOWS_UWP + Assert.ThrowsException(createFileAction); +#else + Assert.That(delegate { createFileAction.Invoke(); }, Throws.TypeOf()); +#endif Assert.IsTrue(file.Exists); file.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CreateFileTwiceUnknown() { var file = new TestFolder().CreateFile(FileName1, CreationCollisionOption.FailIfExists); - Assert.That(() => new TestFolder().CreateFile(FileName1, (CreationCollisionOption) 4), Throws.ArgumentException); + Action createFileAction = () => new TestFolder().CreateFile(FileName1, (CreationCollisionOption)4); + +#if WINDOWS_UWP + Assert.ThrowsException(createFileAction); +#else + Assert.That(delegate { createFileAction.Invoke(); }, Throws.ArgumentException); +#endif file.Delete(); } - #endregion +#endregion - #region Delete +#region Delete +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void Delete() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -175,7 +262,11 @@ public void Delete() Assert.IsFalse(folder.Exists); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void DeleteTwice() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -183,20 +274,40 @@ public void DeleteTwice() folder.Delete(); Assert.IsFalse(folder.Exists); - Assert.That(() => folder.Delete(), Throws.TypeOf()); + Action deleteAction = () => folder.Delete(); + +#if WINDOWS_UWP + Assert.ThrowsException(deleteAction); +#else + Assert.That(delegate { deleteAction.Invoke();}, Throws.TypeOf()); +#endif } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void DeleteRootStorage() { var folder = new RootFolder(); - Assert.That(() => folder.Delete(), Throws.TypeOf()); + Action deleteAction = () => folder.Delete(); + +#if WINDOWS_UWP + Assert.ThrowsException(deleteAction); +#else + Assert.That(delegate { deleteAction.Invoke(); } , Throws.TypeOf()); +#endif } - #endregion +#endregion +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void Rename() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -217,21 +328,35 @@ public void Rename() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void RenameAlreadyExists() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); var folder2 = new TestFolder().CreateFolder(FolderName2, CreationCollisionOption.FailIfExists); - Assert.That(() => folder.Rename(FolderName2), Throws.TypeOf()); + Action renameAction = () => folder.Rename(FolderName2); + +#if WINDOWS_UWP + Assert.ThrowsException(renameAction); +#else + Assert.That(delegate { renameAction.Invoke(); }, Throws.TypeOf()); +#endif folder.Delete(); folder2.Delete(); } - #region GetFile +#region GetFile +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void GetFile() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -244,15 +369,29 @@ public void GetFile() Assert.IsTrue(file1.Path == getFiles.Path); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void GetFileNotExisting() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); - Assert.That(() => folder.GetFile(FileName1), Throws.TypeOf()); + Action getFileAction = () => folder.GetFile(FileName1); + +#if WINDOWS_UWP + Assert.ThrowsException(getFileAction); +#else + Assert.That(delegate { getFileAction.Invoke();}, Throws.TypeOf()); +#endif } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void GetFiles() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -265,19 +404,33 @@ public void GetFiles() Assert.IsTrue(getFiles.Count == 2); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void GetFolderNotExisting() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); - Assert.That(() => folder.GetFolder(FolderName1), Throws.TypeOf()); + Action getFolderAction = () => folder.GetFolder(FolderName1); + +#if WINDOWS_UWP + Assert.ThrowsException(getFolderAction); +#else + Assert.That(delegate { getFolderAction.Invoke(); } , Throws.TypeOf()); +#endif } - #endregion +#endregion - #region GetFolder +#region GetFolder +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void GetFolder() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -290,7 +443,11 @@ public void GetFolder() Assert.IsTrue(folder1.Path == getFolder.Path); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void GetFolders() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -303,11 +460,15 @@ public void GetFolders() Assert.IsTrue(getFolders.Count == 2); } - #endregion +#endregion - #region Copy +#region Copy +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopyGenerateUniqueName() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -330,7 +491,11 @@ public void CopyGenerateUniqueName() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopySelfGenerateUniqueName() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -347,7 +512,11 @@ public void CopySelfGenerateUniqueName() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopyReplaceExisting() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -370,7 +539,11 @@ public void CopyReplaceExisting() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopySelfReplaceExisting() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -388,7 +561,11 @@ public void CopySelfReplaceExisting() } // TODO: Check +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopyFailIfExists() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -399,7 +576,7 @@ public void CopyFailIfExists() var newFile1 = newFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); var newFolder1 = newFolder.CreateFolder(FolderName2, CreationCollisionOption.FailIfExists); - Assert.That(delegate + Action copyAction = () => { try { @@ -414,19 +591,32 @@ public void CopyFailIfExists() { throw new IOException("", e); } + }; + +#if WINDOWS_UWP + Assert.ThrowsException(copyAction); +#else + Assert.That(delegate + { + copyAction.Invoke(); }, Throws.InstanceOf()); +#endif folder.Delete(); newFolder.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void CopySelfFailIfExists() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); var file1 = folder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); - Assert.That(delegate + Action copyAction = () => { try { @@ -441,16 +631,29 @@ public void CopySelfFailIfExists() { throw new IOException("", e); } + }; + +#if WINDOWS_UWP + Assert.ThrowsException(copyAction); +#else + Assert.That(delegate + { + copyAction.Invoke(); }, Throws.InstanceOf()); +#endif folder.Delete(); } - #endregion +#endregion - #region Move +#region Move +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveGenerateUniqueName() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -481,7 +684,11 @@ public void MoveGenerateUniqueName() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveSelfGenerateUniqueName() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -500,7 +707,11 @@ public void MoveSelfGenerateUniqueName() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveReplaceExisting() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -530,7 +741,11 @@ public void MoveReplaceExisting() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveSelfReplaceExisting() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -548,7 +763,11 @@ public void MoveSelfReplaceExisting() } } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveFailIfExists() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); @@ -559,7 +778,7 @@ public void MoveFailIfExists() var newFile1 = newFolder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); var newFolder1 = newFolder.CreateFolder(FolderName2, CreationCollisionOption.FailIfExists); - Assert.That(delegate + Action moveFolderAction = () => { try { @@ -574,19 +793,32 @@ public void MoveFailIfExists() { throw new IOException("", e); } + }; + +#if WINDOWS_UWP + Assert.ThrowsException(moveFolderAction); +#else + Assert.That(delegate + { + moveFolderAction.Invoke(); }, Throws.InstanceOf()); +#endif folder.Delete(); newFolder.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void MoveSelfFailIfExists() { var folder = new TestFolder().CreateFolder(FolderName1, CreationCollisionOption.FailIfExists); var file1 = folder.CreateFile(FileName1, CreationCollisionOption.FailIfExists); - Assert.That(delegate + Action moveSelfAction = () => { try { @@ -601,14 +833,27 @@ public void MoveSelfFailIfExists() { throw new IOException("", e); } + }; + +#if WINDOWS_UWP + Assert.ThrowsException(moveSelfAction); +#else + Assert.That(delegate + { + moveSelfAction.Invoke(); }, Throws.InstanceOf()); +#endif folder.Delete(); } - #endregion +#endregion +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void FolderFromPath() { var folder = new TestFolder().GetFolderFromPath(Path.Combine(FolderName1, FolderName2)); diff --git a/test/PCLExt.FileStorage.NetFX.Test/StandardFoldersTest.cs b/test/PCLExt.FileStorage.NetFX.Test/StandardFoldersTest.cs index 9b69c4f..a6fe8c5 100644 --- a/test/PCLExt.FileStorage.NetFX.Test/StandardFoldersTest.cs +++ b/test/PCLExt.FileStorage.NetFX.Test/StandardFoldersTest.cs @@ -1,14 +1,26 @@ using PCLExt.FileStorage.Folders; +#if WINDOWS_UWP +using Microsoft.VisualStudio.TestTools.UnitTesting; +#else using NUnit.Framework; +#endif namespace PCLExt.FileStorage.Test { +#if WINDOWS_UWP + [TestClass] +#else [TestFixture] +#endif public class StandardFoldersTest { +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void FolderFromPath() { var folder = new FolderFromPath(new TestFolder().Path); @@ -18,7 +30,11 @@ public void FolderFromPath() folder.Delete(); } +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void LocalStorageFolder() => #if __ANDROID__ || __IOS__ Assert.IsTrue(new LocalRootFolder().Exists); @@ -27,7 +43,11 @@ public void LocalStorageFolder() => #endif +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void RoamingStorageFolder() => #if __ANDROID__ || __IOS__ Assert.IsFalse(new RoamingRootFolder().Exists); @@ -35,7 +55,11 @@ public void RoamingStorageFolder() => Assert.IsTrue(new RoamingRootFolder().Exists); #endif +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void ApplicationFolder() => #if __ANDROID__ Assert.IsFalse(new ApplicationRootFolder().Exists); @@ -43,30 +67,59 @@ public void ApplicationFolder() => Assert.IsTrue(new ApplicationRootFolder().Exists); #endif +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void DocumentsFolder() => -//#if __ANDROID__ || __IOS__ -// Assert.IsFalse(new DocumentsRootFolder().Exists); -//#else - Assert.IsTrue(new DocumentsRootFolder().Exists); -//#endif + //#if __ANDROID__ || __IOS__ + // Assert.IsFalse(new DocumentsRootFolder().Exists); + //#else +#if WINDOWS_UWP + Assert.ThrowsException(() => new DocumentsRootFolder()); +#else + Assert.IsTrue(new DocumentsRootFolder().Exists); +#endif + //#endif + +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void TempFolder() => Assert.IsTrue(new TempRootFolder().Exists); +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void TempFolderCreateTemp() => Assert.IsTrue(new TempRootFolder().CreateTempFile().Exists); +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void TempFolderCreateTempExtension() => Assert.IsTrue(new TempRootFolder().CreateTempFile("ps1").Exists); +#if WINDOWS_UWP + [TestMethod] +#else [Test] +#endif public void TempFolderCreateTemp1() { +#if WINDOWS_UWP + Assert.ThrowsException(() => new DocumentsRootFolder()); +#else var t1 = new DocumentsRootFolder(); +#endif var t2 = new ApplicationRootFolder(); var t3 = new LocalRootFolder(); var t4 = new RoamingRootFolder(); diff --git a/test/PCLExt.FileStorage.NetFX.Test/TestFolder.cs b/test/PCLExt.FileStorage.NetFX.Test/TestFolder.cs index dabfe48..8bacb5f 100644 --- a/test/PCLExt.FileStorage.NetFX.Test/TestFolder.cs +++ b/test/PCLExt.FileStorage.NetFX.Test/TestFolder.cs @@ -7,7 +7,7 @@ internal class TestFolder : BaseFolder #if __ANDROID__ || __IOS__ public TestFolder() : base(new LocalRootFolder().CreateFolder("Testing", CreationCollisionOption.OpenIfExists)) { } #else - public TestFolder() : base(new ApplicationRootFolder().CreateFolder("Testing", CreationCollisionOption.OpenIfExists)) { } + public TestFolder() : base(new TempRootFolder().CreateFolder("Testing", CreationCollisionOption.OpenIfExists)) { } #endif } } \ No newline at end of file diff --git a/test/PCLExt.FileStorage.UWP.Test/PCLExt.FileStorage.UWP.Test.csproj b/test/PCLExt.FileStorage.UWP.Test/PCLExt.FileStorage.UWP.Test.csproj index d24ffdf..adf8cfa 100644 --- a/test/PCLExt.FileStorage.UWP.Test/PCLExt.FileStorage.UWP.Test.csproj +++ b/test/PCLExt.FileStorage.UWP.Test/PCLExt.FileStorage.UWP.Test.csproj @@ -141,8 +141,11 @@ 6.0.2 - - 3.8.1 + + 1.2.0 + + + 1.2.0