Elffuss BraidHub

kikocis/java-driver public

0518208fb53c · 10.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 @Table(name = "foo")
56 @UDT(name = "foo")
57 static class Invalid2 {
58 }
59
60 @Table(name = "foo")
61 @Accessor
62 static class Invalid3 {
63 }
64
65 @UDT(name = "foo")
66 @Accessor
67 static class Invalid4 {
68 }
69
70 interface Invalid5 {
71 }
72
73 @Table(name = "foo")
74 @Accessor
75 interface Invalid6 {
76 }
77
78 @UDT(name = "foo")
79 @Accessor
80 interface Invalid7 {
81 }
82
83 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
84 expectedExceptionsMessageRegExp =
85 "@Table annotation was not found on class " +
86 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid1")
87 public void should_throw_IAE_when_Table_annotation_not_found_on_entity_class() throws Exception {
88 AnnotationParser.parseEntity(Invalid1.class, mappingManager);
89 }
90
91 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
92 expectedExceptionsMessageRegExp =
93 "Cannot have both @Table and @UDT on class " +
94 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid2")
95 public void should_throw_IAE_when_UDT_annotation_found_on_entity_class() throws Exception {
96 AnnotationParser.parseEntity(Invalid2.class, mappingManager);
97 }
98
99 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
100 expectedExceptionsMessageRegExp =
101 "Cannot have both @Table and @Accessor on class " +
102 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid3")
103 public void should_throw_IAE_when_Accessor_annotation_found_on_entity_class() throws Exception {
104 AnnotationParser.parseEntity(Invalid3.class, mappingManager);
105 }
106
107 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
108 expectedExceptionsMessageRegExp =
109 "@UDT annotation was not found on class " +
110 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid1")
111 public void should_throw_IAE_when_UDT_annotation_not_found_on_udt_class() throws Exception {
112 AnnotationParser.parseUDT(Invalid1.class, mappingManager);
113 }
114
115 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
116 expectedExceptionsMessageRegExp =
117 "Cannot have both @UDT and @Table on class " +
118 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid2")
119 public void should_throw_IAE_when_Table_annotation_found_on_udt_class() throws Exception {
120 AnnotationParser.parseUDT(Invalid2.class, mappingManager);
121 }
122
123 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
124 expectedExceptionsMessageRegExp =
125 "Cannot have both @UDT and @Accessor on class " +
126 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid4")
127 public void should_throw_IAE_when_Accessor_annotation_found_on_udt_class() throws Exception {
128 AnnotationParser.parseUDT(Invalid4.class, mappingManager);
129 }
130
131 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
132 expectedExceptionsMessageRegExp =
133 "@Accessor annotation was not found on interface " +
134 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid5")
135 public void should_throw_IAE_when_Accessor_annotation_not_found_on_accessor_class() throws Exception {
136 AnnotationParser.parseAccessor(Invalid5.class, mappingManager);
137 }
138
139 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
140 expectedExceptionsMessageRegExp =
141 "Cannot have both @Accessor and @Table on interface " +
142 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid6")
143 public void should_throw_IAE_when_Table_annotation_found_on_accessor_class() throws Exception {
144 AnnotationParser.parseAccessor(Invalid6.class, mappingManager);
145 }
146
147 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
148 expectedExceptionsMessageRegExp =
149 "Cannot have both @Accessor and @UDT on interface " +
150 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid7")
151 public void should_throw_IAE_when_UDT_annotation_found_on_accessor_class() throws Exception {
152 AnnotationParser.parseAccessor(Invalid7.class, mappingManager);
153 }
154
155 @Test(groups = "unit", expectedExceptions = IllegalArgumentException.class,
156 expectedExceptionsMessageRegExp =
157 "@Accessor annotation is only allowed on interfaces, got class " +
158 "com.datastax.driver.mapping.MapperInvalidAnnotationsTest\\$Invalid4")
159 public void should_throw_IAE_when_Accessor_annotation_found_on_concrete_class() throws Exception {
160 AnnotationParser.parseAccessor(Invalid4.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_not_allow_Field_on_entity_class() 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_not_allow_Column_on_udt_class() 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_not_allow_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_not_allow_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_not_allow_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_not_allow_PartitionKey_with_wrong_order() throws Exception {
253 AnnotationParser.parseEntity(Invalid13.class, mappingManager);
254 }
255
256}