Skip to content

Commit

Permalink
Issue checkstyle#4592: Added AbstractPathTestSupport, AbstractModuleT…
Browse files Browse the repository at this point in the history
…estSupport, AbstractIndentationTestSupport and all tests in ITs extend them
  • Loading branch information
subkrish committed Aug 1, 2017
1 parent 622e04c commit 47f5a1d
Show file tree
Hide file tree
Showing 61 changed files with 283 additions and 464 deletions.
4 changes: 2 additions & 2 deletions config/checkstyle_checks.xml
Expand Up @@ -101,9 +101,9 @@
</module>
<module name="RegexpOnFilename">
<property name="folderPattern" value="[\\/]src[\\/]it[\\/]java[\\/]"/>
<property name="fileNamePattern" value="^((\w+Test)|(Base\w+))\.java$"/>
<property name="fileNamePattern" value="^((\w+Test)|(Abstract\w+))\.java$"/>
<property name="match" value="false"/>
<message key="regexp.filepath.mismatch" value="All files in the ''src/it/java'' folder should be named ''*Test.java'' or ''Base*.java''."/>
<message key="regexp.filepath.mismatch" value="All files in the ''src/it/java'' folder should be named ''*Test.java'' or ''Abstract*.java''."/>
</module>

<!-- Size Violations -->
Expand Down
2 changes: 1 addition & 1 deletion config/intellij-idea-inspections.xml
Expand Up @@ -2206,7 +2206,7 @@
<inspection_tool class="TestMethodInProductCode" enabled="true" level="ERROR" enabled_by_default="true" />
<inspection_tool class="TestMethodIsPublicVoidNoArg" enabled="true" level="ERROR" enabled_by_default="true" />
<inspection_tool class="TestMethodWithoutAssertion" enabled="true" level="ERROR" enabled_by_default="true">
<option name="assertionMethods" value="org.junit.Assert,assert.*|fail.*,junit.framework.Assert,assert.*|fail.*,org.mockito.Mockito,verify.*,org.mockito.InOrder,verify,org.junit.rules.ExpectedException,expect.*,org.hamcrest.MatcherAssert,assertThat,com.puppycrawl.tools.checkstyle.BaseCheckTestSupport,verify.*,com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport,verify.*,com.puppycrawl.tools.checkstyle.AbstractTreeTestSupport,verify.*,com.google.checkstyle.test.base.BaseCheckTestSupport,verify.*,com.puppycrawl.tools.checkstyle.internal.TestUtils,assert.*,com.puppycrawl.tools.checkstyle.grammars.AstRegressionTest.AssertGeneratedJavaLexer,verify.*,nl.jqno.equalsverifier.EqualsVerifier,verify.*,org.powermock.api.mockito.PowerMockito,verify.*" />
<option name="assertionMethods" value="org.junit.Assert,assert.*|fail.*,junit.framework.Assert,assert.*|fail.*,org.mockito.Mockito,verify.*,org.mockito.InOrder,verify,org.junit.rules.ExpectedException,expect.*,org.hamcrest.MatcherAssert,assertThat,com.puppycrawl.tools.checkstyle.BaseCheckTestSupport,verify.*,com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport,verify.*,com.puppycrawl.tools.checkstyle.AbstractTreeTestSupport,verify.*,com.google.checkstyle.test.base.AbstractModuleTestSupport,verify.*,com.puppycrawl.tools.checkstyle.internal.TestUtils,assert.*,com.puppycrawl.tools.checkstyle.grammars.AstRegressionTest.AssertGeneratedJavaLexer,verify.*,nl.jqno.equalsverifier.EqualsVerifier,verify.*,org.powermock.api.mockito.PowerMockito,verify.*" />
<option name="assertKeywordIsAssertion" value="false" />
</inspection_tool>
<inspection_tool class="TestNGDataProvider" enabled="true" level="ERROR" enabled_by_default="true" />
Expand Down
Expand Up @@ -33,7 +33,7 @@

