Elffuss BraidHub

kikocis/java-driver public

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