Elffuss BraidHub

kikocis/java-driver public

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