Elffuss BraidHub

kikocis/java-driver public

4a8096457983 · 8.3 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 java.util.Collection;
19
20import com.google.common.collect.Lists;
21import org.testng.annotations.Test;
22
23import static org.assertj.core.api.Assertions.assertThat;
24
25import com.datastax.driver.core.CCMBridge;
26import com.datastax.driver.core.ResultSet;
27import com.datastax.driver.core.Row;
28import com.datastax.driver.core.utils.CassandraVersion;
29import com.datastax.driver.mapping.annotations.*;
30
31public class MapperAccessorParamsTest extends CCMBridge.PerClassSingleNodeCluster {
32 @Override
33 protected Collection<String> getTableDefinitions() {
34 return Lists.newArrayList(
35 "CREATE TABLE user ( key int primary key, gender int, home_phone text, work_phone text)",
36 "CREATE INDEX on user(gender)",
37 "CREATE TABLE user_str ( key int primary key, gender text)",
38 "CREATE INDEX on user_str(gender)"
39 );
40 }
41
42 @Test(groups = "short")
43 public void should_allow_enum_as_int_in_accessor_params() {
44 UserAccessor accessor = new MappingManager(session)
45 .createAccessor(UserAccessor.class);
46
47 accessor.addUser(0, Enum.FEMALE);
48 accessor.addUser(1, Enum.MALE);
49
50 assertThat(accessor.getUser(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE);
51 assertThat(accessor.getUser(Enum.MALE).one().getKey()).isEqualTo(1);
52
53 assertThat(accessor.getUser(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE);
54 assertThat(accessor.getUser(Enum.FEMALE).one().getKey()).isEqualTo(0);
55 }
56
57 @Test(groups = "short")
58 public void should_allow_enum_as_string_in_accessor_params() {
59 UserAccessor accessor = new MappingManager(session)
60 .createAccessor(UserAccessor.class);
61
62 accessor.addUserStr(0, Enum.FEMALE);
63 accessor.addUserStr(1, Enum.MALE);
64
65 assertThat(accessor.getUserStr(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE);
66 assertThat(accessor.getUserStr(Enum.MALE).one().getKey()).isEqualTo(1);
67
68 assertThat(accessor.getUserStr(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE);
69 assertThat(accessor.getUserStr(Enum.FEMALE).one().getKey()).isEqualTo(0);
70 }
71
72 @Test(groups = "short")
73 @CassandraVersion(major = 2.0, description = "Uses named parameters")
74 public void should_allow_less_parameters_than_bind_markers_if_there_are_repeated_names() {
75 UserPhoneAccessor accessor = new MappingManager(session)
76 .createAccessor(UserPhoneAccessor.class);
77
78 session.execute("delete from user where key = 0");
79 accessor.updatePhones_positional("1111", "2222", 0);
80 assertPhonesEqual(0, "1111", "2222");
81
82 session.execute("delete from user where key = 0");
83 accessor.updatePhones_named(0, "1111", "2222");
84 assertPhonesEqual(0, "1111", "2222");
85
86 session.execute("delete from user where key = 0");
87 accessor.updatePhones_fallback("1111", "2222", 0);
88 assertPhonesEqual(0, "1111", "2222");
89
90 session.execute("delete from user where key = 0");
91 accessor.updateBothPhones(0, "1111");
92 assertPhonesEqual(0, "1111", "1111");
93
94 session.execute("delete from user where key = 0");
95 accessor.updatePhones_fallback2("1111", "2222", 0);
96 assertPhonesEqual(0, "1111", "2222");
97 }
98
99 @Test(groups = "short", expectedExceptions = RuntimeException.class)
100 public void should_fail_if_not_enough_parameters() {
101 new MappingManager(session)
102 .createAccessor(UserPhoneAccessor_NotEnoughParams.class);
103 }
104
105 @Test(groups = "short", expectedExceptions = RuntimeException.class)
106 public void should_fail_if_too_many_parameters() {
107 new MappingManager(session)
108 .createAccessor(UserPhoneAccessor_TooManyParams.class);
109 }
110
111 private void assertPhonesEqual(int key, String home, String work) {
112 Row row = session.execute("select * from user where key = " + key).one();
113 assertThat(row.getString("home_phone")).isEqualTo(home);
114 assertThat(row.getString("work_phone")).isEqualTo(work);
115 }
116
117 enum Enum {
118 MALE, FEMALE
119 }
120
121 @Accessor
122 public interface UserAccessor {
123 @Query("select * from user where gender=?")
124 Result<User> getUser(@Enumerated(EnumType.ORDINAL) Enum value);
125
126 @Query("select * from user_str where gender=?")
127 Result<UserStr> getUserStr(@Enumerated(EnumType.STRING) Enum value);
128
129 @Query("insert into user (key, gender) values (?,?)")
130 ResultSet addUser(int key, @Enumerated(EnumType.ORDINAL) Enum value);
131
132 @Query("insert into user_str (key, gender) values (?,?)")
133 ResultSet addUserStr(int key, @Enumerated(EnumType.STRING) Enum value);
134 }
135
136 /** Tests various ways to match method parameters to query bind markers. */
137 @Accessor
138 public interface UserPhoneAccessor {
139 /** Standard positional markers */
140 @Query("update user set home_phone = ?, work_phone = ? where key = ?")
141 void updatePhones_positional(String homePhone, String workPhone, int key);
142
143 /** Standard named markers */
144 @Query("update user set home_phone = :home, work_phone = :work where key = :key")
145 void updatePhones_named(@Param("key") int key, @Param("home") String homePhone, @Param("work") String workPhone);
146
147 /** Named markers with no @Param. Should fallback to positional matching */
148 @Query("update user set home_phone = :home, work_phone = :work where key = :key")
149 void updatePhones_fallback(String homePhone, String workPhone, int key);
150
151 /** Named markers with repeated names */
152 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
153 void updateBothPhones(@Param("key") int key, @Param("phone") String uniquePhone);
154
155 /** Named markers with repeated names, but no @Param. Should fallback to positional matching */
156 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
157 void updatePhones_fallback2(String homePhone, String workPhone, int key);
158 }
159
160 @Accessor
161 public interface UserPhoneAccessor_NotEnoughParams {
162 @SuppressWarnings("unused")
163 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
164 void updateBothPhones(@Param("key") int key);
165 }
166
167 @Accessor
168 public interface UserPhoneAccessor_TooManyParams {
169 @SuppressWarnings("unused")
170 @Query("update user set home_phone = ?, work_phone = ? where key = ?")
171 void updatePhones(String homePhone, String workPhone, int key, int extra);
172 }
173
174 @Table(name = "user")
175 public static class User {
176 @PartitionKey
177 private int key;
178
179 @Enumerated(EnumType.ORDINAL)
180 private Enum gender;
181
182 public User() {
183 }
184
185 public User(int k, Enum val) {
186 this.key = k;
187 this.gender = val;
188 }
189
190 public int getKey() {
191 return this.key;
192 }
193
194 public void setKey(int pk) {
195 this.key = pk;
196 }
197
198 public Enum getGender() {
199 return this.gender;
200 }
201
202 public void setGender(Enum val) {
203 this.gender = val;
204 }
205 }
206
207 @Table(name = "user")
208 public static class UserStr {
209 @PartitionKey
210 private int key;
211
212 private Enum gender;
213
214 public UserStr() {
215 }
216
217 public UserStr(int k, Enum val) {
218 this.key = k;
219 this.gender = val;
220 }
221
222 public int getKey() {
223 return this.key;
224 }
225
226 public void setKey(int pk) {
227 this.key = pk;
228 }
229
230 public Enum getGender() {
231 return this.gender;
232 }
233
234 public void setGender(Enum val) {
235 this.gender = val;
236 }
237 }
238}