@@ -20,7 +20,10 @@ import org.mongodb.Document
2020import org.mongodb.MongoInterruptedException
2121import org.mongodb.command.MongoCommandFailureException
2222import org.mongodb.command.MongoDuplicateKeyException
23+ import org.mongodb.command.MongoWriteConcernException
2324import org.mongodb.connection.MongoSocketReadException
25+ import org.mongodb.connection.MongoTimeoutException
26+ import org.mongodb.connection.MongoWaitQueueFullException
2427import org.mongodb.operation.MongoCursorNotFoundException
2528import org.mongodb.operation.ServerCursor
2629import spock.lang.Specification
@@ -41,6 +44,7 @@ class MongoExceptionsSpecification extends Specification {
4144 actualException instanceof MongoException.Network
4245 actualException. getCause() == cause
4346 actualException. getMessage() == expectedMessage
47+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
4448 }
4549
4650 def ' should convert InterruptedExceptions that are not InterruptedIOExceptions into MongoException' () {
@@ -56,6 +60,7 @@ class MongoExceptionsSpecification extends Specification {
5660 actualException instanceof MongoException
5761 actualException. getCause() == cause
5862 actualException. getMessage() == expectedMessage
63+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
5964 }
6065
6166 def ' should convert IOExceptions into MongoException.Network' () {
@@ -71,6 +76,7 @@ class MongoExceptionsSpecification extends Specification {
7176 actualException instanceof MongoException.Network
7277 actualException. getCause() == cause
7378 actualException. getMessage() == expectedMessage
79+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
7480 }
7581
7682 def ' should convert SocketExceptions that are not IOExceptions into MongoException' () {
@@ -85,6 +91,7 @@ class MongoExceptionsSpecification extends Specification {
8591 ! (actualException instanceof MongoException.Network )
8692 actualException instanceof MongoException
8793 actualException. getMessage() == expectedMessage
94+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
8895 }
8996
9097 def ' should convert MongoDuplicateKeyException into DuplicateKeyException' () {
@@ -93,19 +100,14 @@ class MongoExceptionsSpecification extends Specification {
93100 String expectedMessage = " Command failed with response { \" code\" : $expectedErrorCode } " +
94101 " on server ServerAddress{host='127.0.0.1', port=27017}"
95102
96- def newStyleException = new MongoDuplicateKeyException (
97- new org.mongodb.operation.CommandResult (new Document (),
98- new org.mongodb.connection.ServerAddress (),
99- new Document
100- (' code' , expectedErrorCode),
101- 15L ))
102103 when :
103- MongoException actualException = mapException(newStyleException )
104+ MongoException actualException = mapException(new MongoDuplicateKeyException (commandResultWithErrorCode(expectedErrorCode)) )
104105
105106 then :
106107 actualException instanceof MongoException.DuplicateKey
107108 actualException. getMessage() == expectedMessage
108109 actualException. getCode() == expectedErrorCode
110+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
109111 }
110112
111113 def ' should convert MongoCommandFailureException into CommandFailureException' () {
@@ -114,19 +116,15 @@ class MongoExceptionsSpecification extends Specification {
114116 String expectedMessage = " Command failed with response { \" code\" : $expectedErrorCode } " +
115117 " on server ServerAddress{host='127.0.0.1', port=27017}"
116118
117- def newStyleException = new MongoCommandFailureException (
118- new org.mongodb.operation.CommandResult (new Document (),
119- new org.mongodb.connection.ServerAddress (),
120- new Document
121- (' code' , expectedErrorCode),
122- 15L ))
119+ def newStyleException = new MongoCommandFailureException (commandResultWithErrorCode(expectedErrorCode))
123120 when :
124121 MongoException actualException = mapException(newStyleException)
125122
126123 then :
127124 actualException instanceof CommandFailureException
128125 actualException. getMessage() == expectedMessage
129126 actualException. getCode() == expectedErrorCode
127+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
130128 }
131129
132130 def ' should convert MongoCursorNotFoundException into MongoException.CursorNotFound' () {
@@ -145,56 +143,76 @@ class MongoExceptionsSpecification extends Specification {
145143 actualAsCursorNotFound. getCursorId() == cursorId
146144 actualAsCursorNotFound. getServerAddress(). getHost() == serverAddress. getHost()
147145 actualAsCursorNotFound. getServerAddress(). getPort() == serverAddress. getPort()
146+
147+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
148148 }
149149
150- def ' should not expose org.mongodb Exception in the stack trace of MongoInterruptedException ' () {
150+ def ' should convert org.mongodb.MongoInternalException into com.mongodb.MongoInternalException ' () {
151151 given :
152- String expectedMessage = ' Interrupted IO Exception in the new architecture'
153- def cause = new InterruptedIOException (' The cause' )
152+ String expectedMessage = ' Internal Exception thrown'
154153
155154 when :
156- MongoException actualException = mapException(new MongoInterruptedException (expectedMessage, cause ))
155+ MongoException actualException = mapException(new org.mongodb.MongoInternalException (expectedMessage))
157156
158157 then :
159- ! (actualException. getCause() instanceof org.mongodb.MongoException )
160- ! actualException. getStackTrace(). any { it. className. startsWith(' org.mongodb' ) }
158+ actualException instanceof MongoInternalException
159+ actualException. getMessage() == expectedMessage
160+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
161161 }
162162
163- def ' should not expose org.mongodb Exception in the stack trace of MongoCursorNotFoundException' () {
163+ def ' should convert MongoWriteConcernException into com.mongodb.WriteConcernException' () {
164+ given :
165+ int expectedErrorCode = 500
166+ String expectedMessage = " Command failed with response { \" code\" : $expectedErrorCode } " +
167+ " on server ServerAddress{host='127.0.0.1', port=27017}"
168+
164169 when :
165- MongoException actualException = mapException(new MongoCursorNotFoundException (
166- new ServerCursor (123L , new org.mongodb.connection.ServerAddress ())))
170+ MongoException actualException = mapException(new MongoWriteConcernException (commandResultWithErrorCode(expectedErrorCode)))
167171
168172 then :
169- ! (actualException. getCause() instanceof org.mongodb.MongoException )
170- ! actualException. getStackTrace(). any { it. className. startsWith(' org.mongodb' ) }
173+ actualException instanceof WriteConcernException
174+ actualException. getMessage() == expectedMessage
175+ actualException. getCode() == expectedErrorCode
176+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
171177 }
172178
173- def ' should not expose org.mongodb Exception in the stack trace of MongoCommandFailureException' () {
179+ def ' should convert MongoTimeoutException into com.mongodb.ConnectionWaitTimeOut' () {
180+ given :
181+ String expectedMessage = ' A timeout exception was throwm'
182+
174183 when :
175- MongoException actualException = mapException(new MongoCommandFailureException (
176- new org.mongodb.operation.CommandResult (new Document (),
177- new org.mongodb.connection.ServerAddress (),
178- new Document (),
179- 15L )))
184+ MongoException actualException = mapException(new MongoTimeoutException (expectedMessage))
180185
181186 then :
182- ! (actualException. getCause() instanceof org.mongodb.MongoException )
183- ! actualException. getStackTrace(). any { it. className. startsWith(' org.mongodb' ) }
187+ actualException instanceof ConnectionWaitTimeOut
188+ actualException. getMessage() == expectedMessage
189+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
184190 }
185191
186- def ' should not expose org.mongodb Exception in the stack trace of MongoDuplicateKeyException' () {
187- when :
192+ def ' should convert MongoWaitQueueFullException into com.mongodb.SemaphoresOut' () {
193+ given :
194+ String expectedMessage = ' A queue full exception was throwm'
188195
189- MongoException actualException = mapException(new MongoDuplicateKeyException (
190- new org.mongodb.operation.CommandResult (new Document (),
191- new org.mongodb.connection.ServerAddress (),
192- new Document (),
193- 15L )))
196+ when :
197+ MongoException actualException = mapException(new MongoWaitQueueFullException (expectedMessage))
194198
195199 then :
196- ! (actualException. getCause() instanceof org.mongodb.MongoException )
197- ! actualException. getStackTrace(). any { it. className. startsWith(' org.mongodb' ) }
200+ actualException instanceof SemaphoresOut
201+ actualException. getMessage() == expectedMessage
202+ assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace(actualException)
203+ }
204+
205+ private static org.mongodb.operation.CommandResult commandResultWithErrorCode (int expectedErrorCode ) {
206+ new org.mongodb.operation.CommandResult (new Document (),
207+ new org.mongodb.connection.ServerAddress (),
208+ new Document
209+ (' code' , expectedErrorCode),
210+ 15L )
211+ }
212+
213+ private static void assertExceptionFromNewArchitectureIsNotVisibleOnStackTrace (MongoException actualException ) {
214+ assert ! (actualException. getCause() instanceof org.mongodb.MongoException )
215+ assert ! actualException. getStackTrace(). any { it. className. startsWith(' org.mongodb' ) }
198216 }
199217
200218}
0 commit comments