Elffuss BraidHub

kikocis/java-driver public

9b39ff6a48bf · 11.1 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.*;
19import com.datastax.driver.mapping.annotations.*;
20import org.testng.annotations.BeforeClass;
21import org.testng.annotations.Test;
22
23import static org.mockito.Mockito.*;
24
25@SuppressWarnings({"unused", "WeakerAccess"})
26public class MapperInvalidAnnotationsTest {
27
28 MappingManager mappingManager;
29
30 @BeforeClass(groups = "unit")
31 public void setup() {
32 mappingManager = mock(MappingManager.class);
33 Session session = mock(Session.class);
34 when(mappingManager.getSession()).thenReturn(session);
35 Cluster cluster = mock(Cluster.class);
36 when(session.getCluster()).thenReturn(cluster);
37 Metadata metadata = mock(Metadata.class);
38 when(cluster.getMetadata()).thenReturn(metadata);
39 KeyspaceMetadata keyspace = mock(KeyspaceMetadata.class);
40 when(metadata.getKeyspace(anyString())).thenReturn(keyspace);
41
42 UserType userType = mock(UserType.class);
43 when(keyspace.getUserType(anyString())).thenReturn(userType);
44 when(userType.contains(anyString())).thenReturn(true);
45
46 TableMetadata table = mock(TableMetadata.class);
47 when(keyspace.getTable(anyString())).thenReturn(table);
48 ColumnMetadata column = mock(ColumnMetadata.class);
49 when(table.getColumn(anyString())).thenReturn(column);
50 }
51
52 static class Invalid1 {
53 }
54
55 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
56 expectedExceptionsMessageRegExp =
57 "@Table annotation was not found on class " +
58 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid1")
59 public void should_throw_IAE_when_Table_annotation_not_found_on_entity_class() throws Exception {
60 AnnotationParser.parseEntity(Invalid1.class, mappingManager);
61 }
62
63 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
64 expectedExceptionsMessageRegExp =
65 "@UDT annotation was not found on class " +
66 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid1")
67 public void should_throw_IAE_when_UDT_annotation_not_found_on_udt_class() throws Exception {
68 AnnotationParser.parseUDT(Invalid1.class, mappingManager);
69 }
70
71 @Table(name = "foo")
72 @UDT(name = "foo")
73 static class Invalid2 {
74 }
75
76 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
77 expectedExceptionsMessageRegExp =
78 "Cannot have both @Table and @UDT on class " +
79 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid2")
80 public void should_throw_IAE_when_UDT_annotation_found_on_entity_class() throws Exception {
81 AnnotationParser.parseEntity(Invalid2.class, mappingManager);
82 }
83
84 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
85 expectedExceptionsMessageRegExp =
86 "Cannot have both @UDT and @Table on class " +
87 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid2")
88 public void should_throw_IAE_when_Table_annotation_found_on_udt_class() throws Exception {
89 AnnotationParser.parseUDT(Invalid2.class, mappingManager);
90 }
91
92 @Table(name = "foo")
93 @Accessor
94 static class Invalid3 {
95 }
96
97 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
98 expectedExceptionsMessageRegExp =
99 "Cannot have both @Table and @Accessor on class " +
100 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid3")
101 public void should_throw_IAE_when_Accessor_annotation_found_on_entity_class() throws Exception {
102 AnnotationParser.parseEntity(Invalid3.class, mappingManager);
103 }
104
105 @UDT(name = "foo")
106 @Accessor
107 static class Invalid4 {
108 }
109
110 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
111 expectedExceptionsMessageRegExp =
112 "Cannot have both @UDT and @Accessor on class " +
113 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid4")
114 public void should_throw_IAE_when_Accessor_annotation_found_on_udt_class() throws Exception {
115 AnnotationParser.parseUDT(Invalid4.class, mappingManager);
116 }
117
118 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
119 expectedExceptionsMessageRegExp =
120 "@Accessor annotation is only allowed on interfaces, got class " +
121 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid4")
122 public void should_throw_IAE_when_Accessor_annotation_found_on_concrete_class() throws Exception {
123 AnnotationParser.parseAccessor(Invalid4.class, mappingManager);
124 }
125
126 interface Invalid5 {
127 }
128
129 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
130 expectedExceptionsMessageRegExp =
131 "@Accessor annotation was not found on interface " +
132 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid5")
133 public void should_throw_IAE_when_Accessor_annotation_not_found_on_accessor_class() throws Exception {
134 AnnotationParser.parseAccessor(Invalid5.class, mappingManager);
135 }
136
137 @Table(name = "foo")
138 @Accessor
139 interface Invalid6 {
140 }
141
142 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
143 expectedExceptionsMessageRegExp =
144 "Cannot have both @Accessor and @Table on interface " +
145 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid6")
146 public void should_throw_IAE_when_Table_annotation_found_on_accessor_class() throws Exception {
147 AnnotationParser.parseAccessor(Invalid6.class, mappingManager);
148 }
149
150 @UDT(name = "foo")
151 @Accessor
152 interface Invalid7 {
153 }
154
155 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
156 expectedExceptionsMessageRegExp =
157 "Cannot have both @Accessor and @UDT on interface " +
158 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid7")
159 public void should_throw_IAE_when_UDT_annotation_found_on_accessor_class() throws Exception {
160 AnnotationParser.parseAccessor(Invalid7.class, mappingManager);
161 }
162
163 @Table(name = "foo", keyspace = "ks")
164 static class Invalid8 {
165 @Field
166 int invalid;
167 }
168
169 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
170 expectedExceptionsMessageRegExp =
171 "Annotation @Field is not allowed on property 'invalid'")
172 public void should_throw_IAE_when_Field_annotation_found_on_entity_class_field() throws Exception {
173 AnnotationParser.parseEntity(Invalid8.class, mappingManager);
174 }
175
176 @UDT(name = "foo", keyspace = "ks")
177 static class Invalid9 {
178
179 int invalid;
180
181 @Column
182 public int getInvalid() {
183 return invalid;
184 }
185 }
186
187 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
188 expectedExceptionsMessageRegExp =
189 "Annotation @Column is not allowed on property 'invalid'")
190 public void should_throw_IAE_when_Column_annotation_found_on_udt_class_field() throws Exception {
191 AnnotationParser.parseUDT(Invalid9.class, mappingManager);
192 }
193
194 @Table(name = "foo", keyspace = "ks")
195 static class Invalid10 {
196
197 @PartitionKey
198 @ClusteringColumn
199 int invalid;
200
201 }
202
203 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
204 expectedExceptionsMessageRegExp =
205 "Property 'invalid' cannot be annotated with both @PartitionKey and @ClusteringColumn")
206 public void should_throw_IAE_when_PartitionKey_and_ClusteringColumn_on_same_property() throws Exception {
207 AnnotationParser.parseEntity(Invalid10.class, mappingManager);
208 }
209
210 @Table(name = "foo", keyspace = "ks")
211 static class Invalid11 {
212
213 @Computed("foo")
214 @Column
215 int invalid;
216
217 }
218
219 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
220 expectedExceptionsMessageRegExp =
221 "Property 'invalid' cannot be annotated with both @Column and @Computed")
222 public void should_throw_IAE_when_Computed_and_Column_on_same_property() throws Exception {
223 AnnotationParser.parseEntity(Invalid11.class, mappingManager);
224 }
225
226 @Table(name = "foo", keyspace = "ks")
227 static class Invalid12 {
228
229 @Computed("")
230 int invalid;
231
232 }
233
234 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
235 expectedExceptionsMessageRegExp =
236 "Property 'invalid': attribute 'value' of annotation @Computed is mandatory for computed properties")
237 public void should_throw_IAE_when_Computed_with_empty_value() throws Exception {
238 AnnotationParser.parseEntity(Invalid12.class, mappingManager);
239 }
240
241 @Table(name = "foo", keyspace = "ks")
242 static class Invalid13 {
243
244 @PartitionKey(-1)
245 int invalid;
246
247 }
248
249 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
250 expectedExceptionsMessageRegExp =
251 "Invalid ordering value -1 for annotation @PartitionKey of property 'invalid', was expecting 0")
252 public void should_throw_IAE_when_PartitionKey_with_wrong_order() throws Exception {
253 AnnotationParser.parseEntity(Invalid13.class, mappingManager);
254 }
255
256 @Table(name = "foo", keyspace = "ks")
257 static class Invalid14 {
258
259 public void setNotReadable(int i) {
260 }
261
262 }
263
264 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
265 expectedExceptionsMessageRegExp =
266 "Property 'notReadable' is not readable")
267 public void should_throw_IAE_when_unreadable_property() throws Exception {
268 AnnotationParser.parseEntity(Invalid14.class, mappingManager);
269 }
270
271 @Table(name = "foo", keyspace = "ks")
272 static class Invalid15 {
273
274 public int getNotWritable() {
275 return 0;
276 }
277
278 }
279
280 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
281 expectedExceptionsMessageRegExp =
282 "Property 'notWritable' is not writable")
283 public void should_throw_IAE_when_unwritable_property() throws Exception {
284 AnnotationParser.parseEntity(Invalid15.class, mappingManager);
285 }
286
287}