Skip to content

Commit d73c2e3

Browse files
committed
Polishing.
Reformat code. Reduce method visibility in JUnit 5 tests. Add Nullable annotations to address warnings. See #3568 Original pull request: #3569.
1 parent 39e301d commit d73c2e3

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import com.mongodb.MongoSocketException;
1918
import java.util.Arrays;
2019
import java.util.Collections;
2120
import java.util.HashSet;
2221
import java.util.Set;
2322

2423
import org.bson.BsonInvalidOperationException;
24+
2525
import org.springframework.dao.DataAccessException;
2626
import org.springframework.dao.DataAccessResourceFailureException;
2727
import org.springframework.dao.DataIntegrityViolationException;
@@ -40,6 +40,7 @@
4040
import com.mongodb.MongoBulkWriteException;
4141
import com.mongodb.MongoException;
4242
import com.mongodb.MongoServerException;
43+
import com.mongodb.MongoSocketException;
4344
import com.mongodb.bulk.BulkWriteError;
4445

4546
/**
@@ -94,7 +95,6 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
9495
return new DataAccessResourceFailureException(ex.getMessage(), ex);
9596
}
9697

97-
9898
if (RESOURCE_USAGE_EXCEPTIONS.contains(exception)) {
9999
return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
100100
}

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

+23-24
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

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

20-
import com.mongodb.MongoSocketReadTimeoutException;
21-
import com.mongodb.MongoSocketWriteException;
22-
2320
import org.bson.BsonDocument;
2421
import org.junit.jupiter.api.BeforeEach;
2522
import org.junit.jupiter.api.Test;
@@ -33,11 +30,14 @@
3330
import org.springframework.data.mongodb.ClientSessionException;
3431
import org.springframework.data.mongodb.MongoTransactionException;
3532
import org.springframework.data.mongodb.UncategorizedMongoDbException;
33+
import org.springframework.lang.Nullable;
3634

3735
import com.mongodb.MongoCursorNotFoundException;
3836
import com.mongodb.MongoException;
3937
import com.mongodb.MongoInternalException;
4038
import com.mongodb.MongoSocketException;
39+
import com.mongodb.MongoSocketReadTimeoutException;
40+
import com.mongodb.MongoSocketWriteException;
4141
import com.mongodb.ServerAddress;
4242

4343
/**
@@ -48,36 +48,35 @@
4848
* @author Christoph Strobl
4949
* @author Brice Vandeputte
5050
*/
51-
public class MongoExceptionTranslatorUnitTests {
51+
class MongoExceptionTranslatorUnitTests {
5252

53-
public static final String EXCEPTION_MESSAGE = "IOException";
54-
MongoExceptionTranslator translator;
53+
private static final String EXCEPTION_MESSAGE = "IOException";
54+
private MongoExceptionTranslator translator;
5555

5656
@BeforeEach
57-
public void setUp() {
57+
void setUp() {
5858
translator = new MongoExceptionTranslator();
5959
}
6060

6161
@Test
62-
public void translateDuplicateKey() {
62+
void translateDuplicateKey() {
6363

6464
expectExceptionWithCauseMessage(
6565
translator.translateExceptionIfPossible(
6666
new com.mongodb.DuplicateKeyException(new BsonDocument(), new ServerAddress(), null)),
6767
DuplicateKeyException.class, null);
6868
}
6969

70-
@Test
71-
public void translateSocketException() {
70+
@Test // GH-3568
71+
void translateSocketException() {
7272

7373
expectExceptionWithCauseMessage(
7474
translator.translateExceptionIfPossible(new MongoSocketException(EXCEPTION_MESSAGE, new ServerAddress())),
7575
DataAccessResourceFailureException.class, EXCEPTION_MESSAGE);
76-
7776
}
7877

7978
@Test // GH-3568
80-
public void translateSocketChildrenExceptions() {
79+
void translateSocketExceptionSubclasses() {
8180

8281
expectExceptionWithCauseMessage(
8382
translator.translateExceptionIfPossible(
@@ -94,29 +93,29 @@ public void translateSocketChildrenExceptions() {
9493
}
9594

9695
@Test
97-
public void translateCursorNotFound() {
96+
void translateCursorNotFound() {
9897

9998
expectExceptionWithCauseMessage(
10099
translator.translateExceptionIfPossible(new MongoCursorNotFoundException(1L, new ServerAddress())),
101100
DataAccessResourceFailureException.class);
102101
}
103102

104103
@Test
105-
public void translateToDuplicateKeyException() {
104+
void translateToDuplicateKeyException() {
106105

107106
checkTranslatedMongoException(DuplicateKeyException.class, 11000);
108107
checkTranslatedMongoException(DuplicateKeyException.class, 11001);
109108
}
110109

111110
@Test
112-
public void translateToDataAccessResourceFailureException() {
111+
void translateToDataAccessResourceFailureException() {
113112

114113
checkTranslatedMongoException(DataAccessResourceFailureException.class, 12000);
115114
checkTranslatedMongoException(DataAccessResourceFailureException.class, 13440);
116115
}
117116

118117
@Test
119-
public void translateToInvalidDataAccessApiUsageException() {
118+
void translateToInvalidDataAccessApiUsageException() {
120119

121120
checkTranslatedMongoException(InvalidDataAccessApiUsageException.class, 10003);
122121
checkTranslatedMongoException(InvalidDataAccessApiUsageException.class, 12001);
@@ -126,7 +125,7 @@ public void translateToInvalidDataAccessApiUsageException() {
126125
}
127126

128127
@Test
129-
public void translateToUncategorizedMongoDbException() {
128+
void translateToUncategorizedMongoDbException() {
130129

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

137136
@Test
138-
public void translateMongoInternalException() {
137+
void translateMongoInternalException() {
139138

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

146145
@Test
147-
public void translateUnsupportedException() {
146+
void translateUnsupportedException() {
148147

149148
RuntimeException exception = new RuntimeException();
150149
assertThat(translator.translateExceptionIfPossible(exception)).isNull();
151150
}
152151

153152
@Test // DATAMONGO-2045
154-
public void translateSessionExceptions() {
153+
void translateSessionExceptions() {
155154

156155
checkTranslatedMongoException(ClientSessionException.class, 206);
157156
checkTranslatedMongoException(ClientSessionException.class, 213);
@@ -160,7 +159,7 @@ public void translateSessionExceptions() {
160159
}
161160

162161
@Test // DATAMONGO-2045
163-
public void translateTransactionExceptions() {
162+
void translateTransactionExceptions() {
164163

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

186-
private static void expectExceptionWithCauseMessage(NestedRuntimeException e,
185+
private static void expectExceptionWithCauseMessage(@Nullable NestedRuntimeException e,
187186
Class<? extends NestedRuntimeException> type) {
188187
expectExceptionWithCauseMessage(e, type, null);
189188
}
190189

191-
private static void expectExceptionWithCauseMessage(NestedRuntimeException e,
192-
Class<? extends NestedRuntimeException> type, String message) {
190+
private static void expectExceptionWithCauseMessage(@Nullable NestedRuntimeException e,
191+
Class<? extends NestedRuntimeException> type, @Nullable String message) {
193192

194193
assertThat(e).isInstanceOf(type);
195194

0 commit comments

Comments
 (0)