Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use equals() first for aggregate state check #2196

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static org.axonframework.common.ObjectUtils.getOrDefault;
Expand Down Expand Up @@ -150,11 +151,12 @@ public static boolean hasEqualsMethod(Class<?> type) {
*/
@SuppressWarnings("unchecked")
public static boolean explicitlyUnequal(Object value, Object otherValue) {
if (value == otherValue) { // NOSONAR (The == comparison is intended here)
if (Objects.equals(value, otherValue)) {
return false;
} else if (value == null || otherValue == null) {
return true;
} else if (value instanceof Comparable) {
//noinspection rawtypes
return ((Comparable) value).compareTo(otherValue) != 0;
} else if (hasEqualsMethod(value.getClass())) {
return !value.equals(otherValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2021. Axon Framework
* Copyright (c) 2010-2022. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,6 @@
import org.axonframework.commandhandling.GenericCommandMessage;
import org.axonframework.commandhandling.SimpleCommandBus;
import org.axonframework.common.Assert;
import org.axonframework.common.ReflectionUtils;
import org.axonframework.common.Registration;
import org.axonframework.deadline.DeadlineMessage;
import org.axonframework.eventhandling.DomainEventMessage;
Expand Down Expand Up @@ -585,35 +584,59 @@ private void assertValidWorkingAggregateState(Aggregate<T> eventSourcedAggregate
eventSourcedAggregate.rootType().getName()));
}
ensureValuesEqual(workingAggregate.invoke(Function.identity()),
eventSourcedAggregate.invoke(Function.identity()), eventSourcedAggregate.rootType().getName(),
comparedEntries, fieldFilter);
}

private void ensureValuesEqual(Object workingValue, Object eventSourcedValue, String propertyPath,
Set<ComparationEntry> comparedEntries, FieldFilter fieldFilter) {
if (explicitlyUnequal(workingValue, eventSourcedValue)) {
throw new AxonAssertionError(format("Illegal state change detected! " +
"Property \"%s\" has different value when sourcing events.\n" +
"Working aggregate value: <%s>\n" +
"Value after applying events: <%s>", propertyPath, workingValue,
eventSourcedValue));
} else if (workingValue != null && comparedEntries.add(new ComparationEntry(workingValue, eventSourcedValue)) &&
!hasEqualsMethod(workingValue.getClass())) {
for (Field field : fieldsOf(workingValue.getClass())) {
if (fieldFilter.accept(field) && !Modifier.isStatic(field.getModifiers()) &&
!Modifier.isTransient(field.getModifiers())) {
ensureAccessible(field);
String newPropertyPath = propertyPath + "." + field.getName();

Object workingFieldValue = ReflectionUtils.getFieldValue(field, workingValue);
Object eventSourcedFieldValue = ReflectionUtils.getFieldValue(field, eventSourcedValue);
ensureValuesEqual(workingFieldValue, eventSourcedFieldValue, newPropertyPath, comparedEntries,
fieldFilter);
eventSourcedAggregate.invoke(Function.identity()),
eventSourcedAggregate.rootType().getName(),
comparedEntries,
fieldFilter);
}

private void ensureValuesEqual(Object workingValue,
Object eventSourcedValue,
String propertyPath,
Set<ComparationEntry> comparedEntries,
FieldFilter fieldFilter) {
if (Objects.equals(workingValue, eventSourcedValue)) {
// they're equal, nothing more to check...
return;
}

if ((workingValue == null || hasEqualsMethod(workingValue.getClass()))
|| (eventSourcedValue == null || hasEqualsMethod(eventSourcedValue.getClass()))) {
failIllegalStateChange(workingValue, eventSourcedValue, propertyPath);
} else if (comparedEntries.add(new ComparationEntry(workingValue, eventSourcedValue))
&& !hasEqualsMethod(workingValue.getClass())) {
try {
for (Field field : fieldsOf(workingValue.getClass())) {
if (fieldFilter.accept(field)
&& !Modifier.isStatic(field.getModifiers())
&& !Modifier.isTransient(field.getModifiers())) {
ensureAccessible(field);
String newPropertyPath = propertyPath + "." + field.getName();

Object workingFieldValue = getFieldValue(field, workingValue);
Object eventSourcedFieldValue = getFieldValue(field, eventSourcedValue);
ensureValuesEqual(workingFieldValue,
eventSourcedFieldValue,
newPropertyPath,
comparedEntries,
fieldFilter);
}
}
} catch (Exception e) {
logger.debug("Exception while attempting to verify deep equality.", e);
failIllegalStateChange(workingValue, eventSourcedValue, propertyPath);
}
}
}

private void failIllegalStateChange(Object workingValue, Object eventSourcedValue, String propertyPath) {
throw new AxonAssertionError(format("Illegal state change detected! " +
"Property \"%s\" has different value when sourcing events.\n" +
"Working aggregate value: <%s>\n" +
"Value after applying events: <%s>", propertyPath, workingValue,
eventSourcedValue));
}

private void clearGivenWhenState() {
storedEvents = new LinkedList<>();
publishedEvents = new ArrayList<>();
Expand Down