Skip to content

Commit

Permalink
Fixed IllegalAccessException on DeepEquals for Atomic Booleans, and f…
Browse files Browse the repository at this point in the history
…ixed ThreadDeath Check
  • Loading branch information
kpartlow committed Oct 9, 2023
1 parent b1c50cd commit e6011c8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
29 changes: 27 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
<version.logback>1.4.11</version.logback>
<version.junit>5.10.0</version.junit>
<version.json.io>4.14.1</version.json.io> <!-- testing only -->
<version.mockito>1.10.19</version.mockito> <!-- testing only -->
<version.mockito>5.6.0</version.mockito> <!-- testing only -->
<version.mockito.inline>5.2.0</version.mockito.inline> <!-- testing only -->
<version.agrona>1.19.2</version.agrona> <!-- testing only -->
<version.mockito.inline>5.2.0</version.mockito.inline> <!-- testing only -->
<version.assertj>3.24.2</version.assertj> <!-- testing only -->
<version.plugin.compiler>3.11.0</version.plugin.compiler>
<version.plugin.gpg>3.1.0</version.plugin.gpg>
<version.plugin.javadoc>3.6.0</version.plugin.javadoc>
Expand Down Expand Up @@ -213,11 +216,33 @@

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-core</artifactId>
<version>${version.mockito}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${version.mockito.inline}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${version.mockito.inline}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>json-io</artifactId>
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/cedarsoftware/util/DeepEquals.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.cedarsoftware.util.Converter.convert2BigDecimal;
import static com.cedarsoftware.util.Converter.convert2boolean;
Expand Down Expand Up @@ -211,6 +212,15 @@ private static boolean deepEquals(Object a, Object b, Map<String, ?> options, Se
continue;
}

if (key1 instanceof AtomicBoolean && key2 instanceof AtomicBoolean)
{
if (!compareAtomicBoolean((AtomicBoolean)key1, (AtomicBoolean)key2)) {
return false;
} else {
continue;
}
}

if (key1 instanceof Number || key2 instanceof Number)
{ // If one is a Number and the other one is not, then optionally compare them as strings, otherwise return false
if (allowStringsToMatchNumbers)
Expand Down Expand Up @@ -593,7 +603,11 @@ private static boolean isContained(Object o, Collection<?> other, Set<ItemsToCom
}
return false;
}


private static boolean compareAtomicBoolean(AtomicBoolean a, AtomicBoolean b) {
return a.get() == b.get();
}

private static boolean compareNumbers(Number a, Number b)
{
if (a instanceof Float && (b instanceof Float || b instanceof Double))
Expand Down
20 changes: 7 additions & 13 deletions src/test/java/com/cedarsoftware/util/TestReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import java.net.URLClassLoader;
import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -187,20 +190,11 @@ public void testMethodAnnotation() throws Exception
@Test
public void testGetDeclaredFields() throws Exception
{
Class<?> c = Parent.class;
var fields = mock(ArrayList.class);
when(fields.add(any())).thenThrow(ThreadDeath.class);

Field f = c.getDeclaredField("foo");

Collection<Field> fields = mock(Collection.class);
when(fields.add(f)).thenThrow(new ThreadDeath());
try
{
ReflectionUtils.getDeclaredFields(Parent.class, fields);
fail("should not make it here");
}
catch (ThreadDeath ignored)
{
}
assertThatExceptionOfType(ThreadDeath.class)
.isThrownBy(() -> ReflectionUtils.getDeclaredFields(Parent.class, fields));
}

@Test
Expand Down

0 comments on commit e6011c8

Please sign in to comment.