Skip to content

Commit

Permalink
Fix some warnings
Browse files Browse the repository at this point in the history
AggregateSnapshotter:
- Add wildcards to the factories collection
- Add wildcard to the DomainEventMessage
- Use inline inspection suppressing instead of annotated
- Make factories map in builder final
- Fix typos by adding hyphen
- Update indentation
Reporter:
- Use chained append operations
- Replace String concatenation for StringBuilder#append operations
- Use instanceof check i.o. class.isInstance
- Add wildcard to EventMessage casting
- Update indentation
  • Loading branch information
smcvb committed Nov 28, 2022
1 parent 1823694 commit 7c187b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@

/**
* Implementation of a snapshotter that uses the actual aggregate and its state to create a snapshot event. The
* motivation is that an aggregate always contains all relevant state. Therefore, storing the aggregate itself inside
* an event should capture all necessary information.
* motivation is that an aggregate always contains all relevant state. Therefore, storing the aggregate itself inside an
* event should capture all necessary information.
*
* @author Allard Buijze
* @since 0.6
Expand All @@ -59,7 +59,7 @@ public class AggregateSnapshotter extends AbstractSnapshotter {
private final ParameterResolverFactory parameterResolverFactory;
private final HandlerDefinition handlerDefinition;

private final Map<Class, AggregateModel> aggregateModels = new ConcurrentHashMap<>();
private final Map<Class<?>, AggregateModel<?>> aggregateModels = new ConcurrentHashMap<>();

/**
* Instantiate a {@link AggregateSnapshotter} based on the fields contained in the {@link Builder}.
Expand All @@ -85,27 +85,24 @@ protected AggregateSnapshotter(Builder builder) {
* {@link TransactionManager} defaults to a {@link org.axonframework.common.transaction.NoTransactionManager}.
* Additionally, this Builder has convenience functions to default the {@link ParameterResolverFactory} and
* {@link HandlerDefinition} based on instances of these available on the classpath in case these are not provided
* (respectively {@link Builder#buildParameterResolverFactory()} and {@link Builder#buildHandlerDefinition()}).
* Upon instantiation of a {@link AggregateSnapshotter}, it is recommended to use these function to set those
* fields.
* (respectively {@link Builder#buildParameterResolverFactory()} and {@link Builder#buildHandlerDefinition()}). Upon
* instantiation of a {@link AggregateSnapshotter}, it is recommended to use these function to set those fields.
* <p>
* The {@link EventStore} is a <b>hard requirement</b> and as such should be provided.
*
* @return a Builder to be able to create a {@link AggregateSnapshotter}
*
* @see ClasspathParameterResolverFactory
* @see ClasspathHandlerDefinition
*/
public static Builder builder() {
return new Builder();
}

