Skip to content

Commit fe7a4d7

Browse files
committed
HHH-19383 add more tests
1 parent 9a1bf3b commit fe7a4d7

File tree

1 file changed

+137
-29
lines changed

1 file changed

+137
-29
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/NativeQueryResultCheckingTests.java

Lines changed: 137 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void testForHHH19376(SessionFactoryScope scope) {
5555
}
5656

5757
@Test
58-
public void testOkMutateResultSetMappingWithString(SessionFactoryScope scope) {
58+
public void testOneColumn(SessionFactoryScope scope) {
5959
scope.inTransaction(
6060
session ->
6161
assertDoesNotThrow(
@@ -64,50 +64,139 @@ public void testOkMutateResultSetMappingWithString(SessionFactoryScope scope) {
6464
.getResultList()
6565
)
6666
);
67-
}
68-
69-
@Test
70-
public void testNokMutateResultSetMappingWithString(SessionFactoryScope scope) {
7167
scope.inTransaction(
7268
session ->
7369
assertThrows( IllegalArgumentException.class,
74-
() -> session.createNativeQuery( "select title, isbn from Book", String.class )
75-
.addScalar( "title", String.class )
70+
() -> session.createNativeQuery( "select isbn from Book", Integer.class )
71+
.addScalar( "isbn", String.class )
72+
.getResultList()
73+
)
74+
);
75+
scope.inTransaction(
76+
session ->
77+
assertDoesNotThrow(
78+
() -> session.createNativeQuery( "select isbn from Book", Object[].class )
79+
.addScalar( "isbn", String.class )
80+
.getResultList()
81+
)
82+
);
83+
scope.inTransaction(
84+
session ->
85+
assertDoesNotThrow(
86+
() -> session.createNativeQuery( "select isbn from Book", Tuple.class )
87+
.addScalar( "isbn", String.class )
88+
.getResultList()
89+
)
90+
);
91+
scope.inTransaction(
92+
session ->
93+
assertDoesNotThrow(
94+
() -> session.createNativeQuery( "select isbn from Book", Map.class )
95+
.addScalar( "isbn", String.class )
96+
.getResultList()
97+
)
98+
);
99+
scope.inTransaction(
100+
session ->
101+
assertDoesNotThrow(
102+
() -> session.createNativeQuery( "select isbn from Book", Object.class )
103+
.addScalar( "isbn", String.class )
104+
.getResultList()
105+
)
106+
);
107+
scope.inTransaction(
108+
session ->
109+
assertDoesNotThrow(
110+
() -> session.createNativeQuery( "select isbn from Book" )
76111
.addScalar( "isbn", String.class )
77112
.getResultList()
78113
)
79114
);
80115
}
81116

82117
@Test
83-
public void testOkMutateResultSetMappingWithBook(SessionFactoryScope scope) {
118+
public void testTwoStringColumns(SessionFactoryScope scope) {
119+
scope.inTransaction(
120+
session ->
121+
assertThrows( IllegalArgumentException.class,
122+
() -> session.createNativeQuery( "select name, isbn from Book", String.class )
123+
.addScalar( "name", String.class )
124+
.addScalar( "isbn", String.class )
125+
.getResultList()
126+
)
127+
);
84128
scope.inTransaction(
85129
session ->
86130
assertDoesNotThrow(
87-
() -> session.createNativeQuery( "select id, name from Book", Book.class )
88-
.addScalar( "id", Integer.class )
131+
() -> session.createNativeQuery( "select name, isbn from Book", Object[].class )
89132
.addScalar( "name", String.class )
133+
.addScalar( "isbn", String.class )
134+
.getResultList()
135+
)
136+
);
137+
scope.inTransaction(
138+
session ->
139+
assertDoesNotThrow(
140+
() -> session.createNativeQuery( "select name, isbn from Book", Tuple.class )
141+
.addScalar( "name", String.class )
142+
.addScalar( "isbn", String.class )
143+
.getResultList()
144+
)
145+
);
146+
scope.inTransaction(
147+
session ->
148+
assertDoesNotThrow(
149+
() -> session.createNativeQuery( "select name, isbn from Book", Map.class )
150+
.addScalar( "name", String.class )
151+
.addScalar( "isbn", String.class )
152+
.getResultList()
153+
)
154+
);
155+
scope.inTransaction(
156+
session ->
157+
assertDoesNotThrow(
158+
() -> session.createNativeQuery( "select name, isbn from Book", Object.class )
159+
.addScalar( "name", String.class )
160+
.addScalar( "isbn", String.class )
161+
.getResultList()
162+
)
163+
);
164+
scope.inTransaction(
165+
session ->
166+
assertDoesNotThrow(
167+
() -> session.createNativeQuery( "select name, isbn from Book" )
168+
.addScalar( "name", String.class )
169+
.addScalar( "isbn", String.class )
90170
.getResultList()
91171
)
92172
);
93-
}
94-
95-
@Test
96-
public void testNokMutateResultSetMappingWithBook(SessionFactoryScope scope) {
97173
scope.inTransaction(
98174
session ->
99175
assertThrows( IllegalArgumentException.class,
100-
() -> session.createNativeQuery( "select title, isbn from Book", Book.class )
176+
() -> session.createNativeQuery( "select name, isbn from Book", Book.class )
101177
// this mapping doesn't have an appropriate constructor in Book, should throw error
102-
.addScalar( "title", String.class )
178+
.addScalar( "name", String.class )
103179
.addScalar( "isbn", String.class )
104180
.getResultList()
105181
)
106182
);
107183
}
108184

109185
@Test
110-
public void testMutateResultSetMappingWithObjectArray(SessionFactoryScope scope) {
186+
public void testOkMutateResultSetMappingWithBook(SessionFactoryScope scope) {
187+
scope.inTransaction(
188+
session ->
189+
assertDoesNotThrow(
190+
() -> session.createNativeQuery( "select id, name from Book", Book.class )
191+
.addScalar( "id", Integer.class )
192+
.addScalar( "name", String.class )
193+
.getResultList()
194+
)
195+
);
196+
}
197+
198+
@Test
199+
public void testAllColumns(SessionFactoryScope scope) {
111200
scope.inTransaction(
112201
session ->
113202
assertDoesNotThrow(
@@ -119,10 +208,6 @@ public void testMutateResultSetMappingWithObjectArray(SessionFactoryScope scope)
119208
}
120209
)
121210
);
122-
}
123-
124-
@Test
125-
public void testMutateResultSetMappingWithTuple(SessionFactoryScope scope) {
126211
scope.inTransaction(
127212
session ->
128213
assertDoesNotThrow(
@@ -134,10 +219,6 @@ public void testMutateResultSetMappingWithTuple(SessionFactoryScope scope) {
134219
}
135220
)
136221
);
137-
}
138-
139-
@Test
140-
public void testMutateResultSetMappingWithMap(SessionFactoryScope scope) {
141222
scope.inTransaction(
142223
session ->
143224
assertDoesNotThrow(
@@ -149,10 +230,28 @@ public void testMutateResultSetMappingWithMap(SessionFactoryScope scope) {
149230
}
150231
)
151232
);
152-
}
153-
154-
@Test
155-
public void testMutateResultSetMappingWithList(SessionFactoryScope scope) {
233+
scope.inTransaction(
234+
session ->
235+
assertDoesNotThrow(
236+
() -> {
237+
session.createNativeQuery( "select * from Book", Object.class )
238+
.addScalar( "id", Integer.class )
239+
.addScalar( "name", String.class )
240+
.getResultList();
241+
}
242+
)
243+
);
244+
scope.inTransaction(
245+
session ->
246+
assertDoesNotThrow(
247+
() -> {
248+
session.createNativeQuery( "select * from Book", Book.class )
249+
.addScalar( "id", Integer.class )
250+
.addScalar( "name", String.class )
251+
.getResultList();
252+
}
253+
)
254+
);
156255
scope.inTransaction(
157256
session ->
158257
assertDoesNotThrow(
@@ -164,6 +263,15 @@ public void testMutateResultSetMappingWithList(SessionFactoryScope scope) {
164263
}
165264
)
166265
);
266+
scope.inTransaction(
267+
session ->
268+
assertThrows( IllegalArgumentException.class,
269+
() -> session.createNativeQuery( "select * from Book", Book.class )
270+
.addScalar( "isbn", String.class )
271+
.addScalar( "name", String.class )
272+
.getResultList()
273+
)
274+
);
167275
}
168276

169277
@Test

0 commit comments

Comments
 (0)