21
21
22
22
import java .lang .reflect .Method ;
23
23
import java .sql .ResultSet ;
24
+ import java .util .Properties ;
24
25
25
26
import org .junit .Test ;
26
27
import org .springframework .data .jdbc .repository .query .Query ;
27
28
import org .springframework .data .projection .ProjectionFactory ;
29
+ import org .springframework .data .repository .core .NamedQueries ;
28
30
import org .springframework .data .repository .core .RepositoryMetadata ;
31
+ import org .springframework .data .repository .core .support .PropertiesBasedNamedQueries ;
29
32
import org .springframework .jdbc .core .RowMapper ;
30
33
31
34
/**
36
39
*/
37
40
public class JdbcQueryMethodUnitTests {
38
41
39
- public static final String DUMMY_SELECT = "SELECT something" ;
42
+ public static final String DUMMY_SELECT_VALUE = "SELECT something" ;
43
+ public static final String DUMMY_SELECT_NAME = "DUMMY.SELECT" ;
44
+ public static final String DUMMY_SELECT_NAME_VALUE = "SELECT something NAME AND VALUE" ;
40
45
41
46
@ Test // DATAJDBC-165
42
47
public void returnsSqlStatement () throws NoSuchMethodException {
43
48
44
49
RepositoryMetadata metadata = mock (RepositoryMetadata .class );
45
50
46
51
doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
47
-
48
- JdbcQueryMethod queryMethod = new JdbcQueryMethod (JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethod" ),
49
- metadata , mock (ProjectionFactory .class ));
50
-
51
- assertThat (queryMethod .getAnnotatedQuery ()).isEqualTo (DUMMY_SELECT );
52
+ Properties properties = new Properties ();
53
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
54
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
55
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
56
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethod" ), metadata ,
57
+ mock (ProjectionFactory .class ), nameQueries );
58
+
59
+ assertThat (queryMethod .getAnnotatedQuery ()).isEqualTo (DUMMY_SELECT_VALUE );
52
60
}
53
61
54
62
@ Test // DATAJDBC-165
@@ -57,15 +65,80 @@ public void returnsSpecifiedRowMapperClass() throws NoSuchMethodException {
57
65
RepositoryMetadata metadata = mock (RepositoryMetadata .class );
58
66
59
67
doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
68
+ Properties properties = new Properties ();
69
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
70
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
60
71
61
- JdbcQueryMethod queryMethod = new JdbcQueryMethod (JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethod" ),
62
- metadata , mock (ProjectionFactory .class ));
72
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
73
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethod" ), metadata ,
74
+ mock (ProjectionFactory .class ), nameQueries );
63
75
64
76
assertThat (queryMethod .getRowMapperClass ()).isEqualTo (CustomRowMapper .class );
65
77
}
66
78
67
- @ Query (value = DUMMY_SELECT , rowMapperClass = CustomRowMapper .class )
68
- private void queryMethod () {}
79
+ @ Test // DATAJDBC-234
80
+ public void returnsSqlStatementName () throws NoSuchMethodException {
81
+
82
+ RepositoryMetadata metadata = mock (RepositoryMetadata .class );
83
+
84
+ doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
85
+
86
+ Properties properties = new Properties ();
87
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
88
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
89
+
90
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
91
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethodName" ), metadata ,
92
+ mock (ProjectionFactory .class ), nameQueries );
93
+
94
+ assertThat (queryMethod .getAnnotatedQuery ()).isEqualTo (DUMMY_SELECT_VALUE );
95
+ }
96
+ @ Test // DATAJDBC-234
97
+ public void returnsSqlStatementNameAndValue () throws NoSuchMethodException {
98
+
99
+ RepositoryMetadata metadata = mock (RepositoryMetadata .class );
100
+
101
+ doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
102
+
103
+ Properties properties = new Properties ();
104
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
105
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
106
+
107
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
108
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethodNameAndValue" ), metadata ,
109
+ mock (ProjectionFactory .class ), nameQueries );
110
+
111
+ assertThat (queryMethod .getAnnotatedQuery ()).isEqualTo (DUMMY_SELECT_NAME_VALUE );
112
+ }
113
+
114
+ @ Test // DATAJDBC-234
115
+ public void returnsSpecifiedRowMapperClassName () throws NoSuchMethodException {
116
+
117
+ RepositoryMetadata metadata = mock (RepositoryMetadata .class );
118
+ Properties properties = new Properties ();
119
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
120
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
121
+
122
+ doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
123
+
124
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
125
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethodName" ), metadata ,
126
+ mock (ProjectionFactory .class ), nameQueries );
127
+
128
+ assertThat (queryMethod .getRowMapperClass ()).isEqualTo (CustomRowMapper .class );
129
+ }
130
+
131
+ @ Query (value = DUMMY_SELECT_VALUE , rowMapperClass = CustomRowMapper .class )
132
+ private void queryMethod () {
133
+ }
134
+
135
+ @ Query (name = DUMMY_SELECT_NAME , rowMapperClass = CustomRowMapper .class )
136
+ private void queryMethodName () {
137
+ }
138
+
139
+ @ Query (value = DUMMY_SELECT_NAME_VALUE , name = DUMMY_SELECT_NAME , rowMapperClass = CustomRowMapper .class )
140
+ private void queryMethodNameAndValue () {
141
+ }
69
142
70
143
private class CustomRowMapper implements RowMapper <Object > {
71
144
0 commit comments