Skip to content

Commit

Permalink
Issue #2482: Exclude lines with package in LineLength check
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and romani committed Oct 31, 2015
1 parent f30512c commit 9a39d19
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
* </p>
*
* <p>
* Import statements (lines matching pattern {@code ^import .*}) are not verified by this check.
* Package statements and import statements (lines matching pattern
* {@code ^(package|import) .*}), and are not verified by this check.
* </p>
* <p>
* The default maximum allowable line length is 80 characters. To change the
Expand Down Expand Up @@ -84,8 +85,8 @@ public class LineLengthCheck extends Check {
/** Default maximum number of columns in a line. */
private static final int DEFAULT_MAX_COLUMNS = 80;

/** Pattern matching import and import static statements. */
private static final Pattern IMPORT_PATTERN = Pattern.compile("^import .*");
/** Patterns matching package, import, and import static statements. */
private static final Pattern IGNORE_PATTERN = Pattern.compile("^(package|import) .*");

/** The maximum number of columns in a line. */
private int max = DEFAULT_MAX_COLUMNS;
Expand Down Expand Up @@ -124,7 +125,7 @@ public void beginTree(DetailAST rootAST) {
final int realLength = CommonUtils.lengthExpandedTabs(
line, line.length(), getTabWidth());

if (realLength > max && !IMPORT_PATTERN.matcher(line).find()
if (realLength > max && !IGNORE_PATTERN.matcher(line).find()
&& !ignorePattern.matcher(line).find()) {
log(i + 1, MSG_KEY, max, realLength);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ protected String getPath(String filename) throws IOException {
+ "sizes" + File.separator + filename);
}

@Override
protected String getNonCompilablePath(String filename) throws IOException {
return super.getNonCompilablePath("checks" + File.separator
+ "sizes" + File.separator + filename);
}

@Test
public void testGetRequiredTokens() {
final LineLengthCheck checkObj = new LineLengthCheck();
Expand Down Expand Up @@ -89,4 +95,15 @@ public void shouldNotLogLongImportStatements() throws Exception {
};
verify(checkConfig, getPath("InputLongImportStatements.java"), expected);
}

@Test
public void shouldNotLogLongPackageStatements() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(LineLengthCheck.class);
checkConfig.addAttribute("max", "80");
final String[] expected = {
"6: " + getCheckMessage(MSG_KEY, 80, 88),
};
verify(checkConfig, getNonCompilablePath("InputLongPackageStatement.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nameofcompany.nameofdivision.nameofproject.systemtests.parallel.areaoftest.featuretested.flowtested;

public class InputLongImportStatements {
@Override
public String toString() {
return "This is very long line that should be logged because it is not package";
}
}
2 changes: 1 addition & 1 deletion src/xdocs/config_sizes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
property <code>tabWidth</code> for <code>LineLength</code> alone.
</li>
<li>
Import statements (lines matching pattern <code>^import .*</code>) are not verified by
Package and import statements (lines matching pattern <code>^(package|import) .*</code>) are not verified by
this check.
</li>
</ul>
Expand Down

0 comments on commit 9a39d19

Please sign in to comment.