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
/**
32
35
* Unit tests for {@link JdbcQueryMethod}.
33
36
*
34
37
* @author Jens Schauder
35
38
* @author Oliver Gierke
39
+ * @author Moises Cisneros
36
40
*/
37
41
public class JdbcQueryMethodUnitTests {
38
42
39
- public static final String DUMMY_SELECT = "SELECT something" ;
43
+ public static final String DUMMY_SELECT_VALUE = "SELECT something" ;
44
+ public static final String DUMMY_SELECT_NAME = "DUMMY.SELECT" ;
45
+ public static final String DUMMY_SELECT_METHOD = "queryWhitoutQueryAnnotation" ;
46
+ public static final String DUMMY_SELECT_NAME_VALUE = "SELECT something NAME AND VALUE" ;
40
47
41
48
@ Test // DATAJDBC-165
42
49
public void returnsSqlStatement () throws NoSuchMethodException {
43
50
44
51
RepositoryMetadata metadata = mock (RepositoryMetadata .class );
45
52
46
53
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 );
54
+ Properties properties = new Properties ();
55
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
56
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
57
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
58
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethod" ), metadata ,
59
+ mock (ProjectionFactory .class ), nameQueries );
60
+ assertThat (queryMethod .getAnnotatedQuery ()).isEqualTo (DUMMY_SELECT_VALUE );
52
61
}
53
62
54
63
@ Test // DATAJDBC-165
@@ -57,15 +66,91 @@ public void returnsSpecifiedRowMapperClass() throws NoSuchMethodException {
57
66
RepositoryMetadata metadata = mock (RepositoryMetadata .class );
58
67
59
68
doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
69
+ Properties properties = new Properties ();
70
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
71
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
60
72
61
- JdbcQueryMethod queryMethod = new JdbcQueryMethod (JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethod" ),
62
- metadata , mock (ProjectionFactory .class ));
73
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
74
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethod" ), metadata ,
75
+ mock (ProjectionFactory .class ), nameQueries );
63
76
64
77
assertThat (queryMethod .getRowMapperClass ()).isEqualTo (CustomRowMapper .class );
65
78
}
66
79
67
- @ Query (value = DUMMY_SELECT , rowMapperClass = CustomRowMapper .class )
68
- private void queryMethod () {}
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+ @ Test // DATAJDBC-234
89
+ public void returnsSqlStatementName () throws NoSuchMethodException {
90
+
91
+ RepositoryMetadata metadata = mock (RepositoryMetadata .class );
92
+
93
+ doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
94
+
95
+ Properties properties = new Properties ();
96
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
97
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
98
+
99
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
100
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethodName" ), metadata ,
101
+ mock (ProjectionFactory .class ), nameQueries );
102
+ assertThat (queryMethod .getAnnotatedQuery ()).isEqualTo (DUMMY_SELECT_VALUE );
103
+
104
+ }
105
+ @ Test // DATAJDBC-234
106
+ public void returnsSqlStatementNameAndValue () throws NoSuchMethodException {
107
+
108
+ RepositoryMetadata metadata = mock (RepositoryMetadata .class );
109
+
110
+ doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
111
+
112
+ Properties properties = new Properties ();
113
+ properties .setProperty (DUMMY_SELECT_NAME , DUMMY_SELECT_VALUE );
114
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
115
+
116
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
117
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryMethodNameAndValue" ), metadata ,
118
+ mock (ProjectionFactory .class ), nameQueries );
119
+ assertThat (queryMethod .getAnnotatedQuery ()).isEqualTo (DUMMY_SELECT_NAME_VALUE );
120
+
121
+ }
122
+
123
+ @ Test // DATAJDBC-234
124
+ public void returnsNullNoSqlQuery () throws NoSuchMethodException {
125
+
126
+ RepositoryMetadata metadata = mock (RepositoryMetadata .class );
127
+ Properties properties = new Properties ();
128
+ properties .setProperty (DUMMY_SELECT_METHOD , DUMMY_SELECT_VALUE );
129
+ NamedQueries nameQueries = new PropertiesBasedNamedQueries (properties );
130
+
131
+ doReturn (String .class ).when (metadata ).getReturnedDomainClass (any (Method .class ));
132
+
133
+ JdbcQueryMethod queryMethod = new JdbcQueryMethod (
134
+ JdbcQueryMethodUnitTests .class .getDeclaredMethod ("queryWhitoutQueryAnnotation" ), metadata ,
135
+ mock (ProjectionFactory .class ), nameQueries );
136
+ assertThat (queryMethod .getAnnotatedQuery ()).isEqualTo (DUMMY_SELECT_VALUE );
137
+
138
+ }
139
+
140
+ @ Query (value = DUMMY_SELECT_VALUE , rowMapperClass = CustomRowMapper .class )
141
+ private void queryMethod () {
142
+ }
143
+
144
+ @ Query (name = DUMMY_SELECT_NAME )
145
+ private void queryMethodName () {
146
+ }
147
+
148
+ @ Query (value = DUMMY_SELECT_NAME_VALUE , name = DUMMY_SELECT_NAME )
149
+ private void queryMethodNameAndValue () {
150
+ }
151
+
152
+ private void queryWhitoutQueryAnnotation () {
153
+ }
69
154
70
155
private class CustomRowMapper implements RowMapper <Object > {
71
156
0 commit comments