Skip to content

Commit

Permalink
Issue #7728: Update AbstractChecks to log DetailAST - CustomImportOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
hembhagat99 authored and rnveach committed May 16, 2020
1 parent 7a71e8a commit 8d10bc0
Show file tree
Hide file tree
Showing 13 changed files with 484 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ protected String getPackageLocation() {
@Test
public void testCustomImport1() throws Exception {
final String[] expected = {
"4: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
"4:1: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
"java.io.File.createTempFile"),
"6: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Button"),
"8: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
"12: " + getCheckMessage(clazz, MSG_LEX, "java.io.File", "javax.swing.JTable"),
"13: " + getCheckMessage(clazz, MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
"14: " + getCheckMessage(clazz, MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
"15: " + getCheckMessage(clazz, MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
"17: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"6:1: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Button"),
"8:1: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
"12:1: " + getCheckMessage(clazz, MSG_LEX, "java.io.File", "javax.swing.JTable"),
"13:1: " + getCheckMessage(clazz, MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
"14:1: " + getCheckMessage(clazz, MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
"15:1: " + getCheckMessage(clazz, MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
"17:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"com.google.common.base.Ascii"),
"17: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.base.Ascii",
"17:1: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.base.Ascii",
"javax.swing.JTable"),
};

Expand All @@ -72,22 +72,22 @@ public void testCustomImport1() throws Exception {
@Test
public void testCustomImport2() throws Exception {
final String[] expected = {
"4: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
"4:1: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
"java.io.File.createTempFile"),
"9: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
"11: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
"9:1: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
"11:1: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
"java.util.concurrent.AbstractExecutorService"),
"13: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"13:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"com.google.checkstyle.test.chapter2filebasic.rule21filename.*"),
"13: " + getCheckMessage(clazz, MSG_LEX,
"13:1: " + getCheckMessage(clazz, MSG_LEX,
"com.google.checkstyle.test.chapter2filebasic.rule21filename.*",
"java.util.concurrent.AbstractExecutorService"),
"14: " + getCheckMessage(clazz, MSG_LEX,
"14:1: " + getCheckMessage(clazz, MSG_LEX,
"com.google.checkstyle.test.chapter3filestructure.rule3sourcefile.*",
"java.util.concurrent.AbstractExecutorService"),
"16: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"16:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"com.google.common.reflect.*"),
"16: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
"16:1: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
"java.util.concurrent.AbstractExecutorService"),
};

Expand All @@ -101,22 +101,22 @@ public void testCustomImport2() throws Exception {
@Test
public void testCustomImport3() throws Exception {
final String[] expected = {
"4: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Dialog"),
"5: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
"4:1: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Dialog"),
"5:1: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
"javax.swing.WindowConstants.*"),
"7: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"7:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"com.google.checkstyle.test.chapter2filebasic.rule21filename.*"),
"7: " + getCheckMessage(clazz, MSG_LEX,
"7:1: " + getCheckMessage(clazz, MSG_LEX,
"com.google.checkstyle.test.chapter2filebasic.rule21filename.*", "java.awt.Dialog"),
"8: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
"8:1: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
"java.awt.Dialog"),
"9: " + getCheckMessage(clazz, MSG_LEX,
"9:1: " + getCheckMessage(clazz, MSG_LEX,
"com.google.checkstyle.test.chapter3filestructure.rule3sourcefile.*",
"java.awt.Dialog"),
"11: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
"11:1: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
"java.io.File.createTempFile"),
"13: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
"15: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
"13:1: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
"15:1: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
"java.util.concurrent.AbstractExecutorService"),
};

Expand All @@ -130,11 +130,11 @@ public void testCustomImport3() throws Exception {
@Test
public void testCustomImport4() throws Exception {
final String[] expected = {
"7: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"7:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"javax.swing.WindowConstants.*"),
"15: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"15:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"java.util.StringTokenizer"),
"17: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"17:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"java.util.concurrent.AbstractExecutorService"),
};

