Coverage has been increaded to 100% in HeaderCheck. Issue #1292

This commit is contained in:
Ilja Dubinin 2015-07-29 00:19:30 +01:00 committed by Roman Ivanov
parent e3e0c92185
commit 47cfb3621d
4 changed files with 51 additions and 138 deletions

View File

@ -1112,17 +1112,13 @@
<regex><pattern>.*.checks.coding.SimplifyBooleanReturnCheck</pattern><branchRate>83</branchRate><lineRate>100</lineRate></regex>
<regex><pattern>.*.checks.coding.VariableDeclarationUsageDistanceCheck</pattern><branchRate>90</branchRate><lineRate>98</lineRate></regex>
<regex><pattern>.*.checks.header.AbstractHeaderCheck</pattern><branchRate>90</branchRate><lineRate>87</lineRate></regex>
<regex><pattern>.*.checks.header.HeaderCheck</pattern><branchRate>31</branchRate><lineRate>50</lineRate></regex>
<regex><pattern>.*.checks.javadoc.AbstractJavadocCheck</pattern><branchRate>90</branchRate><lineRate>93</lineRate></regex>
<regex><pattern>.*.checks.javadoc.AbstractJavadocCheck\$.*</pattern><branchRate>50</branchRate><lineRate>68</lineRate></regex>
<regex><pattern>.*.checks.javadoc.AtclauseOrderCheck</pattern><branchRate>88</branchRate><lineRate>88</lineRate></regex>
<regex><pattern>.*.checks.javadoc.JavadocMethodCheck</pattern><branchRate>91</branchRate><lineRate>98</lineRate></regex>
<regex><pattern>.*.checks.javadoc.WriteTagCheck</pattern><branchRate>100</branchRate><lineRate>91</lineRate></regex>
<regex><pattern>.*.checks.header.AbstractHeaderCheck</pattern><branchRate>90</branchRate><lineRate>87</lineRate></regex>
<regex><pattern>.*.checks.metrics.AbstractClassCouplingCheck</pattern><branchRate>87</branchRate><lineRate>97</lineRate></regex>
<regex><pattern>.*.checks.metrics.AbstractClassCouplingCheck\$.*</pattern><branchRate>78</branchRate><lineRate>100</lineRate></regex>

View File

