LediReader
/
EpubReader
/Source
/VersOne.Epub.Test
/Unit
/Content
/Loaders
/EpubLocalContentLoaderTests.cs
| 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); | |
| [] | |
| public void ConstructorWithNonNullParametersTest() | |
| { | |
| _ = new EpubLocalContentLoader(new TestEnvironmentDependencies(), new TestZipFile(), CONTENT_DIRECTORY_PATH, new ContentReaderOptions()); | |
| } | |
| [] | |
| public void ConstructorWithNullEnvironmentDependenciesTest() | |
| { | |
| Assert.Throws<ArgumentNullException>(() => new EpubLocalContentLoader(null!, new TestZipFile(), CONTENT_DIRECTORY_PATH, new ContentReaderOptions())); | |
| } | |
| [] | |
| public void ConstructorWithNullEpubFileTest() | |
| { | |
| Assert.Throws<ArgumentNullException>(() => new EpubLocalContentLoader(new TestEnvironmentDependencies(), null!, CONTENT_DIRECTORY_PATH, new ContentReaderOptions())); | |
| } | |
| [] | |
| public void ConstructorWithNullContentDirectoryPathTest() | |
| { | |
| Assert.Throws<ArgumentNullException>(() => new EpubLocalContentLoader(new TestEnvironmentDependencies(), new TestZipFile(), null!, new ContentReaderOptions())); | |
| } | |
| [] | |
| public void ConstructorWithNullContentReaderOptionsTest() | |
| { | |
| _ = new EpubLocalContentLoader(new TestEnvironmentDependencies(), new TestZipFile(), CONTENT_DIRECTORY_PATH, null); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| public void LoadContentAsTextWithMissingFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<EpubContentException>(() => epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata)); | |
| } | |
| [] | |
| public async Task LoadContentAsTextAsyncWithMissingFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.LoadContentAsTextAsync(TextFileRefMetadata)); | |
| } | |
| [] | |
| public void LoadContentAsBytesWithMissingFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<EpubContentException>(() => epubLocalContentLoader.LoadContentAsBytes(ByteFileRefMetadata)); | |
| } | |
| [] | |
| public async Task LoadContentAsBytesAsyncWithMissingFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.LoadContentAsBytesAsync(ByteFileRefMetadata)); | |
| } | |
| [] | |
| public void GetContentStreamWithMissingFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<EpubContentException>(() => epubLocalContentLoader.GetContentStream(TextFileRefMetadata)); | |
| } | |
| [] | |
| public async Task GetContentStreamAsyncWithMissingFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| await Assert.ThrowsAsync<EpubContentException>(() => epubLocalContentLoader.GetContentStreamAsync(TextFileRefMetadata)); | |
| } | |
| [] | |
| public void LoadContentAsTextWithLargeFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.AddEntry(TEXT_FILE_PATH, new Test4GbZipFileEntry()); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<EpubContentException>(() => epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata)); | |
| } | |
| [] | |
| 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)); | |
| } | |
| [] | |
| public void LoadContentAsBytesWithLargeFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.AddEntry(BYTE_FILE_PATH, new Test4GbZipFileEntry()); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<EpubContentException>(() => epubLocalContentLoader.LoadContentAsBytes(ByteFileRefMetadata)); | |
| } | |
| [] | |
| 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)); | |
| } | |
| [] | |
| public void GetContentStreamWithLargeFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.AddEntry(TEXT_FILE_PATH, new Test4GbZipFileEntry()); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<EpubContentException>(() => epubLocalContentLoader.GetContentStream(TextFileRefMetadata)); | |
| } | |
| [] | |
| 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)); | |
| } | |
| [] | |
| public void LoadContentAsTextWithDisposedEpubFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.Dispose(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<ObjectDisposedException>(() => epubLocalContentLoader.LoadContentAsText(TextFileRefMetadata)); | |
| } | |
| [] | |
| public async Task LoadContentAsTextAsyncWithDisposedEpubFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.Dispose(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| await Assert.ThrowsAsync<ObjectDisposedException>(() => epubLocalContentLoader.LoadContentAsTextAsync(TextFileRefMetadata)); | |
| } | |
| [] | |
| public void LoadContentAsBytesWithDisposedEpubFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.Dispose(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<ObjectDisposedException>(() => epubLocalContentLoader.LoadContentAsBytes(ByteFileRefMetadata)); | |
| } | |
| [] | |
| public async Task LoadContentAsBytesAsyncWithDisposedEpubFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.Dispose(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| await Assert.ThrowsAsync<ObjectDisposedException>(() => epubLocalContentLoader.LoadContentAsBytesAsync(ByteFileRefMetadata)); | |
| } | |
| [] | |
| public void GetContentStreamWithDisposedEpubFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.Dispose(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| Assert.Throws<ObjectDisposedException>(() => epubLocalContentLoader.GetContentStream(TextFileRefMetadata)); | |
| } | |
| [] | |
| public async Task GetContentStreamAsyncWithDisposedEpubFileTest() | |
| { | |
| TestZipFile testZipFile = new(); | |
| testZipFile.Dispose(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile); | |
| await Assert.ThrowsAsync<ObjectDisposedException>(() => epubLocalContentLoader.GetContentStreamAsync(TextFileRefMetadata)); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| 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); | |
| } | |
| [] | |
| public void LoadContentWithMissingFileAndNoContentFileMissingHandlersTest() | |
| { | |
| ContentReaderOptions contentReaderOptions = new(); | |
| TestZipFile testZipFile = new(); | |
| EpubLocalContentLoader epubLocalContentLoader = CreateEpubLocalContentLoader(testZipFile, contentReaderOptions); | |
| Assert.Throws<EpubContentException>(() => epubLocalContentLoader.GetContentStream(TextFileRefMetadata)); | |
| } | |
| [] | |
| 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); | |
| } | |
| } | |
| } | |