| 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.CCMConfig; |
| 19 | import com.datastax.driver.core.CCMTestsSupport; |
| 20 | import com.datastax.driver.core.TestUtils; |
| 21 | import com.datastax.driver.core.utils.CassandraVersion; |
| 22 | import com.datastax.driver.mapping.annotations.*; |
| 23 | import com.google.common.base.Objects; |
| 24 | import org.testng.annotations.Test; |
| 25 | |
| 26 | import static com.datastax.driver.core.Metadata.quote; |
| 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 28 | |
| 29 | @CCMConfig(createKeyspace = false) |
| 30 | @CassandraVersion("2.1") |
| 31 | public class MapperCaseSensitivityTest extends CCMTestsSupport { |
| 32 | |
| 33 | static final String KS = "ks_MapperCaseSensitivityTest"; |
| 34 | static final String TABLE = "table_MapperCaseSensitivityTest"; |
| 35 | static final String TYPE = "udt_MapperCaseSensitivityTest"; |
| 36 | |
| 37 | UserNoKeyspace user = new UserNoKeyspace("id", new Address("street", "zip")); |
| 38 | |
| 39 | @Table(keyspace = KS, name = TABLE, caseSensitiveKeyspace = true, caseSensitiveTable = true) |
| 40 | static class User { |
| 41 | |
| 42 | @PartitionKey |
| 43 | @Column(caseSensitive = true) |
| 44 | private String userId; |
| 45 | |
| 46 | @Column(name = "Address", caseSensitive = true) |
| 47 | private Address address; |
| 48 | |
| 49 | public User() { |
| 50 | } |
| 51 | |
| 52 | public User(String userId, Address address) { |
| 53 | this.userId = userId; |
| 54 | this.address = address; |
| 55 | } |
| 56 | |
| 57 | public String getUserId() { |
| 58 | return userId; |
| 59 | } |
| 60 | |
| 61 | public void setUserId(String userId) { |
| 62 | this.userId = userId; |
| 63 | } |
| 64 | |
| 65 | public Address getAddress() { |
| 66 | return address; |
| 67 | } |
| 68 | |
| 69 | public void setAddress(Address address) { |
| 70 | this.address = address; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean equals(Object o) { |
| 75 | if (this == o) return true; |
| 76 | if (o == null || !(o instanceof User)) return false; |
| 77 | User user = (User) o; |
| 78 | return Objects.equal(userId, user.userId) && |
| 79 | Objects.equal(address, user.address); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int hashCode() { |
| 84 | return Objects.hashCode(userId, address); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @UDT(keyspace = KS, name = TYPE, caseSensitiveKeyspace = true, caseSensitiveType = true) |
| 89 | static class Address { |
| 90 | |
| 91 | @Field(name = "Street", caseSensitive = true) |
| 92 | private String street; |
| 93 | |
| 94 | @Field(caseSensitive = true) |
| 95 | private String zipCode; |
| 96 | |
| 97 | public Address() { |
| 98 | } |
| 99 | |
| 100 | public Address(String street, String zipCode) { |
| 101 | this.street = street; |
| 102 | this.zipCode = zipCode; |
| 103 | } |
| 104 | |
| 105 | public String getStreet() { |
| 106 | return street; |
| 107 | } |
| 108 | |
| 109 | public void setStreet(String street) { |
| 110 | this.street = street; |
| 111 | } |
| 112 | |
| 113 | public String getZipCode() { |
| 114 | return zipCode; |
| 115 | } |
| 116 | |
| 117 | public void setZipCode(String zipCode) { |
| 118 | this.zipCode = zipCode; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean equals(Object o) { |
| 123 | if (this == o) return true; |
| 124 | if (o == null || !(o instanceof Address)) return false; |
| 125 | Address address = (Address) o; |
| 126 | return Objects.equal(street, address.street) && |
| 127 | Objects.equal(zipCode, address.zipCode); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public int hashCode() { |
| 132 | return Objects.hashCode(getStreet(), getZipCode()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Table(name = TABLE, caseSensitiveKeyspace = true, caseSensitiveTable = true) |
| 137 | static class UserNoKeyspace extends User { |
| 138 | |
| 139 | public UserNoKeyspace() { |
| 140 | } |
| 141 | |
| 142 | public UserNoKeyspace(String id, Address address) { |
| 143 | super(id, address); |
| 144 | } |
| 145 | |
| 146 | } |
| 147 | |
| 148 | @UDT(name = TYPE, caseSensitiveKeyspace = true, caseSensitiveType = true) |
| 149 | static class AddressNoKeyspace extends Address { |
| 150 | |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onTestContextInitialized() { |
| 155 | execute(String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, quote(KS), 1)); |
| 156 | useKeyspace(quote(KS)); |
| 157 | execute( |
| 158 | "CREATE TYPE " + quote(TYPE) + " (\"Street\" text, \"zipCode\" text)", |
| 159 | "CREATE TABLE " + quote(TABLE) + " (\"userId\" text PRIMARY KEY, \"Address\" frozen<" + quote(TYPE) + ">)"); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Validates that case sensitive identifiers for fields, types, columns, tables and keyspaces work with the mapper |
| 164 | * when the keyspace name is specified on the mapped class. |
| 165 | * |
| 166 | * @jira_ticket JAVA-564 |
| 167 | * @test_category object_mapper |
| 168 | */ |
| 169 | @Test(groups = "short") |
| 170 | public void should_handle_case_sensitive_identifiers_when_keyspace_specified() throws Exception { |
| 171 | MappingManager mappingManager = new MappingManager(session()); |
| 172 | Mapper<User> mapper = mappingManager.mapper(User.class); |
| 173 | mapper.save(user); |
| 174 | assertThat(mapper.get(user.getUserId())).isEqualTo(user); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Validates that case sensitive identifiers for fields, types, columns, tables and keyspaces work with the mapper |
| 179 | * when the keyspace name is *not* specified on the mapped class. |
| 180 | * |
| 181 | * @jira_ticket JAVA-564 |
| 182 | * @test_category object_mapper |
| 183 | */ |
| 184 | @Test(groups = "short") |
| 185 | public void should_handle_case_sensitive_identifiers_without_keyspace_specified() throws Exception { |
| 186 | MappingManager mappingManager = new MappingManager(session()); |
| 187 | Mapper<UserNoKeyspace> mapper = mappingManager.mapper(UserNoKeyspace.class); |
| 188 | mapper.save(user); |
| 189 | assertThat(mapper.get(user.getUserId())).isEqualTo(user); |
| 190 | } |
| 191 | |
| 192 | } |