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 13, 2024
2 parents c034625 + b13f282 commit 264d72f
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public virtual void ThrowSomeExceptionsTest() {
eventManager.OnEvent(new ITextTestEvent(sequenceId, null, "test-event", ProductNameConstant.ITEXT_CORE));
}
catch (AggregatedException e) {
NUnit.Framework.Assert.AreEqual("Error during event processing:\n" + "0) ThrowArithmeticExpHandler\n" + "1) ThrowIllegalArgumentExpHandler\n"
, e.Message);
IList<Exception> aggregatedExceptions = e.GetAggregatedExceptions();
NUnit.Framework.Assert.AreEqual(2, aggregatedExceptions.Count);
NUnit.Framework.Assert.AreEqual("ThrowArithmeticExpHandler", aggregatedExceptions[0].Message);
NUnit.Framework.Assert.AreEqual("ThrowIllegalArgumentExpHandler", aggregatedExceptions[1].Message);
NUnit.Framework.Assert.AreEqual("ThrowArithmeticExpHandler", FindHandler(aggregatedExceptions, typeof(ArithmeticException
)).Message);
NUnit.Framework.Assert.AreEqual("ThrowIllegalArgumentExpHandler", FindHandler(aggregatedExceptions, typeof(
ArgumentException)).Message);
}
eventManager.Unregister(handler1);
eventManager.Unregister(handler2);
Expand Down Expand Up @@ -106,6 +106,15 @@ public virtual void TurningOffAgplTest() {
NUnit.Framework.Assert.IsTrue(underAgplProductProcessorFactory1 is UnderAgplProductProcessorFactory);
}

private Exception FindHandler(IList<Exception> events, Type eventClass) {
foreach (Exception @event in events) {
if (eventClass.IsInstanceOfType(@event)) {
return @event;
}
}
return null;
}

private class ThrowArithmeticExpHandler : IEventHandler {
public virtual void OnEvent(IEvent @event) {
throw new ArithmeticException("ThrowArithmeticExpHandler");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using iText.Commons.Exceptions;
using iText.Commons.Utils;
using iText.Test;

namespace iText.Commons.Datastructures {
[NUnit.Framework.Category("UnitTest")]
public class ConcurrentHashSetTest : ExtendedITextTest {
[NUnit.Framework.Test]
public virtual void SizeTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
set.Add("1");
set.Add("2");
NUnit.Framework.Assert.AreEqual(2, set.Count);
}

[NUnit.Framework.Test]
public virtual void ContainsKeyTrueTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
set.Add("1");
set.Add("2");
set.Add("3");
NUnit.Framework.Assert.IsTrue(set.Contains("1"));
NUnit.Framework.Assert.IsTrue(set.Contains("2"));
NUnit.Framework.Assert.IsTrue(set.Contains("3"));
}

[NUnit.Framework.Test]
public virtual void ContainsKeyFalseTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
set.Add("1");
NUnit.Framework.Assert.IsFalse(set.Contains("5"));
}

[NUnit.Framework.Test]
public virtual void ClearTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
set.Add("1");
set.Clear();
NUnit.Framework.Assert.IsFalse(set.Contains("1"));
}

[NUnit.Framework.Test]
public virtual void AddTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
set.Add("1");
set.Add("1");
NUnit.Framework.Assert.AreEqual(1, set.Count);
}

[NUnit.Framework.Test]
public virtual void RemoveTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
set.Add("1");
set.Add("2");
set.Remove("1");
NUnit.Framework.Assert.IsFalse(set.Contains("1"));
}

[NUnit.Framework.Test]
public virtual void ForEachTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
ICollection<String> anotherSet = new HashSet<String>();
set.Add("1");
set.Add("2");
set.Add("3");
set.ForEach((str) => {
anotherSet.Add(str);
}
);
NUnit.Framework.Assert.AreEqual(3, anotherSet.Count);
}

[NUnit.Framework.Test]
public virtual void EqualsTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
set.Add("1");
HashSet<String> anotherSet = new HashSet<String>();
NUnit.Framework.Assert.IsFalse(set.Equals(anotherSet));
}

[NUnit.Framework.Test]
public virtual void AddAllTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
HashSet<String> anotherSet = new HashSet<String>();
anotherSet.Add("1");
anotherSet.Add("2");
set.AddAll(anotherSet);
NUnit.Framework.Assert.AreEqual(2, set.Count);
}

[NUnit.Framework.Test]
public virtual void RemoveAllTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
HashSet<String> anotherSet = new HashSet<String>();
Exception e = NUnit.Framework.Assert.Catch(typeof(NotSupportedException), () => set.RemoveAll(anotherSet));
NUnit.Framework.Assert.AreEqual(CommonsExceptionMessageConstant.UNSUPPORTED_OPERATION, e.Message);
}

[NUnit.Framework.Test]
public virtual void RetainAllTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
HashSet<String> anotherSet = new HashSet<String>();
Exception e = NUnit.Framework.Assert.Catch(typeof(NotSupportedException), () => set.RetainAll(anotherSet));
NUnit.Framework.Assert.AreEqual(CommonsExceptionMessageConstant.UNSUPPORTED_OPERATION, e.Message);
}

[NUnit.Framework.Test]
public virtual void ContainsAllTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
HashSet<String> anotherSet = new HashSet<String>();
Exception e = NUnit.Framework.Assert.Catch(typeof(NotSupportedException), () => set.ContainsAll(anotherSet
));
NUnit.Framework.Assert.AreEqual(CommonsExceptionMessageConstant.UNSUPPORTED_OPERATION, e.Message);
}

[NUnit.Framework.Test]
public virtual void HashCodeTest() {
ConcurrentHashSet<String> set = new ConcurrentHashSet<String>();
set.Add("1");
HashSet<String> anotherSet = new HashSet<String>();
anotherSet.Add("2");
NUnit.Framework.Assert.AreNotEqual(set.GetHashCode(), JavaUtil.SetHashCode(anotherSet));
}
}
}
7 changes: 4 additions & 3 deletions itext/itext.commons/itext/commons/actions/EventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ You should have received a copy of the GNU Affero General Public License
using System;
using System.Collections.Generic;
using iText.Commons.Actions.Processors;
using iText.Commons.Datastructures;
using iText.Commons.Exceptions;
using iText.Commons.Utils;

namespace iText.Commons.Actions {
/// <summary>Entry point for event handling mechanism.</summary>
Expand All @@ -37,7 +37,7 @@ public sealed class EventManager {
private static readonly iText.Commons.Actions.EventManager INSTANCE = new iText.Commons.Actions.EventManager
();

private readonly ICollection<IEventHandler> handlers = new LinkedHashSet<IEventHandler>();
private readonly ConcurrentHashSet<IEventHandler> handlers = new ConcurrentHashSet<IEventHandler>();

private EventManager() {
handlers.Add(ProductEventHandler.INSTANCE);
Expand All @@ -63,14 +63,15 @@ public static void AcknowledgeAgplUsageDisableWarningMessage() {
/// <param name="event">to handle</param>
public void OnEvent(IEvent @event) {
IList<Exception> caughtExceptions = new List<Exception>();
foreach (IEventHandler handler in handlers) {
handlers.ForEach((handler) => {
try {
handler.OnEvent(@event);
}
catch (Exception ex) {
caughtExceptions.Add(ex);
}
}
);
if (@event is AbstractITextConfigurationEvent) {
try {
AbstractITextConfigurationEvent itce = (AbstractITextConfigurationEvent)@event;
Expand Down
Loading

0 comments on commit 264d72f

Please sign in to comment.