@SuppressWarnings("unchecked")
@Override
protected DomainEventMessage createSnapshot(Class<?> aggregateType,
String aggregateIdentifier,
DomainEventStream eventStream) {
DomainEventMessage firstEvent = eventStream.peek();
DomainEventMessage<?> firstEvent = eventStream.peek();
AggregateFactory<?> aggregateFactory = getAggregateFactory(aggregateType);
if (aggregateFactory == null) {
throw new IllegalArgumentException(
Expand All @@ -116,6 +113,7 @@ protected DomainEventMessage createSnapshot(Class<?> aggregateType,
k -> AnnotatedAggregateMetaModelFactory
.inspectAggregate(k, parameterResolverFactory, handlerDefinition));
Object aggregateRoot = aggregateFactory.createAggregateRoot(aggregateIdentifier, firstEvent);
//noinspection rawtypes,unchecked
SnapshotAggregate<Object> aggregate = new SnapshotAggregate(aggregateRoot,
aggregateModels.get(aggregateType),
repositoryProvider);
Expand Down Expand Up @@ -169,7 +167,7 @@ protected void registerAggregateFactory(AggregateFactory<?> aggregateFactory) {
*/
public static class Builder extends AbstractSnapshotter.Builder {

private Map<Class<?>, AggregateFactory<?>> aggregateFactories = new ConcurrentHashMap<>();
private final Map<Class<?>, AggregateFactory<?>> aggregateFactories = new ConcurrentHashMap<>();
private RepositoryProvider repositoryProvider;
private ParameterResolverFactory parameterResolverFactory;
private HandlerDefinition handlerDefinition;
Expand Down Expand Up @@ -280,7 +278,7 @@ public AggregateSnapshotter build() {
* {@link AggregateSnapshotter} type or the first {@link AggregateFactory} it's class as input.
* <p>
* <b>Note:</b> it is recommended to use this function when retrieving the ParameterResolverFactory as it
* ensure it is non null.
* ensure it is non-null.
*
* @return the set or instantiated {@link ParameterResolverFactory}
*/
Expand All @@ -296,13 +294,13 @@ protected ParameterResolverFactory buildParameterResolverFactory() {
}

/**
* Return the set {@link HandlerDefinition}, or create and return it if it is {@code null}. In case it
* has not been set yet, a HandlerDefinition is created by calling the
* Return the set {@link HandlerDefinition}, or create and return it if it is {@code null}. In case it has not
* been set yet, a HandlerDefinition is created by calling the
* {@link ClasspathHandlerDefinition#forClass(Class)} function, either providing the
* {@link AggregateSnapshotter} type or the first {@link AggregateFactory} it's class as input.
* <p>
* <b>Note:</b> it is recommended to use this function when retrieving the HandlerDefinition as it
* ensure it is non null.
* ensure it is non-null.
*
* @return the set or instantiated {@link ParameterResolverFactory}
*/
Expand Down
54 changes: 31 additions & 23 deletions test/src/main/java/org/axonframework/test/aggregate/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ public void reportWrongEvent(Collection<?> actualEvents, Collection<?> expectedE
public void reportWrongEvent(Collection<?> actualEvents, StringDescription expectation, Throwable probableCause) {
StringBuilder sb = new StringBuilder(
"The published events do not match the expected events.");
sb.append("Expected :");
sb.append(NEWLINE);
sb.append(expectation);
sb.append(NEWLINE);
sb.append("But got");
sb.append("Expected :")
.append(NEWLINE)
.append(expectation)
.append(NEWLINE)
.append("But got");
if (actualEvents.isEmpty()) {
sb.append(" none");
} else {
sb.append(":");
}
for (Object publishedEvent : actualEvents) {
sb.append(NEWLINE);
sb.append(publishedEvent.getClass().getSimpleName());
sb.append(": ");
sb.append(publishedEvent.toString());
sb.append(NEWLINE)
.append(publishedEvent.getClass().getSimpleName())
.append(": ")
.append(publishedEvent);
}
appendProbableCause(probableCause, sb);

Expand Down Expand Up @@ -155,7 +155,7 @@ public void reportUnexpectedReturnValue(Object actualReturnValue, Description de
}

/**
* Report an error due to a an exception of an unexpected type.
* Report an error due to an exception of an unexpected type.
*
* @param actualException The actual exception
* @param description A description describing the expected value
Expand Down Expand Up @@ -197,8 +197,8 @@ public void reportWrongExceptionMessage(Throwable actualException, Description d
/**
* Report an error due to a difference in exception details.
*
* @param details The actual details
* @param description A description describing the expected value
* @param details The actual details
* @param description A description describing the expected value
*/
public void reportWrongExceptionDetails(Object details, Description description) {
throw new AxonAssertionError("The command handler threw an exception, but not with expected details"
Expand Down Expand Up @@ -284,22 +284,30 @@ public void reportDifferentMetaData(Class<?> messageType, Map<String, Object> mi
.append(messageType.getSimpleName())
.append("], ");
if (!additionalEntries.isEmpty()) {
sb.append("metadata entries" + NEWLINE).append("[");
sb.append("metadata entries")
.append(NEWLINE)
.append("[");
for (Map.Entry<String, Object> entry : additionalEntries.entrySet()) {
sb.append(entryAsString(entry) + ", ");
sb.append(entryAsString(entry))
.append(", ");
}
sb.delete(sb.lastIndexOf(", "), sb.lastIndexOf(",") + 2);
sb.append("] " + NEWLINE);
sb.append("were not expected. ");
sb.append("] ")
.append(NEWLINE)
.append("were not expected. ");
}
if (!missingEntries.isEmpty()) {
sb.append("metadata entries " + NEWLINE).append("[");
sb.append("metadata entries ")
.append(NEWLINE)
.append("[");
for (Map.Entry<String, Object> entry : missingEntries.entrySet()) {
sb.append(entryAsString(entry) + ", ");
sb.append(entryAsString(entry))
.append(", ");
}
sb.delete(sb.lastIndexOf(","), sb.lastIndexOf(",") + 2);
sb.append("] " + NEWLINE);
sb.append("were expected but not seen.");
sb.append("] ")
.append(NEWLINE)
.append("were expected but not seen.");
}
throw new AxonAssertionError(sb.toString());
}
Expand Down Expand Up @@ -349,7 +357,7 @@ private void describe(Object value, StringBuilder sb) {
if (value == null) {
sb.append("null");
} else {
sb.append(value.toString());
sb.append(value);
}
}

Expand Down Expand Up @@ -419,8 +427,8 @@ private void appendEventOverview(StringBuilder sb, Collection<?> leftColumnEvent

private String payloadContentType(Object event) {
String simpleName;
if (EventMessage.class.isInstance(event)) {
simpleName = ((EventMessage) event).getPayload().getClass().getName();
if (event instanceof EventMessage) {
simpleName = ((EventMessage<?>) event).getPayload().getClass().getName();
} else {
simpleName = event.getClass().getName();
}
Expand Down

0 comments on commit 7c187b9

Please sign in to comment.