-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathNetZipArchiveFileSystemTest.cs
132 lines (114 loc) · 4.51 KB
/
NetZipArchiveFileSystemTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using NUnit.Framework;
using SharpFileSystem.IO;
using SharpFileSystem.SharpZipArchive;
namespace SharpFileSystem.Tests.NetZipArchive
{
[TestFixture]
public class NetZipArchiveFileSystemTest
{
private Stream zipStream;
private NetZipArchiveFileSystem fileSystem;
private string fileContentString = "this is a file";
[OneTimeSetUp]
public void Initialize()
{
var memoryStream = new MemoryStream();
zipStream = memoryStream;
var zipOutput = new ZipOutputStream(zipStream);
var fileContentBytes = Encoding.ASCII.GetBytes(fileContentString);
zipOutput.PutNextEntry(new ZipEntry("textfileA.txt")
{
Size = fileContentBytes.Length
});
zipOutput.Write(fileContentBytes);
zipOutput.PutNextEntry(new ZipEntry("directory/fileInDirectory.txt"));
zipOutput.PutNextEntry(new ZipEntry("scratchdirectory/scratch"));
zipOutput.Finish();
memoryStream.Position = 0;
fileSystem = NetZipArchiveFileSystem.Open(zipStream);
}
[OneTimeTearDown]
public void Cleanup()
{
fileSystem.Dispose();
zipStream.Dispose();
}
private readonly FileSystemPath directoryPath = FileSystemPath.Parse("/directory/");
private readonly FileSystemPath textfileAPath = FileSystemPath.Parse("/textfileA.txt");
private readonly FileSystemPath fileInDirectoryPath = FileSystemPath.Parse("/directory/fileInDirectory.txt");
private readonly FileSystemPath scratchDirectoryPath = FileSystemPath.Parse("/scratchdirectory/");
[Test]
public void GetEntitiesOfRootTest()
{
CollectionAssert.AreEquivalent(new[]
{
textfileAPath,
directoryPath,
scratchDirectoryPath
}, fileSystem.GetEntities(FileSystemPath.Root).ToArray());
}
[Test]
public void GetEntitiesOfDirectoryTest()
{
CollectionAssert.AreEquivalent(new[]
{
fileInDirectoryPath
}, fileSystem.GetEntities(directoryPath).ToArray());
}
[Test]
public void ExistsTest()
{
Assert.IsTrue(fileSystem.Exists(FileSystemPath.Root));
Assert.IsTrue(fileSystem.Exists(textfileAPath));
Assert.IsTrue(fileSystem.Exists(directoryPath));
Assert.IsTrue(fileSystem.Exists(fileInDirectoryPath));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/nonExistingFile")));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/nonExistingDirectory/")));
Assert.IsFalse(fileSystem.Exists(FileSystemPath.Parse("/directory/nonExistingFileInDirectory")));
}
[Test]
public void CanReadFile()
{
var file = fileSystem.OpenFile(textfileAPath, FileAccess.ReadWrite);
var text = file.ReadAllText();
Assert.IsTrue(string.Equals(text, fileContentString));
}
[Test]
public void CanWriteFile()
{
var file = fileSystem.OpenFile(textfileAPath, FileAccess.ReadWrite);
var textBytes = Encoding.ASCII.GetBytes(fileContentString + " and a new string");
file.Write(textBytes);
file.Close();
file = fileSystem.OpenFile(textfileAPath, FileAccess.ReadWrite);
var text = file.ReadAllText();
Assert.IsTrue(string.Equals(text, fileContentString + " and a new string"));
}
[Test]
public void CanAddFile()
{
var fsp = FileSystemPath.Parse("/scratchdirectory/recentlyadded.txt");
var file = fileSystem.CreateFile(fsp);
var textBytes = Encoding.ASCII.GetBytes("recently added");
file.Write(textBytes);
file.Close();
Assert.IsTrue(fileSystem.Exists(fsp));
file = fileSystem.OpenFile(fsp, FileAccess.ReadWrite);
var text = file.ReadAllText();
Assert.IsTrue(string.Equals(text, "recently added"));
}
[Test]
public void CanAddDirectory()
{
var fsp = FileSystemPath.Parse("/scratchdirectory/newdir/");
fileSystem.CreateDirectory(fsp);
Assert.IsTrue(fileSystem.Exists(fsp));
}
}
}