| 1 | package com.datastax.driver.mapping; |
| 2 | |
| 3 | import java.lang.reflect.Field; |
| 4 | import java.util.List; |
| 5 | import java.util.Map; |
| 6 | |
| 7 | import com.datastax.driver.mapping.annotations.Frozen; |
| 8 | import com.datastax.driver.mapping.annotations.FrozenValue; |
| 9 | import org.testng.annotations.Test; |
| 10 | |
| 11 | import static org.testng.Assert.fail; |
| 12 | |
| 13 | public class AnnotationChecksTest { |
| 14 | // Dummy UDT class: |
| 15 | public class User { |
| 16 | } |
| 17 | |
| 18 | // Dummy fields to run our checks on: |
| 19 | String string; |
| 20 | @Frozen |
| 21 | String frozenString; |
| 22 | List<String> listOfStrings; |
| 23 | User unfrozenUser; |
| 24 | @Frozen |
| 25 | User frozenUser; |
| 26 | List<User> listOfUnfrozenUsers; |
| 27 | @FrozenValue |
| 28 | List<User> listOfFrozenUsers; |
| 29 | Map<String, Map<String, User>> deeplyNestedUnfrozenUser; |
| 30 | @Frozen("map<text, map<text, frozen<user>>>") |
| 31 | Map<String, Map<String, User>> deeplyNestedFrozenUser; |
| 32 | |
| 33 | @Test(groups = "unit") |
| 34 | public void checkFrozenTypesTest() throws Exception { |
| 35 | checkFrozenTypesTest("string", true); |
| 36 | checkFrozenTypesTest("frozenString", false); |
| 37 | checkFrozenTypesTest("listOfStrings", true); |
| 38 | checkFrozenTypesTest("unfrozenUser", false); |
| 39 | checkFrozenTypesTest("frozenUser", true); |
| 40 | checkFrozenTypesTest("listOfUnfrozenUsers", false); |
| 41 | checkFrozenTypesTest("listOfFrozenUsers", true); |
| 42 | checkFrozenTypesTest("deeplyNestedUnfrozenUser", false); |
| 43 | checkFrozenTypesTest("deeplyNestedFrozenUser", true); |
| 44 | } |
| 45 | |
| 46 | private void checkFrozenTypesTest(String fieldName, boolean expectSuccess) throws Exception { |
| 47 | Field field = AnnotationChecksTest.class.getDeclaredField(fieldName); |
| 48 | Exception exception = null; |
| 49 | try { |
| 50 | AnnotationChecks.checkFrozenTypes(field); |
| 51 | } catch (IllegalArgumentException e) { |
| 52 | exception = e; |
| 53 | } |
| 54 | if (expectSuccess && exception != null) |
| 55 | fail("expected check to succeed but got exception " + exception.getMessage()); |
| 56 | else if (!expectSuccess && exception == null) |
| 57 | fail("expected check to fail but got no exception"); |
| 58 | } |
| 59 | } |