|
| 1 | +using Synercoding.FileFormats.Pdf.Generation; |
| 2 | + |
| 3 | +namespace Synercoding.FileFormats.Pdf.Tests; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Tests for the <see cref="PdfWriter"/> constructors. |
| 7 | +/// </summary> |
| 8 | +/// <remarks> |
| 9 | +/// The file path based tests are regression tests: the file used to be opened for reading instead |
| 10 | +/// of for writing, which made every file path based <see cref="PdfWriter"/> unusable. |
| 11 | +/// </remarks> |
| 12 | +public class PdfWriterConstructorTests : IDisposable |
| 13 | +{ |
| 14 | + private readonly string _tempDirectory; |
| 15 | + |
| 16 | + public PdfWriterConstructorTests() |
| 17 | + { |
| 18 | + _tempDirectory = Path.Combine(Path.GetTempPath(), "PdfWriterConstructorTests", Guid.NewGuid().ToString("N")); |
| 19 | + Directory.CreateDirectory(_tempDirectory); |
| 20 | + } |
| 21 | + |
| 22 | + public void Dispose() |
| 23 | + { |
| 24 | + try |
| 25 | + { |
| 26 | + Directory.Delete(_tempDirectory, true); |
| 27 | + } |
| 28 | + catch (IOException) |
| 29 | + { |
| 30 | + // Best effort cleanup, a leftover temp file should never fail a test. |
| 31 | + } |
| 32 | + GC.SuppressFinalize(this); |
| 33 | + } |
| 34 | + |
| 35 | + private string _getFilePath() |
| 36 | + => Path.Combine(_tempDirectory, Guid.NewGuid().ToString("N") + ".pdf"); |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Test_PdfWriter_FilePathConstructor_CreatesPdfFile() |
| 40 | + { |
| 41 | + // Arrange |
| 42 | + var filePath = _getFilePath(); |
| 43 | + |
| 44 | + // Act |
| 45 | + using (var writer = new PdfWriter(filePath)) |
| 46 | + { |
| 47 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 48 | + } |
| 49 | + |
| 50 | + // Assert |
| 51 | + Assert.True(File.Exists(filePath)); |
| 52 | + var pdfContent = File.ReadAllText(filePath, System.Text.Encoding.ASCII); |
| 53 | + Assert.StartsWith("%PDF-", pdfContent); |
| 54 | + Assert.EndsWith("%%EOF", pdfContent); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Test_PdfWriter_FilePathAndSettingsConstructor_CreatesPdfFile() |
| 59 | + { |
| 60 | + // Arrange |
| 61 | + var filePath = _getFilePath(); |
| 62 | + |
| 63 | + // Act |
| 64 | + using (var writer = new PdfWriter(filePath, new WriterSettings())) |
| 65 | + { |
| 66 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 67 | + } |
| 68 | + |
| 69 | + // Assert |
| 70 | + Assert.True(File.Exists(filePath)); |
| 71 | + var pdfContent = File.ReadAllText(filePath, System.Text.Encoding.ASCII); |
| 72 | + Assert.StartsWith("%PDF-", pdfContent); |
| 73 | + Assert.EndsWith("%%EOF", pdfContent); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void Test_PdfWriter_FilePathConstructor_WithAlreadyExistingFile_WritesPdfContent() |
| 78 | + { |
| 79 | + // Arrange |
| 80 | + var filePath = _getFilePath(); |
| 81 | + File.WriteAllText(filePath, string.Empty); |
| 82 | + |
| 83 | + // Act |
| 84 | + using (var writer = new PdfWriter(filePath)) |
| 85 | + { |
| 86 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 87 | + } |
| 88 | + |
| 89 | + // Assert |
| 90 | + var pdfContent = File.ReadAllText(filePath, System.Text.Encoding.ASCII); |
| 91 | + Assert.StartsWith("%PDF-", pdfContent); |
| 92 | + Assert.EndsWith("%%EOF", pdfContent); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void Test_PdfWriter_FilePathConstructor_WithNonExistingFile_CreatesFile() |
| 97 | + { |
| 98 | + // Arrange |
| 99 | + var filePath = _getFilePath(); |
| 100 | + Assert.False(File.Exists(filePath)); |
| 101 | + |
| 102 | + // Act |
| 103 | + using var writer = new PdfWriter(filePath); |
| 104 | + |
| 105 | + // Assert |
| 106 | + // The file is created by the constructor, opening an existing file for reading would have |
| 107 | + // thrown a FileNotFoundException instead. |
| 108 | + Assert.True(File.Exists(filePath)); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void Test_PdfWriter_FilePathConstructor_DisposeReleasesFileHandle() |
| 113 | + { |
| 114 | + // Arrange |
| 115 | + var filePath = _getFilePath(); |
| 116 | + var writer = new PdfWriter(filePath); |
| 117 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 118 | + |
| 119 | + // Act |
| 120 | + writer.Dispose(); |
| 121 | + |
| 122 | + // Assert |
| 123 | + // Opening with FileShare.None only succeeds when no other handle to the file is left open, |
| 124 | + // proving the writer owns and disposes the file stream it created. |
| 125 | + using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None); |
| 126 | + Assert.True(fileStream.Length > 0); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void Test_PdfWriter_StreamConstructor_DoesNotOwnStream() |
| 131 | + { |
| 132 | + // Arrange |
| 133 | + var stream = new MemoryStream(); |
| 134 | + |
| 135 | + // Act |
| 136 | + using (var writer = new PdfWriter(stream)) |
| 137 | + { |
| 138 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 139 | + } |
| 140 | + |
| 141 | + // Assert |
| 142 | + Assert.True(stream.CanWrite); |
| 143 | + stream.Dispose(); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void Test_PdfWriter_StreamAndSettingsConstructor_DoesNotOwnStream() |
| 148 | + { |
| 149 | + // Arrange |
| 150 | + var stream = new MemoryStream(); |
| 151 | + |
| 152 | + // Act |
| 153 | + using (var writer = new PdfWriter(stream, new WriterSettings())) |
| 154 | + { |
| 155 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 156 | + } |
| 157 | + |
| 158 | + // Assert |
| 159 | + Assert.True(stream.CanWrite); |
| 160 | + stream.Dispose(); |
| 161 | + } |
| 162 | + |
| 163 | + [Fact] |
| 164 | + public void Test_PdfWriter_StreamConstructor_WithOwnsStreamTrue_DisposesStream() |
| 165 | + { |
| 166 | + // Arrange |
| 167 | + var stream = new MemoryStream(); |
| 168 | + |
| 169 | + // Act |
| 170 | + using (var writer = new PdfWriter(stream, new WriterSettings(), true)) |
| 171 | + { |
| 172 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 173 | + } |
| 174 | + |
| 175 | + // Assert |
| 176 | + Assert.False(stream.CanWrite); |
| 177 | + } |
| 178 | + |
| 179 | + [Fact] |
| 180 | + public void Test_PdfWriter_StreamConstructor_WithOwnsStreamFalse_LeavesStreamOpen() |
| 181 | + { |
| 182 | + // Arrange |
| 183 | + var stream = new MemoryStream(); |
| 184 | + |
| 185 | + // Act |
| 186 | + using (var writer = new PdfWriter(stream, new WriterSettings(), false)) |
| 187 | + { |
| 188 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 189 | + } |
| 190 | + |
| 191 | + // Assert |
| 192 | + Assert.True(stream.CanWrite); |
| 193 | + stream.Dispose(); |
| 194 | + } |
| 195 | + |
| 196 | + [Fact] |
| 197 | + public void Test_PdfWriter_PdfStreamConstructor_WritesPdf() |
| 198 | + { |
| 199 | + // Arrange |
| 200 | + using var stream = new MemoryStream(); |
| 201 | + var pdfStream = new PdfStream(stream, false); |
| 202 | + |
| 203 | + // Act |
| 204 | + using (var writer = new PdfWriter(pdfStream, new WriterSettings())) |
| 205 | + { |
| 206 | + writer.AddPage(page => page.MediaBox = Sizes.A4.AsRectangle()); |
| 207 | + } |
| 208 | + |
| 209 | + // Assert |
| 210 | + var pdfContent = System.Text.Encoding.ASCII.GetString(stream.ToArray()); |
| 211 | + Assert.StartsWith("%PDF-", pdfContent); |
| 212 | + Assert.EndsWith("%%EOF", pdfContent); |
| 213 | + } |
| 214 | + |
| 215 | + [Fact] |
| 216 | + public void Test_PdfWriter_WithNonWritableStream_Throws() |
| 217 | + { |
| 218 | + // Arrange |
| 219 | + using var stream = new MemoryStream(new byte[16], writable: false); |
| 220 | + |
| 221 | + // Act & Assert |
| 222 | + Assert.Throws<ArgumentException>(() => new PdfWriter(stream)); |
| 223 | + } |
| 224 | + |
| 225 | + [Fact] |
| 226 | + public void Test_PdfWriter_WithReadOnlyFileStream_Throws() |
| 227 | + { |
| 228 | + // Arrange |
| 229 | + var filePath = _getFilePath(); |
| 230 | + File.WriteAllText(filePath, string.Empty); |
| 231 | + using var stream = File.OpenRead(filePath); |
| 232 | + |
| 233 | + // Act & Assert |
| 234 | + Assert.Throws<ArgumentException>(() => new PdfWriter(stream)); |
| 235 | + } |
| 236 | + |
| 237 | + [Fact] |
| 238 | + public void Test_PdfWriter_Constructor_WritesHeader() |
| 239 | + { |
| 240 | + // Arrange |
| 241 | + using var stream = new MemoryStream(); |
| 242 | + |
| 243 | + // Act |
| 244 | + using var writer = new PdfWriter(stream); |
| 245 | + |
| 246 | + // Assert |
| 247 | + var pdfContent = System.Text.Encoding.ASCII.GetString(stream.ToArray()); |
| 248 | + Assert.StartsWith("%PDF-2.0", pdfContent); |
| 249 | + } |
| 250 | + |
| 251 | + [Fact] |
| 252 | + public void Test_PdfWriter_Constructor_SetsDefaults() |
| 253 | + { |
| 254 | + // Arrange |
| 255 | + using var stream = new MemoryStream(); |
| 256 | + |
| 257 | + // Act |
| 258 | + using var writer = new PdfWriter(stream); |
| 259 | + |
| 260 | + // Assert |
| 261 | + Assert.Equal(0, writer.PageCount); |
| 262 | + Assert.Null(writer.PageMode); |
| 263 | + Assert.Null(writer.PageLayout); |
| 264 | + Assert.NotNull(writer.DocumentInformation); |
| 265 | + Assert.StartsWith("Synercoding.FileFormats.Pdf ", writer.DocumentInformation.Producer); |
| 266 | + Assert.NotNull(writer.DocumentInformation.CreationDate); |
| 267 | + } |
| 268 | +} |
0 commit comments