Elffuss BraidHub

kikocis/java-driver public

7912be4998a7 · 7.3 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2014 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.*;
19
20import com.datastax.driver.core.Session;
21import com.datastax.driver.core.utils.CassandraVersion;
22import com.datastax.driver.mapping.annotations.*;
23import com.google.common.base.Objects;
24import org.testng.annotations.Test;
25
26import static org.testng.Assert.assertEquals;
27import static org.testng.Assert.assertNull;
28
29import com.datastax.driver.core.CCMBridge;
30import com.datastax.driver.core.utils.UUIDs;
31
32/**
33 * Tests usage of mapping annotations without specifying a keyspace.
34 */
35@CassandraVersion(major=2.1)
36public class MapperDefaultKeyspaceTest extends CCMBridge.PerClassSingleNodeCluster {
37 protected Collection<String> getTableDefinitions() {
38 return Arrays.asList("CREATE TABLE groups (group_id uuid PRIMARY KEY, name text)",
39 "CREATE TYPE group_name (name text)",
40 "CREATE TABLE groups2 (group_id uuid PRIMARY KEY, name frozen<group_name>)");
41 }
42
43 /*
44 * An entity that does not specify a keyspace in its @Table annotation. When a keyspace is
45 * not specified, the mapper uses the session's logged in keyspace.
46 */
47 @Table(name = "groups")
48 public static class Group {
49
50 @PartitionKey
51 @Column(name = "group_id")
52 private UUID groupId;
53
54 private String name;
55
56 public Group() {}
57
58 public Group(String name) {
59 this.name = name;
60 this.groupId = UUIDs.random();
61 }
62
63 public UUID getGroupId() {
64 return groupId;
65 }
66
67 public void setGroupId(UUID groupId) {
68 this.groupId = groupId;
69 }
70
71 public String getName() {
72 return name;
73 }
74
75 public void setName(String name) {
76 this.name = name;
77 }
78
79 @Override
80 public boolean equals(Object other) {
81 if (other == null || other.getClass() != this.getClass())
82 return false;
83
84 Group that = (Group)other;
85 return Objects.equal(groupId, that.groupId)
86 && Objects.equal(name, that.name);
87 }
88
89 @Override
90 public int hashCode() {
91 return Objects.hashCode(groupId, name);
92 }
93 }
94
95 @Test(groups = "short")
96 public void testTableWithDefaultKeyspace() throws Exception {
97 // Ensure that the test session is logged into the "ks" keyspace.
98 session.execute("USE ks");
99
100 MappingManager manager = new MappingManager(session);
101 Mapper<Group> m = manager.mapper(Group.class);
102 Group group = new Group("testGroup");
103 UUID groupId = group.getGroupId();
104
105 // Check the save operation.
106 m.save(group);
107
108 // Check the select operation.
109 Group selectedGroup = m.get(groupId);
110 assertEquals(selectedGroup.getGroupId(), groupId);
111
112 // Check the delete operation.
113 m.delete(group);
114 assertNull(m.get(groupId));
115 }
116
117 @Table(keyspace = "ks", name = "groups2")
118 public static class Group2 {
119
120 @PartitionKey
121 @Column(name = "group_id")
122 private UUID groupId;
123
124 @Frozen
125 private GroupName name;
126
127 public Group2() {}
128
129 public Group2(GroupName name) {
130 this.name = name;
131 this.groupId = UUIDs.random();
132 }
133
134 public UUID getGroupId() {
135 return groupId;
136 }
137
138 public void setGroupId(UUID groupId) {
139 this.groupId = groupId;
140 }
141
142 public GroupName getName() {
143 return name;
144 }
145
146 public void setName(GroupName name) {
147 this.name = name;
148 }
149
150 @Override
151 public boolean equals(Object other) {
152 if (other == null || other.getClass() != this.getClass())
153 return false;
154
155 Group that = (Group)other;
156 return Objects.equal(groupId, that.groupId)
157 && Objects.equal(name, that.name);
158 }
159
160 @Override
161 public int hashCode() {
162 return Objects.hashCode(groupId, name);
163 }
164 }
165
166 /*
167 * User defined type without a keyspace specified. The mapper will use the session's logged
168 * keyspace when a keyspace is not specified in the @UDT annotation.
169 */
170 @UDT(name = "group_name")
171 public static class GroupName {
172 private String name;
173
174 public GroupName() {
175 }
176
177 public GroupName(String name) {
178 this.name = name;
179 }
180
181 public String getName() {
182 return name;
183 }
184
185 public void setName(String name) {
186 this.name = name;
187 }
188
189 @Override
190 public boolean equals(Object other) {
191 if (other instanceof GroupName) {
192 GroupName that = (GroupName) other;
193 return this.name == that.name;
194 }
195 return false;
196 }
197
198 @Override
199 public int hashCode() {
200 return Objects.hashCode(name);
201 }
202 }
203
204 @Test(groups = "short")
205 public void testUDTWithDefaultKeyspace() throws Exception {
206 // Ensure that the test session is logged into the "ks" keyspace.
207 session.execute("USE ks");
208
209 MappingManager manager = new MappingManager(session);
210 Mapper<Group2> m = manager.mapper(Group2.class);
211 Group2 group = new Group2(new GroupName("testGroup"));
212 UUID groupId = group.getGroupId();
213
214 // Check the save operation.
215 m.save(group);
216
217 // Check the select operation.
218 Group2 selectedGroup = m.get(groupId);
219 assertEquals(selectedGroup.getGroupId(), groupId);
220
221 // Check the delete operation.
222 m.delete(group);
223 assertNull(m.get(groupId));
224 }
225
226 @Test(groups = "short",
227 expectedExceptions = IllegalArgumentException.class,
228 expectedExceptionsMessageRegExp = "Error creating mapper for class Group, the @Table annotation declares no default keyspace, and the session is not currently logged to any keyspace")
229 public void should_throw_a_meaningful_error_message_when_no_default_table_keyspace_and_session_not_logged() {
230 Session session2 = cluster.connect();
231 MappingManager manager = new MappingManager(session2);
232 manager.mapper(Group.class);
233 }
234
235 @Test(groups = "short",
236 expectedExceptions = IllegalArgumentException.class,
237 expectedExceptionsMessageRegExp = "Error creating UDT mapper for class GroupName, the @UDT annotation declares no default keyspace, and the session is not currently logged to any keyspace")
238 public void should_throw_a_meaningful_error_message_when_no_default_udt_keyspace_and_session_not_logged() {
239 Session session2 = cluster.connect();
240 MappingManager manager = new MappingManager(session2);
241 manager.udtMapper(GroupName.class);
242 }
243}