ConfigPropertyParser { public List<ConfigurationProperty> getConfigProperties(Class configClass) throws ConfigBuilderException { Method[] methods = getMethodsFromConfigClass(configClass); List<ConfigurationProperty> result = new LinkedList<>(); List<Method> l = Arrays.stream(methods) .filter(m -> m.isAnnotationPresent(ConfigProperty.class)) .collect(Collectors.toList()); for (Method m : l) { addConfigProperty(result, m); } return result; }  List<ConfigurationProperty> getConfigProperties(Class configClass);  }
@Test public void aBeanWithNoAnnotatedMethodsProducesAnEmptyList() throws ConfigBuilderException { List properties = configPropertyParser.getConfigProperties(ConfigBeanWithNoAnnotatedProperties.class); assertEquals(0, properties.size()); }
@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 aPropertyIsMandatoryByDefault() throws ConfigBuilderException { List<ConfigurationProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanWithAValidAnnotation.class); assertEquals(1, properties.size()); ConfigurationProperty p = properties.get(0); assertTrue( p.isMandatory()); }
@Test public void aPropertyCanBeConfiguredNotMandatory() throws ConfigBuilderException { List<ConfigurationProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanPropertyCanBeConfiguredNotMandatory.class); assertEquals(1, properties.size()); ConfigurationProperty p = properties.get(0); assertFalse( p.isMandatory()); }
@Test public void enumFieldsCanBeParsed() throws ConfigBuilderException { List<ConfigurationProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanWithEnumTypes.class); assertEquals(1, properties.size()); ConfigurationProperty p = properties.get(0); assertEquals( p.getJavaType(), Scope.class); }
@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 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 testConfigSettersWhichHavePrimitiveArgumentAreDescribedUsingTheEquivalentWrapperType() throws ConfigBuilderException { List<ConfigurationProperty> properties = configPropertyParser.getConfigProperties(ConfigBeanWithASetterWithPrimitiveTypeParameter.class); assertEquals(1, properties.size()); ConfigurationProperty p = properties.get(0); assertTrue( p.isMandatory()); }