File size: 21,383 Bytes
fab29d7 |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
using System.Text;
using VersOne.Epub.Internal;
using VersOne.Epub.Options;
using VersOne.Epub.Test.Unit.Mocks;
namespace VersOne.Epub.Test.Unit.Content.Loaders
{
public class EpubLocalContentLoaderTests
{
private const string CONTENT_DIRECTORY_PATH = "Content";
private const string TEXT_FILE_NAME = "test.html";
private const string TEXT_FILE_PATH = $"{CONTENT_DIRECTORY_PATH}/{TEXT_FILE_NAME}";
private const string TEXT_FILE_CONTENT = "<html><head><title>Test HTML</title></head><body><h1>Test content</h1></body></html>";
private const EpubContentType TEXT_FILE_CONTENT_TYPE = EpubContentType.XHTML_1_1;
private const string TEXT_FILE_CONTENT_MIME_TYPE = "application/xhtml+xml";
private const string BYTE_FILE_NAME = "image.jpg";
private const string BYTE_FILE_PATH = $"{CONTENT_DIRECTORY_PATH}/{BYTE_FILE_NAME}";
private const EpubContentType BYTE_FILE_CONTENT_TYPE = EpubContentType.IMAGE_JPEG;
private const string BYTE_FILE_CONTENT_MIME_TYPE = "image/jpeg";
private static readonly byte[] BYTE_FILE_CONTENT = new byte[] { 0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46 };
private static EpubContentFileRefMetadata TextFileRefMetadata => new(TEXT_FILE_NAME, TEXT_FILE_CONTENT_TYPE, TEXT_FILE_CONTENT_MIME_TYPE);
private static EpubContentFileRefMetadata ByteFileRefMetadata => new(BYTE_FILE_NAME, BYTE_FILE_CONTENT_TYPE, BYTE_FILE_CONTENT_MIME_TYPE);
[Fact(DisplayName = "Constructing a local content loader with non-null constructor parameters should succeed")]
public void ConstructorWithNonNullParametersTest()
{
_ = new EpubLocalContentLoader(new TestEnvironmentDependencies(), new TestZipFile(), CONTENT_DIRECTORY_PATH, new ContentReaderOptions());
}
[Fact(DisplayName = "Constructor should throw ArgumentNullException if environmentDependencies parameter is null")]
public void ConstructorWithNullEnvironmentDependenciesTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalContentLoader(null!, new TestZipFile(), CONTENT_DIRECTORY_PATH, new ContentReaderOptions()));
}
[Fact(DisplayName = "Constructor should throw ArgumentNullException if epubFile parameter is null")]
public void ConstructorWithNullEpubFileTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalContentLoader(new TestEnvironmentDependencies(), null!, CONTENT_DIRECTORY_PATH, new ContentReaderOptions()));
}
[Fact(DisplayName = "Constructor should throw ArgumentNullException if contentDirectoryPath parameter is null")]
public void ConstructorWithNullContentDirectoryPathTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalContentLoader(new TestEnvironmentDependencies(), new TestZipFile(), null!, new ContentReaderOptions()));
}
[Fact(DisplayName = "Constructing a local content loader with null contentReaderOptions parameter should succeed")]
public void ConstructorWithNullContentReaderOptionsTest()
{
_ = new EpubLocalContentLoader(new TestEnvironmentDependencies(), new TestZipFile(), CONTENT_DIRECTORY_PATH, null);
}
[Fact(DisplayName = "Loading content of an existing text file synchronously should succeed")]
public void LoadContentAsTextTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(TEXT_FILE_PATH, TEXT_FILE_CONTENT);
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
string textContent = epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata);
Assert.Equal(TEXT_FILE_CONTENT, textContent);
}
[Fact(DisplayName = "Loading content of an existing text file asynchronously should succeed")]
public async Task LoadContentAsTextAsyncTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(TEXT_FILE_PATH, TEXT_FILE_CONTENT);
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
string textContent = await epubLocalContentLoader.LoadContentAsTextAsync(TextFileRefMetadata);
Assert.Equal(TEXT_FILE_CONTENT, textContent);
}
[Fact(DisplayName = "Loading content of an existing byte file synchronously should succeed")]
public void LoadContentAsBytesTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(BYTE_FILE_PATH, BYTE_FILE_CONTENT);
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
byte[] byteContent = epubLocalContentLoader.LoadContentAsBytes(ByteFileRefMetadata);
Assert.Equal(BYTE_FILE_CONTENT, byteContent);
}
[Fact(DisplayName = "Loading content of an existing byte file asynchronously should succeed")]
public async Task LoadContentAsBytesAsyncTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(BYTE_FILE_PATH, BYTE_FILE_CONTENT);
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
byte[] byteContent = await epubLocalContentLoader.LoadContentAsBytesAsync(ByteFileRefMetadata);
Assert.Equal(BYTE_FILE_CONTENT, byteContent);
}
[Fact(DisplayName = "Getting content stream of an existing file synchronously should succeed")]
public void GetContentStreamTest()
{
TestZipFile testZipFile = new();
using MemoryStream testContentStream = new();
testZipFile.AddEntry(TEXT_FILE_PATH, testContentStream);
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Stream textContentStream = epubLocalContentLoader.GetContentStream(TextFileRefMetadata);
Assert.Equal(testContentStream, textContentStream);
}
[Fact(DisplayName = "Getting content stream of an existing file asynchronously should succeed")]
public async Task GetContentStreamAsyncTest()
{
TestZipFile testZipFile = new();
using MemoryStream testContentStream = new();
testZipFile.AddEntry(TEXT_FILE_PATH, testContentStream);
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Stream textContentStream = await epubLocalContentLoader.GetContentStreamAsync(TextFileRefMetadata);
Assert.Equal(testContentStream, textContentStream);
}
[Fact(DisplayName = "Loading content of multiple existing files should succeed")]
public void LoadContentWithMultipleFilesTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(TEXT_FILE_PATH, TEXT_FILE_CONTENT);
testZipFile.AddEntry(BYTE_FILE_PATH, BYTE_FILE_CONTENT);
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
string textContent = epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata);
Assert.Equal(TEXT_FILE_CONTENT, textContent);
byte[] byteContent = epubLocalContentLoader.LoadContentAsBytes(ByteFileRefMetadata);
Assert.Equal(BYTE_FILE_CONTENT, byteContent);
}
[Fact(DisplayName = "LoadContentAsText should throw EpubContentException if the file is missing in the EPUB archive")]
public void LoadContentAsTextWithMissingFileTest()
{
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<EpubContentException>(() => epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsTextAsync should throw EpubContentException if the file is missing in the EPUB archive")]
public async Task LoadContentAsTextAsyncWithMissingFileTest()
{
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.LoadContentAsTextAsync(TextFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsBytes should throw EpubContentException if the file is missing in the EPUB archive")]
public void LoadContentAsBytesWithMissingFileTest()
{
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<EpubContentException>(() => epubLocalContentLoader.LoadContentAsBytes(ByteFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsBytesAsync should throw EpubContentException if the file is missing in the EPUB archive")]
public async Task LoadContentAsBytesAsyncWithMissingFileTest()
{
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.LoadContentAsBytesAsync(ByteFileRefMetadata));
}
[Fact(DisplayName = "GetContentStream should throw EpubContentException if the file is missing in the EPUB archive")]
public void GetContentStreamWithMissingFileTest()
{
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<EpubContentException>(() => epubLocalContentLoader.GetContentStream(TextFileRefMetadata));
}
[Fact(DisplayName = "GetContentStreamAsync should throw EpubContentException if the file is missing in the EPUB archive")]
public async Task GetContentStreamAsyncWithMissingFileTest()
{
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.GetContentStreamAsync(TextFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsText should throw EpubContentException if the file is larger than 2 GB")]
public void LoadContentAsTextWithLargeFileTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(TEXT_FILE_PATH, new Test4GbZipFileEntry());
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<EpubContentException>(() => epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsTextAsync should throw EpubContentException if the file is larger than 2 GB")]
public async Task LoadContentAsTextAsyncWithLargeFileTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(TEXT_FILE_PATH, new Test4GbZipFileEntry());
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.LoadContentAsTextAsync(TextFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsBytes should throw EpubContentException if the file is larger than 2 GB")]
public void LoadContentAsBytesWithLargeFileTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(BYTE_FILE_PATH, new Test4GbZipFileEntry());
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<EpubContentException>(() => epubLocalContentLoader.LoadContentAsBytes(ByteFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsBytesAsync should throw EpubContentException if the file is larger than 2 GB")]
public async Task LoadContentAsBytesAsyncWithLargeFileTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(BYTE_FILE_PATH, new Test4GbZipFileEntry());
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.LoadContentAsBytesAsync(ByteFileRefMetadata));
}
[Fact(DisplayName = "GetContentStream should throw EpubContentException if the file is larger than 2 GB")]
public void GetContentStreamWithLargeFileTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(TEXT_FILE_PATH, new Test4GbZipFileEntry());
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<EpubContentException>(() => epubLocalContentLoader.GetContentStream(TextFileRefMetadata));
}
[Fact(DisplayName = "GetContentStreamAsync should throw EpubContentException if the file is larger than 2 GB")]
public async Task GetContentStreamAsyncWithLargeFileTest()
{
TestZipFile testZipFile = new();
testZipFile.AddEntry(TEXT_FILE_PATH, new Test4GbZipFileEntry());
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.GetContentStreamAsync(TextFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsText should throw ObjectDisposedException if the EPUB file is disposed")]
public void LoadContentAsTextWithDisposedEpubFileTest()
{
TestZipFile testZipFile = new();
testZipFile.Dispose();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<ObjectDisposedException>(() => epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsTextAsync should throw ObjectDisposedException if the EPUB file is disposed")]
public async Task LoadContentAsTextAsyncWithDisposedEpubFileTest()
{
TestZipFile testZipFile = new();
testZipFile.Dispose();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<ObjectDisposedException>(() => epubLocalContentLoader.LoadContentAsTextAsync(TextFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsBytes should throw ObjectDisposedException if the EPUB file is disposed")]
public void LoadContentAsBytesWithDisposedEpubFileTest()
{
TestZipFile testZipFile = new();
testZipFile.Dispose();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<ObjectDisposedException>(() => epubLocalContentLoader.LoadContentAsBytes(ByteFileRefMetadata));
}
[Fact(DisplayName = "LoadContentAsBytesAsync should throw ObjectDisposedException if the EPUB file is disposed")]
public async Task LoadContentAsBytesAsyncWithDisposedEpubFileTest()
{
TestZipFile testZipFile = new();
testZipFile.Dispose();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<ObjectDisposedException>(() => epubLocalContentLoader.LoadContentAsBytesAsync(ByteFileRefMetadata));
}
[Fact(DisplayName = "GetContentStream should throw ObjectDisposedException if the EPUB file is disposed")]
public void GetContentStreamWithDisposedEpubFileTest()
{
TestZipFile testZipFile = new();
testZipFile.Dispose();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
Assert.Throws<ObjectDisposedException>(() => epubLocalContentLoader.GetContentStream(TextFileRefMetadata));
}
[Fact(DisplayName = "GetContentStreamAsync should throw ObjectDisposedException if the EPUB file is disposed")]
public async Task GetContentStreamAsyncWithDisposedEpubFileTest()
{
TestZipFile testZipFile = new();
testZipFile.Dispose();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile);
await Assert.ThrowsAsync<ObjectDisposedException>(() => epubLocalContentLoader.GetContentStreamAsync(TextFileRefMetadata));
}
[Fact(DisplayName = "Local content loader should return an empty content if the file is missing and SuppressException is true")]
public void LoadContentWithMissingFileAndSuppressExceptionTrueTest()
{
ContentReaderOptions contentReaderOptions = new();
contentReaderOptions.ContentFileMissing += (sender, e) => e.SuppressException = true;
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile, contentReaderOptions);
string textContent = epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata);
Assert.Equal(String.Empty, textContent);
}
[Fact(DisplayName = "Local content loader should return a replacement content if the file is missing and ReplacementContentStream is set")]
public void LoadContentWithMissingFileAndReplacementContentStreamSetTest()
{
ContentReaderOptions contentReaderOptions = new();
contentReaderOptions.ContentFileMissing += (sender, e) => e.ReplacementContentStream = new MemoryStream(Encoding.UTF8.GetBytes(TEXT_FILE_CONTENT));
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile, contentReaderOptions);
string textContent = epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata);
Assert.Equal(TEXT_FILE_CONTENT, textContent);
}
[Fact(DisplayName = "Using replacement content multiple times while loading the same content file should succeed")]
public void LoadContentWithReplacementContentMultipleTimesTest()
{
ContentReaderOptions contentReaderOptions = new();
contentReaderOptions.ContentFileMissing += (sender, e) => e.ReplacementContentStream = new MemoryStream(Encoding.UTF8.GetBytes(TEXT_FILE_CONTENT));
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile, contentReaderOptions);
string textContent = epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata);
Assert.Equal(TEXT_FILE_CONTENT, textContent);
textContent = epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata);
Assert.Equal(TEXT_FILE_CONTENT, textContent);
}
[Fact(DisplayName = "Local content loader should throw EpubContentException if the file is missing and ContentFileMissing has no event handlers")]
public void LoadContentWithMissingFileAndNoContentFileMissingHandlersTest()
{
ContentReaderOptions contentReaderOptions = new();
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile, contentReaderOptions);
Assert.Throws<EpubContentException>(() => epubLocalContentLoader.GetContentStream(TextFileRefMetadata));
}
[Fact(DisplayName = "ContentFileMissingEventArgs should contain details of the missing content file")]
public void ContentFileMissingEventArgsTest()
{
ContentReaderOptions contentReaderOptions = new();
ContentFileMissingEventArgs? contentFileMissingEventArgs = null;
contentReaderOptions.ContentFileMissing += (sender, e) =>
{
e.SuppressException = true;
contentFileMissingEventArgs = e;
};
TestZipFile testZipFile = new();
EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile, contentReaderOptions);
epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata);
Assert.NotNull(contentFileMissingEventArgs);
Assert.Equal(TEXT_FILE_NAME, contentFileMissingEventArgs.FileKey);
Assert.Equal(TEXT_FILE_PATH, contentFileMissingEventArgs.FilePath);
Assert.Equal(TEXT_FILE_CONTENT_TYPE, contentFileMissingEventArgs.ContentType);
Assert.Equal(TEXT_FILE_CONTENT_MIME_TYPE, contentFileMissingEventArgs.ContentMimeType);
}
private static EpubLocalContentLoader CreateEpubLocalContentLoader(TestZipFile testZipFile, ContentReaderOptions? contentReaderOptions = null)
{
return new(new TestEnvironmentDependencies(), testZipFile, CONTENT_DIRECTORY_PATH, contentReaderOptions);
}
}
}
|