Permalink
Browse files

Issue #4675: increased coverage of pitest-checks-misc to 96%

  • Loading branch information...
Nimfadora committed Aug 22, 2017
1 parent d10fb0e commit 566c33256e992a52c55c9bd8c514a9474e9faf40
Showing with 65 additions and 38 deletions.
  1. +1 −1 pom.xml
  2. +64 −37 src/test/java/com/puppycrawl/tools/checkstyle/checks/SuppressWarningsHolderTest.java
View
@@ -1652,7 +1652,7 @@
<param>com.puppycrawl.tools.checkstyle.checks.UniquePropertiesCheckTest</param>
<param>com.puppycrawl.tools.checkstyle.checks.UpperEllCheckTest</param>
</targetTests>
<mutationThreshold>93</mutationThreshold>
<mutationThreshold>96</mutationThreshold>
<timeoutFactor>${pitest.plugin.timeout.factor}</timeoutFactor>
<timeoutConstant>${pitest.plugin.timeout.constant}</timeoutConstant>
<threads>${pitest.plugin.threads}</threads>
@@ -112,65 +112,60 @@ public void testSetAliasListWrong() {
final SuppressWarningsHolder holder = new SuppressWarningsHolder();
try {
holder.setAliasList("SomeAlias");
holder.setAliasList("=SomeAlias");
fail("Exception expected");
}
catch (IllegalArgumentException ex) {
assertEquals("Error message is unexpected",
"'=' expected in alias list item: SomeAlias", ex.getMessage());
"'=' expected in alias list item: =SomeAlias", ex.getMessage());
}
}
@Test
public void testIsSuppressed() throws Exception {
final Class<?> entry = Class
.forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry");
final Constructor<?> entryConstructor = entry.getDeclaredConstructor(String.class,
int.class, int.class, int.class, int.class);
entryConstructor.setAccessible(true);
createHolder("MockEntry", 100, 100, 350, 350);
final AuditEvent event = createAuditEvent("check", 100, 10);
final Object entryInstance = entryConstructor.newInstance("MockEntry", 100, 100, 350, 350);
final List<Object> entriesList = new ArrayList<>();
entriesList.add(entryInstance);
assertFalse("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
}
final ThreadLocal<?> threadLocal = mock(ThreadLocal.class);
PowerMockito.doReturn(entriesList).when(threadLocal, "get");
@Test
public void testIsSuppressedByName() throws Exception {
final SuppressWarningsHolder holder = createHolder("check", 100, 100, 350, 350);
final AuditEvent event = createAuditEvent("id", 110, 10);
holder.setAliasList(MemberNameCheck.class.getName() + "=check");
final SuppressWarningsHolder holder = new SuppressWarningsHolder();
final Field entries = holder.getClass().getDeclaredField("ENTRIES");
entries.setAccessible(true);
entries.set(holder, threadLocal);
assertTrue("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
}
final Checker source = new Checker();
final LocalizedMessage message =
new LocalizedMessage(100, 10, null, null, null, "id", MemberNameCheck.class, "message");
final AuditEvent event = new AuditEvent(source, "fileName", message);
@Test
public void testIsSuppressedByModuleId() throws Exception {
createHolder("check", 100, 100, 350, 350);
final AuditEvent event = createAuditEvent("check", 350, 350);
assertFalse("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
assertTrue("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
}
@Test
public void testIsSuppressedWithAllArgument() throws Exception {
final Class<?> entry = Class
.forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry");
final Constructor<?> entryConstr = entry.getDeclaredConstructor(String.class, int.class,
int.class, int.class, int.class);
entryConstr.setAccessible(true);
public void testIsSuppressedAfterEventEnd() throws Exception {
createHolder("check", 100, 100, 350, 350);
final AuditEvent event = createAuditEvent("check", 350, 352);
final Object entryInstance = entryConstr.newInstance("all", 100, 100, 350, 350);
assertFalse("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
}
final List<Object> entriesList = new ArrayList<>();
entriesList.add(entryInstance);
@Test
public void testIsSuppressedAfterEventStart() throws Exception {
createHolder("check", 100, 100, 350, 350);
final AuditEvent event = createAuditEvent("check", 100, 100);
final ThreadLocal<?> threadLocal = mock(ThreadLocal.class);
PowerMockito.doReturn(entriesList).when(threadLocal, "get");
assertTrue("Event is not suppressed", SuppressWarningsHolder.isSuppressed(event));
}
final SuppressWarningsHolder holder = new SuppressWarningsHolder();
final Field entries = holder.getClass().getDeclaredField("ENTRIES");
entries.setAccessible(true);
entries.set(holder, threadLocal);
@Test
public void testIsSuppressedWithAllArgument() throws Exception {
createHolder("all", 100, 100, 350, 350);
final Checker source = new Checker();
final LocalizedMessage firstMessageForTest =
@@ -328,4 +323,36 @@ public void testAnnotationWithFullName() throws Exception {
verify(checkConfig, getPath("InputSuppressWarningsHolder4.java"), expected);
}
private static SuppressWarningsHolder createHolder(String checkName, int firstLine,
int firstColumn, int lastLine,
int lastColumn) throws Exception {
final Class<?> entry = Class
.forName("com.puppycrawl.tools.checkstyle.checks.SuppressWarningsHolder$Entry");
final Constructor<?> entryConstr = entry.getDeclaredConstructor(String.class, int.class,
int.class, int.class, int.class);
entryConstr.setAccessible(true);
final Object entryInstance = entryConstr.newInstance(checkName, firstLine,
firstColumn, lastLine, lastColumn);
final List<Object> entriesList = new ArrayList<>();
entriesList.add(entryInstance);
final ThreadLocal<?> threadLocal = mock(ThreadLocal.class);
PowerMockito.doReturn(entriesList).when(threadLocal, "get");
final SuppressWarningsHolder holder = new SuppressWarningsHolder();
final Field entries = holder.getClass().getDeclaredField("ENTRIES");
entries.setAccessible(true);
entries.set(holder, threadLocal);
return holder;
}
private static AuditEvent createAuditEvent(String moduleId, int line, int column) {
final Checker source = new Checker();
final LocalizedMessage message = new LocalizedMessage(line, column, null, null, null,
moduleId, MemberNameCheck.class, "message");
return new AuditEvent(source, "filename", message);
}
}

0 comments on commit 566c332

Please sign in to comment.