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

Make MongoWriteException Serializable #1302

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion driver-core/src/main/com/mongodb/WriteError.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@

import org.bson.BsonDocument;

import java.io.Serializable;

import static com.mongodb.assertions.Assertions.notNull;

/**
* Represents the details of a write error , e.g. a duplicate key error
*
* @since 3.0
*/
public class WriteError {
public class WriteError implements Serializable {

private static final long serialVersionUID = 5582035144266419294L;
private final int code;
private final String message;
private final BsonDocument details;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@

import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Path;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MongoWriteExceptionTest {

@TempDir
Path tempDir;

@Test
public void testExceptionProperties() {
WriteError writeError = new WriteError(11000, "Duplicate key", new BsonDocument("x", new BsonInt32(1)));
Expand All @@ -38,4 +47,23 @@ public void testExceptionProperties() {
assertEquals(writeError.getCode(), e.getCode());
assertEquals(writeError, e.getError());
}

@Test
public void testExceptionSerializable() throws Exception {
WriteError writeError = new WriteError(11000, "Duplicate key", new BsonDocument("x", new BsonInt32(1)));
MongoWriteException writeException = new MongoWriteException(writeError, new ServerAddress("host1"), Collections.emptySet());


Path tempPath = tempDir.resolve("testFile");
try (FileOutputStream fileOutputStream = new FileOutputStream(tempPath.toFile());
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)) {
objectOutputStream.writeObject(writeException);
}

try (FileInputStream fileInputStream = new FileInputStream(tempPath.toFile());
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
MongoWriteException deserializedException = (MongoWriteException) objectInputStream.readObject();
assertEquals(writeException.getError(), deserializedException.getError());
}
}
}