From b178b232c9ec9c3d20f7445a59d87b12eb8b2ad5 Mon Sep 17 00:00:00 2001 From: Benjamin Marwell Date: Tue, 12 May 2020 06:44:00 +0200 Subject: [PATCH] [SHIRO-777] remove powermock. --- pom.xml | 32 ++-- .../cache/HazelcastCacheManager.java | 1 + .../cache/HazelcastCacheManagerTest.groovy | 170 ++++++++---------- .../web/env/EnvironmentLoaderServiceTest.java | 2 +- 4 files changed, 93 insertions(+), 112 deletions(-) diff --git a/pom.xml b/pom.xml index a31b45c90b..39b301ab31 100644 --- a/pom.xml +++ b/pom.xml @@ -114,9 +114,6 @@ 0.11.0 5.4.3.Final 1.2.5 - - 2.0.2 ${jdk.version} ${jdk.version} @@ -636,6 +633,23 @@ + + enforce-forbidden-dependencies + + enforce + + + true + + + + *:powermock-api-easymock + *:powermock-module-junit4 + + + + + @@ -670,18 +684,6 @@ ${groovy.version} test - - org.powermock - powermock-module-junit4 - ${powermock.version} - test - - - org.powermock - powermock-api-easymock - ${powermock.version} - test - diff --git a/support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java b/support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java index 68db4c9ae4..a9e69de69d 100644 --- a/support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java +++ b/support/hazelcast/src/main/java/org/apache/shiro/hazelcast/cache/HazelcastCacheManager.java @@ -242,4 +242,5 @@ public Config getConfig() { public void setConfig(Config config) { this.config = config; } + } diff --git a/support/hazelcast/src/test/groovy/org/apache/shiro/hazelcast/cache/HazelcastCacheManagerTest.groovy b/support/hazelcast/src/test/groovy/org/apache/shiro/hazelcast/cache/HazelcastCacheManagerTest.groovy index 49b8e4a768..e8901a2523 100644 --- a/support/hazelcast/src/test/groovy/org/apache/shiro/hazelcast/cache/HazelcastCacheManagerTest.groovy +++ b/support/hazelcast/src/test/groovy/org/apache/shiro/hazelcast/cache/HazelcastCacheManagerTest.groovy @@ -19,164 +19,142 @@ package org.apache.shiro.hazelcast.cache import com.hazelcast.config.Config -import com.hazelcast.core.Hazelcast import com.hazelcast.core.HazelcastInstance -import com.hazelcast.core.IMap import com.hazelcast.core.LifecycleService -import org.apache.shiro.cache.MapCache import org.junit.Test -import org.junit.runner.RunWith -import org.powermock.core.classloader.annotations.PrepareForTest -import org.powermock.modules.junit4.PowerMockRunner -import static org.easymock.EasyMock.expect -import static org.easymock.EasyMock.same +import static org.easymock.EasyMock.* import static org.junit.Assert.* -import static org.powermock.api.easymock.PowerMock.* /** - * Unit tests for {@link HazelcastCacheManager}. Uses PowerMock to mock Hazelcast's static method calls. + * Unit tests for {@link HazelcastCacheManager}. * * @since 1.3 */ -@RunWith(PowerMockRunner) -@PrepareForTest(Hazelcast) class HazelcastCacheManagerTest { @Test void testGetSetHazelcastInstance() { - def hc = createStrictMock(HazelcastInstance) + + // given + HazelcastInstance hc = mock(HazelcastInstance) def manager = new HazelcastCacheManager(); - manager.hazelcastInstance = hc replay hc + // when + manager.hazelcastInstance = hc + + // then assertSame hc, manager.hazelcastInstance verify hc } @Test - void testInit() { - - mockStatic(Hazelcast) - //create a mock instead of starting a networked node: - def hc = createStrictMock(HazelcastInstance) - - expect(Hazelcast.newHazelcastInstance(null)).andReturn(hc) - - replay Hazelcast, hc + void testCustomConfig() { - def manager = new HazelcastCacheManager() + // given + Config config = mock(Config) + def manager = new HazelcastCacheManager(); - try { - manager.init() + // when + manager.config = config - assertNull manager.config - assertSame hc, manager.hazelcastInstance - assertTrue manager.implicitlyCreated - } finally { - verify Hazelcast, hc - } + // then + assertSame config, manager.config } @Test - void testDestroy() { - - mockStatic Hazelcast - def hc = createStrictMock(HazelcastInstance) - def lcService = createStrictMock(LifecycleService) + void testImplicitlyCreated() { - expect(Hazelcast.newHazelcastInstance(null)).andReturn(hc) - expect(hc.getLifecycleService()).andReturn(lcService) - lcService.shutdown() + // given + HazelcastInstance hazelcastInstance = niceMock(HazelcastInstance) - replay Hazelcast, hc, lcService + HazelcastCacheManager manager = createMockBuilder(HazelcastCacheManager) + .addMockedMethod("createHazelcastInstance") + .niceMock(); + expect(manager.createHazelcastInstance()).andReturn(hazelcastInstance) - def manager = new HazelcastCacheManager() - manager.init() //force implicit creation - - manager.destroy() - - assertNull manager.hazelcastInstance - assertFalse manager.implicitlyCreated + // when + manager.init() - verify Hazelcast, hc, lcService + // then + assertTrue manager.implicitlyCreated } @Test - void testDestroyWithThrowable() { + void testDestroy() { - mockStatic Hazelcast - def hc = createStrictMock(HazelcastInstance) - def lcService = createStrictMock(LifecycleService) + // given + LifecycleService lifecycleService = niceMock(LifecycleService) - expect(Hazelcast.newHazelcastInstance(null)).andReturn(hc) - expect(hc.getLifecycleService()).andReturn(lcService) - lcService.shutdown() - expectLastCall().andThrow(new IllegalStateException()) + HazelcastInstance hazelcastInstance = niceMock(HazelcastInstance) + expect(hazelcastInstance.getLifecycleService()).andReturn(lifecycleService) - replay Hazelcast, hc, lcService + HazelcastCacheManager manager = createMockBuilder(HazelcastCacheManager) + .addMockedMethod("createHazelcastInstance") + .niceMock(); + expect(manager.createHazelcastInstance()).andReturn(hazelcastInstance) - def manager = new HazelcastCacheManager() - manager.init() //force implicit creation + replay lifecycleService, hazelcastInstance, manager + // when + manager.init() manager.destroy() - assertNull manager.hazelcastInstance + // then assertFalse manager.implicitlyCreated - - verify Hazelcast, hc, lcService + assertNull manager.hazelcastInstance + verify hazelcastInstance + verify manager } - @Test - void testGetCache() { - - mockStatic Hazelcast - def hc = createStrictMock(HazelcastInstance) - def hcMap = createStrictMock(IMap) + void testDestroyExplicit() { - expect(Hazelcast.newHazelcastInstance(null)).andReturn(hc) - expect(hc.getMap("foo")).andReturn(hcMap) + // given + HazelcastInstance hazelcastInstance = niceMock(HazelcastInstance) + HazelcastCacheManager manager = new HazelcastCacheManager() + manager.hazelcastInstance = hazelcastInstance - replay Hazelcast, hc, hcMap + replay hazelcastInstance - try { - def manager = new HazelcastCacheManager() - def cache = manager.getCache("foo") + // when + manager.init() + manager.destroy() - assertNotNull cache - assertTrue cache instanceof MapCache - assertNotNull cache.map - assertTrue cache.map instanceof IMap - } finally { - verify Hazelcast, hc, hcMap - } + // then + assertNotNull manager.hazelcastInstance + assertFalse manager.implicitlyCreated } @Test - void testCustomConfig() { - - mockStatic Hazelcast + void testUncleanShutdown() { - def hc = createStrictMock(HazelcastInstance) - def config = createStrictMock(Config) + // given + LifecycleService lifecycleService = mock(LifecycleService) + expect(lifecycleService.shutdown()).andThrow(new IllegalStateException()) - expect(Hazelcast.newHazelcastInstance(same(config))).andReturn(hc) + HazelcastInstance hazelcastInstance = mock(HazelcastInstance) + expect(hazelcastInstance.getLifecycleService()).andReturn(lifecycleService) - replay Hazelcast, config + HazelcastCacheManager manager = createMockBuilder(HazelcastCacheManager) + .addMockedMethod("createHazelcastInstance") + .niceMock(); + expect(manager.createHazelcastInstance()).andReturn(hazelcastInstance) - def manager = new HazelcastCacheManager() - manager.config = config + replay lifecycleService, hazelcastInstance, manager + // when manager.init() + manager.destroy() - assertSame config, manager.config - assertSame hc, manager.hazelcastInstance - - verify Hazelcast, config + // then + assertFalse manager.implicitlyCreated + verify lifecycleService + verify hazelcastInstance + verify manager } - } diff --git a/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java b/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java index 18ce9afd65..250370a600 100644 --- a/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java +++ b/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java @@ -32,7 +32,7 @@ import static org.hamcrest.MatcherAssert.*; /** - * Tests for {@link EnvironmentLoader} that depend on PowerMock the stub out a ServiceLoader. + * Tests for {@link EnvironmentLoader} which will use a ServiceLoader. */ public class EnvironmentLoaderServiceTest {