Refactored UTs, metrics package, issue #537

This commit is contained in:
alexkravin 2015-02-20 17:00:46 +04:00
parent 0b98c0389e
commit d8f8de2b53
12 changed files with 114 additions and 44 deletions

View File

@ -33,6 +33,13 @@ import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
*/
public final class BooleanExpressionComplexityCheck extends Check
{
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "booleanExpressionComplexity";
/** Default allowed complexity. */
private static final int DEFAULT_MAX = 3;
@ -230,7 +237,7 @@ public final class BooleanExpressionComplexityCheck extends Check
final DetailAST parentAST = ast.getParent();
log(parentAST.getLineNo(), parentAST.getColumnNo(),
"booleanExpressionComplexity", count, getMax());
MSG_KEY, count, getMax());
}
}
}

View File

@ -30,6 +30,13 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public final class ClassDataAbstractionCouplingCheck
extends AbstractClassCouplingCheck
{
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "classDataAbstractionCoupling";
/** Default allowed complexity. */
private static final int DEFAULT_MAX = 7;
@ -67,6 +74,6 @@ public final class ClassDataAbstractionCouplingCheck
@Override
protected String getLogMessageId()
{
return "classDataAbstractionCoupling";
return MSG_KEY;
}
}

View File

@ -30,6 +30,13 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
*/
public final class ClassFanOutComplexityCheck extends AbstractClassCouplingCheck
{
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "classFanOutComplexity";
/** default value of max value. */
private static final int DEFAULT_MAX = 20;
@ -72,6 +79,6 @@ public final class ClassFanOutComplexityCheck extends AbstractClassCouplingCheck
@Override
protected String getLogMessageId()
{
return "classFanOutComplexity";
return MSG_KEY;
}
}

View File

@ -37,6 +37,13 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
public class CyclomaticComplexityCheck
extends AbstractComplexityCheck
{
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "cyclomaticComplexity";
/** default allowed complexity */
private static final int DEFAULT_VALUE = 10;
@ -95,6 +102,6 @@ public class CyclomaticComplexityCheck
@Override
protected final String getMessageID()
{
return "cyclomaticComplexity";
return MSG_KEY;
}
}

View File

