| 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 java.util.Collection; |
| 19 | |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.testng.annotations.Test; |
| 22 | |
| 23 | import static org.assertj.core.api.Assertions.assertThat; |
| 24 | |
| 25 | import com.datastax.driver.core.CCMBridge; |
| 26 | import com.datastax.driver.mapping.annotations.*; |
| 27 | |
| 28 | public class MapperAccessorParamsTest extends CCMBridge.PerClassSingleNodeCluster { |
| 29 | @Override |
| 30 | protected Collection<String> getTableDefinitions() { |
| 31 | return Lists.newArrayList("CREATE TABLE user ( key int primary key, gender int)", |
| 32 | "CREATE INDEX on user(gender)"); |
| 33 | |
| 34 | } |
| 35 | |
| 36 | @Test(groups = "short") |
| 37 | void should_include_enumtype_in_accessor() { |
| 38 | Mapper<User> userMapper = new MappingManager(session).mapper(User.class); |
| 39 | userMapper.save(new User(1, 0)); |
| 40 | userMapper.save(new User(2, 1)); |
| 41 | |
| 42 | UserAccessor accessor = new MappingManager(session) |
| 43 | .createAccessor(UserAccessor.class); |
| 44 | assertThat(accessor.getUser(Enum.MALE).one().getGender()).isEqualTo(0); |
| 45 | assertThat(accessor.getUser(Enum.MALE).one().getKey()).isEqualTo(1); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | enum Enum { |
| 50 | MALE, FEMALE |
| 51 | } |
| 52 | |
| 53 | @Accessor |
| 54 | public interface UserAccessor { |
| 55 | @Query("select * from user where gender=:val") |
| 56 | Result<User> getUser(@Param("val") @Enumerated(EnumType.ORDINAL) Enum value); |
| 57 | } |
| 58 | |
| 59 | @Table(name = "user") |
| 60 | public static class User { |
| 61 | @PartitionKey |
| 62 | private int key; |
| 63 | |
| 64 | private int gender; |
| 65 | |
| 66 | public User() { |
| 67 | } |
| 68 | |
| 69 | public User(int k, int val) { |
| 70 | this.key = k; |
| 71 | this.gender = val; |
| 72 | } |
| 73 | |
| 74 | public int getKey() { |
| 75 | return this.key; |
| 76 | } |
| 77 | |
| 78 | public void setKey(int pk) { |
| 79 | this.key = pk; |
| 80 | } |
| 81 | |
| 82 | public int getGender() { |
| 83 | return this.gender; |
| 84 | } |
| 85 | |
| 86 | public void setGender(int val) { |
| 87 | this.gender = val; |
| 88 | } |
| 89 | } |
| 90 | } |