|
17 | 17 |
|
18 | 18 | import static org.hamcrest.CoreMatchers.*;
|
19 | 19 | import static org.junit.Assert.*;
|
| 20 | +import static org.junit.Assume.*; |
20 | 21 | import static org.mockito.Mockito.*;
|
21 | 22 |
|
22 | 23 | import java.io.IOException;
|
| 24 | +import java.lang.reflect.Constructor; |
| 25 | +import java.lang.reflect.InvocationTargetException; |
| 26 | +import java.lang.reflect.Method; |
23 | 27 | import java.net.UnknownHostException;
|
| 28 | +import java.util.Map; |
24 | 29 |
|
25 | 30 | import org.junit.Before;
|
26 | 31 | import org.junit.Test;
|
|
33 | 38 | import org.springframework.dao.DuplicateKeyException;
|
34 | 39 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
35 | 40 | import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
| 41 | +import org.springframework.dao.TransientDataAccessResourceException; |
36 | 42 | import org.springframework.data.mongodb.UncategorizedMongoDbException;
|
| 43 | +import org.springframework.data.mongodb.util.MongoClientVersion; |
| 44 | +import org.springframework.test.util.ReflectionTestUtils; |
37 | 45 |
|
38 | 46 | import com.mongodb.MongoCursorNotFoundException;
|
39 | 47 | import com.mongodb.MongoException;
|
40 | 48 | import com.mongodb.MongoInternalException;
|
41 | 49 | import com.mongodb.MongoSocketException;
|
42 | 50 | import com.mongodb.ServerAddress;
|
| 51 | +import com.mongodb.WriteConcernException; |
43 | 52 |
|
44 | 53 | /**
|
45 | 54 | * Unit tests for {@link MongoExceptionTranslator}.
|
46 | 55 | *
|
47 | 56 | * @author Michal Vich
|
48 | 57 | * @author Oliver Gierke
|
49 | 58 | * @author Christoph Strobl
|
| 59 | + * @author Mark Paluch |
50 | 60 | */
|
51 | 61 | @RunWith(MockitoJUnitRunner.class)
|
52 | 62 | public class MongoExceptionTranslatorUnitTests {
|
@@ -133,6 +143,62 @@ public void translateMongoInternalException() {
|
133 | 143 | expectExceptionWithCauseMessage(translatedException, InvalidDataAccessResourceUsageException.class);
|
134 | 144 | }
|
135 | 145 |
|
| 146 | + /** |
| 147 | + * @see DATAMONGO-1451 |
| 148 | + */ |
| 149 | + @Test |
| 150 | + @SuppressWarnings("unchecked") |
| 151 | + public void translateTimeoutToTransientDataAccessResourceExceptionWith2xDriver() throws Exception { |
| 152 | + |
| 153 | + assumeThat(MongoClientVersion.isMongo3Driver(), is(false)); |
| 154 | + |
| 155 | + Constructor<?> constructor = Class.forName("com.mongodb.CommandResult").getDeclaredConstructor(ServerAddress.class); |
| 156 | + constructor.setAccessible(true); |
| 157 | + |
| 158 | + Map<String, Object> commandResult = (Map<String, Object>) constructor.newInstance(new ServerAddress("localhost")); |
| 159 | + commandResult.put("wtimeout", true); |
| 160 | + commandResult.put("ok", 1); |
| 161 | + commandResult.put("n", 0); |
| 162 | + commandResult.put("err", "waiting for replication timed out"); |
| 163 | + commandResult.put("code", 64); |
| 164 | + |
| 165 | + DataAccessException translatedException = translator.translateExceptionIfPossible( |
| 166 | + (RuntimeException) ReflectionTestUtils.invokeMethod(commandResult, "getException")); |
| 167 | + |
| 168 | + expectExceptionWithCauseMessage(translatedException, TransientDataAccessResourceException.class); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @see DATAMONGO-1451 |
| 173 | + */ |
| 174 | + @Test |
| 175 | + public void translateTimeoutToTransientDataAccessResourceExceptionWith3xDriver() throws Exception { |
| 176 | + |
| 177 | + assumeThat(MongoClientVersion.isMongo3Driver(), is(true)); |
| 178 | + |
| 179 | + Class<?> bsonDocumentClass = Class.forName("org.bson.BsonDocument"); |
| 180 | + |
| 181 | + Method getWriteResult = Class.forName("com.mongodb.connection.ProtocolHelper").getDeclaredMethod("getWriteResult", |
| 182 | + bsonDocumentClass, ServerAddress.class); |
| 183 | + |
| 184 | + String response = "{ \"serverUsed\" : \"10.10.17.35:27017\" , \"ok\" : 1 , \"n\" : 0 , \"wtimeout\" : true , \"err\" : \"waiting for replication timed out\" , \"code\" : 64}"; |
| 185 | + Object bsonDocument = bsonDocumentClass.getDeclaredMethod("parse", String.class).invoke(null, response); |
| 186 | + |
| 187 | + try { |
| 188 | + getWriteResult.setAccessible(true); |
| 189 | + getWriteResult.invoke(null, bsonDocument, new ServerAddress("localhost")); |
| 190 | + fail("Missing Exception"); |
| 191 | + } catch (InvocationTargetException e) { |
| 192 | + |
| 193 | + assertThat(e.getTargetException(), is(instanceOf(WriteConcernException.class))); |
| 194 | + |
| 195 | + DataAccessException translatedException = translator |
| 196 | + .translateExceptionIfPossible((RuntimeException) e.getTargetException()); |
| 197 | + expectExceptionWithCauseMessage(translatedException, TransientDataAccessResourceException.class); |
| 198 | + } |
| 199 | + |
| 200 | + } |
| 201 | + |
136 | 202 | @Test
|
137 | 203 | public void translateUnsupportedException() {
|
138 | 204 |
|
|
0 commit comments