@ -76,7 +76,7 @@ public class HeaderCheck extends AbstractHeaderCheck {
* @param list comma separated list of line numbers to ignore in header.
*/
public void setIgnoreLines(int... list) {
if (list == null || list.length == 0) {
if (list.length == 0) {
ignoreLines = EMPTY_INT_ARRAY;
return;
}

View File

@ -23,9 +23,6 @@ import static com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck.MSG_MISM
import static com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck.MSG_MISSING;
import static org.junit.Assert.fail;
import java.io.File;
import java.net.URI;
import org.junit.Test;
import com.puppycrawl.tools.checkstyle.BaseFileSetCheckTestSupport;
@ -33,10 +30,11 @@ import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
public class HeaderCheckTest extends BaseFileSetCheckTestSupport {
@Test
public void testStaticHeader() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(HeaderCheck.class);
createCheckConfig(HeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("configs/java.header"));
checkConfig.addAttribute("ignoreLines", "");
final String[] expected = {
@ -45,135 +43,11 @@ public class HeaderCheckTest extends BaseFileSetCheckTestSupport {
verify(checkConfig, getPath("inputHeader.java"), expected);
}
@Test
public void testRegexpHeader() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("regexp.header"));
final String[] expected = {
"3: " + getCheckMessage(MSG_MISMATCH, "// Created: 2002"),
};
verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
}
@Test
public void testRegexpHeaderURL() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
URI uri = new File(getPath("regexp.header")).toURI();
checkConfig.addAttribute("headerFile", uri.toString());
final String[] expected = {
"3: " + getCheckMessage(MSG_MISMATCH, "// Created: 2002"),
};
verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
}
@Test
public void testInlineRegexpHeader() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("header", "^/*$\\n// .*\\n// Created: 2002\\n^//.*\\n^//.*");
final String[] expected = {
"3: " + getCheckMessage(MSG_MISMATCH, "// Created: 2002"),
};
verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
}
@Test
public void testFailureForMultilineRegexp() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("header", "^(.*\\n.*)");
try {
createChecker(checkConfig);
fail("Checker creation should not succeed when regexp spans multiple lines");
}
catch (CheckstyleException ex) {
// expected exception
}
}
@Test
public void testRegexpHeaderIgnore() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("regexp.header1"));
final String[] expected = {
};
verify(checkConfig, getPath("InputScopeAnonInner.java"), expected);
}
@Test
public void testRegexpHeaderMulti1() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("regexp.header2"));
checkConfig.addAttribute("multiLines", "3, 6");
final String[] expected = {
};
verify(checkConfig, getPath("InputRegexpHeader1.java"), expected);
}
@Test
public void testRegexpHeaderMulti2() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("regexp.header2"));
checkConfig.addAttribute("multiLines", "3, 6");
final String[] expected = {
};
verify(checkConfig, getPath("InputRegexpHeader2.java"), expected);
}
@Test
public void testRegexpHeaderMulti3() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("regexp.header2"));
checkConfig.addAttribute("multiLines", "3, 7");
final String[] expected = {
};
verify(checkConfig, getPath("InputRegexpHeader1.java"), expected);
}
@Test
public void testRegexpHeaderMulti4() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("regexp.header2"));
checkConfig.addAttribute("multiLines", "3, 5, 6, 7");
final String[] expected = {
};
verify(checkConfig, getPath("InputRegexpHeader3.java"), expected);
}
@Test
public void testRegexpHeaderMulti5() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("regexp.header2"));
checkConfig.addAttribute("multiLines", "3");
final String[] expected = {
"1: " + getCheckMessage(MSG_MISSING),
};
verify(checkConfig, getPath("InputRegexpHeader4.java"), expected);
}
@Test
public void testRegexpHeaderSmallHeader() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(RegexpHeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("regexp.header2"));
checkConfig.addAttribute("multiLines", "3, 6");
final String[] expected = {
};
verify(checkConfig, getPath("InputRegexpSmallHeader.java"), expected);
}
@Test
public void testNoHeader() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(HeaderCheck.class);
createCheckConfig(HeaderCheck.class);
try {
createChecker(checkConfig);
final String[] expected = {
@ -189,7 +63,7 @@ public class HeaderCheckTest extends BaseFileSetCheckTestSupport {
@Test
public void testNonExistingHeaderFile() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(HeaderCheck.class);
createCheckConfig(HeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("nonexisting.file"));
try {
createChecker(checkConfig);
@ -203,7 +77,7 @@ public class HeaderCheckTest extends BaseFileSetCheckTestSupport {
@Test
public void testInvalidCharset() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(HeaderCheck.class);
createCheckConfig(HeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("config/java.header"));
checkConfig.addAttribute("charset", "XSO-8859-1");
try {
@ -218,7 +92,7 @@ public class HeaderCheckTest extends BaseFileSetCheckTestSupport {
@Test
public void testEmptyFilename() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(HeaderCheck.class);
createCheckConfig(HeaderCheck.class);
checkConfig.addAttribute("headerFile", "");
try {
createChecker(checkConfig);
@ -228,4 +102,29 @@ public class HeaderCheckTest extends BaseFileSetCheckTestSupport {
// expected exception
}
}
@Test
public void testNotMatch() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(HeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("configs/java.header"));
checkConfig.addAttribute("ignoreLines", "");
final String[] expected = {
"2: " + getCheckMessage(MSG_MISMATCH,
"// checkstyle: Checks Java source code for adherence to a set of rules."),
};
verify(checkConfig, getPath("configs/java2.header"), expected);
}
@Test
public void testIgnore() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(HeaderCheck.class);
checkConfig.addAttribute("headerFile", getPath("configs/java.header"));
checkConfig.addAttribute("ignoreLines", "2");
final String[] expected = {
};
verify(checkConfig, getPath("configs/java2.header"), expected);
}
}

View File

@ -0,0 +1,18 @@
////////////////////////////////////////////////////////////////////////////////
//testvdfvcheckstyle: 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
////////////////////////////////////////////////////////////////////////////////