Skip to content

Commit

Permalink
Issue checkstyle#7733: Update AbstractChecks to log DetailAST - Impor…
Browse files Browse the repository at this point in the history
…tOrder
  • Loading branch information
DXTkastb authored and RayRCaringal committed Apr 7, 2020
1 parent 4d0e525 commit f2ac27c
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 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
////////////////////////////////////////////////////////////////////////////////

package org.checkstyle.suppressionxpathfilter;

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck;

public class XpathRegressionImportOrderTest extends AbstractXpathTestSupport {

private final String checkName = ImportOrderCheck.class.getSimpleName();

@Override
protected String getCheckName() {
return checkName;
}

@Test
public void testOne() throws Exception {
final File fileToProcess =
new File(getPath("SuppressionXpathRegressionImportOrderOne.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(ImportOrderCheck.class);

final String[] expectedViolation = {
"4:1: " + getCheckMessage(ImportOrderCheck.class,
ImportOrderCheck.MSG_ORDERING, "java.util.Set"),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/IMPORT"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

@Test
public void testTwo() throws Exception {
final File fileToProcess =
new File(getPath("SuppressionXpathRegressionImportOrderTwo.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(ImportOrderCheck.class);

final String[] expectedViolation = {
"5:1: " + getCheckMessage(ImportOrderCheck.class,
ImportOrderCheck.MSG_SEPARATED_IN_GROUP, "java.util.Set"),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/IMPORT[./DOT/IDENT[@text='Set']]"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

@Test
public void testThree() throws Exception {
final File fileToProcess =
new File(getPath("SuppressionXpathRegressionImportOrderThree.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(ImportOrderCheck.class);
moduleConfig.addAttribute("groups", "/^java\\./,javax,org");
moduleConfig.addAttribute("separated", "true");

final String[] expectedViolation = {
"4:1: " + getCheckMessage(ImportOrderCheck.class,
ImportOrderCheck.MSG_SEPARATION, "org.junit.*"),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/IMPORT[./DOT/DOT/IDENT[@text='org']]"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

@Test
public void testFour() throws Exception {
final File fileToProcess =
new File(getPath("SuppressionXpathRegressionImportOrderFour.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(ImportOrderCheck.class);
moduleConfig.addAttribute("option", "inflow");

final String[] expectedViolation = {
"5:1: " + getCheckMessage(ImportOrderCheck.class,
ImportOrderCheck.MSG_ORDERING, "java.lang.Math.PI"),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/STATIC_IMPORT"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

@Test
public void testFive() throws Exception {
final File fileToProcess =
new File(getPath("SuppressionXpathRegressionImportOrderFive.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(ImportOrderCheck.class);
moduleConfig.addAttribute("groups", "/^java\\./,javax,org");

final String[] expectedViolation = {
"5:1: " + getCheckMessage(ImportOrderCheck.class,
ImportOrderCheck.MSG_ORDERING, "java.util.Date"),
};

final List<String> expectedXpathQueries = Collections.singletonList(
"/IMPORT[./DOT/IDENT[@text='Date']]"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.checkstyle.suppressionxpathfilter.importorder;

import java.util.List;
import static org.junit.After.*;
import java.util.Date; // warn

public class SuppressionXpathRegressionImportOrderFive {
// code
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.checkstyle.suppressionxpathfilter.importorder;

import java.io.*;
import java.util.Set;
import static java.lang.Math.PI; // warn

public class SuppressionXpathRegressionImportOrderFour {
// code
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.checkstyle.suppressionxpathfilter.importorder;

import static java.lang.Math.PI;
import java.util.Set; // warn

public class SuppressionXpathRegressionImportOrderOne {
// code
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.checkstyle.suppressionxpathfilter.importorder;

import java.io.*;
import org.junit.*; // warn

public class SuppressionXpathRegressionImportOrderThree {
// code
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.checkstyle.suppressionxpathfilter.importorder;

import java.util.List;

import java.util.Set; // warn

public class SuppressionXpathRegressionImportOrderTwo {
// code
}
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,6 @@ public void beginTree(DetailAST rootAST) {
// -@cs[CyclomaticComplexity] SWITCH was transformed into IF-ELSE.
@Override
public void visitToken(DetailAST ast) {
final int line = ast.getLineNo();
final FullIdent ident;
final boolean isStatic;

Expand All @@ -628,15 +627,15 @@ public void visitToken(DetailAST ast) {
// https://github.com/checkstyle/checkstyle/issues/1387
if (option == ImportOrderOption.TOP || option == ImportOrderOption.ABOVE) {
final boolean isStaticAndNotLastImport = isStatic && !lastImportStatic;
doVisitToken(ident, isStatic, isStaticAndNotLastImport, line);
doVisitToken(ident, isStatic, isStaticAndNotLastImport, ast);
}
else if (option == ImportOrderOption.BOTTOM || option == ImportOrderOption.UNDER) {
final boolean isLastImportAndNonStatic = lastImportStatic && !isStatic;
doVisitToken(ident, isStatic, isLastImportAndNonStatic, line);
doVisitToken(ident, isStatic, isLastImportAndNonStatic, ast);
}
else if (option == ImportOrderOption.INFLOW) {
// "previous" argument is useless here
doVisitToken(ident, isStatic, true, line);
doVisitToken(ident, isStatic, true, ast);
}
else {
throw new IllegalStateException(
Expand All @@ -655,25 +654,27 @@ else if (option == ImportOrderOption.INFLOW) {
* @param isStatic whether the token is static or not.
* @param previous previous non-static but current is static (above), or
* previous static but current is non-static (under).
* @param line the line of the current import.
* @param ast node of the AST.
*/
private void doVisitToken(FullIdent ident, boolean isStatic, boolean previous, int line) {
private void doVisitToken(FullIdent ident, boolean isStatic, boolean previous, DetailAST ast) {
final String name = ident.getText();
final int groupIdx = getGroupNumber(isStatic && staticImportsApart, name);

if (groupIdx > lastGroup) {
if (!beforeFirstImport && line - lastImportLine < 2 && needSeparator(isStatic)) {
log(line, MSG_SEPARATION, name);
if (!beforeFirstImport
&& ast.getLineNo() - lastImportLine < 2
&& needSeparator(isStatic)) {
log(ast, MSG_SEPARATION, name);
}
}
else if (groupIdx == lastGroup) {
doVisitTokenInSameGroup(isStatic, previous, name, line);
doVisitTokenInSameGroup(isStatic, previous, name, ast);
}
else {
log(line, MSG_ORDERING, name);
log(ast, MSG_ORDERING, name);
}
if (isSeparatorInGroup(groupIdx, isStatic, line)) {
log(line, MSG_SEPARATED_IN_GROUP, name);
if (isSeparatorInGroup(groupIdx, isStatic, ast.getLineNo())) {
log(ast, MSG_SEPARATED_IN_GROUP, name);
}

lastGroup = groupIdx;
Expand Down Expand Up @@ -728,14 +729,14 @@ private boolean isSeparatorBeforeImport(int line) {
* @param previous previous non-static but current is static (above), or
* previous static but current is non-static (under).
* @param name the name of the current import.
* @param line the line of the current import.
* @param ast node of the AST.
*/
private void doVisitTokenInSameGroup(boolean isStatic,
boolean previous, String name, int line) {
boolean previous, String name, DetailAST ast) {
if (ordered) {
if (option == ImportOrderOption.INFLOW) {
if (isWrongOrder(name, isStatic)) {
log(line, MSG_ORDERING, name);
log(ast, MSG_ORDERING, name);
}
}
else {
Expand All @@ -751,7 +752,7 @@ private void doVisitTokenInSameGroup(boolean isStatic,
&& isWrongOrder(name, isStatic);

if (shouldFireError) {
log(line, MSG_ORDERING, name);
log(ast, MSG_ORDERING, name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@
* IllegalCatch
* </li>
* <li>
* ImportOrder
* </li>
* <li>
* Indentation
* </li>
* <li>
Expand Down

0 comments on commit f2ac27c

Please sign in to comment.