import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class BaseIndentationCheckSupport extends BaseCheckTestSupport {
public abstract class AbstractIndentationTestSupport extends AbstractModuleTestSupport {
private static final int TAB_WIDTH = 4;

private static final Pattern NONEMPTY_LINE_REGEX =
Expand Down
Expand Up @@ -51,7 +51,7 @@
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class BaseCheckTestSupport {
public abstract class AbstractModuleTestSupport extends AbstractPathTestSupport {
private static final Pattern WARN_PATTERN = CommonUtils
.createPattern(".*[ ]*//[ ]*warn[ ]*|/[*]\\s?warn\\s?[*]/");

Expand Down Expand Up @@ -132,26 +132,13 @@ protected DefaultConfiguration createCheckerConfig(Configuration config) {
return dc;
}

/**
* Returns canonical path for the file with the given file name.
* The path is formed based on the specific root location.
* This implementation uses 'src/it/resources/com/google/checkstyle/test/' as a root location.
* @param fileName file name.
* @return canonical path for the the file with the given file name.
* @throws IOException if I/O exception occurs while forming the path.
*/
protected String getPath(String fileName) throws IOException {
return new File("src/it/resources/com/google/checkstyle/test/" + fileName)
.getCanonicalPath();
}

/**
* Performs verification of the file with given file name. Uses specified configuration.
* Expected messages are represented by the array of strings, warning line numbers are
* represented by the array of integers.
* This implementation uses overloaded
* {@link BaseCheckTestSupport#verify(Checker, File[], String, String[], Integer...)} method
* inside.
* {@link AbstractModuleTestSupport#verify(Checker, File[], String, String[], Integer...)}
* method inside.
* @param config configuration.
* @param fileName file name to verify.
* @param expected an array of expected messages.
Expand Down Expand Up @@ -260,7 +247,7 @@ protected String getCheckMessage(Map<String, String> messages, String messageKey

/**
* Returns {@link Configuration} instance for the given check name.
* This implementation uses {@link BaseCheckTestSupport#getConfiguration()} method inside.
* This implementation uses {@link AbstractModuleTestSupport#getConfiguration()} method inside.
* @param checkName check name.
* @return {@link Configuration} instance for the given check name.
* @throws CheckstyleException if exception occurs during configuration loading.
Expand All @@ -279,7 +266,7 @@ protected static Configuration getCheckConfig(String checkName) throws Checkstyl

/**
* Returns {@link Configuration} instance for the given check name.
* This implementation uses {@link BaseCheckTestSupport#getConfiguration()} method inside.
* This implementation uses {@link AbstractModuleTestSupport#getConfiguration()} method inside.
* @param checkName check name.
* @return {@link Configuration} instance for the given check name.
* @throws CheckstyleException if exception occurs during configuration loading.
Expand Down Expand Up @@ -307,7 +294,7 @@ protected static Configuration getCheckConfig(String checkName, String checkId)

/**
* Returns a list of all {@link Configuration} instances for the given check name.
* This implementation uses {@link BaseCheckTestSupport#getConfiguration()} method inside.
* This implementation uses {@link AbstractModuleTestSupport#getConfiguration()} method inside.
* @param checkName check name.
* @return {@link Configuration} instance for the given check name.
* @throws CheckstyleException if exception occurs during configuration loading.
Expand Down
@@ -0,0 +1,45 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 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 com.google.checkstyle.test.base;

import java.io.File;
import java.io.IOException;

public abstract class AbstractPathTestSupport {
/**
* Returns the exact location for the package where the file is present.
* @return path for the package name for the file.
*/
protected abstract String getPackageLocation();

/**
* Returns canonical path for the file with the given file name.
* The path is formed base on the root location.
* This implementation uses 'src/test/resources/'
* as a root location.
* @param filename file name.
* @return canonical path for the file name.
* @throws IOException if I/O exception occurs while forming the path.
*/
protected final String getPath(String filename) throws IOException {
return new File("src/it/resources/" + getPackageLocation() + "/" + filename)
.getCanonicalPath();
}
}
Expand Up @@ -21,22 +21,18 @@

import static com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheck.MSG_KEY;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheck;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class OuterTypeFilenameTest extends BaseCheckTestSupport {
public class OuterTypeFilenameTest extends AbstractModuleTestSupport {

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter2filebasic" + File.separator + "rule21filename"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter2filebasic/rule21filename";
}

@Test
Expand Down
Expand Up @@ -19,22 +19,18 @@

package com.google.checkstyle.test.chapter2filebasic.rule231filetab;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.whitespace.FileTabCharacterCheck;

public class FileTabCharacterTest extends BaseCheckTestSupport {
public class FileTabCharacterTest extends AbstractModuleTestSupport {

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter2filebasic" + File.separator + "rule231filetab"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter2filebasic/rule231filetab";
}

@Override
Expand Down
Expand Up @@ -19,20 +19,16 @@

package com.google.checkstyle.test.chapter2filebasic.rule232specialescape;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;

public class IllegalTokenTextTest extends BaseCheckTestSupport {
public class IllegalTokenTextTest extends AbstractModuleTestSupport {

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter2filebasic" + File.separator + "rule232specialescape"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter2filebasic/rule232specialescape";
}

@Test
Expand Down
Expand Up @@ -21,21 +21,17 @@

import static com.puppycrawl.tools.checkstyle.checks.AvoidEscapedUnicodeCharactersCheck.MSG_KEY;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.AvoidEscapedUnicodeCharactersCheck;

public class AvoidEscapedUnicodeCharactersTest extends BaseCheckTestSupport {
public class AvoidEscapedUnicodeCharactersTest extends AbstractModuleTestSupport {

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter2filebasic" + File.separator + "rule233nonascii"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter2filebasic/rule233nonascii";
}

@Test
Expand Down
Expand Up @@ -19,21 +19,17 @@

package com.google.checkstyle.test.chapter3filestructure.rule32packagestate;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck;

public class LineLengthTest extends BaseCheckTestSupport {
public class LineLengthTest extends AbstractModuleTestSupport {

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter3filestructure" + File.separator + "rule32packagestate"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter3filestructure/rule32packagestate";
}

@Test
Expand Down
Expand Up @@ -19,20 +19,16 @@

package com.google.checkstyle.test.chapter3filestructure.rule331nowildcard;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;

public class AvoidStarImportTest extends BaseCheckTestSupport {
public class AvoidStarImportTest extends AbstractModuleTestSupport {

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter3filestructure" + File.separator + "rule331nowildcard"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter3filestructure/rule331nowildcard";
}

@Test
Expand Down
Expand Up @@ -19,23 +19,19 @@

package com.google.checkstyle.test.chapter3filestructure.rule332nolinewrap;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck;
import com.puppycrawl.tools.checkstyle.checks.whitespace.NoLineWrapCheck;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class NoLineWrapTest extends BaseCheckTestSupport {
public class NoLineWrapTest extends AbstractModuleTestSupport {

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter3filestructure" + File.separator + "rule332nolinewrap"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter3filestructure/rule332nolinewrap";
}

@Test
Expand Down
Expand Up @@ -19,17 +19,14 @@

package com.google.checkstyle.test.chapter3filestructure.rule333orderingandspacing;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class CustomImportOrderTest extends BaseCheckTestSupport {
public class CustomImportOrderTest extends AbstractModuleTestSupport {

/** Shortcuts to make code more compact. */
private static final String MSG_LINE_SEPARATOR = CustomImportOrderCheck.MSG_LINE_SEPARATOR;
Expand All @@ -42,9 +39,8 @@ public class CustomImportOrderTest extends BaseCheckTestSupport {
private final Class<CustomImportOrderCheck> clazz = CustomImportOrderCheck.class;

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter3filestructure" + File.separator + "rule333orderingandspacing"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter3filestructure/rule333orderingandspacing";
}

@Test
Expand Down
Expand Up @@ -19,22 +19,18 @@

package com.google.checkstyle.test.chapter3filestructure.rule341onetoplevel;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.google.checkstyle.test.base.BaseCheckTestSupport;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class OneTopLevelClassTest extends BaseCheckTestSupport {
public class OneTopLevelClassTest extends AbstractModuleTestSupport {

@Override
protected String getPath(String fileName) throws IOException {
return super.getPath("chapter3filestructure" + File.separator + "rule341onetoplevel"
+ File.separator + fileName);
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter3filestructure/rule341onetoplevel";
}

@Test
Expand Down

0 comments on commit 47f5a1d

Please sign in to comment.