Skip to content

Commit

Permalink
Issue checkstyle#10649: Migrate to Inline Config Parser in Annotation…
Browse files Browse the repository at this point in the history
…LocationCheckTest
  • Loading branch information
shashwatj07 committed Aug 19, 2021
1 parent 67675f2 commit 489d74b
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 61 deletions.
1 change: 0 additions & 1 deletion pom.xml
Expand Up @@ -1745,7 +1745,6 @@

<exclude>**/MethodParamPadCheckTest.class</exclude>

<exclude>**/AnnotationLocationCheckTest.class</exclude>

<exclude>**/VisibilityModifierCheckTest.class</exclude>

Expand Down
Expand Up @@ -27,6 +27,7 @@
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -79,12 +80,11 @@ public static TestInputConfiguration parse(String inputFilePath) throws Exceptio
return inputConfigBuilder.build();
}

private static String getPackageFromFilePath(String filePath) {
private static String getFullyQualifiedClassName(String filePath, String checkName) {
final String path = SLASH_PATTERN.matcher(filePath).replaceAll("\\.");
final int beginIndex = path.indexOf("com.puppycrawl");
final int endIndex = path.lastIndexOf('.',
path.lastIndexOf('.', path.lastIndexOf('.') - 1) - 1);
return path.substring(beginIndex, endIndex);
final int endIndex = path.lastIndexOf(checkName.toLowerCase(Locale.ROOT));
return path.substring(beginIndex, endIndex) + checkName + "Check";
}

private static List<String> readFile(Path filePath) throws CheckstyleException {
Expand All @@ -103,8 +103,8 @@ private static void setCheckName(TestInputConfiguration.Builder inputConfigBuild
throw new CheckstyleException("Config not specified in " + filePath);
}
final String checkName = lines.get(1);
final String checkPath = getPackageFromFilePath(filePath) + "." + checkName + "Check";
inputConfigBuilder.setCheckName(checkPath);
final String fullyQualifiedClassName = getFullyQualifiedClassName(filePath, checkName);
inputConfigBuilder.setCheckName(fullyQualifiedClassName);
}

private static void setCheckProperties(TestInputConfiguration.Builder inputConfigBuilder,
Expand Down
Expand Up @@ -49,7 +49,8 @@ public void testCorrect() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;

verify(checkConfig, getPath("InputAnnotationLocationCorrect.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationCorrect.java"), expected);
}

@Test
Expand Down Expand Up @@ -81,7 +82,8 @@ public void testIncorrect() throws Exception {
"99:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"106:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_21", 0, 3),
};
verify(checkConfig, getPath("InputAnnotationLocationIncorrect.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationIncorrect.java"), expected);
}

@Test
Expand Down Expand Up @@ -116,7 +118,8 @@ public void testIncorrectAllTokens() throws Exception {
"99:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation_13"),
"106:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_23", 0, 3),
};
verify(checkConfig, getPath("InputAnnotationLocationIncorrect3.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationIncorrect3.java"), expected);
}

@Test
Expand Down Expand Up @@ -144,7 +147,8 @@ public void testGetAcceptableTokens() {
public void testWithoutAnnotations() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputAnnotationLocationEmpty.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationEmpty.java"), expected);
}

@Test
Expand All @@ -168,7 +172,8 @@ public void testWithParameters() throws Exception {
"96:11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 10, 8),
"106:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnn_22", 0, 3),
};
verify(checkConfig, getPath("InputAnnotationLocationIncorrect2.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationIncorrect2.java"), expected);
}

@Test
Expand All @@ -179,8 +184,9 @@ public void testWithMultipleAnnotations() throws Exception {
"14:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation11"),
"14:17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation12"),
"14:33: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation13"),
};
verify(checkConfig, getPath("InputAnnotationLocationCustomAnnotationsDeclared.java"),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationCustomAnnotationsDeclared.java"),
expected);
}

Expand All @@ -190,20 +196,22 @@ public void testAllTokens() throws Exception {
checkConfig.addProperty("tokens", "CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, "
+ "CTOR_DEF, VARIABLE_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputAnnotationLocationWithoutAnnotations.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationWithoutAnnotations.java"), expected);
}

@Test
public void testAnnotation() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
checkConfig.addProperty("tokens", "ANNOTATION_DEF, ANNOTATION_FIELD_DEF");
final String[] expected = {
"17:3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "AnnotationAnnotation", 2, 0),
"18:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "AnnotationAnnotation"),
"21:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "AnnotationAnnotation", 6, 4),
"22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "AnnotationAnnotation"),
"18:3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "AnnotationAnnotation", 2, 0),
"19:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "AnnotationAnnotation"),
"22:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "AnnotationAnnotation", 6, 4),
"23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "AnnotationAnnotation"),
};
verify(checkConfig, getPath("InputAnnotationLocationAnnotation.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationAnnotation.java"), expected);
}