Expand All @@ -148,15 +148,15 @@ public void testCustomImport4() throws Exception {
@Test
public void testCustomImport5() throws Exception {
final String[] expected = {
"9: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"9:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"javax.swing.WindowConstants.*"),
"13: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR,
"13:1: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR,
"com.google.checkstyle.test.chapter2filebasic.rule21filename.*"),
"17: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"17:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"com.google.common.reflect.*"),
"21: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"21:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"java.util.StringTokenizer"),
"25: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"25:1: " + getCheckMessage(clazz, MSG_SEPARATED_IN_GROUP,
"java.util.concurrent.AbstractExecutorService"),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
////////////////////////////////////////////////////////////////////////////////
// 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.CustomImportOrderCheck;

public class XpathRegressionCustomImportOrderTest extends AbstractXpathTestSupport {

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

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(CustomImportOrderCheck.class);
moduleConfig.addAttribute("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");
moduleConfig.addAttribute("sortImportsInGroupAlphabetically", "true");

final String[] expectedViolation = {
"4:1: " + getCheckMessage(CustomImportOrderCheck.class,
CustomImportOrderCheck.MSG_LEX, "java.lang.Math.PI",
"java.util.Arrays.sort"),
};

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

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(CustomImportOrderCheck.class);
moduleConfig.addAttribute("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");

final String[] expectedViolation = {
"5:1: " + getCheckMessage(CustomImportOrderCheck.class,
CustomImportOrderCheck.MSG_LINE_SEPARATOR, "java.io.File"),
};

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

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(CustomImportOrderCheck.class);
moduleConfig.addAttribute("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");

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

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

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(CustomImportOrderCheck.class);
moduleConfig.addAttribute("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");

final String[] expectedViolation = {
"5:1: " + getCheckMessage(CustomImportOrderCheck.class,
CustomImportOrderCheck.MSG_NONGROUP_IMPORT,
"com.puppycrawl.tools.checkstyle.api.DetailAST"),
};

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

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(CustomImportOrderCheck.class);
moduleConfig.addAttribute("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");

final String[] expectedViolation = {
"7:1: " + getCheckMessage(CustomImportOrderCheck.class,
CustomImportOrderCheck.MSG_NONGROUP_EXPECTED, "STATIC",
"java.lang.Math.PI"),
};

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

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

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

final DefaultConfiguration moduleConfig =
createModuleConfig(CustomImportOrderCheck.class);
moduleConfig.addAttribute("customImportOrderRules",
"STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE");

final String[] expectedViolation = {
"6:1: " + getCheckMessage(CustomImportOrderCheck.class,
CustomImportOrderCheck.MSG_ORDER, "THIRD_PARTY_PACKAGE",
"STANDARD_JAVA_PACKAGE",
"com.puppycrawl.tools.checkstyle.api.DetailAST"),
};

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

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

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

import static java.util.Arrays.sort;

import java.io.File;
import java.io.FileInputStream;
import static java.lang.Math.PI; // warn
import java.util.HashMap;
import java.util.Scanner;

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

import static java.util.Arrays.sort;
import static java.lang.Math.PI;
import com.puppycrawl.tools.checkstyle.api.DetailAST; // warn

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Scanner;

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

import static java.util.Arrays.sort;
import static java.lang.Math.PI; // warn

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Scanner;

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

import static java.util.Arrays.sort;
import static java.lang.Math.PI;

import com.puppycrawl.tools.checkstyle.api.DetailAST; // warn

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Scanner;

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

import static java.util.Arrays.sort;

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

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Scanner;

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

import static java.util.Arrays.sort;
import static java.lang.Math.PI;
import java.io.File; // warn
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Scanner;

public class SuppressionXpathRegressionCustomImportOrderTwo {
// code
}

0 comments on commit 8d10bc0

Please sign in to comment.