Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Issue checkstyle#4395: increase coverage of pitest-checkstyle-utils p…
Browse files Browse the repository at this point in the history
…rofile to 99%
  • Loading branch information
Nimfadora committed Jun 22, 2017
1 parent d72e6f7 commit 10918f5
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 6 deletions.
17 changes: 16 additions & 1 deletion pom.xml
Expand Up @@ -2103,8 +2103,23 @@
</targetClasses>
<targetTests>
<param>com.puppycrawl.tools.checkstyle.utils.*</param>
<param>com.puppycrawl.tools.checkstyle.AstTreeStringPrinterTest</param>
<param>com.puppycrawl.tools.checkstyle.ConfigurationLoaderTest</param>
<param>com.puppycrawl.tools.checkstyle.DetailNodeTreeStringPrinterTest</param>
<param>com.puppycrawl.tools.checkstyle.PackageObjectFactoryTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.FinalParametersCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.TranslationCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.coding.MultipleVariableDeclarationsCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.design.FinalClassCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.javadoc.*</param>
<param>com.puppycrawl.tools.checkstyle.checks.regexp.RegexpOnFilenameCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.whitespace.*</param>
<param>com.puppycrawl.tools.checkstyle.filters.SuppressionCommentFilter</param>
<param>com.puppycrawl.tools.checkstyle.filters.SuppressWithNearbyCommentFilterTest</param>
<param>com.puppycrawl.tools.checkstyle.internal.AllChecksTest</param>
</targetTests>
<mutationThreshold>67</mutationThreshold>
<mutationThreshold>99</mutationThreshold>
<timeoutFactor>${pitest.plugin.timeout.factor}</timeoutFactor>
<timeoutConstant>${pitest.plugin.timeout.constant}</timeoutConstant>
<threads>${pitest.plugin.threads}</threads>
Expand Down
Expand Up @@ -21,6 +21,7 @@

import static com.puppycrawl.tools.checkstyle.internal.TestUtils.assertUtilsClassHasPrivateConstructor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.InvocationTargetException;

Expand Down Expand Up @@ -91,7 +92,7 @@ public void testContainsAnnotationTrue() {
final DetailAST ast3 = new DetailAST();
ast3.setType(TokenTypes.ANNOTATION);
ast2.addChild(ast3);
Assert.assertTrue(AnnotationUtility.containsAnnotation(ast));
assertTrue(AnnotationUtility.containsAnnotation(ast));
}

@Test
Expand Down Expand Up @@ -137,4 +138,37 @@ public void testAnnotationEmpty() {
assertEquals("the annotation is empty or spaces", ex.getMessage());
}
}

@Test
public void testContainsAnnotationWithNull() {
try {
AnnotationUtility.getAnnotation(null, "");
Assert.fail("IllegalArgumentException is expected");
}
catch (IllegalArgumentException ex) {
assertEquals("the ast is null", ex.getMessage());
}
}

@Test
public void testContainsAnnotation() {
final DetailAST astForTest = new DetailAST();
astForTest.setType(TokenTypes.PACKAGE_DEF);
final DetailAST child = new DetailAST();
final DetailAST annotations = new DetailAST();
final DetailAST annotation = new DetailAST();
final DetailAST annotationNameHolder = new DetailAST();
final DetailAST annotationName = new DetailAST();
annotations.setType(TokenTypes.ANNOTATIONS);
annotation.setType(TokenTypes.ANNOTATION);
annotationName.setText("Annotation");

annotationNameHolder.setNextSibling(annotationName);
annotation.setFirstChild(annotationNameHolder);
annotations.setFirstChild(annotation);
child.setNextSibling(annotations);
astForTest.setFirstChild(child);

assertTrue(AnnotationUtility.containsAnnotation(astForTest, "Annotation"));
}
}
@@ -0,0 +1,118 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 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.utils;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

import org.junit.Test;

import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.internal.TestUtils;

