Elffuss BraidHub

kikocis/java-driver public

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