Skip to content

Commit b9346b8

Browse files
committed
Test MongoResultSet.getObject
1 parent fdefbd9 commit b9346b8

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/main/java/com/mongodb/hibernate/jdbc/MongoResultSet.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ public double getDouble(int columnIndex) throws SQLException {
203203

204204
@Override
205205
public <T> @Nullable T getObject(int columnIndex, Class<T> type) throws SQLException {
206-
// VAKOTODO add MongoResultSet tests
207206
checkClosed();
208207
checkColumnIndex(columnIndex);
209208
Object value;

src/test/java/com/mongodb/hibernate/jdbc/MongoResultSetTests.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
import org.bson.BsonInt32;
4242
import org.bson.BsonInt64;
4343
import org.bson.BsonNull;
44+
import org.bson.BsonObjectId;
4445
import org.bson.BsonString;
4546
import org.bson.BsonValue;
4647
import org.bson.types.Decimal128;
48+
import org.bson.types.ObjectId;
4749
import org.junit.jupiter.api.AutoClose;
4850
import org.junit.jupiter.api.BeforeEach;
4951
import org.junit.jupiter.api.Nested;
@@ -106,7 +108,10 @@ void testGettersForNull() throws SQLException {
106108
() -> assertEquals(0, mongoResultSet.getInt(1)),
107109
() -> assertEquals(0L, mongoResultSet.getLong(1)),
108110
() -> assertEquals(0D, mongoResultSet.getDouble(1)),
109-
() -> assertNull(mongoResultSet.getBigDecimal(1)));
111+
() -> assertNull(mongoResultSet.getBytes(1)),
112+
() -> assertNull(mongoResultSet.getBigDecimal(1)),
113+
() -> assertNull(mongoResultSet.getObject(1, ObjectId.class)),
114+
() -> assertTrue(mongoResultSet.wasNull()));
110115
}
111116

112117
@Test
@@ -118,7 +123,10 @@ void testGettersForBoolean() throws SQLException {
118123
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getInt(1)),
119124
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getLong(1)),
120125
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getDouble(1)),
121-
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)));
126+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBytes(1)),
127+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)),
128+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getObject(1, ObjectId.class)),
129+
() -> assertFalse(mongoResultSet.wasNull()));
122130
}
123131

124132
@Test
@@ -130,7 +138,10 @@ void testGettersForDouble() throws SQLException {
130138
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getInt(1)),
131139
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getLong(1)),
132140
() -> assertEquals(3.1415, mongoResultSet.getDouble(1)),
133-
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)));
141+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBytes(1)),
142+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)),
143+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getObject(1, ObjectId.class)),
144+
() -> assertFalse(mongoResultSet.wasNull()));
134145
}
135146

136147
@Test
@@ -142,7 +153,10 @@ void testGettersForInt() throws SQLException {
142153
() -> assertEquals(120, mongoResultSet.getInt(1)),
143154
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getLong(1)),
144155
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getDouble(1)),
145-
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)));
156+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBytes(1)),
157+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)),
158+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getObject(1, ObjectId.class)),
159+
() -> assertFalse(mongoResultSet.wasNull()));
146160
}
147161

148162
@Test
@@ -154,7 +168,10 @@ void testGettersForLong() throws SQLException {
154168
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getInt(1)),
155169
() -> assertEquals(12345678L, mongoResultSet.getLong(1)),
156170
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getDouble(1)),
157-
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)));
171+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBytes(1)),
172+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)),
173+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getObject(1, ObjectId.class)),
174+
() -> assertFalse(mongoResultSet.wasNull()));
158175
}
159176

160177
@Test
@@ -166,7 +183,10 @@ void testGettersForString() throws SQLException {
166183
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getInt(1)),
167184
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getLong(1)),
168185
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getDouble(1)),
169-
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)));
186+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBytes(1)),
187+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)),
188+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getObject(1, ObjectId.class)),
189+
() -> assertFalse(mongoResultSet.wasNull()));
170190
}
171191

172192
@Test
@@ -180,7 +200,10 @@ void testGettersForBigDecimal() throws SQLException {
180200
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getInt(1)),
181201
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getLong(1)),
182202
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getDouble(1)),
183-
() -> assertEquals(bigDecimalValue, mongoResultSet.getBigDecimal(1)));
203+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBytes(1)),
204+
() -> assertEquals(bigDecimalValue, mongoResultSet.getBigDecimal(1)),
205+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getObject(1, ObjectId.class)),
206+
() -> assertFalse(mongoResultSet.wasNull()));
184207
}
185208

186209
@Test
@@ -194,8 +217,27 @@ void testGettersForBinary() throws SQLException {
194217
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getInt(1)),
195218
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getLong(1)),
196219
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getDouble(1)),
220+
() -> assertEquals(bytes, mongoResultSet.getBytes(1)),
221+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)),
222+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getObject(1, ObjectId.class)),
223+
() -> assertFalse(mongoResultSet.wasNull()));
224+
}
225+
226+
@Test
227+
void testGettersForObject() throws SQLException {
228+
var objectId = new ObjectId(1, 0);
229+
var value = new BsonObjectId(objectId);
230+
createResultSetWith(value);
231+
assertAll(
232+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getString(1)),
233+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBoolean(1)),
234+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getInt(1)),
235+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getLong(1)),
236+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getDouble(1)),
237+
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBytes(1)),
197238
() -> assertThrowsTypeMismatchException(() -> mongoResultSet.getBigDecimal(1)),
198-
() -> assertEquals(bytes, mongoResultSet.getBytes(1)));
239+
() -> assertEquals(objectId, mongoResultSet.getObject(1, ObjectId.class)),
240+
() -> assertFalse(mongoResultSet.wasNull()));
199241
}
200242
}
201243

0 commit comments

Comments
 (0)