Elffuss BraidHub

kikocis/java-driver public

424cf2096c8c · 8.4 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2015 DataStax Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.datastax.driver.mapping;
17
18import com.datastax.driver.core.CCMTestsSupport;
19import com.datastax.driver.core.ResultSet;
20import com.datastax.driver.core.Row;
21import com.datastax.driver.core.utils.CassandraVersion;
22import com.datastax.driver.mapping.annotations.*;
23import com.google.common.collect.Lists;
24import org.testng.annotations.Test;
25
26import java.util.Collection;
27
28import static org.assertj.core.api.Assertions.assertThat;
29
30public class MapperAccessorParamsTest extends CCMTestsSupport {
31 @Override
32 public Collection<String> createTestFixtures() {
33 return Lists.newArrayList(
34 "CREATE TABLE user ( key int primary key, gender int, home_phone text, work_phone text)",
35 "CREATE INDEX on user(gender)",
36 "CREATE TABLE user_str ( key int primary key, gender text)",
37 "CREATE INDEX on user_str(gender)"
38 );
39 }
40
41 @Test(groups = "short")
42 public void should_allow_enum_as_int_in_accessor_params() {
43 UserAccessor accessor = new MappingManager(session)
44 .createAccessor(UserAccessor.class);
45
46 accessor.addUser(0, Enum.FEMALE);
47 accessor.addUser(1, Enum.MALE);
48
49 assertThat(accessor.getUser(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE);
50 assertThat(accessor.getUser(Enum.MALE).one().getKey()).isEqualTo(1);
51
52 assertThat(accessor.getUser(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE);
53 assertThat(accessor.getUser(Enum.FEMALE).one().getKey()).isEqualTo(0);
54 }
55
56 @Test(groups = "short")
57 public void should_allow_enum_as_string_in_accessor_params() {
58 UserAccessor accessor = new MappingManager(session)
59 .createAccessor(UserAccessor.class);
60
61 accessor.addUserStr(0, Enum.FEMALE);
62 accessor.addUserStr(1, Enum.MALE);
63
64 assertThat(accessor.getUserStr(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE);
65 assertThat(accessor.getUserStr(Enum.MALE).one().getKey()).isEqualTo(1);
66
67 assertThat(accessor.getUserStr(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE);
68 assertThat(accessor.getUserStr(Enum.FEMALE).one().getKey()).isEqualTo(0);
69 }
70
71 @Test(groups = "short")
72 @CassandraVersion(major = 2.0, description = "Uses named parameters")
73 public void should_allow_less_parameters_than_bind_markers_if_there_are_repeated_names() {
74 UserPhoneAccessor accessor = new MappingManager(session)
75 .createAccessor(UserPhoneAccessor.class);
76
77 session.execute("delete from user where key = 0");
78 accessor.updatePhones_positional("1111", "2222", 0);
79 assertPhonesEqual(0, "1111", "2222");
80
81 session.execute("delete from user where key = 0");
82 accessor.updatePhones_named(0, "1111", "2222");
83 assertPhonesEqual(0, "1111", "2222");
84
85 session.execute("delete from user where key = 0");
86 accessor.updatePhones_fallback("1111", "2222", 0);
87 assertPhonesEqual(0, "1111", "2222");
88
89 session.execute("delete from user where key = 0");
90 accessor.updateBothPhones(0, "1111");
91 assertPhonesEqual(0, "1111", "1111");
92
93 session.execute("delete from user where key = 0");
94 accessor.updatePhones_fallback2("1111", "2222", 0);
95 assertPhonesEqual(0, "1111", "2222");
96 }
97
98 @Test(groups = "short", expectedExceptions = RuntimeException.class)
99 public void should_fail_if_not_enough_parameters() {
100 new MappingManager(session)
101 .createAccessor(UserPhoneAccessor_NotEnoughParams.class);
102 }
103
104 @Test(groups = "short", expectedExceptions = RuntimeException.class)
105 public void should_fail_if_too_many_parameters() {
106 new MappingManager(session)
107 .createAccessor(UserPhoneAccessor_TooManyParams.class);
108 }
109
110 private void assertPhonesEqual(int key, String home, String work) {
111 Row row = session.execute("select * from user where key = " + key).one();
112 assertThat(row.getString("home_phone")).isEqualTo(home);
113 assertThat(row.getString("work_phone")).isEqualTo(work);
114 }
115
116 enum Enum {
117 MALE, FEMALE
118 }
119
120 @Accessor
121 public interface UserAccessor {
122 @Query("select * from user where gender=?")
123 Result<User> getUser(@Enumerated(EnumType.ORDINAL) Enum value);
124
125 @Query("select * from user_str where gender=?")
126 Result<UserStr> getUserStr(@Enumerated(EnumType.STRING) Enum value);
127
128 @Query("insert into user (key, gender) values (?,?)")
129 ResultSet addUser(int key, @Enumerated(EnumType.ORDINAL) Enum value);
130
131 @Query("insert into user_str (key, gender) values (?,?)")
132 ResultSet addUserStr(int key, @Enumerated(EnumType.STRING) Enum value);
133 }
134
135 /**
136 * Tests various ways to match method parameters to query bind markers.
137 */
138 @Accessor
139 public interface UserPhoneAccessor {
140 /**
141 * Standard positional markers
142 */
143 @Query("update user set home_phone = ?, work_phone = ? where key = ?")
144 void updatePhones_positional(String homePhone, String workPhone, int key);
145
146 /**
147 * Standard named markers
148 */
149 @Query("update user set home_phone = :home, work_phone = :work where key = :key")
150 void updatePhones_named(@Param("key") int key, @Param("home") String homePhone, @Param("work") String workPhone);
151
152 /**
153 * Named markers with no @Param. Should fallback to positional matching
154 */
155 @Query("update user set home_phone = :home, work_phone = :work where key = :key")
156 void updatePhones_fallback(String homePhone, String workPhone, int key);
157
158 /**
159 * Named markers with repeated names
160 */
161 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
162 void updateBothPhones(@Param("key") int key, @Param("phone") String uniquePhone);
163
164 /**
165 * Named markers with repeated names, but no @Param. Should fallback to positional matching
166 */
167 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
168 void updatePhones_fallback2(String homePhone, String workPhone, int key);
169 }
170
171 @Accessor
172 public interface UserPhoneAccessor_NotEnoughParams {
173 @SuppressWarnings("unused")
174 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
175 void updateBothPhones(@Param("key") int key);
176 }
177
178 @Accessor
179 public interface UserPhoneAccessor_TooManyParams {
180 @SuppressWarnings("unused")
181 @Query("update user set home_phone = ?, work_phone = ? where key = ?")
182 void updatePhones(String homePhone, String workPhone, int key, int extra);
183 }
184
185 @Table(name = "user")
186 public static class User {
187 @PartitionKey
188 private int key;
189
190 @Enumerated(EnumType.ORDINAL)
191 private Enum gender;
192
193 public User() {
194 }
195
196 public User(int k, Enum val) {
197 this.key = k;
198 this.gender = val;
199 }
200
201 public int getKey() {
202 return this.key;
203 }
204
205 public void setKey(int pk) {
206 this.key = pk;
207 }
208
209 public Enum getGender() {
210 return this.gender;
211 }
212
213 public void setGender(Enum val) {
214 this.gender = val;
215 }
216 }
217
218 @Table(name = "user")
219 public static class UserStr {
220 @PartitionKey
221 private int key;
222
223 private Enum gender;
224
225 public UserStr() {
226 }
227
228 public UserStr(int k, Enum val) {
229 this.key = k;
230 this.gender = val;
231 }
232
233 public int getKey() {
234 return this.key;
235 }
236
237 public void setKey(int pk) {
238 this.key = pk;
239 }
240
241 public Enum getGender() {
242 return this.gender;
243 }
244
245 public void setGender(Enum val) {
246 this.gender = val;
247 }
248 }
249}