1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import java .sql .Types ;
20
20
import java .util .Arrays ;
21
21
22
+ import org .hamcrest .Matchers ;
22
23
import org .junit .Test ;
23
24
24
25
import org .springframework .tests .sample .beans .TestBean ;
29
30
* @author Rick Evans
30
31
* @author Juergen Hoeller
31
32
* @author Arjen Poutsma
33
+ * @author Juergen Hoeller
32
34
*/
33
35
public class BeanPropertySqlParameterSourceTests {
34
36
35
37
@ Test (expected = IllegalArgumentException .class )
36
- public void withNullBeanPassedToCtor () throws Exception {
38
+ public void withNullBeanPassedToCtor () {
37
39
new BeanPropertySqlParameterSource (null );
38
40
}
39
41
40
42
@ Test (expected = IllegalArgumentException .class )
41
- public void getValueWhereTheUnderlyingBeanHasNoSuchProperty () throws Exception {
43
+ public void getValueWhereTheUnderlyingBeanHasNoSuchProperty () {
42
44
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource (new TestBean ());
43
45
source .getValue ("thisPropertyDoesNotExist" );
44
46
}
@@ -65,23 +67,57 @@ public void successfulPropertyAccessWithOverriddenSqlType() {
65
67
}
66
68
67
69
@ Test
68
- public void hasValueWhereTheUnderlyingBeanHasNoSuchProperty () throws Exception {
70
+ public void hasValueWhereTheUnderlyingBeanHasNoSuchProperty () {
69
71
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource (new TestBean ());
70
72
assertFalse (source .hasValue ("thisPropertyDoesNotExist" ));
71
73
}
72
74
73
75
@ Test (expected = IllegalArgumentException .class )
74
- public void getValueWhereTheUnderlyingBeanPropertyIsNotReadable () throws Exception {
76
+ public void getValueWhereTheUnderlyingBeanPropertyIsNotReadable () {
75
77
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource (new NoReadableProperties ());
76
78
source .getValue ("noOp" );
77
79
}
78
80
79
81
@ Test
80
- public void hasValueWhereTheUnderlyingBeanPropertyIsNotReadable () throws Exception {
82
+ public void hasValueWhereTheUnderlyingBeanPropertyIsNotReadable () {
81
83
BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource (new NoReadableProperties ());
82
84
assertFalse (source .hasValue ("noOp" ));
83
85
}
84
86
87
+ @ Test
88
+ public void toStringShowsParameterDetails () {
89
+ BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource (new TestBean ("tb" , 99 ));
90
+ assertThat (source .toString (), Matchers .allOf (
91
+ Matchers .startsWith ("BeanPropertySqlParameterSource {" ),
92
+ Matchers .endsWith ("}" ),
93
+ Matchers .containsString ("name=tb (type:VARCHAR)" ),
94
+ Matchers .containsString ("age=99 (type:INTEGER)" )
95
+ ));
96
+ }
97
+
98
+ @ Test
99
+ public void toStringShowsCustomSqlType () {
100
+ BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource (new TestBean ("tb" , 99 ));
101
+ source .registerSqlType ("name" , Integer .MAX_VALUE );
102
+ assertThat (source .toString (), Matchers .allOf (
103
+ Matchers .startsWith ("BeanPropertySqlParameterSource {" ),
104
+ Matchers .endsWith ("}" ),
105
+ Matchers .containsString ("name=tb (type:" + Integer .MAX_VALUE + ")" ),
106
+ Matchers .containsString ("age=99 (type:INTEGER)" )
107
+ ));
108
+ }
109
+
110
+ @ Test
111
+ public void toStringDoesNotShowTypeUnknown () {
112
+ BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource (new TestBean ("tb" , 99 ));
113
+ assertThat (source .toString (), Matchers .allOf (
114
+ Matchers .startsWith ("BeanPropertySqlParameterSource {" ),
115
+ Matchers .endsWith ("}" ),
116
+ Matchers .containsString ("beanFactory=null" ),
117
+ Matchers .not (Matchers .containsString ("beanFactory=null (type:" ))
118
+ ));
119
+ }
120
+
85
121
86
122
@ SuppressWarnings ("unused" )
87
123
private static final class NoReadableProperties {
0 commit comments