Browse Source

Polishing.

Reformat code. Reduce method visibility in JUnit 5 tests. Add Nullable annotations to address warnings.

See #3568
Original pull request: #3569.
pull/3581/head
Mark Paluch 5 years ago
parent
commit
d73c2e3602
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoExceptionTranslator.java
  2. 47
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoExceptionTranslatorUnitTests.java

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoExceptionTranslator.java

@ -15,13 +15,13 @@
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import com.mongodb.MongoSocketException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.bson.BsonInvalidOperationException; import org.bson.BsonInvalidOperationException;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
@ -40,6 +40,7 @@ import org.springframework.util.ClassUtils;
import com.mongodb.MongoBulkWriteException; import com.mongodb.MongoBulkWriteException;
import com.mongodb.MongoException; import com.mongodb.MongoException;
import com.mongodb.MongoServerException; import com.mongodb.MongoServerException;
import com.mongodb.MongoSocketException;
import com.mongodb.bulk.BulkWriteError; import com.mongodb.bulk.BulkWriteError;
/** /**
@ -94,7 +95,6 @@ public class MongoExceptionTranslator implements PersistenceExceptionTranslator
return new DataAccessResourceFailureException(ex.getMessage(), ex); return new DataAccessResourceFailureException(ex.getMessage(), ex);
} }
if (RESOURCE_USAGE_EXCEPTIONS.contains(exception)) { if (RESOURCE_USAGE_EXCEPTIONS.contains(exception)) {
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex); return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
} }

47
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoExceptionTranslatorUnitTests.java

@ -17,9 +17,6 @@ package org.springframework.data.mongodb.core;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import com.mongodb.MongoSocketReadTimeoutException;
import com.mongodb.MongoSocketWriteException;
import org.bson.BsonDocument; import org.bson.BsonDocument;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -33,11 +30,14 @@ import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.data.mongodb.ClientSessionException; import org.springframework.data.mongodb.ClientSessionException;
import org.springframework.data.mongodb.MongoTransactionException; import org.springframework.data.mongodb.MongoTransactionException;
import org.springframework.data.mongodb.UncategorizedMongoDbException; import org.springframework.data.mongodb.UncategorizedMongoDbException;
import org.springframework.lang.Nullable;
import com.mongodb.MongoCursorNotFoundException; import com.mongodb.MongoCursorNotFoundException;
import com.mongodb.MongoException; import com.mongodb.MongoException;
import com.mongodb.MongoInternalException; import com.mongodb.MongoInternalException;
import com.mongodb.MongoSocketException; import com.mongodb.MongoSocketException;
import com.mongodb.MongoSocketReadTimeoutException;
import com.mongodb.MongoSocketWriteException;
import com.mongodb.ServerAddress; import com.mongodb.ServerAddress;
/** /**
@ -48,18 +48,18 @@ import com.mongodb.ServerAddress;
* @author Christoph Strobl * @author Christoph Strobl
* @author Brice Vandeputte * @author Brice Vandeputte
*/ */
public class MongoExceptionTranslatorUnitTests { class MongoExceptionTranslatorUnitTests {
public static final String EXCEPTION_MESSAGE = "IOException"; private static final String EXCEPTION_MESSAGE = "IOException";
MongoExceptionTranslator translator; private MongoExceptionTranslator translator;
@BeforeEach @BeforeEach
public void setUp() { void setUp() {
translator = new MongoExceptionTranslator(); translator = new MongoExceptionTranslator();
} }
@Test @Test
public void translateDuplicateKey() { void translateDuplicateKey() {
expectExceptionWithCauseMessage( expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible( translator.translateExceptionIfPossible(
@ -67,17 +67,16 @@ public class MongoExceptionTranslatorUnitTests {
DuplicateKeyException.class, null); DuplicateKeyException.class, null);
} }
@Test @Test // GH-3568
public void translateSocketException() { void translateSocketException() {
expectExceptionWithCauseMessage( expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(new MongoSocketException(EXCEPTION_MESSAGE, new ServerAddress())), translator.translateExceptionIfPossible(new MongoSocketException(EXCEPTION_MESSAGE, new ServerAddress())),
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE); DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);
} }
@Test // GH-3568 @Test // GH-3568
public void translateSocketChildrenExceptions() { void translateSocketExceptionSubclasses() {
expectExceptionWithCauseMessage( expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible( translator.translateExceptionIfPossible(
@ -94,7 +93,7 @@ public class MongoExceptionTranslatorUnitTests {
} }
@Test @Test
public void translateCursorNotFound() { void translateCursorNotFound() {
expectExceptionWithCauseMessage( expectExceptionWithCauseMessage(
translator.translateExceptionIfPossible(new MongoCursorNotFoundException(1L, new ServerAddress())), translator.translateExceptionIfPossible(new MongoCursorNotFoundException(1L, new ServerAddress())),
@ -102,21 +101,21 @@ public class MongoExceptionTranslatorUnitTests {
} }
@Test @Test
public void translateToDuplicateKeyException() { void translateToDuplicateKeyException() {
checkTranslatedMongoException(DuplicateKeyException.class, 11000); checkTranslatedMongoException(DuplicateKeyException.class, 11000);
checkTranslatedMongoException(DuplicateKeyException.class, 11001); checkTranslatedMongoException(DuplicateKeyException.class, 11001);
} }
@Test @Test
public void translateToDataAccessResourceFailureException() { void translateToDataAccessResourceFailureException() {
checkTranslatedMongoException(DataAccessResourceFailureException.class, 12000); checkTranslatedMongoException(DataAccessResourceFailureException.class, 12000);
checkTranslatedMongoException(DataAccessResourceFailureException.class, 13440); checkTranslatedMongoException(DataAccessResourceFailureException.class, 13440);
} }
@Test @Test
public void translateToInvalidDataAccessApiUsageException() { void translateToInvalidDataAccessApiUsageException() {
checkTranslatedMongoException(InvalidDataAccessApiUsageException.class, 10003); checkTranslatedMongoException(InvalidDataAccessApiUsageException.class, 10003);
checkTranslatedMongoException(InvalidDataAccessApiUsageException.class, 12001); checkTranslatedMongoException(InvalidDataAccessApiUsageException.class, 12001);
@ -126,7 +125,7 @@ public class MongoExceptionTranslatorUnitTests {
} }
@Test @Test
public void translateToUncategorizedMongoDbException() { void translateToUncategorizedMongoDbException() {
MongoException exception = new MongoException(0, ""); MongoException exception = new MongoException(0, "");
DataAccessException translatedException = translator.translateExceptionIfPossible(exception); DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
@ -135,7 +134,7 @@ public class MongoExceptionTranslatorUnitTests {
} }
@Test @Test
public void translateMongoInternalException() { void translateMongoInternalException() {
MongoInternalException exception = new MongoInternalException("Internal exception"); MongoInternalException exception = new MongoInternalException("Internal exception");
DataAccessException translatedException = translator.translateExceptionIfPossible(exception); DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
@ -144,14 +143,14 @@ public class MongoExceptionTranslatorUnitTests {
} }
@Test @Test
public void translateUnsupportedException() { void translateUnsupportedException() {
RuntimeException exception = new RuntimeException(); RuntimeException exception = new RuntimeException();
assertThat(translator.translateExceptionIfPossible(exception)).isNull(); assertThat(translator.translateExceptionIfPossible(exception)).isNull();
} }
@Test // DATAMONGO-2045 @Test // DATAMONGO-2045
public void translateSessionExceptions() { void translateSessionExceptions() {
checkTranslatedMongoException(ClientSessionException.class, 206); checkTranslatedMongoException(ClientSessionException.class, 206);
checkTranslatedMongoException(ClientSessionException.class, 213); checkTranslatedMongoException(ClientSessionException.class, 213);
@ -160,7 +159,7 @@ public class MongoExceptionTranslatorUnitTests {
} }
@Test // DATAMONGO-2045 @Test // DATAMONGO-2045
public void translateTransactionExceptions() { void translateTransactionExceptions() {
checkTranslatedMongoException(MongoTransactionException.class, 217); checkTranslatedMongoException(MongoTransactionException.class, 217);
checkTranslatedMongoException(MongoTransactionException.class, 225); checkTranslatedMongoException(MongoTransactionException.class, 225);
@ -183,13 +182,13 @@ public class MongoExceptionTranslatorUnitTests {
assertThat(((MongoException) cause).getCode()).isEqualTo(code); assertThat(((MongoException) cause).getCode()).isEqualTo(code);
} }
private static void expectExceptionWithCauseMessage(NestedRuntimeException e, private static void expectExceptionWithCauseMessage(@Nullable NestedRuntimeException e,
Class<? extends NestedRuntimeException> type) { Class<? extends NestedRuntimeException> type) {
expectExceptionWithCauseMessage(e, type, null); expectExceptionWithCauseMessage(e, type, null);
} }
private static void expectExceptionWithCauseMessage(NestedRuntimeException e, private static void expectExceptionWithCauseMessage(@Nullable NestedRuntimeException e,
Class<? extends NestedRuntimeException> type, String message) { Class<? extends NestedRuntimeException> type, @Nullable String message) {
assertThat(e).isInstanceOf(type); assertThat(e).isInstanceOf(type);

Loading…
Cancel
Save