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