From 59413cadd7518f1e4f15bb1a70b89cadb6343e7d Mon Sep 17 00:00:00 2001 From: rnveach Date: Wed, 20 Feb 2019 14:09:48 -0500 Subject: [PATCH] minor: changed powermock tests to normal tests for TranslationCheck --- .ci/pitest.sh | 12 +- pom.xml | 2 +- .../checks/NewlineAtEndOfFileCheckTest.java | 1 - .../checks/TranslationCheckTest.java | 73 ++++++++++++ .../powermock/TranslationCheckPowerTest.java | 110 ------------------ 5 files changed, 75 insertions(+), 123 deletions(-) delete mode 100644 src/test/java/com/puppycrawl/tools/checkstyle/internal/powermock/TranslationCheckPowerTest.java diff --git a/.ci/pitest.sh b/.ci/pitest.sh index 122857b8fa1..09c7f56bfd3 100755 --- a/.ci/pitest.sh +++ b/.ci/pitest.sh @@ -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[@]}" @@ -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:
        if (exception instanceof NoSuchFileException) {
" - "TranslationCheck.java.html:
            args = null;
" - "TranslationCheck.java.html:
            key = "general.fileNotFound";
" - ); - checkPitestReport "${ignoredItems[@]}" - ;; - pitest-utils) mvn -e -P$1 clean test org.pitest:pitest-maven:mutationCoverage; declare -a ignoredItems=( diff --git a/pom.xml b/pom.xml index 6a083839446..c1bcbac7940 100644 --- a/pom.xml +++ b/pom.xml @@ -1827,7 +1827,7 @@ com.puppycrawl.tools.checkstyle.filters.SuppressWarningsFilterTest 100 - 99 + 100 ${pitest.plugin.timeout.factor} ${pitest.plugin.timeout.constant} ${pitest.plugin.threads} diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheckTest.java index 288bed5fee2..4d38fca886f 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/NewlineAtEndOfFileCheckTest.java @@ -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; diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java index eddebdeae78..0b916f27164 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/TranslationCheckTest.java @@ -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; @@ -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; @@ -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 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); @@ -563,4 +615,25 @@ public void testWrongUserSpecifiedLanguageCodes() { } } + private static class TestMessageDispatcher implements MessageDispatcher { + + private Set 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 errors) { + savedErrors = new TreeSet<>(errors); + } + + } + } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/powermock/TranslationCheckPowerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/powermock/TranslationCheckPowerTest.java deleted file mode 100644 index 28dfee63496..00000000000 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/powermock/TranslationCheckPowerTest.java +++ /dev/null @@ -1,110 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// checkstyle: Checks Java source code for adherence to a set of rules. -// Copyright (C) 2001-2019 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.puppycrawl.tools.checkstyle.internal.powermock; - -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.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Method; -import java.util.Set; -import java.util.SortedSet; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mockito; -import org.powermock.modules.junit4.PowerMockRunner; - -import com.puppycrawl.tools.checkstyle.AbstractXmlTestSupport; -import com.puppycrawl.tools.checkstyle.DefaultConfiguration; -import com.puppycrawl.tools.checkstyle.Definitions; -import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; -import com.puppycrawl.tools.checkstyle.api.MessageDispatcher; -import com.puppycrawl.tools.checkstyle.checks.TranslationCheck; - -@RunWith(PowerMockRunner.class) -public class TranslationCheckPowerTest extends AbstractXmlTestSupport { - - @Captor - private ArgumentCaptor> captor; - - @Override - protected String getPackageLocation() { - return "com/puppycrawl/tools/checkstyle/checks/translation"; - } - - @Test - @SuppressWarnings("unchecked") - 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 MessageDispatcher dispatcher = mock(MessageDispatcher.class); - check.configure(checkConfig); - check.setMessageDispatcher(dispatcher); - - final Method loadKeys = - check.getClass().getDeclaredMethod("getTranslationKeys", File.class); - loadKeys.setAccessible(true); - final Set keys = (Set) loadKeys.invoke(check, new File(".no.such.file")); - assertTrue("Translation keys should be empty when File is not found", keys.isEmpty()); - - Mockito.verify(dispatcher, times(1)).fireErrors(any(String.class), captor.capture()); - final String actual = captor.getValue().first().getMessage(); - final LocalizedMessage localizedMessage = new LocalizedMessage(1, - Definitions.CHECKSTYLE_BUNDLE, "general.fileNotFound", - null, null, getClass(), null); - assertEquals("Invalid message", localizedMessage.getMessage(), actual); - } - - @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 MessageDispatcher dispatcher = mock(MessageDispatcher.class); - check.configure(checkConfig); - check.setMessageDispatcher(dispatcher); - - final Method logException = check.getClass().getDeclaredMethod("logException", - Exception.class, - File.class); - logException.setAccessible(true); - final File file = new File(""); - logException.invoke(check, new IOException("test exception"), file); - - Mockito.verify(dispatcher, times(1)).fireErrors(any(String.class), captor.capture()); - final String actual = captor.getValue().first().getMessage(); - assertThat("Invalid message: " + actual, actual, endsWith("test exception")); - } - -}