@Test
Expand All @@ -218,7 +226,8 @@ public void testClass() throws Exception {
"26:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "ClassAnnotation", 6, 4),
"27:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "ClassAnnotation"),
};
verify(checkConfig, getPath("InputAnnotationLocationClass.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationClass.java"), expected);
}

@Test
Expand All @@ -231,7 +240,8 @@ public void testEnum() throws Exception {
"22:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "EnumAnnotation", 6, 4),
"23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "EnumAnnotation"),
};
verify(checkConfig, getPath("InputAnnotationLocationEnum.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationEnum.java"), expected);
}

@Test
Expand All @@ -244,7 +254,8 @@ public void testInterface() throws Exception {
"22:7: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "InterfaceAnnotation", 6, 4),
"23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "InterfaceAnnotation"),
};
verify(checkConfig, getPath("InputAnnotationLocationInterface.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationInterface.java"), expected);
}

@Test
Expand All @@ -255,7 +266,8 @@ public void testPackage() throws Exception {
"12:3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "PackageAnnotation", 2, 0),
"13:1: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "PackageAnnotation"),
};
verify(checkConfig, getPath("inputs/package-info.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("inputs/package-info.java"), expected);
}

@Test
Expand All @@ -267,7 +279,8 @@ public void testAnnotationInForEachLoopParameterAndVariableDef() throws Exceptio
checkConfig.addProperty("allowSamelineSingleParameterlessAnnotation", "false");
checkConfig.addProperty("allowSamelineParameterizedAnnotation", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputAnnotationLocationDeprecatedAndCustom.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationDeprecatedAndCustom.java"), expected);
}

@Test
Expand All @@ -277,7 +290,8 @@ public void testAnnotationMultiple() throws Exception {
checkConfig.addProperty("allowSamelineSingleParameterlessAnnotation", "false");
checkConfig.addProperty("allowSamelineParameterizedAnnotation", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputAnnotationLocationMultiple.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationMultiple.java"), expected);
}

@Test
Expand All @@ -295,7 +309,8 @@ public void testAnnotationParameterized() throws Exception {
"26:33: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"28:21: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
};
verify(checkConfig, getPath("InputAnnotationLocationParameterized.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationParameterized.java"), expected);
}

@Test
Expand All @@ -313,22 +328,23 @@ public void testAnnotationSingleParameterless() throws Exception {
"30:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"30:21: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
};
verify(checkConfig, getPath("InputAnnotationLocationSingleParameterless.java"), expected);
verifyWithInlineConfigParser(checkConfig,
getPath("InputAnnotationLocationSingleParameterless.java"), expected);
}

@Test
public void testAnnotationLocationRecordsAndCompactCtors() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
final String[] expected = {
"20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"23:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"26:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"27:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"33:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"44:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"54:34: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"19:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"22:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"25:5: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"26:9: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"32:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"43:13: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
"53:34: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "SuppressWarnings"),
};
verify(checkConfig,
verifyWithInlineConfigParser(checkConfig,
getNonCompilablePath("InputAnnotationLocationRecordsAndCompactCtors.java"),
expected);
}
Expand Down
Expand Up @@ -3,9 +3,8 @@
allowSamelineMultipleAnnotations = (default)false
allowSamelineSingleParameterlessAnnotation = (default)true
allowSamelineParameterizedAnnotation = (default)false
tokens = CLASS_DEF, INTERFACE_DEF, PACKAGE_DEF, ENUM_CONSTANT_DEF, \
ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF, RECORD_DEF, COMPACT_CTOR_DEF, \
ANNOTATION_DEF, ANNOTATION_FIELD_DEF
tokens = (default)CLASS_DEF, INTERFACE_DEF, PACKAGE_DEF, ENUM_CONSTANT_DEF, \
ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF, RECORD_DEF, COMPACT_CTOR_DEF
*/
Expand Down Expand Up @@ -52,7 +51,7 @@ record MyInnerRecord () {
*/
public record MyRecord7() {
record MyInnerRecord () {@SuppressWarnings("Annotation")public MyInnerRecord {
} // violation
} // violation above
}
}
}
Expand Down
@@ -1,18 +1,19 @@
/*
AnnotationLocation
allowSamelineMultipleAnnotations = (default)false
allowSamelineSingleParameterlessAnnotation = (default)true
allowSamelineParameterizedAnnotation = (default)false
tokens = ANNOTATION_DEF, ANNOTATION_FIELD_DEF
*/

