Improved UT coverage for CustomImportOrder #1128

This commit is contained in:
Aleksandr Ivanov 2015-06-26 08:24:14 +03:00 committed by Roman Ivanov
parent d0e8581e9c
commit 6f51fa7e63
5 changed files with 86 additions and 29 deletions

View File

@ -803,12 +803,10 @@
<regex><pattern>.*.checks.header.RegexpHeaderCheck</pattern><branchRate>87</branchRate><lineRate>93</lineRate></regex>
<regex><pattern>.*.checks.imports.CustomImportOrderCheck</pattern><branchRate>93</branchRate><lineRate>91</lineRate></regex>
<regex><pattern>.*.checks.imports.Guard</pattern><branchRate>86</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.imports.CustomImportOrderCheck</pattern><branchRate>98</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.imports.ImportControlCheck</pattern><branchRate>85</branchRate><lineRate>73</lineRate></regex>
<regex><pattern>.*.checks.imports.ImportControlLoader</pattern><branchRate>72</branchRate><lineRate>88</lineRate></regex>
<regex><pattern>.*.checks.imports.ImportOrderCheck</pattern><branchRate>91</branchRate><lineRate>99</lineRate></regex>
<regex><pattern>.*.checks.imports.PkgControl</pattern><branchRate>80</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.indentation.ArrayInitHandler</pattern><branchRate>83</branchRate><lineRate>97</lineRate></regex>

View File

@ -296,22 +296,16 @@ public class CustomImportOrderCheck extends Check {
/**
* Sets a custom import order from the rules in the string format specified
* by user.
* @param inputCustoimportOrder
* @param inputCustomImportOrder
* user value.
*/
public final void setCustomImportOrderRules(final String inputCustoimportOrder) {
public final void setCustomImportOrderRules(final String inputCustomImportOrder) {
customImportOrderRules.clear();
try {
for (String currentState : inputCustoimportOrder
.split("\\s*###\\s*")) {
addRuleastoList(currentState);
}
customImportOrderRules.add(NON_GROUP_RULE_GROUP);
}
catch (StringIndexOutOfBoundsException exp) {
//if the structure of the input rule isn't correct
throw new RuntimeException("Unable to parse input rule: " + exp);
for (String currentState : inputCustomImportOrder
.split("\\s*###\\s*")) {
addRuleastoList(currentState);
}
customImportOrderRules.add(NON_GROUP_RULE_GROUP);
}
@Override
@ -340,8 +334,7 @@ public class CustomImportOrderCheck extends Check {
@Override
public void visitToken(DetailAST ast) {
if (ast.getType() == TokenTypes.PACKAGE_DEF) {
if (customImportOrderRules.contains(SAME_PACKAGE_RULE_GROUP)
&& samePackageMatchingDepth != -1) {
if (customImportOrderRules.contains(SAME_PACKAGE_RULE_GROUP)) {
samePackageDomainsRegExp = createSamePackageRegexp(
samePackageMatchingDepth, ast);
}
@ -640,12 +633,7 @@ public class CustomImportOrderCheck extends Check {
final String rule = ruleStr.substring(ruleStr.indexOf('(') + 1,
ruleStr.indexOf(')'));
try {
samePackageMatchingDepth = Integer.parseInt(rule);
}
catch (NumberFormatException e) {
samePackageDomainsRegExp = rule;
}
samePackageMatchingDepth = Integer.parseInt(rule);
if (samePackageMatchingDepth <= 0) {
throw new IllegalArgumentException(
"SAME_PACKAGE rule parameter should be positive integer: " + ruleStr);

View File

@ -22,9 +22,11 @@ package com.puppycrawl.tools.checkstyle.checks.imports;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import java.io.File;
import java.lang.reflect.Method;
import org.junit.Test;
@ -33,6 +35,7 @@ import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCh
import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_NONGROUP_IMPORT;
import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_ORDER;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class CustomImportOrderCheckTest extends BaseCheckTestSupport {
/**
@ -73,7 +76,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport {
* @throws Exception
*/
@Test
public void testDefaultPackage() throws Exception {
public void testGoogleStyleguideConfiguraiton() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(CustomImportOrderCheck.class);
checkConfig.addAttribute("thirdPartyPackageRegExp", "com.|org.");
@ -326,6 +329,28 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport {
assertArrayEquals(expected, actual);
}
@Test
// UT uses Reflection to avoid removing null-validation from static method,
// which is a candidate for utility method in the future
public void testGetFullImportIdent() {
Object actual;
try {
Class<?> c = Class.forName(
"com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck");
Object t = c.newInstance();
Method m = c.getDeclaredMethod("getFullImportIdent", DetailAST.class);
m.setAccessible(true);
actual = m.invoke(t, (DetailAST) null);
}
catch (Exception e) {
e.printStackTrace();
actual = null;
}
String expected = "";
assertEquals(expected, (String) actual);
}
@Test(expected = CheckstyleException.class)
public void testSamePackageDepthNegative() throws Exception {
final DefaultConfiguration checkConfig =
@ -336,9 +361,8 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport {
"SAME_PACKAGE(-1)");
final String[] expected = {};
verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/"
+ "checkstyle/imports/"
+ "InputCustomImportOrderSamePackageDepth2-5.java").getCanonicalPath(), expected);
verify(checkConfig, getPath("imports" + File.separator
+ "InputCustomImportOrder.java"), expected);
}
@Test(expected = CheckstyleException.class)
@ -351,9 +375,50 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport {
"SAME_PACKAGE(0)");
final String[] expected = {};
verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/"
verify(checkConfig, getPath("imports" + File.separator
+ "InputCustomImportOrder.java"), expected);
}
@Test(expected = CheckstyleException.class)
public void testUnsupportedRule() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(CustomImportOrderCheck.class);
checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(3)###UNSUPPORTED_RULE"); //#AAA##BBBB###CCCC####DDDD
checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
final String[] expected = {
"4: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"),
"6: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"),
"7: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"),
"8: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"),
"9: " + getCheckMessage(MSG_ORDER, "SAME_PACKAGE"),
};
verify(checkConfig, getPath("imports" + File.separator
+ "InputCustomImportOrder.java"), expected);
}
@Test(expected = CheckstyleException.class)
public void testSamePackageDepthNotInt() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(CustomImportOrderCheck.class);
checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(INT_IS_REQUIRED_HERE)");
checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
final String[] expected = {};
verify(checkConfig, getPath("imports" + File.separator
+ "InputCustomImportOrder.java"), expected);
}
@Test
public void testNoImports() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(CustomImportOrderCheck.class);
checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(3)");
final String[] expected = {};
verify(checkConfig, new File("src/test/resources/com/puppycrawl/tools/"
+ "checkstyle/imports/"
+ "InputCustomImportOrderSamePackageDepth2-5.java").getCanonicalPath(), expected);
+ "InputCustomImportOrder_NoImports.java").getCanonicalPath(), expected);
}
}

View File

@ -9,6 +9,7 @@ import javax.swing.*;
import static sun.tools.util.CommandLine.parse;
import static sun.tools.util.ModifierFilter.ALL_ACCESS;
import static sun.tools.util.ModifierFilter.ALL_ACCESS;
public class InputCustomImportOrderThirdPartyPackage
{

View File

@ -0,0 +1,5 @@
package com.puppycrawl.tools.checkstyle.imports;
public class InputCustomImportOrder_NoImports {
}