File size: 20,556 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 |
using VersOne.Epub.Test.Unit.Mocks;
namespace VersOne.Epub.Test.Unit.Content.Base
{
public class EpubContentFileRefTests
{
private const string CONTENT_DIRECTORY_PATH = "Content";
private const string LOCAL_TEXT_FILE_NAME = "test.html";
private const string LOCAL_TEXT_FILE_PATH = $"{CONTENT_DIRECTORY_PATH}/{LOCAL_TEXT_FILE_NAME}";
private const string REMOTE_TEXT_FILE_HREF = "https://example.com/books/123/test.html";
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 LOCAL_BYTE_FILE_NAME = "image.jpg";
private const string LOCAL_BYTE_FILE_PATH = $"{CONTENT_DIRECTORY_PATH}/{LOCAL_BYTE_FILE_NAME}";
private const string REMOTE_BYTE_FILE_HREF = "https://example.com/books/123/image.jpg";
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 LocalTextFileRefMetadata => new(LOCAL_TEXT_FILE_NAME, TEXT_FILE_CONTENT_TYPE, TEXT_FILE_CONTENT_MIME_TYPE);
private static EpubContentFileRefMetadata LocalByteFileRefMetadata => new(LOCAL_BYTE_FILE_NAME, BYTE_FILE_CONTENT_TYPE, BYTE_FILE_CONTENT_MIME_TYPE);
private static EpubContentFileRefMetadata RemoteTextFileRefMetadata => new(REMOTE_TEXT_FILE_HREF, TEXT_FILE_CONTENT_TYPE, TEXT_FILE_CONTENT_MIME_TYPE);
private static EpubContentFileRefMetadata RemoteByteFileRefMetadata => new(REMOTE_BYTE_FILE_HREF, BYTE_FILE_CONTENT_TYPE, BYTE_FILE_CONTENT_MIME_TYPE);
[Fact(DisplayName = "EpubLocalTextContentFileRef constructor should set correct property values")]
public void LocalTextContentFileRefConstructorTest()
{
EpubLocalTextContentFileRef epubLocalTextContentFileRef = new(LocalTextFileRefMetadata, LOCAL_TEXT_FILE_PATH, new TestEpubContentLoader());
Assert.Equal(LOCAL_TEXT_FILE_NAME, epubLocalTextContentFileRef.Key);
Assert.Equal(TEXT_FILE_CONTENT_TYPE, epubLocalTextContentFileRef.ContentType);
Assert.Equal(TEXT_FILE_CONTENT_MIME_TYPE, epubLocalTextContentFileRef.ContentMimeType);
Assert.Equal(EpubContentLocation.LOCAL, epubLocalTextContentFileRef.ContentLocation);
Assert.Equal(EpubContentFileType.TEXT, epubLocalTextContentFileRef.ContentFileType);
Assert.Equal(LOCAL_TEXT_FILE_PATH, epubLocalTextContentFileRef.FilePath);
}
[Fact(DisplayName = "EpubLocalByteContentFileRef constructor should set correct property values")]
public void LocalByteContentFileRefConstructorTest()
{
EpubLocalByteContentFileRef epubLocalByteContentFileRef = new(LocalByteFileRefMetadata, LOCAL_BYTE_FILE_PATH, new TestEpubContentLoader());
Assert.Equal(LOCAL_BYTE_FILE_NAME, epubLocalByteContentFileRef.Key);
Assert.Equal(BYTE_FILE_CONTENT_TYPE, epubLocalByteContentFileRef.ContentType);
Assert.Equal(BYTE_FILE_CONTENT_MIME_TYPE, epubLocalByteContentFileRef.ContentMimeType);
Assert.Equal(EpubContentLocation.LOCAL, epubLocalByteContentFileRef.ContentLocation);
Assert.Equal(EpubContentFileType.BYTE_ARRAY, epubLocalByteContentFileRef.ContentFileType);
Assert.Equal(LOCAL_BYTE_FILE_PATH, epubLocalByteContentFileRef.FilePath);
}
[Fact(DisplayName = "EpubRemoteTextContentFileRef constructor should set correct property values")]
public void RemoteTextContentFileRefConstructorTest()
{
EpubRemoteTextContentFileRef epubRemoteTextContentFileRef = new(RemoteTextFileRefMetadata, new TestEpubContentLoader());
Assert.Equal(REMOTE_TEXT_FILE_HREF, epubRemoteTextContentFileRef.Key);
Assert.Equal(TEXT_FILE_CONTENT_TYPE, epubRemoteTextContentFileRef.ContentType);
Assert.Equal(TEXT_FILE_CONTENT_MIME_TYPE, epubRemoteTextContentFileRef.ContentMimeType);
Assert.Equal(EpubContentLocation.REMOTE, epubRemoteTextContentFileRef.ContentLocation);
Assert.Equal(EpubContentFileType.TEXT, epubRemoteTextContentFileRef.ContentFileType);
Assert.Equal(REMOTE_TEXT_FILE_HREF, epubRemoteTextContentFileRef.Url);
}
[Fact(DisplayName = "EpubRemoteByteContentFileRef constructor should set correct property values")]
public void RemoteByteContentFileRefConstructorTest()
{
EpubRemoteByteContentFileRef epubRemoteByteContentFileRef = new(RemoteByteFileRefMetadata, new TestEpubContentLoader());
Assert.Equal(REMOTE_BYTE_FILE_HREF, epubRemoteByteContentFileRef.Key);
Assert.Equal(BYTE_FILE_CONTENT_TYPE, epubRemoteByteContentFileRef.ContentType);
Assert.Equal(BYTE_FILE_CONTENT_MIME_TYPE, epubRemoteByteContentFileRef.ContentMimeType);
Assert.Equal(EpubContentLocation.REMOTE, epubRemoteByteContentFileRef.ContentLocation);
Assert.Equal(EpubContentFileType.BYTE_ARRAY, epubRemoteByteContentFileRef.ContentFileType);
Assert.Equal(REMOTE_BYTE_FILE_HREF, epubRemoteByteContentFileRef.Url);
}
[Fact(DisplayName = "EpubLocalTextContentFileRef constructor should throw ArgumentNullException if the supplied metadata parameter is null")]
public void CreateEpubLocalTextContentFileRefWithNullMetadataTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalTextContentFileRef(null!, LOCAL_TEXT_FILE_PATH, new TestEpubContentLoader()));
}
[Fact(DisplayName = "EpubLocalByteContentFileRef constructor should throw ArgumentNullException if the supplied metadata parameter is null")]
public void CreateEpubLocalByteContentFileRefWithNullMetadataTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalByteContentFileRef(null!, LOCAL_BYTE_FILE_PATH, new TestEpubContentLoader()));
}
[Fact(DisplayName = "EpubRemoteTextContentFileRef constructor should throw ArgumentNullException if the supplied metadata parameter is null")]
public void CreateEpubRemoteTextContentFileRefWithNullMetadataTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubRemoteTextContentFileRef(null!, new TestEpubContentLoader()));
}
[Fact(DisplayName = "EpubRemoteByteContentFileRef constructor should throw ArgumentNullException if the supplied metadata parameter is null")]
public void CreateEpubRemoteByteContentFileRefWithNullMetadataTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubRemoteByteContentFileRef(null!, new TestEpubContentLoader()));
}
[Fact(DisplayName = "EpubLocalTextContentFileRef constructor should throw ArgumentNullException if the supplied epubContentLoader parameter is null")]
public void CreateEpubLocalTextContentFileRefWithNullContentLoaderTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalTextContentFileRef(LocalTextFileRefMetadata, LOCAL_TEXT_FILE_PATH, null!));
}
[Fact(DisplayName = "EpubLocalByteContentFileRef constructor should throw ArgumentNullException if the supplied epubContentLoader parameter is null")]
public void CreateEpubLocalByteContentFileRefWithNullContentLoaderTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalByteContentFileRef(LocalByteFileRefMetadata, LOCAL_BYTE_FILE_PATH, null!));
}
[Fact(DisplayName = "EpubRemoteTextContentFileRef constructor should throw ArgumentNullException if the supplied epubContentLoader parameter is null")]
public void CreateEpubRemoteTextContentFileRefWithNullContentLoaderTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubRemoteTextContentFileRef(RemoteTextFileRefMetadata, null!));
}
[Fact(DisplayName = "EpubRemoteByteContentFileRef constructor should throw ArgumentNullException if the supplied epubContentLoader parameter is null")]
public void CreateEpubRemoteByteContentFileRefWithNullContentLoaderTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubRemoteByteContentFileRef(RemoteByteFileRefMetadata, null!));
}
[Fact(DisplayName = "EpubLocalTextContentFileRef constructor should throw ArgumentNullException if the supplied file path is null")]
public void CreateEpubLocalTextContentFileRefWithNullFilePathTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalTextContentFileRef(LocalTextFileRefMetadata, null!, new TestEpubContentLoader()));
}
[Fact(DisplayName = "EpubLocalByteContentFileRef constructor should throw ArgumentNullException if the supplied file path is null")]
public void CreateEpubLocalByteContentFileRefWithNullFilePathTest()
{
Assert.Throws<ArgumentNullException>(() => new EpubLocalByteContentFileRef(LocalByteFileRefMetadata, null!, new TestEpubContentLoader()));
}
[Fact(DisplayName = "Reading local text content file synchronously should succeed")]
public void LocalTextContentFileRefReadContentTest()
{
TestEpubContentLoader testEpubContentLoader = new(TEXT_FILE_CONTENT);
EpubLocalTextContentFileRef epubLocalTextContentFileRef = new(LocalTextFileRefMetadata, LOCAL_TEXT_FILE_PATH, testEpubContentLoader);
string content = epubLocalTextContentFileRef.ReadContent();
Assert.Equal(TEXT_FILE_CONTENT, content);
}
[Fact(DisplayName = "Reading local text content file asynchronously should succeed")]
public async Task LocalTextContentFileRefReadContentAsyncTest()
{
TestEpubContentLoader testEpubContentLoader = new(TEXT_FILE_CONTENT);
EpubLocalTextContentFileRef epubLocalTextContentFileRef = new(LocalTextFileRefMetadata, LOCAL_TEXT_FILE_PATH, testEpubContentLoader);
string content = await epubLocalTextContentFileRef.ReadContentAsync();
Assert.Equal(TEXT_FILE_CONTENT, content);
}
[Fact(DisplayName = "Reading local byte content file synchronously should succeed")]
public void LocalByteContentFileRefReadContentTest()
{
TestEpubContentLoader testEpubContentLoader = new(BYTE_FILE_CONTENT);
EpubLocalByteContentFileRef epubLocalByteContentFileRef = new(LocalByteFileRefMetadata, LOCAL_BYTE_FILE_PATH, testEpubContentLoader);
byte[] content = epubLocalByteContentFileRef.ReadContent();
Assert.Equal(BYTE_FILE_CONTENT, content);
}
[Fact(DisplayName = "Reading local byte content file asynchronously should succeed")]
public async Task LocalByteContentFileRefReadContentAsyncTest()
{
TestEpubContentLoader testEpubContentLoader = new(BYTE_FILE_CONTENT);
EpubLocalByteContentFileRef epubLocalByteContentFileRef = new(LocalByteFileRefMetadata, LOCAL_BYTE_FILE_PATH, testEpubContentLoader);
byte[] content = await epubLocalByteContentFileRef.ReadContentAsync();
Assert.Equal(BYTE_FILE_CONTENT, content);
}
[Fact(DisplayName = "Reading local content file as text synchronously should succeed")]
public void LocalContentFileRefReadContentAsTextTest()
{
TestEpubContentLoader testEpubContentLoader = new(TEXT_FILE_CONTENT);
EpubLocalTextContentFileRef epubLocalTextContentFileRef = new(LocalTextFileRefMetadata, LOCAL_TEXT_FILE_PATH, testEpubContentLoader);
string content = epubLocalTextContentFileRef.ReadContentAsText();
Assert.Equal(TEXT_FILE_CONTENT, content);
}
[Fact(DisplayName = "Reading local content file as text asynchronously should succeed")]
public async Task LocalContentFileRefReadContentAsTextAsyncTest()
{
TestEpubContentLoader testEpubContentLoader = new(TEXT_FILE_CONTENT);
EpubLocalTextContentFileRef epubLocalTextContentFileRef = new(LocalTextFileRefMetadata, LOCAL_TEXT_FILE_PATH, testEpubContentLoader);
string content = await epubLocalTextContentFileRef.ReadContentAsTextAsync();
Assert.Equal(TEXT_FILE_CONTENT, content);
}
[Fact(DisplayName = "Reading local content file as bytes synchronously should succeed")]
public void LocalContentFileRefReadContentAsBytesTest()
{
TestEpubContentLoader testEpubContentLoader = new(BYTE_FILE_CONTENT);
EpubLocalByteContentFileRef epubLocalByteContentFileRef = new(LocalByteFileRefMetadata, LOCAL_BYTE_FILE_PATH, testEpubContentLoader);
byte[] content = epubLocalByteContentFileRef.ReadContentAsBytes();
Assert.Equal(BYTE_FILE_CONTENT, content);
}
[Fact(DisplayName = "Reading local content file as bytes asynchronously should succeed")]
public async Task LocalContentFileRefReadContentAsBytesAsyncTest()
{
TestEpubContentLoader testEpubContentLoader = new(BYTE_FILE_CONTENT);
EpubLocalByteContentFileRef epubLocalByteContentFileRef = new(LocalByteFileRefMetadata, LOCAL_BYTE_FILE_PATH, testEpubContentLoader);
byte[] content = await epubLocalByteContentFileRef.ReadContentAsBytesAsync();
Assert.Equal(BYTE_FILE_CONTENT, content);
}
[Fact(DisplayName = "Getting local content file stream synchronously should succeed")]
public void LocalContentFileRefGetContentStreamTest()
{
using MemoryStream testStream = new();
TestEpubContentLoader testEpubContentLoader = new(testStream);
EpubLocalTextContentFileRef epubLocalTextContentFileRef = new(LocalTextFileRefMetadata, LOCAL_TEXT_FILE_PATH, testEpubContentLoader);
Stream contentStream = epubLocalTextContentFileRef.GetContentStream();
Assert.Equal(testStream, contentStream);
}
[Fact(DisplayName = "Getting local content file stream asynchronously should succeed")]
public async Task LocalContentFileRefGetContentStreamAsyncTest()
{
using MemoryStream testStream = new();
TestEpubContentLoader testEpubContentLoader = new(testStream);
EpubLocalTextContentFileRef epubLocalTextContentFileRef = new(LocalTextFileRefMetadata, LOCAL_TEXT_FILE_PATH, testEpubContentLoader);
Stream contentStream = await epubLocalTextContentFileRef.GetContentStreamAsync();
Assert.Equal(testStream, contentStream);
}
[Fact(DisplayName = "Downloading remote text content file synchronously should succeed")]
public void RemoteTextContentFileRefDownloadContentTest()
{
TestEpubContentLoader testEpubContentLoader = new(TEXT_FILE_CONTENT);
EpubRemoteTextContentFileRef epubRemoteTextContentFileRef = new(RemoteTextFileRefMetadata, testEpubContentLoader);
string content = epubRemoteTextContentFileRef.DownloadContent();
Assert.Equal(TEXT_FILE_CONTENT, content);
}
[Fact(DisplayName = "Downloading remote text content file asynchronously should succeed")]
public async Task RemoteTextContentFileRefDownloadContentAsyncTest()
{
TestEpubContentLoader testEpubContentLoader = new(TEXT_FILE_CONTENT);
EpubRemoteTextContentFileRef epubRemoteTextContentFileRef = new(RemoteTextFileRefMetadata, testEpubContentLoader);
string content = await epubRemoteTextContentFileRef.DownloadContentAsync();
Assert.Equal(TEXT_FILE_CONTENT, content);
}
[Fact(DisplayName = "Downloading remote byte content file synchronously should succeed")]
public void RemoteByteContentFileRefDownloadContentTest()
{
TestEpubContentLoader testEpubContentLoader = new(BYTE_FILE_CONTENT);
EpubRemoteByteContentFileRef epubRemoteByteContentFileRef = new(RemoteByteFileRefMetadata, testEpubContentLoader);
byte[] content = epubRemoteByteContentFileRef.DownloadContent();
Assert.Equal(BYTE_FILE_CONTENT, content);
}
[Fact(DisplayName = "Downloading remote byte content file asynchronously should succeed")]
public async Task RemoteByteContentFileRefDownloadContentAsyncTest()
{
TestEpubContentLoader testEpubContentLoader = new(BYTE_FILE_CONTENT);
EpubRemoteByteContentFileRef epubRemoteByteContentFileRef = new(RemoteByteFileRefMetadata, testEpubContentLoader);
byte[] content = await epubRemoteByteContentFileRef.DownloadContentAsync();
Assert.Equal(BYTE_FILE_CONTENT, content);
}
[Fact(DisplayName = "Downloading remote content file as text synchronously should succeed")]
public void RemoteContentFileRefDownloadContentAsTextTest()
{
TestEpubContentLoader testEpubContentLoader = new(TEXT_FILE_CONTENT);
EpubRemoteTextContentFileRef epubRemoteTextContentFileRef = new(RemoteTextFileRefMetadata, testEpubContentLoader);
string content = epubRemoteTextContentFileRef.DownloadContentAsText();
Assert.Equal(TEXT_FILE_CONTENT, content);
}
[Fact(DisplayName = "Downloading remote content file as text asynchronously should succeed")]
public async Task RemoteContentFileRefDownloadContentAsTextAsyncTest()
{
TestEpubContentLoader testEpubContentLoader = new(TEXT_FILE_CONTENT);
EpubRemoteTextContentFileRef epubRemoteTextContentFileRef = new(RemoteTextFileRefMetadata, testEpubContentLoader);
string content = await epubRemoteTextContentFileRef.DownloadContentAsTextAsync();
Assert.Equal(TEXT_FILE_CONTENT, content);
}
[Fact(DisplayName = "Downloading remote content file as bytes synchronously should succeed")]
public void RemoteContentFileRefDownloadContentAsBytesTest()
{
TestEpubContentLoader testEpubContentLoader = new(BYTE_FILE_CONTENT);
EpubRemoteByteContentFileRef epubRemoteByteContentFileRef = new(RemoteByteFileRefMetadata, testEpubContentLoader);
byte[] content = epubRemoteByteContentFileRef.DownloadContentAsBytes();
Assert.Equal(BYTE_FILE_CONTENT, content);
}
[Fact(DisplayName = "Downloading remote content file as bytes asynchronously should succeed")]
public async Task RemoteContentFileRefDownloadContentAsBytesAsyncTest()
{
TestEpubContentLoader testEpubContentLoader = new(BYTE_FILE_CONTENT);
EpubRemoteByteContentFileRef epubRemoteByteContentFileRef = new(RemoteByteFileRefMetadata, testEpubContentLoader);
byte[] content = await epubRemoteByteContentFileRef.DownloadContentAsBytesAsync();
Assert.Equal(BYTE_FILE_CONTENT, content);
}
[Fact(DisplayName = "Getting remote content file stream synchronously should succeed")]
public void RemoteContentFileRefGetDownloadingContentStreamTest()
{
MemoryStream testStream = new();
TestEpubContentLoader testEpubContentLoader = new(testStream);
EpubRemoteTextContentFileRef epubRemoteTextContentFileRef = new(RemoteTextFileRefMetadata, testEpubContentLoader);
Stream contentStream = epubRemoteTextContentFileRef.GetDownloadingContentStream();
Assert.Equal(testStream, contentStream);
}
[Fact(DisplayName = "Getting remote content file stream asynchronously should succeed")]
public async Task RemoteContentFileRefGetDownloadingContentStreamAsyncTest()
{
MemoryStream testStream = new();
TestEpubContentLoader testEpubContentLoader = new(testStream);
EpubRemoteTextContentFileRef epubRemoteTextContentFileRef = new(RemoteTextFileRefMetadata, testEpubContentLoader);
Stream contentStream = await epubRemoteTextContentFileRef.GetDownloadingContentStreamAsync();
Assert.Equal(testStream, contentStream);
}
}
}
|