Skip to content

Commit

Permalink
minor: changed powermock tests to normal tests for TranslationCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Feb 23, 2019
1 parent 906adae commit 59413ca
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 123 deletions.
12 changes: 1 addition & 11 deletions .ci/pitest.sh
Expand Up @@ -35,7 +35,7 @@ pitest-annotation|pitest-design \
|pitest-metrics|pitest-modifier|pitest-naming \
|pitest-sizes|pitest-whitespace \
|pitest-packagenamesloader \
|pitest-common-2)
|pitest-common-2|pitest-misc)
mvn -e -P$1 clean test org.pitest:pitest-maven:mutationCoverage;
declare -a ignoredItems=();
checkPitestReport "${ignoredItems[@]}"
Expand Down Expand Up @@ -322,16 +322,6 @@ pitest-tree-walker)
checkPitestReport "${ignoredItems[@]}"
;;

pitest-misc)
mvn -e -P$1 clean test org.pitest:pitest-maven:mutationCoverage;
declare -a ignoredItems=(
"TranslationCheck.java.html:<td class='covered'><pre><span class='survived'> if (exception instanceof NoSuchFileException) {</span></pre></td></tr>"
"TranslationCheck.java.html:<td class='uncovered'><pre><span class=''> args = null;</span></pre></td></tr>"
"TranslationCheck.java.html:<td class='uncovered'><pre><span class=''> key = &#34;general.fileNotFound&#34;;</span></pre></td></tr>"
);
checkPitestReport "${ignoredItems[@]}"
;;

pitest-utils)
mvn -e -P$1 clean test org.pitest:pitest-maven:mutationCoverage;
declare -a ignoredItems=(
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -1827,7 +1827,7 @@
<param>com.puppycrawl.tools.checkstyle.filters.SuppressWarningsFilterTest</param>
</targetTests>
<coverageThreshold>100</coverageThreshold>
<mutationThreshold>99</mutationThreshold>
<mutationThreshold>100</mutationThreshold>
<timeoutFactor>${pitest.plugin.timeout.factor}</timeoutFactor>
<timeoutConstant>${pitest.plugin.timeout.constant}</timeoutConstant>
<threads>${pitest.plugin.threads}</threads>
Expand Down
Expand Up @@ -23,7 +23,6 @@
import static com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck.MSG_KEY_UNABLE_OPEN;
import static java.util.Locale.ENGLISH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
Expand Down
Expand Up @@ -23,22 +23,28 @@
import static com.puppycrawl.tools.checkstyle.checks.TranslationCheck.MSG_KEY_MISSING_TRANSLATION_FILE;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.powermock.reflect.Whitebox;

import com.google.common.collect.ImmutableMap;
import com.puppycrawl.tools.checkstyle.AbstractXmlTestSupport;
Expand All @@ -50,6 +56,7 @@
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
import com.puppycrawl.tools.checkstyle.internal.utils.XmlUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;

Expand Down Expand Up @@ -218,6 +225,51 @@ public void testOnePropertyFileSet() throws Exception {
expected);
}

@Test
public void testLogIoExceptionFileNotFound() throws Exception {
//I can't put wrong file here. Checkstyle fails before check started.
//I saw some usage of file or handling of wrong file in Checker, or somewhere
//in checks running part. So I had to do it with reflection to improve coverage.
final TranslationCheck check = new TranslationCheck();
final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class);
final TestMessageDispatcher dispatcher = new TestMessageDispatcher();
check.configure(checkConfig);
check.setMessageDispatcher(dispatcher);

final Set<String> keys = Whitebox.invokeMethod(check, "getTranslationKeys",
new File(".no.such.file"));
assertTrue("Translation keys should be empty when File is not found", keys.isEmpty());

assertEquals("expected number of errors to fire", 1, dispatcher.savedErrors.size());
final LocalizedMessage localizedMessage = new LocalizedMessage(1,
Definitions.CHECKSTYLE_BUNDLE, "general.fileNotFound",
null, null, getClass(), null);
assertEquals("Invalid message", localizedMessage.getMessage(),
dispatcher.savedErrors.iterator().next().getMessage());
}

@Test
public void testLogIoException() throws Exception {
//I can't put wrong file here. Checkstyle fails before check started.
//I saw some usage of file or handling of wrong file in Checker, or somewhere
//in checks running part. So I had to do it with reflection to improve coverage.
final TranslationCheck check = new TranslationCheck();
final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class);
final TestMessageDispatcher dispatcher = new TestMessageDispatcher();
check.configure(checkConfig);
check.setMessageDispatcher(dispatcher);

final Exception exception = new IOException("test exception");
Whitebox.invokeMethod(check, "logException", exception, new File(""));

assertEquals("expected number of errors to fire", 1, dispatcher.savedErrors.size());
final LocalizedMessage localizedMessage = new LocalizedMessage(1,
Definitions.CHECKSTYLE_BUNDLE, "general.exception",
new String[] {exception.getMessage()}, null, getClass(), null);
assertEquals("Invalid message", localizedMessage.getMessage(),
dispatcher.savedErrors.iterator().next().getMessage());
}

@Test
public void testLogIllegalArgumentException() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(TranslationCheck.class);
Expand Down Expand Up @@ -563,4 +615,25 @@ public void testWrongUserSpecifiedLanguageCodes() {
}
}

private static class TestMessageDispatcher implements MessageDispatcher {

private Set<LocalizedMessage> savedErrors;

@Override
public void fireFileStarted(String fileName) {
throw new IllegalStateException(fileName);
}

@Override
public void fireFileFinished(String fileName) {
throw new IllegalStateException(fileName);
}

@Override
public void fireErrors(String fileName, SortedSet<LocalizedMessage> errors) {
savedErrors = new TreeSet<>(errors);
}

}

}

This file was deleted.

0 comments on commit 59413ca

Please sign in to comment.