Elffuss BraidHub

kikocis/java-driver public

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