From f7895501bf435564bf954c6fc04f91c438fbd7a2 Mon Sep 17 00:00:00 2001 From: rnveach Date: Sun, 5 Jun 2016 13:53:44 -0400 Subject: [PATCH] Issue #3187: validate property values in xdoc --- .../checkstyle/internal/XDocsPagesTest.java | 71 ++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java index f46047964..e0149de6e 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XDocsPagesTest.java @@ -482,6 +482,8 @@ public class XDocsPagesTest { final String actualTypeName = columns.get(2).getTextContent().replace("\n", "") .replace("\r", "").replaceAll(" +", " ").trim(); + final String actualValue = columns.get(3).getTextContent().replace("\n", "") + .replace("\r", "").replaceAll(" +", " ").trim(); Assert.assertFalse(fileName + " section '" + sectionName + "' should have a type for " + propertyName, actualTypeName.isEmpty()); @@ -489,18 +491,26 @@ public class XDocsPagesTest { final PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(instance, propertyName); final Class clss = descriptor.getPropertyType(); - final String expectedTypeName = getExpectedTypeName(clss, propertyName); + final String expectedTypeName = + getCheckPropertyExpectedTypeName(clss, propertyName); + final String expectedValue = getCheckPropertyExpectedValue(clss, instance, + propertyName); if (expectedTypeName != null) { Assert.assertEquals(fileName + " section '" + sectionName + "' should have the type for " + propertyName, expectedTypeName, actualTypeName); + if (expectedValue != null) { + Assert.assertEquals(fileName + " section '" + sectionName + + "' should have the value for " + propertyName, expectedValue, + actualValue); + } } } } } - private static String getExpectedTypeName(Class clss, String propertyName) { + private static String getCheckPropertyExpectedTypeName(Class clss, String propertyName) { String result = null; if (clss == boolean.class) { @@ -531,6 +541,63 @@ public class XDocsPagesTest { return result; } + private static String getCheckPropertyExpectedValue(Class clss, Object instance, + String propertyName) throws Exception { + final Field field = getField(instance.getClass(), propertyName); + String result = null; + + if (field != null) { + final Object value = field.get(instance); + + if (clss == boolean.class) { + result = value.toString(); + } + else if (clss == int.class) { + if (value.equals(Integer.MAX_VALUE)) { + result = "java.lang.Integer.MAX_VALUE"; + } + else { + result = value.toString(); + } + } + else if (clss == int[].class) { + result = Arrays.toString((int[]) value).replace("[", "").replace("]", ""); + if (result.isEmpty()) { + result = "{}"; + } + } + else if (clss == double[].class) { + result = Arrays.toString((double[]) value).replace("[", "").replace("]", "") + .replace(".0", ""); + if (result.isEmpty()) { + result = "{}"; + } + } + + if (clss != String.class && clss != String[].class && result == null) { + result = "null"; + } + } + + return result; + } + + private static Field getField(Class clss, String propertyName) { + Field result = null; + + if (clss != null) { + try { + result = clss.getDeclaredField(propertyName); + result.setAccessible(true); + } + catch (NoSuchFieldException ex) { + result = getField(clss.getSuperclass(), propertyName); + } + } + + return result; + } + private static void validateErrorSection(String fileName, String sectionName, Node subSection, Object instance) throws Exception { final Class clss = instance.getClass();