Elffuss BraidHub

kikocis/java-driver public

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