Elffuss BraidHub

kikocis/java-driver public

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