|
20 | 20 | import org.jetbrains.annotations.Nullable; |
21 | 21 | import org.junit.Assert; |
22 | 22 | import org.junit.Test; |
23 | | -import org.labkey.api.action.BaseViewAction; |
24 | 23 | import org.labkey.api.collections.CaseInsensitiveCollection; |
25 | 24 | import org.labkey.api.collections.CaseInsensitiveHashMap; |
26 | 25 | import org.labkey.api.collections.RowMap; |
27 | 26 | import org.labkey.api.query.FieldKey; |
28 | | -import org.labkey.api.util.DateUtil; |
29 | 27 | import org.labkey.api.util.ResultSetUtil; |
30 | | -import org.springframework.beans.MutablePropertyValues; |
31 | | -import org.springframework.beans.PropertyValues; |
32 | | -import org.springframework.validation.BindException; |
33 | | -import org.springframework.validation.ObjectError; |
34 | 28 |
|
35 | 29 | import java.lang.reflect.Constructor; |
36 | 30 | import java.lang.reflect.Field; |
@@ -190,154 +184,17 @@ public void testDatabase() throws SQLException |
190 | 184 | rs.next(); |
191 | 185 | Assert.assertEquals(users.getFirst(), factory.handle(rs)); |
192 | 186 | } |
193 | | - MiniUser randomUser = users.get((int)(Math.random() * users.size())); |
| 187 | + MiniUser randomUser = users.get((int) (Math.random() * users.size())); |
194 | 188 | MiniUser selectedUser = new TableSelector(CoreSchema.getInstance().getTableInfoUsers(), new SimpleFilter(FieldKey.fromString("UserId"), randomUser.UserId), null).getObject(MiniUser.class); |
195 | 189 | Assert.assertEquals(randomUser, selectedUser); |
196 | 190 |
|
197 | 191 | // Test fromMap() variant (should ignore selectedUser) |
198 | 192 | assertEquals(adHocUser, factory.fromMap(selectedUser, adHocMap)); |
199 | 193 | } |
| 194 | + } |
200 | 195 |
|
201 | | - @Test |
202 | | - public void testBinding() |
203 | | - { |
204 | | - Date lastLogin = new Date(); |
205 | | - |
206 | | - // Provide all parameters |
207 | | - Map<String, Object> params = Map.of( |
208 | | - "firstName", "Fred", |
209 | | - "lastName", "Flintstone", |
210 | | - "lastLogin", DateUtil.formatIsoDateLongTime(lastLogin), |
211 | | - "userId", 1009 |
212 | | - ); |
213 | | - String toString = "MiniUser[FIRSTname=Fred, LASTNAME=Flintstone, lastLogin=" + lastLogin + ", UserId=1009]"; |
214 | | - testRecordBinding(params, toString); |
215 | | - testFormBinding(params, toString); |
216 | | - |
217 | | - // Provide just the primitive parameter; others are nullable |
218 | | - params = Map.of( |
219 | | - "userId", 1009 |
220 | | - ); |
221 | | - toString = "MiniUser[FIRSTname=null, LASTNAME=null, lastLogin=null, UserId=1009]"; |
222 | | - testRecordBinding(params, toString); |
223 | | - testFormBinding(params, toString); |
224 | | - |
225 | | - // No parameters should fail for record due to "userid" primitive. Ensure a reasonable error message. |
226 | | - testRecordBinding(Map.of(), "Primitive parameter \"UserId\" is required"); |
227 | | - // No parameters should succeed for form class. UserId simply defaults to 0; |
228 | | - testFormBinding(Map.of(), "MiniUser[FIRSTname=null, LASTNAME=null, lastLogin=null, UserId=0]"); |
229 | | - |
230 | | - // Verify message for conversion error |
231 | | - params = Map.of("UserId", "abc"); |
232 | | - String errorMessage = "Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'UserId'; Could not convert 'abc' to an integer"; |
233 | | - testRecordBinding(params, errorMessage); |
234 | | - testFormBinding(params, errorMessage); |
235 | | - } |
236 | | - |
237 | | - private void testRecordBinding(Map<String, Object> map, String expectedToStringOrError) |
238 | | - { |
239 | | - PropertyValues pvs = new MutablePropertyValues(map); |
240 | | - BindException be = BaseViewAction.bindParametersToRecord(MiniUser.class, pvs, "form"); |
241 | | - |
242 | | - if (be.hasErrors()) |
243 | | - { |
244 | | - validateError(be, expectedToStringOrError); |
245 | | - } |
246 | | - else |
247 | | - { |
248 | | - validateTarget(be.getTarget(), expectedToStringOrError); |
249 | | - } |
250 | | - } |
251 | | - |
252 | | - private void testFormBinding(Map<String, Object> map, String expectedToStringOrError) |
253 | | - { |
254 | | - PropertyValues pvs = new MutablePropertyValues(map); |
255 | | - BindException be = BaseViewAction.defaultBindParameters(new MiniUserForm(), "form", pvs); |
256 | | - |
257 | | - if (be.hasErrors()) |
258 | | - { |
259 | | - validateError(be, expectedToStringOrError); |
260 | | - } |
261 | | - else |
262 | | - { |
263 | | - validateTarget(be.getTarget(), expectedToStringOrError); |
264 | | - } |
265 | | - } |
266 | | - |
267 | | - private void validateError(BindException be, String expectedErrorMessage) |
268 | | - { |
269 | | - ObjectError error = be.getAllErrors().getFirst(); |
270 | | - assertNotNull(error); |
271 | | - assertEquals(expectedErrorMessage, error.getDefaultMessage()); |
272 | | - } |
273 | | - |
274 | | - private void validateTarget(Object user, String expectedToString) |
275 | | - { |
276 | | - assertNotNull(user); |
277 | | - assertEquals(expectedToString, user.toString()); |
278 | | - } |
279 | | - |
280 | | - // Simple test record. Weird casing is intentional to test case-insensitivity. |
281 | | - private record MiniUser(String FIRSTname, String LASTNAME, Date lastLogin, int UserId) |
282 | | - { |
283 | | - } |
284 | | - |
285 | | - // Simple test form |
286 | | - @SuppressWarnings("unused") |
287 | | - private static class MiniUserForm |
288 | | - { |
289 | | - String _firstName; |
290 | | - String _lastName; |
291 | | - Date _lastLogin; |
292 | | - int _userId; |
293 | | - |
294 | | - public String getFirstName() |
295 | | - { |
296 | | - return _firstName; |
297 | | - } |
298 | | - |
299 | | - public void setFirstName(String firstName) |
300 | | - { |
301 | | - _firstName = firstName; |
302 | | - } |
303 | | - |
304 | | - public String getLastName() |
305 | | - { |
306 | | - return _lastName; |
307 | | - } |
308 | | - |
309 | | - public void setLastName(String lastName) |
310 | | - { |
311 | | - _lastName = lastName; |
312 | | - } |
313 | | - |
314 | | - public Date getLastLogin() |
315 | | - { |
316 | | - return _lastLogin; |
317 | | - } |
318 | | - |
319 | | - public void setLastLogin(Date lastLogin) |
320 | | - { |
321 | | - _lastLogin = lastLogin; |
322 | | - } |
323 | | - |
324 | | - public int getUserId() |
325 | | - { |
326 | | - return _userId; |
327 | | - } |
328 | | - |
329 | | - public void setUserId(int userId) |
330 | | - { |
331 | | - _userId = userId; |
332 | | - } |
333 | | - |
334 | | - @Override |
335 | | - public String toString() |
336 | | - { |
337 | | - // No useful default toString(), so emulate the standard record toString(). That's good enough to verify |
338 | | - // that the parameters were bound correctly. |
339 | | - return "MiniUser[FIRSTname=" + _firstName + ", LASTNAME=" + _lastName + ", lastLogin=" + _lastLogin + ", UserId=" + _userId + "]"; |
340 | | - } |
341 | | - } |
| 196 | + // Simple test record. Weird casing is intentional to test case-insensitivity. |
| 197 | + record MiniUser(String FIRSTname, String LASTNAME, Date lastLogin, int UserId) |
| 198 | + { |
342 | 199 | } |
343 | 200 | } |
0 commit comments