Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Dec 4, 2024
2 parents 007001c + 1894324 commit 5f5a09f
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,21 @@ public virtual void MemoryFirstWriterNoFileTest() {
NUnit.Framework.Assert.IsFalse(new FileInfo(secondPdf).Exists);
}

[NUnit.Framework.Test]
public virtual void MemoryFirstWriterCmpMissingTest() {
String firstPdf = destinationFolder + "memoryFirstWriterCmpMissingTest.pdf";
String secondPdf = destinationFolder + "cmp_memoryFirstWriterCmpMissingTest.pdf";
PdfDocument firstDocument = new PdfDocument(CompareTool.CreateTestPdfWriter(firstPdf));
PdfPage page1FirstDocument = firstDocument.AddNewPage();
page1FirstDocument.AddAnnotation(new PdfLinkAnnotation(new Rectangle(100, 560, 400, 50)).SetDestination(PdfExplicitDestination
.CreateFit(page1FirstDocument)).SetBorder(new PdfArray(new float[] { 0, 0, 1 })));
page1FirstDocument.Flush();
firstDocument.Close();
NUnit.Framework.Assert.Catch(typeof(System.IO.IOException), () => new CompareTool().CompareByContent(firstPdf
, secondPdf, destinationFolder));
NUnit.Framework.Assert.IsTrue(new FileInfo(firstPdf).Exists);
}

