| 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 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.CCMTestsSupport; |
| 19 | import com.datastax.driver.core.ResultSet; |
| 20 | import com.datastax.driver.core.Row; |
| 21 | import com.datastax.driver.core.utils.CassandraVersion; |
| 22 | import com.datastax.driver.mapping.annotations.*; |
| 23 | import org.testng.annotations.Test; |
| 24 | |
| 25 | import static org.assertj.core.api.Assertions.assertThat; |
| 26 | |
| 27 | @SuppressWarnings("unused") |
| 28 | public class MapperAccessorParamsTest extends CCMTestsSupport { |
| 29 | |
| 30 | @Override |
| 31 | public void onTestContextInitialized() { |
| 32 | execute( |
| 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 | public void should_allow_enum_as_int_in_accessor_params() { |
| 42 | UserAccessor accessor = new MappingManager(session()) |
| 43 | .createAccessor(UserAccessor.class); |
| 44 | |
| 45 | accessor.addUser(0, Enum.FEMALE); |
| 46 | accessor.addUser(1, Enum.MALE); |
| 47 | |
| 48 | assertThat(accessor.getUser(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE); |
| 49 | assertThat(accessor.getUser(Enum.MALE).one().getKey()).isEqualTo(1); |
| 50 | |
| 51 | assertThat(accessor.getUser(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE); |
| 52 | assertThat(accessor.getUser(Enum.FEMALE).one().getKey()).isEqualTo(0); |
| 53 | } |
| 54 | |
| 55 | @Test(groups = "short") |
| 56 | public void should_allow_enum_as_string_in_accessor_params() { |
| 57 | UserAccessor accessor = new MappingManager(session()) |
| 58 | .createAccessor(UserAccessor.class); |
| 59 | |
| 60 | accessor.addUserStr(0, Enum.FEMALE); |
| 61 | accessor.addUserStr(1, Enum.MALE); |
| 62 | |
| 63 | assertThat(accessor.getUserStr(Enum.MALE).one().getGender()).isEqualTo(Enum.MALE); |
| 64 | assertThat(accessor.getUserStr(Enum.MALE).one().getKey()).isEqualTo(1); |
| 65 | |
| 66 | assertThat(accessor.getUserStr(Enum.FEMALE).one().getGender()).isEqualTo(Enum.FEMALE); |
| 67 | assertThat(accessor.getUserStr(Enum.FEMALE).one().getKey()).isEqualTo(0); |
| 68 | } |
| 69 | |
| 70 | @Test(groups = "short") |
| 71 | @CassandraVersion(major = 2.0, description = "Uses named parameters") |
| 72 | public void should_allow_less_parameters_than_bind_markers_if_there_are_repeated_names() { |
| 73 | UserPhoneAccessor accessor = new MappingManager(session()) |
| 74 | .createAccessor(UserPhoneAccessor.class); |
| 75 | |
| 76 | session().execute("delete from user where key = 0"); |
| 77 | accessor.updatePhones_positional("1111", "2222", 0); |
| 78 | assertPhonesEqual(0, "1111", "2222"); |
| 79 | |
| 80 | session().execute("delete from user where key = 0"); |
| 81 | accessor.updatePhones_named(0, "1111", "2222"); |
| 82 | assertPhonesEqual(0, "1111", "2222"); |
| 83 | |
| 84 | session().execute("delete from user where key = 0"); |
| 85 | accessor.updatePhones_fallback("1111", "2222", 0); |
| 86 | assertPhonesEqual(0, "1111", "2222"); |
| 87 | |
| 88 | session().execute("delete from user where key = 0"); |
| 89 | accessor.updateBothPhones(0, "1111"); |
| 90 | assertPhonesEqual(0, "1111", "1111"); |
| 91 | |
| 92 | session().execute("delete from user where key = 0"); |
| 93 | accessor.updatePhones_fallback2("1111", "2222", 0); |
| 94 | assertPhonesEqual(0, "1111", "2222"); |
| 95 | } |
| 96 | |
| 97 | @Test(groups = "short", expectedExceptions = RuntimeException.class) |
| 98 | public void should_fail_if_not_enough_parameters() { |
| 99 | new MappingManager(session()) |
| 100 | .createAccessor(UserPhoneAccessor_NotEnoughParams.class); |
| 101 | } |
| 102 | |
| 103 | @Test(groups = "short", expectedExceptions = RuntimeException.class) |
| 104 | public void should_fail_if_too_many_parameters() { |
| 105 | new MappingManager(session()) |
| 106 | .createAccessor(UserPhoneAccessor_TooManyParams.class); |
| 107 | } |
| 108 | |
| 109 | private void assertPhonesEqual(int key, String home, String work) { |
| 110 | Row row = session().execute("select * from user where key = " + key).one(); |
| 111 | assertThat(row.getString("home_phone")).isEqualTo(home); |
| 112 | assertThat(row.getString("work_phone")).isEqualTo(work); |
| 113 | } |
| 114 | |
| 115 | enum Enum { |
| 116 | MALE, FEMALE |
| 117 | } |
| 118 | |
| 119 | @Accessor |
| 120 | public interface UserAccessor { |
| 121 | @Query("select * from user where gender=?") |
| 122 | Result<User> getUser(@Enumerated(EnumType.ORDINAL) Enum value); |
| 123 | |
| 124 | @Query("select * from user_str where gender=?") |
| 125 | Result<UserStr> getUserStr(@Enumerated(EnumType.STRING) Enum value); |
| 126 | |
| 127 | @Query("insert into user (key, gender) values (?,?)") |
| 128 | ResultSet addUser(int key, @Enumerated(EnumType.ORDINAL) Enum value); |
| 129 | |
| 130 | @Query("insert into user_str (key, gender) values (?,?)") |
| 131 | ResultSet addUserStr(int key, @Enumerated(EnumType.STRING) Enum value); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests various ways to match method parameters to query bind markers. |
| 136 | */ |
| 137 | @Accessor |
| 138 | public interface UserPhoneAccessor { |
| 139 | /** |
| 140 | * Standard positional markers |
| 141 | */ |
| 142 | @Query("update user set home_phone = ?, work_phone = ? where key = ?") |
| 143 | void updatePhones_positional(String homePhone, String workPhone, int key); |
| 144 | |
| 145 | /** |
| 146 | * Standard named markers |
| 147 | */ |
| 148 | @Query("update user set home_phone = :home, work_phone = :work where key = :key") |
| 149 | void updatePhones_named(@Param("key") int key, @Param("home") String homePhone, @Param("work") String workPhone); |
| 150 | |
| 151 | /** |
| 152 | * Named markers with no @Param. Should fallback to positional matching |
| 153 | */ |
| 154 | @Query("update user set home_phone = :home, work_phone = :work where key = :key") |
| 155 | void updatePhones_fallback(String homePhone, String workPhone, int key); |
| 156 | |
| 157 | /** |
| 158 | * Named markers with repeated names |
| 159 | */ |
| 160 | @Query("update user set home_phone = :phone, work_phone = :phone where key = :key") |
| 161 | void updateBothPhones(@Param("key") int key, @Param("phone") String uniquePhone); |
| 162 | |
| 163 | /** |
| 164 | * Named markers with repeated names, but no @Param. Should fallback to positional matching |
| 165 | */ |
| 166 | @Query("update user set home_phone = :phone, work_phone = :phone where key = :key") |
| 167 | void updatePhones_fallback2(String homePhone, String workPhone, int key); |
| 168 | } |
| 169 | |
| 170 | @Accessor |
| 171 | public interface UserPhoneAccessor_NotEnoughParams { |
| 172 | @SuppressWarnings("unused") |
| 173 | @Query("update user set home_phone = :phone, work_phone = :phone where key = :key") |
| 174 | void updateBothPhones(@Param("key") int key); |
| 175 | } |
| 176 | |
| 177 | @Accessor |
| 178 | public interface UserPhoneAccessor_TooManyParams { |
| 179 | @SuppressWarnings("unused") |
| 180 | @Query("update user set home_phone = ?, work_phone = ? where key = ?") |
| 181 | void updatePhones(String homePhone, String workPhone, int key, int extra); |
| 182 | } |
| 183 | |
| 184 | @Table(name = "user") |
| 185 | public static class User { |
| 186 | @PartitionKey |
| 187 | private int key; |
| 188 | |
| 189 | @Enumerated(EnumType.ORDINAL) |
| 190 | private Enum gender; |
| 191 | |
| 192 | public User() { |
| 193 | } |
| 194 | |
| 195 | public User(int k, Enum val) { |
| 196 | this.key = k; |
| 197 | this.gender = val; |
| 198 | } |
| 199 | |
| 200 | public int getKey() { |
| 201 | return this.key; |
| 202 | } |
| 203 | |
| 204 | public void setKey(int pk) { |
| 205 | this.key = pk; |
| 206 | } |
| 207 | |
| 208 | public Enum getGender() { |
| 209 | return this.gender; |
| 210 | } |
| 211 | |
| 212 | public void setGender(Enum val) { |
| 213 | this.gender = val; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | @Table(name = "user") |
| 218 | public static class UserStr { |
| 219 | @PartitionKey |
| 220 | private int key; |
| 221 | |
| 222 | private Enum gender; |
| 223 | |
| 224 | public UserStr() { |
| 225 | } |
| 226 | |
| 227 | public UserStr(int k, Enum val) { |
| 228 | this.key = k; |
| 229 | this.gender = val; |
| 230 | } |
| 231 | |
| 232 | public int getKey() { |
| 233 | return this.key; |
| 234 | } |
| 235 | |
| 236 | public void setKey(int pk) { |
| 237 | this.key = pk; |
| 238 | } |
| 239 | |
| 240 | public Enum getGender() { |
| 241 | return this.gender; |
| 242 | } |
| 243 | |
| 244 | public void setGender(Enum val) { |
| 245 | this.gender = val; |
| 246 | } |
| 247 | } |
| 248 | } |