Map<String, HandlerConfigProperty> getConfigPropertiesByName(Class configClass) throws ConfigBuilderException { List<HandlerConfigProperty> properties = getConfigProperties(configClass); return properties.stream().collect(toMap(HandlerConfigProperty::getName, identity())); }
@Test(expected = ConfigBuilderException.class) public void anAnnotationOnAMethodWhichDoesNotStartWithSetThrowsAnException() throws ConfigBuilderException { configPropertyParser.getConfigProperties(ConfigBeanWithAnnotationOnMethodWhichDoesNotStartWithSet.class); }
@Test(expected = ConfigBuilderException.class) public void anAnnotationOnAMethodWithNoArgumentThrowsAnException() throws ConfigBuilderException { configPropertyParser.getConfigProperties(ConfigBeanWithAnnotationOnSetterWithNoArgument.class); }
@Test public void aBeanWithNoAnnotatedMethodsProducesAnEmptyList() throws ConfigBuilderException { List properties = configPropertyParser.getConfigProperties(ConfigBeanWithNoAnnotatedProperties.class); assertEquals(0, properties.size()); }
@Test public void testDefaultValuesWhichDoNotMatchValidationPatternThrowException() { try { configPropertyParser.getConfigProperties(ConfigBeanWithADefaultValueWhichDoesNotSatisfyValidation.class); fail("Should fail to convert"); } catch (Exception e) { System.out.println(e.getMessage()); assertEquals("The default value [the other] did not match the validation pattern [(this|that)], for ConfigProperty annotation with name prop", e.getMessage()); } }
@Test public void testAValidationPatternWhichCannotCompileThrowsException() { try { configPropertyParser.getConfigProperties(ConfigBeanWithAValidationPatternWhichCannotBeCompiled.class); fail("Should fail to convert"); } catch (Exception e) { System.out.println(e.getMessage()); assertEquals("The validation pattern '^&*(%' could not be compiled, for ConfigProperty annotation with name prop", e.getMessage()); } }
@Test public void testAValidationPatternCanBeSetWithoutADefaultValue() throws ConfigBuilderException { List<HandlerConfigProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanWithValidationPatternAndNoDefaultValue.class); HandlerConfigProperty p = properties.get(0); assertEquals("test.*", p.getValidationPattern().get().pattern()); }
@Test public void testDefaultValuesWhichCannotBeConvertedToJavaTypeThrowsException() { try { configPropertyParser.getConfigProperties(ConfigBeanWithADefaultValueWhichCannotConvertToJavaType.class); fail("Should fail to convert"); } catch (Exception e) { System.out.println(e.getMessage()); assertEquals("The default value [wibble] did not match the validation pattern [^[-+]?\\d+$], for ConfigProperty annotation with name badDefaultProperty", e.getMessage()); } }
@Test public void testConfigSettersWhichHavePrimitiveArgumentAreDescribedUsingTheEquivalentWrapperType() throws ConfigBuilderException { List<HandlerConfigProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanWithASetterWithPrimitiveTypeParameter.class); assertEquals(1, properties.size()); HandlerConfigProperty p = properties.get(0); assertTrue( p.isMandatory()); }
@Test public void aPropertyCanBeConfiguredNotMandatory() throws ConfigBuilderException { List<HandlerConfigProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanPropertyCanBeConfiguredNotMandatory.class); assertEquals(1, properties.size()); HandlerConfigProperty p = properties.get(0); assertFalse( p.isMandatory()); }
@Test public void enumFieldsCanBeParsed() throws ConfigBuilderException { List<HandlerConfigProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanWithEnumTypes.class); assertEquals(1, properties.size()); HandlerConfigProperty p = properties.get(0); assertEquals( p.getJavaType(), Scope.class); }
@Test public void aPropertyIsMandatoryByDefault() throws ConfigBuilderException { List<HandlerConfigProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanWithAValidAnnotation.class); assertEquals(1, properties.size()); HandlerConfigProperty p = properties.get(0); assertTrue( p.isMandatory()); }
@Test public void aBeanWithAValidAnnotationReturnsAConfigProperty() throws ConfigBuilderException { List<HandlerConfigProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanWithAValidAnnotation.class); assertEquals(1, properties.size()); HandlerConfigProperty p = properties.get(0); assertEquals( p.getName(), "myProperty"); assertEquals( p.getDescription(), "My Property Description"); assertFalse(p.getValidationPattern().isPresent()); assertFalse(p.getDefaultValue().isPresent()); }