Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Reformat code. Reduce method visibility in JUnit 5 tests. Add Nullable annotations to address warnings.

See #3568
Original pull request: #3569.
  • Loading branch information
mp911de committed Mar 2, 2021
1 parent bf642ad commit 22ed860
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package org.springframework.data.mongodb.core;

import com.mongodb.MongoSocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.bson.BsonInvalidOperationException;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException;
Expand All @@ -40,6 +40,7 @@
import com.mongodb.MongoBulkWriteException;
import com.mongodb.MongoException;
import com.mongodb.MongoServerException;
import com.mongodb.MongoSocketException;
import com.mongodb.bulk.BulkWriteError;

/**
Expand Down Expand Up @@ -94,7 +95,6 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
return new DataAccessResourceFailureException(ex.getMessage(), ex);
}


if (RESOURCE_USAGE_EXCEPTIONS.contains(exception)) {
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

import static org.assertj.core.api.Assertions.*;

import com.mongodb.MongoSocketReadTimeoutException;
import com.mongodb.MongoSocketWriteException;

import org.bson.BsonDocument;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -33,11 +30,14 @@
import org.springframework.data.mongodb.ClientSessionException;
import org.springframework.data.mongodb.MongoTransactionException;
import org.springframework.data.mongodb.UncategorizedMongoDbException;
import org.springframework.lang.Nullable;

import com.mongodb.MongoCursorNotFoundException;
import com.mongodb.MongoException;
import com.mongodb.MongoInternalException;
import com.mongodb.MongoSocketException;
import com.mongodb.MongoSocketReadTimeoutException;
import com.mongodb.MongoSocketWriteException;
import com.mongodb.ServerAddress;

/**
Expand All @@ -48,36 +48,35 @@
* @author Christoph Strobl
* @author Brice Vandeputte
*/
public class MongoExceptionTranslatorUnitTests {
class MongoExceptionTranslatorUnitTests {

public static final String EXCEPTION_MESSAGE = "IOException";
MongoExceptionTranslator translator;
private static final String EXCEPTION_MESSAGE = "IOException";
private MongoExceptionTranslator translator;

@BeforeEach
public void setUp() {
void setUp() {
translator = new MongoExceptionTranslator();
}

@Test
public void translateDuplicateKey() {
void translateDuplicateKey() {

expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(
new com.mongodb.DuplicateKeyException(new BsonDocument(), new ServerAddress(), null)),
DuplicateKeyException.class, null);
}

@Test
public void translateSocketException() {
@Test // GH-3568
void translateSocketException() {

expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(new MongoSocketException(EXCEPTION_MESSAGE, new ServerAddress())),
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);

}

@Test // GH-3568
public void translateSocketChildrenExceptions() {
void translateSocketExceptionSubclasses() {

expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(
Expand All @@ -94,29 +93,29 @@ public void translateSocketChildrenExceptions() {
}

@Test
public void translateCursorNotFound() {
void translateCursorNotFound() {

expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(new MongoCursorNotFoundException(1L, new ServerAddress())),
DataAccessResourceFailureException.class);
}

@Test
public void translateToDuplicateKeyException() {
void translateToDuplicateKeyException() {

checkTranslatedMongoException(DuplicateKeyException.class, 11000);
checkTranslatedMongoException(DuplicateKeyException.class, 11001);
}

@Test
public void translateToDataAccessResourceFailureException() {
void translateToDataAccessResourceFailureException() {

checkTranslatedMongoException(DataAccessResourceFailureException.class, 12000);
checkTranslatedMongoException(DataAccessResourceFailureException.class, 13440);
}

@Test
public void translateToInvalidDataAccessApiUsageException() {
void translateToInvalidDataAccessApiUsageException() {

checkTranslatedMongoException(InvalidDataAccessApiUsageException.class, 10003);
checkTranslatedMongoException(InvalidDataAccessApiUsageException.class, 12001);
Expand All @@ -126,7 +125,7 @@ public void translateToInvalidDataAccessApiUsageException() {
}

@Test
public void translateToUncategorizedMongoDbException() {
void translateToUncategorizedMongoDbException() {

MongoException exception = new MongoException(0, "");
DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
Expand All @@ -135,7 +134,7 @@ public void translateToUncategorizedMongoDbException() {
}

@Test
public void translateMongoInternalException() {
void translateMongoInternalException() {

MongoInternalException exception = new MongoInternalException("Internal exception");
DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
Expand All @@ -144,14 +143,14 @@ public void translateMongoInternalException() {
}

@Test
public void translateUnsupportedException() {
void translateUnsupportedException() {

RuntimeException exception = new RuntimeException();
assertThat(translator.translateExceptionIfPossible(exception)).isNull();
}

@Test // DATAMONGO-2045
public void translateSessionExceptions() {
void translateSessionExceptions() {

checkTranslatedMongoException(ClientSessionException.class, 206);
checkTranslatedMongoException(ClientSessionException.class, 213);
Expand All @@ -160,7 +159,7 @@ public void translateSessionExceptions() {
}

@Test // DATAMONGO-2045
public void translateTransactionExceptions() {
void translateTransactionExceptions() {

checkTranslatedMongoException(MongoTransactionException.class, 217);
checkTranslatedMongoException(MongoTransactionException.class, 225);
Expand All @@ -183,13 +182,13 @@ private void checkTranslatedMongoException(Class<? extends Exception> clazz, int
assertThat(((MongoException) cause).getCode()).isEqualTo(code);
}

private static void expectExceptionWithCauseMessage(NestedRuntimeException e,
private static void expectExceptionWithCauseMessage(@Nullable NestedRuntimeException e,
Class<? extends NestedRuntimeException> type) {
expectExceptionWithCauseMessage(e, type, null);
}

private static void expectExceptionWithCauseMessage(NestedRuntimeException e,
Class<? extends NestedRuntimeException> type, String message) {
private static void expectExceptionWithCauseMessage(@Nullable NestedRuntimeException e,
Class<? extends NestedRuntimeException> type, @Nullable String message) {

assertThat(e).isInstanceOf(type);

Expand Down

0 comments on commit 22ed860

Please sign in to comment.