public class BlockCommentPositionTest {

@Test
public void testJavaDocsRecognition() throws Exception {
final List<BlockCommentPositionTestMetadata> metadataList = Arrays.asList(
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnClass.java",
BlockCommentPosition::isOnClass, 3),
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnMethod.java",
BlockCommentPosition::isOnMethod, 3),
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnField.java",
BlockCommentPosition::isOnField, 3),
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnEnum.java",
BlockCommentPosition::isOnEnum, 3),
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnConstructor.java",
BlockCommentPosition::isOnConstructor, 3),
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnInterface.java",
BlockCommentPosition::isOnInterface, 3),
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnAnnotation.java",
BlockCommentPosition::isOnAnnotationDef, 3),
new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnEnumMember.java",
BlockCommentPosition::isOnEnumConstant, 2)
);

for (BlockCommentPositionTestMetadata metadata : metadataList) {
final DetailAST ast = TestUtils.parseFile(
new File(getPath(metadata.getFileName()))
);
final int matches = getJavadocsCount(ast, metadata.getFileName(),
metadata.getValidation());
assertEquals(metadata.getMatchesNum(), matches);
}
}

private static int getJavadocsCount(DetailAST detailAST, String filename,
Function<DetailAST, Boolean> validation) {
int matchFound = 0;
DetailAST node = detailAST;
while (node != null) {
if (node.getType() == TokenTypes.BLOCK_COMMENT_BEGIN
&& JavadocUtils.isJavadocComment(node)) {
if (!validation.apply(node)) {
throw new IllegalStateException("Position of comment isn't correct. "
+ "File name is " + filename + ". "
+ "Node line number is " + node.getLineNo());
}
matchFound++;
}
matchFound += getJavadocsCount(node.getFirstChild(), filename, validation);
node = node.getNextSibling();
}
return matchFound;
}

private static String getPath(String filename) {
return "src/test/resources/com/puppycrawl/tools/checkstyle/utils/blockcommentposition/"
+ filename;
}

private static final class BlockCommentPositionTestMetadata {

private final String fileName;
private final Function<DetailAST, Boolean> validation;
private final int matchesNum;

private BlockCommentPositionTestMetadata(String fileName, Function<DetailAST,
Boolean> validation, int matchesNum) {
this.fileName = fileName;
this.validation = validation;
this.matchesNum = matchesNum;
}

public String getFileName() {
return fileName;
}

public Function<DetailAST, Boolean> getValidation() {
return validation;
}

public int getMatchesNum() {
return matchesNum;
}
}
}
Expand Up @@ -31,6 +31,7 @@

import com.puppycrawl.tools.checkstyle.api.Comment;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.DetailNode;
import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl;
Expand Down Expand Up @@ -247,4 +248,40 @@ public void testGetTokenIdThatIsUnknown() {
assertEquals("Unknown javadoc token name. Given name ", ex.getMessage());
}
}

@Test
public void testGetTokenId() {
final int tokenId = JavadocUtils.getTokenId("JAVADOC");

assertEquals(JavadocTokenTypes.JAVADOC, tokenId);
}

@Test
public void testGetJavadocCommentContent() {
final DetailAST detailAST = new DetailAST();
final DetailAST javadoc = new DetailAST();

javadoc.setText("1javadoc");
detailAST.setFirstChild(javadoc);
final String commentContent = JavadocUtils.getJavadocCommentContent(detailAST);

assertEquals("javadoc", commentContent);
}

@Test
public void testGetFirstToken() {
final JavadocNodeImpl javadocNode = new JavadocNodeImpl();
final JavadocNodeImpl basetag = new JavadocNodeImpl();
basetag.setType(JavadocTokenTypes.BASE_TAG);
final JavadocNodeImpl body = new JavadocNodeImpl();
body.setType(JavadocTokenTypes.BODY);

body.setParent(javadocNode);
basetag.setParent(javadocNode);
javadocNode.setChildren(basetag, body);

final DetailNode result = JavadocUtils.findFirstToken(javadocNode, JavadocTokenTypes.BODY);

assertEquals(body, result);
}
}
Expand Up @@ -26,9 +26,12 @@

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Optional;

