Skip to content

Commit

Permalink
Enforce configuration of an XStream instance
Browse files Browse the repository at this point in the history
Enforce configuration of an XStream instance

#49
  • Loading branch information
smcvb committed Oct 25, 2021
1 parent b9e402b commit 479ee29
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. 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 Down Expand Up @@ -52,9 +52,9 @@ protected DBObjectXStreamSerializer(Builder builder) {
/**
* Instantiate a Builder to be able to create a {@link DBObjectXStreamSerializer}.
* <p>
* The {@link XStream} is defaulted to a {@link XStream#XStream()} call, the {@link Charset} is defaulted to a
* {@link Charset#forName(String)} using the {@code UTF-8} character set, the {@link RevisionResolver} defaults to
* an {@link AnnotationRevisionResolver} and the {@link Converter} defaults to a {@link ChainingConverter}.
* The {@link Charset} is defaulted to a {@link Charset#forName(String)} using the {@code UTF-8} character set, the
* {@link RevisionResolver} defaults to an {@link AnnotationRevisionResolver} and the {@link Converter} defaults to
* a {@link ChainingConverter}.
* <p>
* Upon instantiation, several defaults aliases are added to the XStream instance, for example for the {@link
* org.axonframework.eventhandling.GenericDomainEventMessage}, the {@link org.axonframework.commandhandling.GenericCommandMessage}
Expand Down Expand Up @@ -94,9 +94,9 @@ protected Object doDeserialize(SerializedObject serializedObject, XStream xStrea
/**
* Builder class to instantiate a {@link DBObjectXStreamSerializer}.
* <p>
* The {@link XStream} is defaulted to a {@link XStream#XStream()} call, the {@link Charset} is defaulted to a
* {@link Charset#forName(String)} using the {@code UTF-8} character set, the {@link RevisionResolver} defaults to
* an {@link AnnotationRevisionResolver} and the {@link Converter} defaults to a {@link ChainingConverter}.
* The {@link Charset} is defaulted to a {@link Charset#forName(String)} using the {@code UTF-8} character set, the
* {@link RevisionResolver} defaults to an {@link AnnotationRevisionResolver} and the {@link Converter} defaults to
* a {@link ChainingConverter}.
* <p>
* Upon instantiation, several defaults aliases are added to the XStream instance, for example for the {@link
* org.axonframework.eventhandling.GenericDomainEventMessage}, the {@link org.axonframework.commandhandling.GenericCommandMessage},
Expand All @@ -108,13 +108,6 @@ protected Object doDeserialize(SerializedObject serializedObject, XStream xStrea
*/
public static class Builder extends AbstractXStreamSerializer.Builder {

private Builder() {
xStream(new XStream());
}

/**
* {@inheritDoc} Defaults to a {@link XStream#XStream()} call.
*/
@Override
public Builder xStream(XStream xStream) {
super.xStream(xStream);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. 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,7 @@
import org.axonframework.extensions.mongo.MongoTemplate;
import org.axonframework.extensions.mongo.serialization.DBObjectXStreamSerializer;
import org.axonframework.extensions.mongo.util.MongoTemplateFactory;
import org.axonframework.serialization.Serializer;
import org.axonframework.extensions.mongo.utils.TestSerializer;
import org.junit.jupiter.api.*;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
Expand All @@ -48,7 +48,6 @@ class MongoEventStorageEngineTest_DBObjectSerialization extends AbstractMongoEve

@Container
private static final MongoDBContainer MONGO_CONTAINER = new MongoDBContainer("mongo");
private static final Serializer DB_OBJECT_XSTREAM_SERIALIZER = DBObjectXStreamSerializer.builder().build();

private MongoTemplate mongoTemplate;
@SuppressWarnings("FieldCanBeLocal")
Expand Down Expand Up @@ -78,7 +77,7 @@ public void tearDown() {
}

/**
* Mongo orders events in time instead of per global index. Thus events with mixing timestamps will be read in the
* Mongo orders events in time instead of per global index. Thus, events with mixing timestamps will be read in the
* time based order, if the {@link org.axonframework.extensions.mongo.eventsourcing.eventstore.documentperevent.DocumentPerEventStorageStrategy}
* is used.
*/
Expand All @@ -103,8 +102,8 @@ public void testCreateTokenAtTimeBeforeFirstEvent() {
protected MongoEventStorageEngine createEngine(UnaryOperator<MongoEventStorageEngine.Builder> customization) {
MongoEventStorageEngine.Builder engineBuilder =
MongoEventStorageEngine.builder()
.snapshotSerializer(DB_OBJECT_XSTREAM_SERIALIZER)
.eventSerializer(DB_OBJECT_XSTREAM_SERIALIZER)
.snapshotSerializer(TestSerializer.dbObjectXStreamSerializer())
.eventSerializer(TestSerializer.dbObjectXStreamSerializer())
.mongoTemplate(mongoTemplate);
return customization.apply(engineBuilder).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. 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 @@ -25,10 +25,9 @@
import org.axonframework.eventsourcing.eventstore.EventStoreException;
import org.axonframework.extensions.mongo.MongoTemplate;
import org.axonframework.extensions.mongo.eventsourcing.eventstore.documentpercommit.DocumentPerCommitStorageStrategy;
import org.axonframework.extensions.mongo.serialization.DBObjectXStreamSerializer;
import org.axonframework.extensions.mongo.util.MongoTemplateFactory;
import org.axonframework.extensions.mongo.utils.TestSerializer;
import org.axonframework.modelling.command.AggregateStreamCreationException;
import org.axonframework.serialization.Serializer;
import org.junit.jupiter.api.*;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
Expand All @@ -55,7 +54,6 @@ class MongoEventStorageEngineTest_DocPerCommit extends AbstractMongoEventStorage

@Container
private static final MongoDBContainer MONGO_CONTAINER = new MongoDBContainer("mongo");
private static final Serializer DB_OBJECT_XSTREAM_SERIALIZER = DBObjectXStreamSerializer.builder().build();

private MongoTemplate mongoTemplate;
private MongoEventStorageEngine testSubject;
Expand Down Expand Up @@ -192,8 +190,8 @@ public void testCreateTokenWithUnorderedEvents() {
protected MongoEventStorageEngine createEngine(UnaryOperator<MongoEventStorageEngine.Builder> customization) {
MongoEventStorageEngine.Builder engineBuilder =
MongoEventStorageEngine.builder()
.snapshotSerializer(DB_OBJECT_XSTREAM_SERIALIZER)
.eventSerializer(DB_OBJECT_XSTREAM_SERIALIZER)
.snapshotSerializer(TestSerializer.dbObjectXStreamSerializer())
.eventSerializer(TestSerializer.dbObjectXStreamSerializer())
.mongoTemplate(mongoTemplate)
.storageStrategy(new DocumentPerCommitStorageStrategy());
return customization.apply(engineBuilder).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. 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 @@ -16,6 +16,7 @@

package org.axonframework.extensions.mongo.serialization;

import com.thoughtworks.xstream.XStream;
import org.axonframework.serialization.Revision;
import org.axonframework.serialization.SerializedObject;
import org.junit.jupiter.api.*;
Expand Down Expand Up @@ -45,7 +46,9 @@ class DBObjectXStreamSerializerTest {

@BeforeEach
void setUp() {
this.testSubject = DBObjectXStreamSerializer.builder().build();
this.testSubject = DBObjectXStreamSerializer.builder()
.xStream(new XStream())
.build();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.axonframework.extensions.mongo.utils;

import com.thoughtworks.xstream.XStream;
import org.axonframework.extensions.mongo.serialization.DBObjectXStreamSerializer;
import org.axonframework.serialization.xml.CompactDriver;
import org.axonframework.serialization.xml.XStreamSerializer;

Expand All @@ -41,4 +42,15 @@ public static XStreamSerializer xStreamSerializer() {
.xStream(new XStream(new CompactDriver()))
.build();
}

/**
* Return a {@link DBObjectXStreamSerializer} using a default {@link XStream} instance.
*
* @return a {@link DBObjectXStreamSerializer} using a default {@link XStream} instance
*/
public static DBObjectXStreamSerializer dbObjectXStreamSerializer() {
return DBObjectXStreamSerializer.builder()
.xStream(new XStream())
.build();
}
}

0 comments on commit 479ee29

Please sign in to comment.