Make coverage 100% for all classes at package com.puppycrawl.tools.checkstyle . #1294
This commit is contained in:
parent
146e2657d3
commit
dab75cd8a0
1
pom.xml
1
pom.xml
|
|
@ -692,7 +692,6 @@
|
|||
<totalBranchRate>85</totalBranchRate>
|
||||
<totalLineRate>93</totalLineRate>
|
||||
<regexes>
|
||||
<regex><pattern>.*.checkstyle.AnnotationUtility</pattern><branchRate>60</branchRate><lineRate>60</lineRate></regex>
|
||||
<regex><pattern>.*.Checker</pattern><branchRate>79</branchRate><lineRate>84</lineRate></regex>
|
||||
<regex><pattern>.*.ConfigurationLoader</pattern><branchRate>86</branchRate><lineRate>79</lineRate></regex>
|
||||
<regex><pattern>.*.ConfigurationLoader\$.*</pattern><branchRate>65</branchRate><lineRate>84</lineRate></regex>
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ package com.puppycrawl.tools.checkstyle;
|
|||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.FullIdent;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -30,6 +31,12 @@ import org.apache.commons.lang3.StringUtils;
|
|||
* @author Travis Schneeberger
|
||||
*/
|
||||
public final class AnnotationUtility {
|
||||
|
||||
/**
|
||||
* Common message
|
||||
*/
|
||||
private static final String THE_AST_IS_NULL = "the ast is null";
|
||||
|
||||
/**
|
||||
* private utility constructor.
|
||||
* @throws UnsupportedOperationException if called
|
||||
|
|
@ -58,11 +65,12 @@ public final class AnnotationUtility {
|
|||
* @param ast the current node
|
||||
* @param annotation the annotation name to check for
|
||||
* @return true if contains the annotation
|
||||
* @throws NullPointerException if the ast or
|
||||
* annotation is null
|
||||
*/
|
||||
public static boolean containsAnnotation(final DetailAST ast,
|
||||
String annotation) {
|
||||
if (ast == null) {
|
||||
throw new IllegalArgumentException(THE_AST_IS_NULL);
|
||||
}
|
||||
return AnnotationUtility.getAnnotation(ast, annotation) != null;
|
||||
}
|
||||
|
||||
|
|
@ -72,9 +80,11 @@ public final class AnnotationUtility {
|
|||
*
|
||||
* @param ast the current node
|
||||
* @return true if contains an annotation
|
||||
* @throws NullPointerException if the ast is null
|
||||
*/
|
||||
public static boolean containsAnnotation(final DetailAST ast) {
|
||||
if (ast == null) {
|
||||
throw new IllegalArgumentException(THE_AST_IS_NULL);
|
||||
}
|
||||
final DetailAST holder = AnnotationUtility.getAnnotationHolder(ast);
|
||||
return holder != null && holder.branchContains(TokenTypes.ANNOTATION);
|
||||
}
|
||||
|
|
@ -86,11 +96,10 @@ public final class AnnotationUtility {
|
|||
*
|
||||
* @param ast the current node
|
||||
* @return the Annotation Holder
|
||||
* @throws NullPointerException if the ast is null
|
||||
*/
|
||||
public static DetailAST getAnnotationHolder(DetailAST ast) {
|
||||
if (ast == null) {
|
||||
throw new IllegalArgumentException("the ast is null");
|
||||
throw new IllegalArgumentException(THE_AST_IS_NULL);
|
||||
}
|
||||
|
||||
final DetailAST annotationHolder;
|
||||
|
|
@ -127,13 +136,11 @@ public final class AnnotationUtility {
|
|||
* @param ast the current node
|
||||
* @param annotation the annotation name to check for
|
||||
* @return the AST representing that annotation
|
||||
* @throws NullPointerException if the ast or
|
||||
* annotation is null
|
||||
*/
|
||||
public static DetailAST getAnnotation(final DetailAST ast,
|
||||
String annotation) {
|
||||
if (ast == null) {
|
||||
throw new IllegalArgumentException("the ast is null");
|
||||
throw new IllegalArgumentException(THE_AST_IS_NULL);
|
||||
}
|
||||
|
||||
if (annotation == null) {
|
||||
|
|
@ -141,8 +148,8 @@ public final class AnnotationUtility {
|
|||
}
|
||||
|
||||
if (StringUtils.isBlank(annotation)) {
|
||||
throw new IllegalArgumentException("the annotation"
|
||||
+ "is empty or spaces");
|
||||
throw new IllegalArgumentException(
|
||||
"the annotation is empty or spaces");
|
||||
}
|
||||
|
||||
final DetailAST holder = AnnotationUtility.getAnnotationHolder(ast);
|
||||
|
|
@ -162,42 +169,4 @@ public final class AnnotationUtility {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see what the passed in AST (representing
|
||||
* an annotation) is annotating.
|
||||
*
|
||||
* @param ast the AST representing an annotation.
|
||||
* @return the AST the annotation is annotating.
|
||||
* @throws NullPointerException if the ast is null
|
||||
* @throws IllegalArgumentException if the ast is not
|
||||
* an {@link TokenTypes#ANNOTATION}
|
||||
*/
|
||||
public static DetailAST annotatingWhat(DetailAST ast) {
|
||||
if (ast == null) {
|
||||
throw new IllegalArgumentException("the ast is null");
|
||||
}
|
||||
|
||||
if (ast.getType() != TokenTypes.ANNOTATION) {
|
||||
throw new IllegalArgumentException(
|
||||
"The ast is not an annotation. AST: " + ast);
|
||||
}
|
||||
|
||||
return ast.getParent().getParent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the passed in AST (representing
|
||||
* an annotation) is annotating the passed in type.
|
||||
* @param ast the AST representing an annotation
|
||||
* @param tokenType the passed in type
|
||||
* @return true if the annotation is annotating a type
|
||||
* equal to the passed in type
|
||||
* @throws NullPointerException if the ast is null
|
||||
* @throws IllegalArgumentException if the ast is not
|
||||
* an {@link TokenTypes#ANNOTATION}
|
||||
*/
|
||||
public static boolean isAnnotatingType(DetailAST ast, int tokenType) {
|
||||
final DetailAST astNode = AnnotationUtility.annotatingWhat(ast);
|
||||
return astNode.getType() == tokenType;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,140 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// checkstyle: Checks Java source code for adherence to a set of rules.
|
||||
// Copyright (C) 2001-2015 the original author or authors.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
package com.puppycrawl.tools.checkstyle;
|
||||
|
||||
import static com.puppycrawl.tools.checkstyle.TestUtils.assertUtilsClassHasPrivateConstructor;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
||||
public class AnnotationUtilityTest {
|
||||
|
||||
@Test
|
||||
public void testIsProperUtilsClass() throws ReflectiveOperationException {
|
||||
try {
|
||||
assertUtilsClassHasPrivateConstructor(AnnotationUtility.class);
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
Assert.assertTrue("do not instantiate.".equals(ex.getCause().getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAnnotationNull() throws ReflectiveOperationException {
|
||||
try {
|
||||
AnnotationUtility.containsAnnotation(null);
|
||||
Assert.fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
Assert.assertTrue("the ast is null".equals(ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAnnotationNull2() throws ReflectiveOperationException {
|
||||
try {
|
||||
AnnotationUtility.containsAnnotation(null, "");
|
||||
Assert.fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
Assert.assertTrue("the ast is null".equals(ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAnnotationFalse() throws ReflectiveOperationException {
|
||||
DetailAST ast = new DetailAST();
|
||||
ast.setType(1);
|
||||
Assert.assertFalse(AnnotationUtility.containsAnnotation(ast));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAnnotationFalse2() throws ReflectiveOperationException {
|
||||
DetailAST ast = new DetailAST();
|
||||
ast.setType(1);
|
||||
DetailAST ast2 = new DetailAST();
|
||||
ast2.setType(TokenTypes.MODIFIERS);
|
||||
ast.addChild(ast2);
|
||||
Assert.assertFalse(AnnotationUtility.containsAnnotation(ast));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAnnotationTrue() throws ReflectiveOperationException {
|
||||
DetailAST ast = new DetailAST();
|
||||
ast.setType(1);
|
||||
DetailAST ast2 = new DetailAST();
|
||||
ast2.setType(TokenTypes.MODIFIERS);
|
||||
ast.addChild(ast2);
|
||||
DetailAST ast3 = new DetailAST();
|
||||
ast3.setType(TokenTypes.ANNOTATION);
|
||||
ast2.addChild(ast3);
|
||||
Assert.assertTrue(AnnotationUtility.containsAnnotation(ast));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotationHolderNull() throws ReflectiveOperationException {
|
||||
try {
|
||||
AnnotationUtility.getAnnotationHolder(null);
|
||||
Assert.fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
Assert.assertTrue("the ast is null".equals(ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotationNull() throws ReflectiveOperationException {
|
||||
try {
|
||||
AnnotationUtility.getAnnotation(null, null);
|
||||
Assert.fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
Assert.assertTrue("the ast is null".equals(ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotationNull2() throws ReflectiveOperationException {
|
||||
try {
|
||||
AnnotationUtility.getAnnotation(new DetailAST(), null);
|
||||
Assert.fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
Assert.assertTrue("the annotation is null".equals(ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotationEmpty() throws ReflectiveOperationException {
|
||||
try {
|
||||
AnnotationUtility.getAnnotation(new DetailAST(), "");
|
||||
Assert.fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
Assert.assertTrue("the annotation is empty or spaces"
|
||||
.equals(ex.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue