Elffuss BraidHub

kikocis/java-driver public

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