import org.junit.Test;

import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public class TokenUtilsTest {
Expand Down Expand Up @@ -62,14 +65,45 @@ public void testGetIntFromInaccessibleField() throws NoSuchFieldException {
}

@Test
public void testTokenValueIncorrect() {
final Integer id = Integer.MAX_VALUE - 1;
public void testTokenValueIncorrect() throws IllegalAccessException {
int maxId = 0;
final Field[] fields = TokenTypes.class.getDeclaredFields();
for (final Field field : fields) {
// Only process the int declarations.
if (field.getType() != Integer.TYPE) {
continue;
}

final String name = field.getName();
final int id = field.getInt(name);
if (id > maxId) {
maxId = id;
}
}

final int nextAfterMaxId = maxId + 1;
try {
TokenUtils.getTokenName(id);
TokenUtils.getTokenName(nextAfterMaxId);
fail("IllegalArgumentException is expected");
}
catch (IllegalArgumentException expected) {
assertEquals("given id " + id, expected.getMessage());
assertEquals("given id " + nextAfterMaxId, expected.getMessage());
}
}

@Test
public void testTokenValueCorrect() throws IllegalAccessException {
final Field[] fields = TokenTypes.class.getDeclaredFields();
for (final Field field : fields) {
// Only process the int declarations.
if (field.getType() != Integer.TYPE) {
continue;
}

final String name = field.getName();
final int id = field.getInt(name);

assertEquals(name, TokenUtils.getTokenName(id));
}
}

Expand Down Expand Up @@ -138,6 +172,66 @@ public void testIsCommentType() {
assertTrue(TokenUtils.isCommentType(TokenTypes.BLOCK_COMMENT_BEGIN));
assertTrue(TokenUtils.isCommentType(TokenTypes.BLOCK_COMMENT_END));
assertTrue(TokenUtils.isCommentType(TokenTypes.COMMENT_CONTENT));
assertTrue(TokenUtils.isCommentType("COMMENT_CONTENT"));
}

@Test
public void tetsGetTokenTypesTotalNumber() {
final int tokenTypesTotalNumber = TokenUtils.getTokenTypesTotalNumber();

assertEquals(169, tokenTypesTotalNumber);
}

@Test
public void testGetAllTokenIds() {
final int[] allTokenIds = TokenUtils.getAllTokenIds();
final int sum = Arrays.stream(allTokenIds).sum();

assertEquals(169, allTokenIds.length);
assertEquals(15662, sum);
}

@Test
public void testGetTokenNameWithGreatestPossibleId() {
final Integer id = TokenTypes.COMMENT_CONTENT;
final String tokenName = TokenUtils.getTokenName(id);

assertEquals("COMMENT_CONTENT", tokenName);
}

@Test
public void testCorrectBehaviourOfGetTokenId() {
final String id = "EOF";

assertEquals(TokenTypes.EOF, TokenUtils.getTokenId(id));

}

@Test
public void testCorrectBehaviourOfShortDescription() {
final String id = "EOF";
final String shortDescription = TokenUtils.getShortDescription(id);

assertEquals("The end of file token.", shortDescription);
}

@Test
public void testFindFirstTokenByPredicate() {
final DetailAST astForTest = new DetailAST();
final DetailAST child = new DetailAST();
final DetailAST firstSibling = new DetailAST();
final DetailAST secondSibling = new DetailAST();
final DetailAST thirdSibling = new DetailAST();
firstSibling.setText("first");
secondSibling.setText("second");
thirdSibling.setText("third");
secondSibling.setNextSibling(thirdSibling);
firstSibling.setNextSibling(secondSibling);
child.setNextSibling(firstSibling);
astForTest.setFirstChild(child);
final Optional<DetailAST> result = TokenUtils.findFirstTokenByPredicate(astForTest,
ast -> "second".equals(ast.getText()));

assertEquals(secondSibling, result.get());
}
}

0 comments on commit 10918f5

Please sign in to comment.