Elffuss BraidHub

kikocis/java-driver public

9b39ff6a48bf · 7.1 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.Row;
20import com.datastax.driver.core.exceptions.CodecNotFoundException;
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.List;
27
28import static org.assertj.core.api.Assertions.assertThat;
29
30@SuppressWarnings("unused")
31public class MapperAccessorParamsTest extends CCMTestsSupport {
32
33 @Override
34 public void onTestContextInitialized() {
35 execute(
36 "CREATE TABLE user ( key int primary key, gender int, home_phone text, work_phone text)",
37 "CREATE INDEX on user(gender)",
38 "CREATE TABLE user_str ( key int primary key, gender text)",
39 "CREATE INDEX on user_str(gender)"
40 );
41 }
42
43 @Test(groups = "short")
44 @CassandraVersion(value = "2.0", description = "Uses named parameters")
45 public void should_allow_less_parameters_than_bind_markers_if_there_are_repeated_names() {
46 UserPhoneAccessor accessor = new MappingManager(session())
47 .createAccessor(UserPhoneAccessor.class);
48
49 session().execute("delete from user where key = 0");
50 accessor.updatePhones_positional("1111", "2222", 0);
51 assertPhonesEqual(0, "1111", "2222");
52
53 session().execute("delete from user where key = 0");
54 accessor.updatePhones_named(0, "1111", "2222");
55 assertPhonesEqual(0, "1111", "2222");
56
57 session().execute("delete from user where key = 0");
58 accessor.updatePhones_fallback("1111", "2222", 0);
59 assertPhonesEqual(0, "1111", "2222");
60
61 session().execute("delete from user where key = 0");
62 accessor.updateBothPhones(0, "1111");
63 assertPhonesEqual(0, "1111", "1111");
64
65 session().execute("delete from user where key = 0");
66 accessor.updatePhones_fallback2("1111", "2222", 0);
67 assertPhonesEqual(0, "1111", "2222");
68 }
69
70 @Test(groups = "short", expectedExceptions = RuntimeException.class)
71 public void should_fail_if_not_enough_parameters() {
72 new MappingManager(session())
73 .createAccessor(UserPhoneAccessor_NotEnoughParams.class);
74 }
75
76 @Test(groups = "short", expectedExceptions = RuntimeException.class)
77 public void should_fail_if_too_many_parameters() {
78 new MappingManager(session())
79 .createAccessor(UserPhoneAccessor_TooManyParams.class);
80 }
81
82 /**
83 * Ensures that a wrong parameter type is detected when
84 * the accessor is called with a wrong data type.
85 *
86 * @jira_ticket JAVA-974
87 */
88 @Test(groups = "short", expectedExceptions = CodecNotFoundException.class)
89 public void should_fail_if_wrong_parameter_type() {
90 UserPhoneAccessor_WrongParameterTypes accessor = new MappingManager(session())
91 .createAccessor(UserPhoneAccessor_WrongParameterTypes.class);
92 accessor.findUsersBykeys(Lists.newArrayList(1, 2, 3));
93 }
94
95 private void assertPhonesEqual(int key, String home, String work) {
96 Row row = session().execute("select * from user where key = " + key).one();
97 assertThat(row.getString("home_phone")).isEqualTo(home);
98 assertThat(row.getString("work_phone")).isEqualTo(work);
99 }
100
101 enum Enum {
102 MALE, FEMALE
103 }
104
105 /**
106 * Tests various ways to match method parameters to query bind markers.
107 */
108 @Accessor
109 public interface UserPhoneAccessor {
110 /**
111 * Standard positional markers
112 */
113 @Query("update user set home_phone = ?, work_phone = ? where key = ?")
114 void updatePhones_positional(String homePhone, String workPhone, int key);
115
116 /**
117 * Standard named markers
118 */
119 @Query("update user set home_phone = :home, work_phone = :work where key = :key")
120 void updatePhones_named(@Param("key") int key, @Param("home") String homePhone, @Param("work") String workPhone);
121
122 /**
123 * Named markers with no @Param. Should fallback to positional matching
124 */
125 @Query("update user set home_phone = :home, work_phone = :work where key = :key")
126 void updatePhones_fallback(String homePhone, String workPhone, int key);
127
128 /**
129 * Named markers with repeated names
130 */
131 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
132 void updateBothPhones(@Param("key") int key, @Param("phone") String uniquePhone);
133
134 /**
135 * Named markers with repeated names, but no @Param. Should fallback to positional matching
136 */
137 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
138 void updatePhones_fallback2(String homePhone, String workPhone, int key);
139 }
140
141 @Accessor
142 public interface UserPhoneAccessor_NotEnoughParams {
143 @SuppressWarnings("unused")
144 @Query("update user set home_phone = :phone, work_phone = :phone where key = :key")
145 void updateBothPhones(@Param("key") int key);
146 }
147
148 @Accessor
149 public interface UserPhoneAccessor_TooManyParams {
150 @SuppressWarnings("unused")
151 @Query("update user set home_phone = ?, work_phone = ? where key = ?")
152 void updatePhones(String homePhone, String workPhone, int key, int extra);
153 }
154
155 @Table(name = "user")
156 public static class User {
157 @PartitionKey
158 private int key;
159
160 public User() {
161 }
162
163 public User(int k) {
164 this.key = k;
165 }
166
167 public int getKey() {
168 return this.key;
169 }
170
171 public void setKey(int pk) {
172 this.key = pk;
173 }
174
175 }
176
177 @Table(name = "user")
178 public static class UserStr {
179 @PartitionKey
180 private int key;
181
182 private Enum gender;
183
184 public UserStr() {
185 }
186
187 public UserStr(int k, Enum val) {
188 this.key = k;
189 this.gender = val;
190 }
191
192 public int getKey() {
193 return this.key;
194 }
195
196 public void setKey(int pk) {
197 this.key = pk;
198 }
199
200 public Enum getGender() {
201 return this.gender;
202 }
203
204 public void setGender(Enum val) {
205 this.gender = val;
206 }
207 }
208
209 @Accessor
210 public interface UserPhoneAccessor_WrongParameterTypes {
211 @Query("select * from user where key IN (?)")
212 // WRONG, should be "where key IN ?"
213 void findUsersBykeys(List<Integer> keys);
214 }
215
216}