Search in sources :

Example 1 with PropertyType

use of com.puppycrawl.tools.checkstyle.PropertyType in project checkstyle by checkstyle.

the class XdocsPagesTest method validatePropertySectionPropertyEx.

private static void validatePropertySectionPropertyEx(String fileName, String sectionName, Object instance, List<Node> columns, String propertyName) throws Exception {
    assertWithMessage("%s section '%s' should have a description for %s", fileName, sectionName, propertyName).that(columns.get(1).getTextContent().trim()).isNotEmpty();
    final String actualTypeName = columns.get(2).getTextContent().replace("\n", "").replace("\r", "").replaceAll(" +", " ").trim();
    assertWithMessage(fileName + " section '" + sectionName + "' should have a type for " + propertyName).that(actualTypeName).isNotEmpty();
    final Field field = getField(instance.getClass(), propertyName);
    final Class<?> fieldClass = getFieldClass(fileName, sectionName, instance, field, propertyName);
    final String expectedTypeName;
    // SuppressWarningsHolder#aliasList is backed by a static (upper case) property.
    if ("SuppressWarningsHolder".equals(sectionName) && "aliasList".equals(propertyName)) {
        expectedTypeName = "String[] in a format of comma separated attribute=value entries. " + "The attribute is the fully qualified name of the Check and value is its alias.";
    } else {
        expectedTypeName = Optional.ofNullable(field).map(nonNullField -> nonNullField.getAnnotation(XdocsPropertyType.class)).map(propertyType -> propertyType.value().getDescription()).orElse(fieldClass.getSimpleName());
    }
    final String expectedValue = getModulePropertyExpectedValue(sectionName, propertyName, field, fieldClass, instance);
    assertWithMessage(fileName + " section '" + sectionName + "' should have the type for " + propertyName).that(actualTypeName).isEqualTo(expectedTypeName);
    if (expectedValue != null) {
        final String actualValue = columns.get(3).getTextContent().trim().replaceAll("\\s+", " ").replaceAll("\\s,", ",");
        assertWithMessage(fileName + " section '" + sectionName + "' should have the value for " + propertyName).that(actualValue).isEqualTo(expectedValue);
    }
}
Also used : Arrays(java.util.Arrays) Array(java.lang.reflect.Array) PropertyUtils(org.apache.commons.beanutils.PropertyUtils) CheckUtil(com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil) AbstractJavadocCheck(com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck) Matcher(java.util.regex.Matcher) Locale(java.util.Locale) Document(org.w3c.dom.Document) Map(java.util.Map) TokenUtil(com.puppycrawl.tools.checkstyle.utils.TokenUtil) URI(java.net.URI) ConfigurationLoader(com.puppycrawl.tools.checkstyle.ConfigurationLoader) Path(java.nio.file.Path) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck) Collection(java.util.Collection) AbstractFileSetCheck(com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck) Set(java.util.Set) Collectors(java.util.stream.Collectors) IgnoredModulesOptions(com.puppycrawl.tools.checkstyle.ConfigurationLoader.IgnoredModulesOptions) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) XdocUtil(com.puppycrawl.tools.checkstyle.internal.utils.XdocUtil) PropertyDescriptor(java.beans.PropertyDescriptor) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IntStream(java.util.stream.IntStream) TestUtil(com.puppycrawl.tools.checkstyle.internal.utils.TestUtil) AccessModifierOption(com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption) XdocsPropertyType(com.puppycrawl.tools.checkstyle.XdocsPropertyType) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Node(org.w3c.dom.Node) NoSuchElementException(java.util.NoSuchElementException) XmlUtil(com.puppycrawl.tools.checkstyle.internal.utils.XmlUtil) InputSource(org.xml.sax.InputSource) Properties(java.util.Properties) NodeList(org.w3c.dom.NodeList) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) Iterator(java.util.Iterator) Files(java.nio.file.Files) ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) IOException(java.io.IOException) Field(java.lang.reflect.Field) Integer.parseInt(java.lang.Integer.parseInt) File(java.io.File) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) StringReader(java.io.StringReader) ParameterizedType(java.lang.reflect.ParameterizedType) PropertyType(com.puppycrawl.tools.checkstyle.PropertyType) Paths(java.nio.file.Paths) Checker(com.puppycrawl.tools.checkstyle.Checker) BitSet(java.util.BitSet) Collections(java.util.Collections) Field(java.lang.reflect.Field)

Aggregations

Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1 Checker (com.puppycrawl.tools.checkstyle.Checker)1 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)1 IgnoredModulesOptions (com.puppycrawl.tools.checkstyle.ConfigurationLoader.IgnoredModulesOptions)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)1 PropertyType (com.puppycrawl.tools.checkstyle.PropertyType)1 XdocsPropertyType (com.puppycrawl.tools.checkstyle.XdocsPropertyType)1 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)1 AbstractFileSetCheck (com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 AbstractJavadocCheck (com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck)1 AccessModifierOption (com.puppycrawl.tools.checkstyle.checks.naming.AccessModifierOption)1 CheckUtil (com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil)1 TestUtil (com.puppycrawl.tools.checkstyle.internal.utils.TestUtil)1 XdocUtil (com.puppycrawl.tools.checkstyle.internal.utils.XdocUtil)1 XmlUtil (com.puppycrawl.tools.checkstyle.internal.utils.XmlUtil)1 TokenUtil (com.puppycrawl.tools.checkstyle.utils.TokenUtil)1 PropertyDescriptor (java.beans.PropertyDescriptor)1