Elffuss BraidHub

kikocis/java-driver public

787c0e2ddd14 · 3.6 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 java.lang.reflect.Field;
19import java.util.List;
20import java.util.Map;
21import java.util.Set;
22
23import com.datastax.driver.mapping.annotations.Frozen;
24import com.datastax.driver.mapping.annotations.FrozenValue;
25import com.datastax.driver.mapping.annotations.UDT;
26
27import org.testng.annotations.Test;
28
29import static org.testng.Assert.fail;
30
31public class AnnotationChecksTest {
32 // Dummy UDT class:
33 @UDT(name = "user")
34 public class User {
35 }
36
37 // Dummy fields to run our checks on:
38 String string;
39 @Frozen
40 String frozenString;
41 List<String> listOfStrings;
42 User unfrozenUser;
43 @Frozen
44 User frozenUser;
45 List<User> listOfUnfrozenUsers;
46 @FrozenValue
47 List<User> listOfFrozenUsers;
48 Map<String, Map<String, User>> deeplyNestedUnfrozenUser;
49 @Frozen("map<text, frozen<map<text, frozen<user>>>>")
50 Map<String, Map<String, User>> deeplyNestedFrozenUser;
51
52 Map<String, List<Integer>> mapOfUnfrozenListOfInt;
53 @FrozenValue
54 Map<String, List<Integer>> mapOfFrozenListOfInt;
55 @FrozenValue
56 Map<Integer, List<Set<Integer>>> deeplyNestedUnfrozenSet;
57 @Frozen("map<int, frozen<list<set<int>>>>")
58 Map<Integer, List<Set<Integer>>> deeplyNestedUnfrozenSet2;
59 @Frozen("map<int, frozen<list<frozen<set<int>>>>>")
60 Map<Integer, List<Set<Integer>>> deeplyNestedFrozenSet;
61
62 @Test(groups = "unit")
63 public void should_fail_if_udt_not_frozen() throws Exception {
64 checkFrozenTypesTest("string", true);
65 checkFrozenTypesTest("frozenString", false);
66 checkFrozenTypesTest("listOfStrings", true);
67 checkFrozenTypesTest("unfrozenUser", false);
68 checkFrozenTypesTest("frozenUser", true);
69 checkFrozenTypesTest("listOfUnfrozenUsers", false);
70 checkFrozenTypesTest("listOfFrozenUsers", true);
71 checkFrozenTypesTest("deeplyNestedUnfrozenUser", false);
72 checkFrozenTypesTest("deeplyNestedFrozenUser", true);
73 }
74
75 @Test(groups = "unit")
76 public void should_fail_if_nested_collection_not_frozen() throws Exception {
77 checkFrozenTypesTest("mapOfUnfrozenListOfInt", false);
78 checkFrozenTypesTest("mapOfFrozenListOfInt", true);
79 checkFrozenTypesTest("deeplyNestedUnfrozenSet", false);
80 checkFrozenTypesTest("deeplyNestedUnfrozenSet2", false);
81 checkFrozenTypesTest("deeplyNestedFrozenSet", true);
82 }
83
84 private void checkFrozenTypesTest(String fieldName, boolean expectSuccess) throws Exception {
85 Field field = AnnotationChecksTest.class.getDeclaredField(fieldName);
86 Exception exception = null;
87 try {
88 AnnotationChecks.checkFrozenTypes(field);
89 } catch (IllegalArgumentException e) {
90 exception = e;
91 }
92 if (expectSuccess && exception != null)
93 fail("expected check to succeed but got exception " + exception.getMessage());
94 else if (!expectSuccess && exception == null)
95 fail("expected check to fail but got no exception");
96 }
97}