ExplicitNullableTypeChecker { String checkValueForType(Object value, Type expectedType) { return new ExplicitNullableTypeCheckerHelper().checkValueForType(value, expectedType); }  String checkValueForAnnotatedElement(Object value,
            AnnotatedElement annotatedElement);  }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForPrimitiveType() { Assert.assertNull( explicitNullableTypeChecker.checkValueForType(0, byte.class)); Assert.assertNull( explicitNullableTypeChecker.checkValueForType(0, short.class)); Assert.assertNull( explicitNullableTypeChecker.checkValueForType(0, int.class)); Assert.assertNull( explicitNullableTypeChecker.checkValueForType(0L, long.class)); Assert.assertNull(explicitNullableTypeChecker.checkValueForType(0.0f, float.class)); Assert.assertNull(explicitNullableTypeChecker.checkValueForType(0.0d, double.class)); Assert.assertNull(explicitNullableTypeChecker.checkValueForType(true, boolean.class)); Assert.assertNull( explicitNullableTypeChecker.checkValueForType('a', char.class)); }
@Test public void should_ReturnNull_When_GivenNullValue_ForVoidType() { Assert.assertNull(explicitNullableTypeChecker.checkValueForType(null, void.class)); Assert.assertNull(explicitNullableTypeChecker.checkValueForType(null, Void.class)); }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForStringType() { Assert.assertNull(explicitNullableTypeChecker.checkValueForType("", String.class)); }
@Test public void should_ReturnError_When_GivenNullValue_ForStringType() { String error = explicitNullableTypeChecker.checkValueForType(null, String.class); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("String")); }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForDateType() { Assert.assertNull(explicitNullableTypeChecker .checkValueForType(new Date(), Date.class)); }
@Test public void should_ReturnError_When_GivenNullValue_ForDateType() { String error = explicitNullableTypeChecker.checkValueForType(null, Date.class); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("Date")); }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForDateTimeType() { Assert.assertNull(explicitNullableTypeChecker .checkValueForType(LocalDateTime.now(), LocalDateTime.class)); }
@Test public void should_ReturnError_When_GivenNullValue_ForDateTimeType() { String error = explicitNullableTypeChecker.checkValueForType(null, LocalDateTime.class); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("LocalDateTime")); }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForLocalTimeType() { Assert.assertNull(explicitNullableTypeChecker .checkValueForType(LocalTime.now(), LocalTime.class)); }
@Test public void should_ReturnError_When_GivenNullValue_ForLcalTimeType() { String error = explicitNullableTypeChecker.checkValueForType(null, LocalTime.class); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("LocalTime")); }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForMapType() { Assert.assertNull(explicitNullableTypeChecker .checkValueForType(new HashMap<String, String>(), Map.class)); }
@Test public void should_ReturnError_When_GivenNullValue_ForMapType() { String error = explicitNullableTypeChecker.checkValueForType(null, Map.class); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("Map")); }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForObjectType() { Assert.assertNull(explicitNullableTypeChecker .checkValueForType(new Object(), Object.class)); }
@Test public void should_ReturnError_When_GivenNullValue_ForObjectType() { String error = explicitNullableTypeChecker.checkValueForType(null, Object.class); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("Object")); }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForOptionalType() { Assert.assertNull(explicitNullableTypeChecker .checkValueForType(Optional.empty(), Optional.class)); }
@Test public void should_ReturnError_When_GivenNullValue_ForOptionalType() { String error = explicitNullableTypeChecker.checkValueForType(null, Optional.class); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("Optional.empty")); }
@Test public void should_ReturnNull_When_GivenNonNullValue_ForCollectionType() { Assert.assertNull(explicitNullableTypeChecker .checkValueForType(new ArrayList<String>(), stringListType)); }
@Test public void should_ReturnError_When_GivenNullValue_ForCollectionType() { String error = explicitNullableTypeChecker.checkValueForType(null, stringListType); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("List")); Assert.assertTrue(error.contains("String")); }
@Test public void should_Recursively_Check_List_Items() { ExplicitNullableTypeCheckerHelper checker = spy(helper); List<String> list = parametrizedListMethod("foo", "bar"); checker.checkValueForType(list, stringListType); verify(checker).checkValueForType(list, stringListType); verify(checker).checkValueForType("foo", String.class); verify(checker).checkValueForType("bar", String.class); }
@Test public void should_ReturnNull_When_GivenNonNullItems_InListType() { Assert.assertNull(explicitNullableTypeChecker .checkValueForType(Arrays.asList("", ""), stringListType)); }
@Test public void should_ReturnError_When_GivenNullItem_InListType() { String error = explicitNullableTypeChecker .checkValueForType(Arrays.asList("", null, ""), stringListType); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("List")); Assert.assertTrue(error.contains("String")); }
@Test public void should_Recursively_Check_Map_Values() { ExplicitNullableTypeCheckerHelper checker = spy(helper); Date dateOne = new Date(); Date dateTwo = new Date(); Map<String, Date> map = parametrizedMapMethod(dateOne, dateTwo); checker.checkValueForType(map, stringToDateMapType); verify(checker).checkValueForType(map, stringToDateMapType); verify(checker).checkValueForType(dateOne, Date.class); verify(checker).checkValueForType(dateTwo, Date.class); }
@Test public void should_ReturnNull_When_GivenNonNullValues_InMapType() { Assert.assertNull(explicitNullableTypeChecker.checkValueForType( parametrizedMapMethod(new Date(), new Date()), stringToDateMapType)); }
@Test public void should_ReturnError_When_GivenNullValues_InMapType() { String error = explicitNullableTypeChecker.checkValueForType( parametrizedMapMethod(new Date(), null), stringToDateMapType); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null value")); Assert.assertTrue(error.contains("key 'null'")); Assert.assertTrue(error.contains("Map")); Assert.assertTrue(error.contains("Date")); }
@Test public void should_Recursively_Check_GenericArray_Items() { ExplicitNullableTypeCheckerHelper checker = spy(helper); String[] array = arrayMethod("foo", "bar"); checker.checkValueForType(array, stringArrayType); verify(checker).checkValueForType(array, stringArrayType); verify(checker).checkValueForType("foo", String.class); verify(checker).checkValueForType("bar", String.class); }
@Test public void should_ReturnNull_When_GivenNonNull_BeanProperties() { final Bean bean = new Bean(); bean.setTitle("foo"); Assert.assertNull(explicitNullableTypeChecker.checkValueForType(bean, Bean.class)); }
@Test public void should_ReturnError_When_GivenNull_BeanProperty() { Bean bean = new Bean(); String error = explicitNullableTypeChecker.checkValueForType(bean, Bean.class); Assert.assertNotNull(error); Assert.assertTrue(error.contains("null")); Assert.assertTrue(error.contains("Bean")); }
@Test public void should_Recursively_Check_BeanProperties() { ExplicitNullableTypeCheckerHelper checker = spy(helper); final Bean bean = new Bean(); bean.setTitle("foo"); bean.description = "bar"; checker.checkValueForType(bean, Bean.class); verify(checker).checkValueForType(bean, Bean.class); verify(checker).checkValueForType("foo", String.class); verify(checker, never()).checkValueForType("bar", String.class); }
@Test public void should_ReturnNull_When_GivenNonNull_Generic_BeanProperties() { final Person person = new Person(); person.setName("foo"); person.setId(1); Assert.assertNull(explicitNullableTypeChecker.checkValueForType(person, Person.class)); }
@Test public void should_ReturnNull_When_GivenNonNull_CircularReference_BeanProperties() { final Employee employee = new Employee(); final Company company = new Company(); employee.setId(1); employee.setCompany(company); company.setId(1); company.setEmployees(Arrays.asList(employee)); Assert.assertNull(explicitNullableTypeChecker.checkValueForType(employee, Employee.class)); Assert.assertNull(explicitNullableTypeChecker.checkValueForType(company, Company.class)); }