package com.puppycrawl.tools.checkstyle.checks.annotation.annotationlocation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Target;


/* Config
*
* allowSamelineSingleParameterlessAnnotation = true
* allowSamelineParameterizedAnnotation = false
* allowSamelineMultipleAnnotations = false
* tokens = ["ANNOTATION_DEF", "ANNOTATION_FIELD_DEF"]
*/

@AnnotationAnnotation(value = "foo")
@AnnotationAnnotation // violation
@AnnotationAnnotation("bar") @interface InputAnnotationLocationAnnotation { // violation
Expand Down
Expand Up @@ -11,7 +11,7 @@

package com.puppycrawl.tools.checkstyle.checks.annotation.annotationlocation;

@MyAnnotation11 @MyAnnotation12 @MyAnnotation13 // violation
@MyAnnotation11 @MyAnnotation12 @MyAnnotation13 // 3 violations
public class InputAnnotationLocationCustomAnnotationsDeclared {

@MyAnnotation13 // ok
Expand Down
@@ -1,7 +1,7 @@
/*
AnnotationLocation
allowSamelineMultipleAnnotations = (default)false
allowSamelineSingleParameterlessAnnotation = (default)true
allowSamelineSingleParameterlessAnnotation = false
allowSamelineParameterizedAnnotation = (default)false
tokens = CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, \
VARIABLE_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF
Expand Down
Expand Up @@ -34,7 +34,7 @@ class InputAnnotationLocationIncorrect
(value = "")
public InputAnnotationLocationIncorrect() {}

@MyAnnotation1("foo") @MyAnn_21 void foo1() {} // violation
@MyAnnotation1("foo") @MyAnn_21 void foo1() {} // 2 violations

@MyAnnotation1(value = "") // ok
@MyAnn_21 // violation
Expand Down
Expand Up @@ -3,8 +3,8 @@
allowSamelineMultipleAnnotations = (default)false
allowSamelineSingleParameterlessAnnotation = (default)true
allowSamelineParameterizedAnnotation = (default)false
tokens = CLASS_DEF, INTERFACE_DEF, PACKAGE_DEF, ENUM_CONSTANT_DEF, ENUM_DEF, \
METHOD_DEF, CTOR_DEF, VARIABLE_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF
tokens = CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF, \
ANNOTATION_DEF, ANNOTATION_FIELD_DEF, ENUM_CONSTANT_DEF, PACKAGE_DEF
*/
Expand Down Expand Up @@ -34,7 +34,7 @@ class InputAnnotationLocationIncorrect3
(value = "")
public InputAnnotationLocationIncorrect3() {}

@MyAnnotation_13("foo") @MyAnn_23 void foo1() {} // violation
@MyAnnotation_13("foo") @MyAnn_23 void foo1() {} // 2 violations

@MyAnnotation_13(value = "") // ok
@MyAnn_23 // violation
Expand Down
Expand Up @@ -17,13 +17,13 @@ class InputAnnotationLocationParameterized {

@Annotation void singleParameterless() {} // violation

@Annotation @Annotation void multipleParameterless() {} // violation
@Annotation @Annotation void multipleParameterless() {} // 2 violations

@Annotation("") void parameterized() {}

@Annotation(value = "") void namedParameterized() {}

@Annotation @Annotation("") @Annotation(value = "") void multiple() {} // violation
@Annotation @Annotation("") @Annotation(value = "") void multiple() {} // 3 violations

@Annotation("") @Annotation(value = "") void multipleParametrized() {} // violation

Expand Down
Expand Up @@ -25,9 +25,9 @@ class InputAnnotationLocationSingleParameterless {

@Annotation(value = "") void namedParameterized() {} // violation

@Annotation @Annotation("") @Annotation(value = "") void multiple() {} // violation
@Annotation @Annotation("") @Annotation(value = "") void multiple() {} // 2 violations

@Annotation("") @Annotation(value = "") void multipleParametrized() {} // violation
@Annotation("") @Annotation(value = "") void multipleParametrized() {} // 2 violations

void parameterlessSamelineInForEach() {
for (@Annotation Object o : new Object[0]) break; //ok
Expand Down
Expand Up @@ -9,6 +9,6 @@
*/

@PackageAnnotation(value = "foo")
@PackageAnnotation //warn
@PackageAnnotation("bar") package com.puppycrawl.tools.checkstyle.checks.annotation.annotationlocation.inputs; //warn
@PackageAnnotation // violation
@PackageAnnotation("bar") package com.puppycrawl.tools.checkstyle.checks.annotation.annotationlocation.inputs; // violation

0 comments on commit 489d74b

Please sign in to comment.