@ -37,6 +37,25 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
*/
public class JavaNCSSCheck extends Check
{
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_METHOD = "ncss.method";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_CLASS = "ncss.class";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_FILE = "ncss.file";
/** default constant for max file ncss */
private static final int FILE_MAX_NCSS = 2000;
@ -210,7 +229,7 @@ public class JavaNCSSCheck extends Check
final int count = counter.getCount();
if (count > methodMax) {
log(ast.getLineNo(), ast.getColumnNo(), "ncss.method",
log(ast.getLineNo(), ast.getColumnNo(), MSG_METHOD,
count, methodMax);
}
}
@ -220,7 +239,7 @@ public class JavaNCSSCheck extends Check
final int count = counter.getCount();
if (count > classMax) {
log(ast.getLineNo(), ast.getColumnNo(), "ncss.class",
log(ast.getLineNo(), ast.getColumnNo(), MSG_CLASS,
count, classMax);
}
}
@ -234,7 +253,7 @@ public class JavaNCSSCheck extends Check
final int count = counter.getCount();
if (count > fileMax) {
log(rootAST.getLineNo(), rootAST.getColumnNo(), "ncss.file",
log(rootAST.getLineNo(), rootAST.getColumnNo(), MSG_FILE,
count, fileMax);
}
}

View File

@ -37,6 +37,13 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
*/
public final class NPathComplexityCheck extends AbstractComplexityCheck
{
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "npathComplexity";
/** Default allowed complexity. */
private static final int DEFAULT_MAX = 200;
@ -137,7 +144,7 @@ public final class NPathComplexityCheck extends AbstractComplexityCheck
@Override
protected String getMessageID()
{
return "npathComplexity";
return MSG_KEY;
}
/** Visits else, catch or case. */

View File

@ -23,6 +23,9 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import java.io.File;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.metrics.BooleanExpressionComplexityCheck
.MSG_KEY;
public class BooleanExpressionComplexityCheckTest extends BaseCheckTestSupport
{
@Test
@ -32,8 +35,8 @@ public class BooleanExpressionComplexityCheckTest extends BaseCheckTestSupport
createCheckConfig(BooleanExpressionComplexityCheck.class);
String[] expected = {
"13:9: Boolean expression complexity is 4 (max allowed is 3).",
"32:9: Boolean expression complexity is 6 (max allowed is 3).",
"13:9: " + getCheckMessage(MSG_KEY, 4, 3),
"32:9: " + getCheckMessage(MSG_KEY, 6, 3),
};
verify(checkConfig, getPath("metrics" + File.separator + "BooleanExpressionComplexityCheckTestInput.java"), expected);

View File

@ -23,6 +23,9 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import java.io.File;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck
.MSG_KEY;
public class ClassDataAbstractionCouplingCheckTest extends BaseCheckTestSupport
{
@Test
@ -35,9 +38,9 @@ public class ClassDataAbstractionCouplingCheckTest extends BaseCheckTestSupport
checkConfig.addAttribute("excludedClasses", "InnerClass");
String[] expected = {
"6:1: Class Data Abstraction Coupling is 4 (max allowed is 0) classes [AnotherInnerClass, HashMap, HashSet, int].",
"7:5: Class Data Abstraction Coupling is 1 (max allowed is 0) classes [ArrayList].",
"27:1: Class Data Abstraction Coupling is 2 (max allowed is 0) classes [HashMap, HashSet].",
"6:1: " + getCheckMessage(MSG_KEY, 4, 0, "[AnotherInnerClass, HashMap, HashSet, int]"),
"7:5: " + getCheckMessage(MSG_KEY, 1, 0, "[ArrayList]"),
"27:1: " + getCheckMessage(MSG_KEY, 2, 0, "[HashMap, HashSet]"),
};
verify(checkConfig,

View File

@ -23,6 +23,8 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import java.io.File;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck.MSG_KEY;
public class ClassFanOutComplexityCheckTest extends BaseCheckTestSupport
{
@Test
@ -33,7 +35,7 @@ public class ClassFanOutComplexityCheckTest extends BaseCheckTestSupport
checkConfig.addAttribute("max", "0");
String[] expected = {
"6:1: Class Fan-Out Complexity is 3 (max allowed is 0).",
"6:1: " + getCheckMessage(MSG_KEY, 3, 0),
};
verify(checkConfig,

View File

@ -22,6 +22,8 @@ import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck.MSG_KEY;
public class CyclomaticComplexityCheckTest
extends BaseCheckTestSupport
{
@ -34,16 +36,16 @@ public class CyclomaticComplexityCheckTest
checkConfig.addAttribute("max", "0");
final String[] expected = {
"4:5: Cyclomatic Complexity is 2 (max allowed is 0).",
"7:17: Cyclomatic Complexity is 2 (max allowed is 0).",
"17:5: Cyclomatic Complexity is 6 (max allowed is 0).",
"27:5: Cyclomatic Complexity is 3 (max allowed is 0).",
"34:5: Cyclomatic Complexity is 5 (max allowed is 0).",
"48:5: Cyclomatic Complexity is 3 (max allowed is 0).",
"58:5: Cyclomatic Complexity is 3 (max allowed is 0).",
"67:5: Cyclomatic Complexity is 3 (max allowed is 0).",
"76:5: Cyclomatic Complexity is 1 (max allowed is 0).",
"79:13: Cyclomatic Complexity is 2 (max allowed is 0).",
"4:5: " + getCheckMessage(MSG_KEY, 2, 0),
"7:17: " + getCheckMessage(MSG_KEY, 2, 0),
"17:5: " + getCheckMessage(MSG_KEY, 6, 0),
"27:5: " + getCheckMessage(MSG_KEY, 3, 0),
"34:5: " + getCheckMessage(MSG_KEY, 5, 0),
"48:5: " + getCheckMessage(MSG_KEY, 3, 0),
"58:5: " + getCheckMessage(MSG_KEY, 3, 0),
"67:5: " + getCheckMessage(MSG_KEY, 3, 0),
"76:5: " + getCheckMessage(MSG_KEY, 1, 0),
"79:13: " + getCheckMessage(MSG_KEY, 2, 0),
};
verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected);

View File

@ -23,6 +23,10 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import java.io.File;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck.MSG_CLASS;
import static com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck.MSG_FILE;
import static com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck.MSG_METHOD;
/**
* Testcase for the JavaNCSS-Check.
*
@ -41,15 +45,15 @@ public class JavaNCSSCheckTest extends BaseCheckTestSupport
checkConfig.addAttribute("fileMaximum", "2");
String[] expected = {
"2:1: NCSS for this file is 35 (max allowed is 2).",
"9:1: NCSS for this class is 22 (max allowed is 1).",
"14:5: NCSS for this method is 2 (max allowed is 0).",
"21:5: NCSS for this method is 4 (max allowed is 0).",
"30:5: NCSS for this method is 12 (max allowed is 0).",
"42:13: NCSS for this method is 2 (max allowed is 0).",
"49:5: NCSS for this class is 2 (max allowed is 1).",
"56:1: NCSS for this class is 10 (max allowed is 1).",
"61:5: NCSS for this method is 8 (max allowed is 0).",
"2:1: " + getCheckMessage(MSG_FILE, 35, 2),
"9:1: " + getCheckMessage(MSG_CLASS, 22, 1),
"14:5: " + getCheckMessage(MSG_METHOD, 2, 0),
"21:5: " + getCheckMessage(MSG_METHOD, 4, 0),
"30:5: " + getCheckMessage(MSG_METHOD, 12, 0),
"42:13: " + getCheckMessage(MSG_METHOD, 2, 0),
"49:5: " + getCheckMessage(MSG_CLASS, 2, 1),
"56:1: " + getCheckMessage(MSG_CLASS, 10, 1),
"61:5: " + getCheckMessage(MSG_METHOD, 8, 0),
};
verify(checkConfig, getPath("metrics" + File.separator

View File

@ -23,6 +23,8 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import java.text.NumberFormat;
import org.junit.Test;
import static com.puppycrawl.tools.checkstyle.checks.metrics.NPathComplexityCheck.MSG_KEY;
public class NPathComplexityCheckTest extends BaseCheckTestSupport
{
@Test
@ -33,16 +35,16 @@ public class NPathComplexityCheckTest extends BaseCheckTestSupport
checkConfig.addAttribute("max", "0");
String[] expected = {
"4:5: NPath Complexity is 2 (max allowed is 0).",
"7:17: NPath Complexity is 2 (max allowed is 0).",
"17:5: NPath Complexity is 5 (max allowed is 0).",
"27:5: NPath Complexity is 3 (max allowed is 0).",
"34:5: NPath Complexity is 7 (max allowed is 0).",
"48:5: NPath Complexity is 3 (max allowed is 0).",
"58:5: NPath Complexity is 3 (max allowed is 0).",
"67:5: NPath Complexity is 3 (max allowed is 0).",
"76:5: NPath Complexity is 1 (max allowed is 0).",
"79:13: NPath Complexity is 2 (max allowed is 0).",
"4:5: " + getCheckMessage(MSG_KEY, 2, 0),
"7:17: " + getCheckMessage(MSG_KEY, 2, 0),
"17:5: " + getCheckMessage(MSG_KEY, 5, 0),
"27:5: " + getCheckMessage(MSG_KEY, 3, 0),
"34:5: " + getCheckMessage(MSG_KEY, 7, 0),
"48:5: " + getCheckMessage(MSG_KEY, 3, 0),
"58:5: " + getCheckMessage(MSG_KEY, 3, 0),
"67:5: " + getCheckMessage(MSG_KEY, 3, 0),
"76:5: " + getCheckMessage(MSG_KEY, 1, 0),
"79:13: " + getCheckMessage(MSG_KEY, 2, 0),
};
verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected);
@ -61,7 +63,7 @@ public class NPathComplexityCheckTest extends BaseCheckTestSupport
final String expectedComplexity = NumberFormat.getInstance().format(largerThanMaxInt);
String[] expected = {
"9:5: NPath Complexity is " + expectedComplexity + " (max allowed is 0).",
"9:5: " + getCheckMessage(MSG_KEY, largerThanMaxInt, 0),
};
verify(checkConfig, getPath("ComplexityOverflow.java"), expected);