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

Commit

Permalink
Browse files Browse the repository at this point in the history
Issue checkstyle#4395: increase coverage of pitest-checkstyle-utils p…
…rofile to 67%
  • Loading branch information
Nimfadora committed Jun 12, 2017
1 parent 48830f1 commit 602fd7d
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -2087,7 +2087,7 @@
<targetTests>
<param>com.puppycrawl.tools.checkstyle.utils.*</param>
</targetTests>
<mutationThreshold>49</mutationThreshold>
<mutationThreshold>67</mutationThreshold>
<timeoutFactor>${pitest.plugin.timeout.factor}</timeoutFactor>
<timeoutConstant>${pitest.plugin.timeout.constant}</timeoutConstant>
<threads>${pitest.plugin.threads}</threads>
Expand Down
Expand Up @@ -19,15 +19,25 @@

package com.puppycrawl.tools.checkstyle.internal;

import static com.puppycrawl.tools.checkstyle.TreeWalker.parseWithComments;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;

import org.junit.Assert;

import antlr.ANTLRException;
import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
import com.puppycrawl.tools.checkstyle.PackageObjectFactory;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.FileText;

public final class TestUtils {

Expand Down Expand Up @@ -56,4 +66,47 @@ public static PackageObjectFactory getPackageObjectFactory() throws CheckstyleEx
final Set<String> packageNames = PackageNamesLoader.getPackageNames(cl);
return new PackageObjectFactory(packageNames, cl);
}

/**
* Finds node of specified type among root children, siblings, siblings children
* on any deep level.
* @param root DetailAST
* @param predicate predicate
* @return {@link Optional} of {@link DetailAST} node which matches the predicate.
*/
public static Optional<DetailAST> findTokenInAstByPredicate(DetailAST root,
Predicate<DetailAST> predicate) {
DetailAST curNode = root;
while (!predicate.test(curNode)) {
DetailAST toVisit = curNode.getFirstChild();
while (curNode != null && toVisit == null) {
toVisit = curNode.getNextSibling();
if (toVisit == null) {
curNode = curNode.getParent();
}
}

if (curNode == toVisit || curNode == root.getParent()) {
curNode = null;
break;
}

curNode = toVisit;
}
return Optional.ofNullable(curNode);
}

/**
* Parses Java source file. Results in AST which contains comment nodes.
* @param file file to parse
* @return DetailAST tree
* @throws NullPointerException if the text is null
* @throws IOException if the file could not be read
* @throws ANTLRException if parser or lexer failed
*/
public static DetailAST parseFile(File file) throws IOException, ANTLRException {
final FileText text = new FileText(file.getAbsoluteFile(), "UTF-8");
final FileContents contents = new FileContents(text);
return parseWithComments(contents);
}
}
Expand Up @@ -20,14 +20,23 @@
package com.puppycrawl.tools.checkstyle.utils;

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

import org.junit.Assert;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.Test;

import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier;

