From ef58cc5a0d38ef2fbe1b18b1889bba57509980e8 Mon Sep 17 00:00:00 2001 From: Andrei Selkin Date: Mon, 17 Aug 2015 21:04:43 +0300 Subject: [PATCH] Workaround problem of interoperability between PowerMock and JaCoCo, issue #1173 --- .../checkstyle/ConfigurationLoaderTest.java | 7 +- .../filters/SuppressionsLoaderTest.java | 65 +++++++++---------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java index ad79b54f5..551a25362 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/ConfigurationLoaderTest.java @@ -36,6 +36,7 @@ import java.util.Properties; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.xml.sax.Attributes; @@ -452,16 +453,12 @@ public class ConfigurationLoaderTest { @Test @SuppressWarnings("unchecked") public void testLoadConfiguration_URISyntaxException() throws CheckstyleException { - mockStatic(ConfigurationLoader.class); + mockStatic(ConfigurationLoader.class, Mockito.CALLS_REAL_METHODS); PropertiesExpander expander = new PropertiesExpander(new Properties()); when(ConfigurationLoader.class.getResource("config_with_ignore.xml")) .thenThrow(URISyntaxException.class); - when(ConfigurationLoader.loadConfiguration("config_with_ignore.xml", - expander, - true)) - .thenCallRealMethod(); try { ConfigurationLoader.loadConfiguration( diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java index 8ffc002d2..72594dd93 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoaderTest.java @@ -26,31 +26,39 @@ import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; -import java.io.FileNotFoundException; import java.io.IOException; +import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.URISyntaxException; import java.net.URL; import java.util.regex.PatternSyntaxException; import org.junit.Assume; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; import org.xml.sax.InputSource; +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.FilterSet; /** * Tests SuppressionsLoader. * @author Rick Giles + * @author Andrei Selkin */ @RunWith(PowerMockRunner.class) @PrepareForTest({ SuppressionsLoader.class, SuppressionsLoaderTest.class }) -public class SuppressionsLoaderTest { +public class SuppressionsLoaderTest extends BaseCheckTestSupport { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testNoSuppressions() throws CheckstyleException { @@ -189,43 +197,35 @@ public class SuppressionsLoaderTest { @Test @SuppressWarnings("unchecked") public void testUnableToFindSuppressions() throws Exception { - mockStatic(SuppressionsLoader.class); + Class loaderClass = SuppressionsLoader.class; + Method loadSuppressions = + loaderClass.getDeclaredMethod("loadSuppressions", InputSource.class, String.class); + loadSuppressions.setAccessible(true); - String fileName = "suppressions_none.xml"; - InputSource source = mock(InputSource.class); + String sourceName = "suppressions_none.xml"; + InputSource inputSource = new InputSource(sourceName); - when(source.getByteStream()).thenThrow(FileNotFoundException.class); - when(SuppressionsLoader.class, "loadSuppressions", source, fileName).thenCallRealMethod(); + thrown.expect(CheckstyleException.class); + thrown.expectMessage("unable to find " + sourceName); - try { - Whitebox.invokeMethod(SuppressionsLoader.class, "loadSuppressions", source, fileName); - fail("Exception is expected"); - } - catch (CheckstyleException ex) { - assertTrue(ex.getCause() instanceof FileNotFoundException); - assertEquals("unable to find " + fileName, ex.getMessage()); - } + loadSuppressions.invoke(loaderClass, inputSource, sourceName); } @Test @SuppressWarnings("unchecked") public void testUnableToReadSuppressions() throws Exception { - mockStatic(SuppressionsLoader.class); + Class loaderClass = SuppressionsLoader.class; + Method loadSuppressions = + loaderClass.getDeclaredMethod("loadSuppressions", InputSource.class, String.class); + loadSuppressions.setAccessible(true); - String fileName = "suppressions_none.xml"; - InputSource source = mock(InputSource.class); + String sourceName = "suppressions_none.xml"; + InputSource inputSource = new InputSource(); - when(source.getByteStream()).thenThrow(IOException.class); - when(SuppressionsLoader.class, "loadSuppressions", source, fileName).thenCallRealMethod(); + thrown.expect(CheckstyleException.class); + thrown.expectMessage("unable to read " + sourceName); - try { - Whitebox.invokeMethod(SuppressionsLoader.class, "loadSuppressions", source, fileName); - fail("Exception is expected"); - } - catch (CheckstyleException ex) { - assertTrue(ex.getCause() instanceof IOException); - assertEquals("unable to read " + fileName, ex.getMessage()); - } + loadSuppressions.invoke(loaderClass, inputSource, sourceName); } @Test @@ -273,15 +273,12 @@ public class SuppressionsLoaderTest { @Test @SuppressWarnings("unchecked") public void testloadSuppressionsURISyntaxException() throws Exception { - mockStatic(SuppressionsLoader.class); - URL configUrl = mock(URL.class); String fileName = "suppressions_none.xml"; - when(SuppressionsLoader.class.getResource(fileName)).thenReturn(configUrl); when(configUrl.toURI()).thenThrow(URISyntaxException.class); - when(SuppressionsLoader.loadSuppressions(fileName)) - .thenCallRealMethod(); + mockStatic(SuppressionsLoader.class, Mockito.CALLS_REAL_METHODS); + when(SuppressionsLoader.class.getResource(fileName)).thenReturn(configUrl); try { SuppressionsLoader.loadSuppressions(fileName);