Issue #1290: SuppressWarningHolder coverage has been increased

This commit is contained in:
Ilja Dubinin 2015-09-22 01:27:13 +01:00 committed by Roman Ivanov
parent fff24e8900
commit d877cb15cf
4 changed files with 131 additions and 40 deletions

View File

@ -1668,8 +1668,8 @@
</regex>
<regex>
<pattern>.*.checks.SuppressWarningsHolder</pattern>
<branchRate>75</branchRate>
<lineRate>93</lineRate>
<branchRate>86</branchRate>
<lineRate>97</lineRate>
</regex>
<regex>
<!-- till https://github.com/checkstyle/checkstyle/issues/2243 -->

View File

@ -151,8 +151,8 @@ public class SuppressWarningsHolder
for (Entry entry : entries) {
final boolean afterStart =
entry.getFirstLine() < line
|| entry.getFirstLine() == line && entry
.getFirstColumn() <= column;
|| entry.getFirstLine() == line
&& entry.getFirstColumn() <= column;
final boolean beforeEnd =
entry.getLastLine() > line
|| entry.getLastLine() == line && entry
@ -306,42 +306,36 @@ public class SuppressWarningsHolder
private static DetailAST getAnnotationTarget(DetailAST ast) {
DetailAST targetAST = null;
DetailAST parentAST = ast.getParent();
if (parentAST != null) {
switch (parentAST.getType()) {
case TokenTypes.MODIFIERS:
case TokenTypes.ANNOTATIONS:
parentAST = parentAST.getParent();
if (parentAST != null) {
switch (parentAST.getType()) {
case TokenTypes.ANNOTATION_DEF:
case TokenTypes.PACKAGE_DEF:
case TokenTypes.CLASS_DEF:
case TokenTypes.INTERFACE_DEF:
case TokenTypes.ENUM_DEF:
case TokenTypes.ENUM_CONSTANT_DEF:
case TokenTypes.CTOR_DEF:
case TokenTypes.METHOD_DEF:
case TokenTypes.PARAMETER_DEF:
case TokenTypes.VARIABLE_DEF:
case TokenTypes.ANNOTATION_FIELD_DEF:
case TokenTypes.TYPE:
case TokenTypes.LITERAL_NEW:
case TokenTypes.LITERAL_THROWS:
case TokenTypes.TYPE_ARGUMENT:
case TokenTypes.IMPLEMENTS_CLAUSE:
case TokenTypes.DOT:
targetAST = parentAST;
break;
default:
// unexpected target type
}
}
break;
default:
// unexpected container type
}
switch (parentAST.getType()) {
case TokenTypes.MODIFIERS:
case TokenTypes.ANNOTATIONS:
parentAST = parentAST.getParent();
switch (parentAST.getType()) {
case TokenTypes.ANNOTATION_DEF:
case TokenTypes.PACKAGE_DEF:
case TokenTypes.CLASS_DEF:
case TokenTypes.INTERFACE_DEF:
case TokenTypes.ENUM_DEF:
case TokenTypes.ENUM_CONSTANT_DEF:
case TokenTypes.CTOR_DEF:
case TokenTypes.METHOD_DEF:
case TokenTypes.PARAMETER_DEF:
case TokenTypes.VARIABLE_DEF:
case TokenTypes.ANNOTATION_FIELD_DEF:
case TokenTypes.TYPE:
case TokenTypes.LITERAL_NEW:
case TokenTypes.LITERAL_THROWS:
case TokenTypes.TYPE_ARGUMENT:
case TokenTypes.IMPLEMENTS_CLAUSE:
case TokenTypes.DOT:
targetAST = parentAST;
break;
default:
// unexpected target type
}
break;
default:
// unexpected container type
}
return targetAST;
}

View File

@ -20,16 +20,31 @@
package com.puppycrawl.tools.checkstyle.checks;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import static org.powermock.api.mockito.PowerMockito.mock;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ SuppressWarningsHolder.class, SuppressWarningsHolderTest.class })
public class SuppressWarningsHolderTest extends BaseCheckTestSupport {
@Test
@ -57,4 +72,73 @@ public class SuppressWarningsHolderTest extends BaseCheckTestSupport {
verify(checkConfig, new File("src/test/resources-noncompilable/com/puppycrawl/tools/"
+ "checkstyle/InputSuppressWarningsHolder.java").getCanonicalPath(), expected);
}
@Test
public void testGetDefaultAlias() {
assertEquals("somename", SuppressWarningsHolder.getDefaultAlias("SomeName"));
assertEquals("somename", SuppressWarningsHolder.getDefaultAlias("SomeNameCheck"));
}
@Test
public void testSetAliasListEmpty() {
SuppressWarningsHolder holder = new SuppressWarningsHolder();
holder.setAliasList("");
}
@Test
public void testSetAliasListCorrect() {
SuppressWarningsHolder holder = new SuppressWarningsHolder();
holder.setAliasList("alias=value");
assertEquals("value", SuppressWarningsHolder.getAlias("alias"));
}
@Test
public void testSetAliasListWrong() {
SuppressWarningsHolder holder = new SuppressWarningsHolder();
try {
holder.setAliasList("SomeAlias");
fail("Exception expected");
}
catch (ConversionException ex) {
assertEquals("'=' expected in alias list item: SomeAlias", ex.getMessage());
}
}
@Test
public void testIsSuppressed() throws Exception {
Class<?> entry = Class
.forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry");
Constructor<?> entryConstr = entry.getDeclaredConstructor(String.class, int.class,
int.class, int.class, int.class);
entryConstr.setAccessible(true);
Object entryInstance = entryConstr.newInstance("MockEntry", 100, 100, 350, 350);
List<Object> entriesList = new ArrayList<>();
entriesList.add(entryInstance);
ThreadLocal<?> threadLocal = mock(ThreadLocal.class);
PowerMockito.doReturn(entriesList).when(threadLocal, "get");
SuppressWarningsHolder holder = new SuppressWarningsHolder();
Field entries = holder.getClass().getDeclaredField("ENTRIES");
entries.setAccessible(true);
entries.set(holder, threadLocal);
assertFalse(SuppressWarningsHolder.isSuppressed("SourceName", 100, 10));
}
@Test
public void testAnnotationInTry() throws Exception {
Configuration checkConfig = createCheckConfig(SuppressWarningsHolder.class);
final String[] expected = {
"11: " + getCheckMessage("suppress.warnings.invalid.target"),
};
verify(checkConfig, getPath("InputSuppressWarningsHolder2.java"), expected);
}
}

View File

@ -0,0 +1,13 @@
package com.puppycrawl.tools.checkstyle.grammars;
public class InputSuppressWarningsHolder2
{
public static class MyResource implements AutoCloseable {
@Override
public void close() throws Exception { }
}
public static void main(String[] args) throws Exception {
try (@SuppressWarnings("all") final MyResource resource = new MyResource()) { }
}
}