public class CheckUtilsTest {

Expand All @@ -47,7 +56,7 @@ public void testElseWithCurly() {
final DetailAST ast = new DetailAST();
ast.setType(TokenTypes.ASSIGN);
ast.setText("ASSIGN");
Assert.assertFalse(CheckUtils.isElseIf(ast));
assertFalse(CheckUtils.isElseIf(ast));

final DetailAST parentAst = new DetailAST();
parentAst.setType(TokenTypes.LCURLY);
Expand All @@ -58,21 +67,21 @@ public void testElseWithCurly() {
ifAst.setText("IF");
parentAst.addChild(ifAst);

Assert.assertFalse(CheckUtils.isElseIf(ifAst));
assertFalse(CheckUtils.isElseIf(ifAst));

final DetailAST parentAst2 = new DetailAST();
parentAst2.setType(TokenTypes.SLIST);
parentAst2.setText("SLIST");

parentAst2.addChild(ifAst);

Assert.assertFalse(CheckUtils.isElseIf(ifAst));
assertFalse(CheckUtils.isElseIf(ifAst));

final DetailAST elseAst = new DetailAST();
elseAst.setType(TokenTypes.LITERAL_ELSE);

elseAst.setFirstChild(ifAst);
Assert.assertTrue(CheckUtils.isElseIf(ifAst));
assertTrue(CheckUtils.isElseIf(ifAst));
}

@Test
Expand All @@ -88,7 +97,7 @@ public void testEquals() {
metDef.setType(TokenTypes.METHOD_DEF);
metDef.addChild(modifiers);

Assert.assertFalse(CheckUtils.isEqualsMethod(metDef));
assertFalse(CheckUtils.isEqualsMethod(metDef));

metDef.removeChildren();

Expand All @@ -112,7 +121,7 @@ public void testEquals() {
parameters.addChild(parameter1);
metDef.addChild(parameters);

Assert.assertFalse(CheckUtils.isEqualsMethod(metDef));
assertFalse(CheckUtils.isEqualsMethod(metDef));
}

@Test
Expand Down Expand Up @@ -143,4 +152,160 @@ public void testGetAccessModifierFromModifiersTokenWithNullParameter() {
assertEquals(expectedExceptionMsg, actualExceptionMsg);
}
}

@Test
public void testCreateFullType() throws Exception {
final DetailAST typeNode = getNodeFromFile(TokenTypes.TYPE);

assertEquals("Map[12x12]", CheckUtils.createFullType(typeNode).toString());
}

@Test
public void testCreateFullTypeOfArray() throws Exception {
final DetailAST arrayTypeNode = getNodeFromFile(TokenTypes.VARIABLE_DEF)
.getNextSibling().getFirstChild().getNextSibling();

assertEquals("int[13x14]", CheckUtils.createFullType(arrayTypeNode).toString());
}

@Test
public void testGetTypeParameterNames() throws Exception {
final DetailAST parameterisedClassNode = getNodeFromFile(TokenTypes.CLASS_DEF);
final List<String> expected = Arrays.asList("V", "C");

assertEquals(expected, CheckUtils.getTypeParameterNames(parameterisedClassNode));
}

@Test
public void testGetTypeParameters() throws Exception {
final DetailAST parameterisedClassNode = getNodeFromFile(TokenTypes.CLASS_DEF);
final DetailAST firstTypeParameter =
getNode(parameterisedClassNode, TokenTypes.TYPE_PARAMETER);
final List<DetailAST> expected = Arrays.asList(firstTypeParameter,
firstTypeParameter.getNextSibling().getNextSibling());

assertEquals(expected, CheckUtils.getTypeParameters(parameterisedClassNode));
}

@Test
public void testIsEqualsMethod() throws Exception {
final DetailAST equalsMethodNode = getNodeFromFile(TokenTypes.METHOD_DEF);
final DetailAST someOtherMethod = equalsMethodNode.getNextSibling();

assertTrue(CheckUtils.isEqualsMethod(equalsMethodNode));
assertFalse(CheckUtils.isEqualsMethod(someOtherMethod));
}

@Test
public void testIsElseIf() throws Exception {
final DetailAST targetMethodNode = getNodeFromFile(TokenTypes.METHOD_DEF).getNextSibling();
final DetailAST firstElseNode = getNode(targetMethodNode, TokenTypes.LITERAL_ELSE);
final DetailAST ifElseWithCurlyBraces = firstElseNode.getFirstChild().getFirstChild();
final DetailAST ifElse = getNode(firstElseNode.getParent().getNextSibling(),
TokenTypes.LITERAL_ELSE).getFirstChild();
final DetailAST ifWithoutElse =
firstElseNode.getParent().getNextSibling().getNextSibling();

assertTrue(CheckUtils.isElseIf(ifElseWithCurlyBraces));
assertTrue(CheckUtils.isElseIf(ifElse));
assertFalse(CheckUtils.isElseIf(ifWithoutElse));
}

@Test
public void testIsNonViodMethod() throws Exception {
final DetailAST nonVoidMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
final DetailAST voidMethod = nonVoidMethod.getNextSibling();

assertTrue(CheckUtils.isNonVoidMethod(nonVoidMethod));
assertFalse(CheckUtils.isNonVoidMethod(voidMethod));
}

@Test
public void testIsGetterMethod() throws Exception {
final DetailAST notGetterMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
final DetailAST getterMethod = notGetterMethod.getNextSibling().getNextSibling();

assertTrue(CheckUtils.isGetterMethod(getterMethod));
assertFalse(CheckUtils.isGetterMethod(notGetterMethod));
}

@Test
public void testIsSetterMethod() throws Exception {
final DetailAST firstClassMethod = getNodeFromFile(TokenTypes.METHOD_DEF);
final DetailAST setterMethod =
firstClassMethod.getNextSibling().getNextSibling().getNextSibling();
final DetailAST notSetterMethod = setterMethod.getNextSibling();

assertTrue(CheckUtils.isSetterMethod(setterMethod));
assertFalse(CheckUtils.isSetterMethod(notSetterMethod));
}

@Test
public void testGetAccessModifierFromModifiersToken() throws Exception {
final DetailAST privateVariable = getNodeFromFile(TokenTypes.VARIABLE_DEF);
final DetailAST protectedVariable = privateVariable.getNextSibling();
final DetailAST publicVariable = protectedVariable.getNextSibling();
final DetailAST packageVariable = publicVariable.getNextSibling();

assertEquals(AccessModifier.PRIVATE,
CheckUtils.getAccessModifierFromModifiersToken(privateVariable.getFirstChild()));
assertEquals(AccessModifier.PROTECTED,
CheckUtils.getAccessModifierFromModifiersToken(protectedVariable.getFirstChild()));
assertEquals(AccessModifier.PUBLIC,
CheckUtils.getAccessModifierFromModifiersToken(publicVariable.getFirstChild()));
assertEquals(AccessModifier.PACKAGE,
CheckUtils.getAccessModifierFromModifiersToken(packageVariable.getFirstChild()));
}

@Test
public void testGetFirstNode() throws Exception {
final DetailAST classDef = getNodeFromFile(TokenTypes.CLASS_DEF);

assertEquals(classDef.getFirstChild().getFirstChild(), CheckUtils.getFirstNode(classDef));
}

@Test
public void testIsReceiverParameter() throws Exception {
final DetailAST objBlock = getNodeFromFile(TokenTypes.OBJBLOCK);
final DetailAST methodWithReceiverParameter = objBlock.getLastChild().getPreviousSibling();
final DetailAST receiverParameter =
getNode(methodWithReceiverParameter, TokenTypes.PARAMETER_DEF);
final DetailAST simpleParameter =
receiverParameter.getNextSibling().getNextSibling();

assertTrue(CheckUtils.isReceiverParameter(receiverParameter));
assertFalse(CheckUtils.isReceiverParameter(simpleParameter));
}

@Test
public void testParseDouble() throws Exception {
assertEquals(1.0, CheckUtils.parseDouble("1", TokenTypes.NUM_INT), 0);
assertEquals(-0.05, CheckUtils.parseDouble("-0.05f", TokenTypes.NUM_FLOAT), 0);
assertEquals(8.0, CheckUtils.parseDouble("8L", TokenTypes.NUM_LONG), 0);
assertEquals(0.0, CheckUtils.parseDouble("0.0", TokenTypes.NUM_DOUBLE), 0);
assertEquals(2915.0, CheckUtils.parseDouble("0x0B63", TokenTypes.NUM_INT), 0);
assertEquals(2.147_483_647E10,
CheckUtils.parseDouble("21474836470", TokenTypes.NUM_LONG), 0);
assertEquals(59.0, CheckUtils.parseDouble("073L", TokenTypes.NUM_LONG), 0);
}

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

private static DetailAST getNodeFromFile(int type) throws Exception {
return getNode(parseFile(getPath("InputCheckUtilsTest.java")), type);
}

private static DetailAST getNode(DetailAST root, int type) {
final Optional<DetailAST> node = findTokenInAstByPredicate(root,
ast -> ast.getType() == type);

if (!node.isPresent()) {
fail("Cannot find node of specified type: " + type);
}

return node.get();
}
}
@@ -0,0 +1,69 @@
import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Objects;

/**
* It is class.
* @param <V> ssss
* @param <C> dddd
*/
public class InputCheckUtilsTest<V, C> {
private Map<String, Integer> field = new HashMap<>();
protected int[] array = new int[10];
public static final Long VAR_1 = 1L;
static final double VAR_2 = 5.0;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
InputCheckUtilsTest<V, C> that = (InputCheckUtilsTest<V, C>) o;
return Objects.equal(field, that.field) &&
Objects.equal(array, that.array);
}

public void doSomething(int value) {
if (field.isEmpty()) {
field.put(String.valueOf(value), value << 1);
} else {
if(!field.containsKey(String.valueOf(value))){
field.put(String.valueOf(value), value << 1);
}
}

if(!field.containsKey(String.valueOf(value))){
field.put(String.valueOf(value), value << 1);
} else if (value == 10) {
array[9] = -1;
}

if(field.size() < 10){
array[9] = field.getOrDefault(String.valueOf(value), -1);
}

}

public Map<String, Integer> getField() {
return new HashMap<>(field);
}

public void setField(Map<String, Integer> field) {
this.field = field;
}

public int [] setArray(int... array) {
this.array = array;
if(array.length > 0){
return this.array;
} else {
return new int[4];
}
}

public void testReceiver(InputCheckUtilsTest<V, C> this, int variable) {}
}

0 comments on commit 602fd7d

Please sign in to comment.