Elffuss BraidHub

kikocis/java-driver public

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