[NUnit.Framework.Test]
public virtual void DumpMemoryFirstWriterOnDiskTest() {
String firstPdf = destinationFolder + "dumpMemoryFirstWriterOnDiskTest.pdf";
Expand Down
199 changes: 109 additions & 90 deletions itext/itext.kernel/itext/kernel/utils/CompareTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public class CompareTool {

private const String COPYRIGHT_REPLACEMENT = "\u00a9<copyright years> Apryse Group NV";

private static readonly bool MEMORY_FIRST_WRITER_DISABLED;

private const String NEW_LINES = "\\r|\\n";

private String cmpPdfName;
Expand Down Expand Up @@ -118,6 +120,11 @@ public class CompareTool {

private String compareExec;

static CompareTool() {
MEMORY_FIRST_WRITER_DISABLED = "true".EqualsIgnoreCase(SystemUtil.GetEnvironmentVariable("DISABLE_MEMORY_FIRST_WRITER"
));
}

/// <summary>
/// Create new
/// <see cref="CompareTool"/>
Expand Down Expand Up @@ -165,25 +172,15 @@ public static PdfWriter CreateTestPdfWriter(String filename) {
/// to be used in tests.
/// </returns>
public static PdfWriter CreateTestPdfWriter(String filename, WriterProperties properties) {
return new MemoryFirstPdfWriter(filename, properties);
if (MEMORY_FIRST_WRITER_DISABLED) {
return new PdfWriter(filename, properties);
}
else {
return new MemoryFirstPdfWriter(filename, properties);
}
}

// Android-Conversion-Replace return new PdfWriter(filename, properties);
/// <summary>
/// Create
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// out of the data created recently or read from disk.
/// </summary>
/// <param name="filename">File to read the data from when necessary.</param>
/// <returns>
///
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// to be used in tests.
/// </returns>
public static PdfReader CreateOutputReader(String filename) {
return iText.Kernel.Utils.CompareTool.CreateOutputReader(filename, new ReaderProperties());
}

/// <summary>
/// Create
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
Expand All @@ -210,6 +207,21 @@ public static PdfReader CreateOutputReader(String filename, ReaderProperties pro
}
}

/// <summary>
/// Create
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// out of the data created recently or read from disk.
/// </summary>
/// <param name="filename">File to read the data from when necessary.</param>
/// <returns>
///
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// to be used in tests.
/// </returns>
public static PdfReader CreateOutputReader(String filename) {
return iText.Kernel.Utils.CompareTool.CreateOutputReader(filename, new ReaderProperties());
}

/// <summary>Clean up memory occupied for the tests.</summary>
/// <param name="path">Path to clean up memory for.</param>
public static void Cleanup(String path) {
Expand Down Expand Up @@ -394,18 +406,18 @@ public virtual iText.Kernel.Utils.CompareTool EnableEncryptionCompare(bool kdfSa
/// <see cref="iText.Kernel.Pdf.ReaderProperties"/>
/// to be passed later to the
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// of the output document.
/// of the cmp document.
/// </summary>
/// <remarks>
/// Gets
/// <see cref="iText.Kernel.Pdf.ReaderProperties"/>
/// to be passed later to the
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// of the output document.
/// of the cmp document.
/// <para />
/// Documents for comparison are opened in reader mode. This method is intended to alter
/// <see cref="iText.Kernel.Pdf.ReaderProperties"/>
/// which are used to open the output document. This is particularly useful for comparison of encrypted documents.
/// which are used to open the cmp document. This is particularly useful for comparison of encrypted documents.
/// <para />
/// For more explanations about what outDoc and cmpDoc are see last paragraph of the
/// <see cref="CompareTool"/>
Expand All @@ -416,32 +428,32 @@ public virtual iText.Kernel.Utils.CompareTool EnableEncryptionCompare(bool kdfSa
/// <see cref="iText.Kernel.Pdf.ReaderProperties"/>
/// instance to be passed later to the
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// of the output document.
/// of the cmp document.
/// </returns>
public virtual ReaderProperties GetOutReaderProperties() {
if (outProps == null) {
outProps = new ReaderProperties();
public virtual ReaderProperties GetCmpReaderProperties() {
if (cmpProps == null) {
cmpProps = new ReaderProperties();
}
return outProps;
return cmpProps;
}

/// <summary>
/// Gets
/// <see cref="iText.Kernel.Pdf.ReaderProperties"/>
/// to be passed later to the
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// of the cmp document.
/// of the output document.
/// </summary>
/// <remarks>
/// Gets
/// <see cref="iText.Kernel.Pdf.ReaderProperties"/>
/// to be passed later to the
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// of the cmp document.
/// of the output document.
/// <para />
/// Documents for comparison are opened in reader mode. This method is intended to alter
/// <see cref="iText.Kernel.Pdf.ReaderProperties"/>
/// which are used to open the cmp document. This is particularly useful for comparison of encrypted documents.
/// which are used to open the output document. This is particularly useful for comparison of encrypted documents.
/// <para />
/// For more explanations about what outDoc and cmpDoc are see last paragraph of the
/// <see cref="CompareTool"/>
Expand All @@ -452,13 +464,13 @@ public virtual ReaderProperties GetOutReaderProperties() {
/// <see cref="iText.Kernel.Pdf.ReaderProperties"/>
/// instance to be passed later to the
/// <see cref="iText.Kernel.Pdf.PdfReader"/>
/// of the cmp document.
/// of the output document.
/// </returns>
public virtual ReaderProperties GetCmpReaderProperties() {
if (cmpProps == null) {
cmpProps = new ReaderProperties();
public virtual ReaderProperties GetOutReaderProperties() {
if (outProps == null) {
outProps = new ReaderProperties();
}
return cmpProps;
return outProps;
}

/// <summary>Compares two documents visually.</summary>
Expand Down Expand Up @@ -1405,73 +1417,80 @@ private String CompareByContent(String outPath, String differenceImagePrefix, ID
>> ignoredAreas) {
PrintOutCmpDirectories();
System.Console.Out.Write("Comparing by content..........");
using (PdfReader readerOut = iText.Kernel.Utils.CompareTool.CreateOutputReader(outPdf, GetOutReaderProperties
())) {
using (PdfDocument outDocument = new PdfDocument(readerOut, new DocumentProperties().SetEventCountingMetaInfo
(metaInfo))) {
using (PdfReader readerCmp = iText.Kernel.Utils.CompareTool.CreateOutputReader(cmpPdf, GetCmpReaderProperties
())) {
using (PdfDocument cmpDocument = new PdfDocument(readerCmp, new DocumentProperties().SetEventCountingMetaInfo
(metaInfo))) {
IList<PdfDictionary> outPages = new List<PdfDictionary>();
outPagesRef = new List<PdfIndirectReference>();
LoadPagesFromReader(outDocument, outPages, outPagesRef);
IList<PdfDictionary> cmpPages = new List<PdfDictionary>();
cmpPagesRef = new List<PdfIndirectReference>();
LoadPagesFromReader(cmpDocument, cmpPages, cmpPagesRef);
if (outPages.Count != cmpPages.Count) {
iText.Kernel.Utils.CompareTool.WriteOnDisk(outPdf);
iText.Kernel.Utils.CompareTool.WriteOnDiskIfNotExists(cmpPdf);
return CompareVisuallyAndCombineReports("Documents have different numbers of pages.", outPath, differenceImagePrefix
, ignoredAreas, null);
}
CompareTool.CompareResult compareResult = new CompareTool.CompareResult(compareByContentErrorsLimit);
IList<int> equalPages = new List<int>(cmpPages.Count);
for (int i = 0; i < cmpPages.Count; i++) {
ObjectPath currentPath = new ObjectPath(cmpPagesRef[i], outPagesRef[i]);
if (CompareDictionariesExtended(outPages[i], cmpPages[i], currentPath, compareResult)) {
equalPages.Add(i);
try {
using (PdfReader readerOut = iText.Kernel.Utils.CompareTool.CreateOutputReader(outPdf, GetOutReaderProperties
())) {
using (PdfDocument outDocument = new PdfDocument(readerOut, new DocumentProperties().SetEventCountingMetaInfo
(metaInfo))) {
using (PdfReader readerCmp = iText.Kernel.Utils.CompareTool.CreateOutputReader(cmpPdf, GetCmpReaderProperties
())) {
using (PdfDocument cmpDocument = new PdfDocument(readerCmp, new DocumentProperties().SetEventCountingMetaInfo
(metaInfo))) {
IList<PdfDictionary> outPages = new List<PdfDictionary>();
outPagesRef = new List<PdfIndirectReference>();
LoadPagesFromReader(outDocument, outPages, outPagesRef);
IList<PdfDictionary> cmpPages = new List<PdfDictionary>();
cmpPagesRef = new List<PdfIndirectReference>();
LoadPagesFromReader(cmpDocument, cmpPages, cmpPagesRef);
if (outPages.Count != cmpPages.Count) {
iText.Kernel.Utils.CompareTool.WriteOnDisk(outPdf);
iText.Kernel.Utils.CompareTool.WriteOnDiskIfNotExists(cmpPdf);
return CompareVisuallyAndCombineReports("Documents have different numbers of pages.", outPath, differenceImagePrefix
, ignoredAreas, null);
}
}
ObjectPath catalogPath = new ObjectPath(cmpDocument.GetCatalog().GetPdfObject().GetIndirectReference(), outDocument
.GetCatalog().GetPdfObject().GetIndirectReference());
ICollection<PdfName> ignoredCatalogEntries = new LinkedHashSet<PdfName>(JavaUtil.ArraysAsList(PdfName.Pages
, PdfName.Metadata));
CompareDictionariesExtended(outDocument.GetCatalog().GetPdfObject(), cmpDocument.GetCatalog().GetPdfObject
(), catalogPath, compareResult, ignoredCatalogEntries);
if (encryptionCompareEnabled) {
CompareDocumentsEncryption(outDocument, cmpDocument, compareResult);
CompareDocumentsMac(outDocument, cmpDocument, compareResult);
}
if (generateCompareByContentXmlReport) {
String outPdfName = new FileInfo(outPdf).Name;
Stream xml = FileUtil.GetFileOutputStream(outPath + "/" + outPdfName.JSubstring(0, outPdfName.Length - 3)
+ "report.xml");
try {
compareResult.WriteReportToXml(xml);
CompareTool.CompareResult compareResult = new CompareTool.CompareResult(compareByContentErrorsLimit);
IList<int> equalPages = new List<int>(cmpPages.Count);
for (int i = 0; i < cmpPages.Count; i++) {
ObjectPath currentPath = new ObjectPath(cmpPagesRef[i], outPagesRef[i]);
if (CompareDictionariesExtended(outPages[i], cmpPages[i], currentPath, compareResult)) {
equalPages.Add(i);
}
}
catch (Exception e) {
throw new Exception(e.Message, e);
ObjectPath catalogPath = new ObjectPath(cmpDocument.GetCatalog().GetPdfObject().GetIndirectReference(), outDocument
.GetCatalog().GetPdfObject().GetIndirectReference());
ICollection<PdfName> ignoredCatalogEntries = new LinkedHashSet<PdfName>(JavaUtil.ArraysAsList(PdfName.Pages
, PdfName.Metadata));
CompareDictionariesExtended(outDocument.GetCatalog().GetPdfObject(), cmpDocument.GetCatalog().GetPdfObject
(), catalogPath, compareResult, ignoredCatalogEntries);
if (encryptionCompareEnabled) {
CompareDocumentsEncryption(outDocument, cmpDocument, compareResult);
CompareDocumentsMac(outDocument, cmpDocument, compareResult);
}
finally {
xml.Dispose();
if (generateCompareByContentXmlReport) {
String outPdfName = new FileInfo(outPdf).Name;
Stream xml = FileUtil.GetFileOutputStream(outPath + "/" + outPdfName.JSubstring(0, outPdfName.Length - 3)
+ "report.xml");
try {
compareResult.WriteReportToXml(xml);
}
catch (Exception e) {
throw new Exception(e.Message, e);
}
finally {
xml.Dispose();
}
}
if (equalPages.Count == cmpPages.Count && compareResult.IsOk()) {
System.Console.Out.WriteLine("OK");
System.Console.Out.Flush();
return null;
}
else {
iText.Kernel.Utils.CompareTool.WriteOnDisk(outPdf);
iText.Kernel.Utils.CompareTool.WriteOnDiskIfNotExists(cmpPdf);
return CompareVisuallyAndCombineReports(compareResult.GetReport(), outPath, differenceImagePrefix, ignoredAreas
, equalPages);
}
}
if (equalPages.Count == cmpPages.Count && compareResult.IsOk()) {
System.Console.Out.WriteLine("OK");
System.Console.Out.Flush();
return null;
}
else {
iText.Kernel.Utils.CompareTool.WriteOnDisk(outPdf);
iText.Kernel.Utils.CompareTool.WriteOnDiskIfNotExists(cmpPdf);
return CompareVisuallyAndCombineReports(compareResult.GetReport(), outPath, differenceImagePrefix, ignoredAreas
, equalPages);
}
}
}
}
}
catch (Exception e) {
iText.Kernel.Utils.CompareTool.WriteOnDisk(outPdf);
iText.Kernel.Utils.CompareTool.WriteOnDiskIfNotExists(cmpPdf);
throw;
}
}

private static void WriteOnDisk(String filename) {
Expand Down
16 changes: 8 additions & 8 deletions itext/itext.kernel/itext/kernel/utils/MemoryFirstPdfWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ namespace iText.Kernel.Utils {
/// </summary>
/// <remarks>
/// PdfWriter implementation which allows to create documents in memory and dump them on disk on purpose.
/// Currently it's private and used in automated tests only.
/// Currently, it's private and used in automated tests only.
/// </remarks>
internal class MemoryFirstPdfWriter : PdfWriter {
private const int MAX_ALLOWED_STREAMS = 100;

private static IDictionary<String, iText.Kernel.Utils.MemoryFirstPdfWriter> waitingStreams = new ConcurrentDictionary
<String, iText.Kernel.Utils.MemoryFirstPdfWriter>();
private static readonly IDictionary<String, iText.Kernel.Utils.MemoryFirstPdfWriter> waitingStreams = new
ConcurrentDictionary<String, iText.Kernel.Utils.MemoryFirstPdfWriter>();

private String filePath;
private readonly String filePath;

private MemoryStream outStream;
private readonly MemoryStream outStream;

//\cond DO_NOT_DOCUMENT
internal MemoryFirstPdfWriter(String filename, WriterProperties properties)
Expand Down Expand Up @@ -84,9 +84,9 @@ internal static void Cleanup(String path) {

//\cond DO_NOT_DOCUMENT
internal virtual void Dump() {
Stream fos = FileUtil.GetFileOutputStream(filePath);
outStream.WriteTo(fos);
fos.Dispose();
using (Stream fos = FileUtil.GetFileOutputStream(filePath)) {
outStream.WriteTo(fos);
}
}
//\endcond

Expand Down
Loading

0 comments on commit 5f5a09f

Please sign in to comment.