Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SHIRO-777] remove powermock. #235

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[SHIRO-777] remove powermock.
  • Loading branch information
bmarwell committed May 12, 2020
commit b178b232c9ec9c3d20f7445a59d87b12eb8b2ad5
32 changes: 17 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@
<junit.server.jetty.version>0.11.0</junit.server.jetty.version>
<hibernate.version>5.4.3.Final</hibernate.version>
<taglibs.standard.version>1.2.5</taglibs.standard.version>
<!-- so we can mock static methods in 3rd party libraries that sometimes don't use proper interfaces
ahem, hazelcast, ahem... -->
<powermock.version>2.0.2</powermock.version>

<maven.compiler.source>${jdk.version}</maven.compiler.source>
<maven.compiler.target>${jdk.version}</maven.compiler.target>
Expand Down Expand Up @@ -636,6 +633,23 @@
</rules>
</configuration>
</execution>
<execution>
<id>enforce-forbidden-dependencies</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<fail>true</fail>
<rules>
<bannedDependencies>
<excludes>
<exclude>*:powermock-api-easymock</exclude>
<exclude>*:powermock-module-junit4</exclude>
</excludes>
</bannedDependencies>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down Expand Up @@ -670,18 +684,6 @@
<version>${groovy.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,5 @@ public Config getConfig() {
public void setConfig(Config config) {
this.config = config;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down