From dafa765d88f91dd432d25b160f7a48006ddf6936 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 12 Mar 2014 19:06:29 +0200 Subject: [PATCH] INT-3321: Remove `powermock` Dependency JIRA: https://jira.spring.io/browse/INT-3321 Refactor `StoredProcExecutorTests` just to use Mockito INT-3321 Polishing Mock the operations cache loader; remove static method. --- build.gradle | 4 -- .../integration/jdbc/StoredProcExecutor.java | 12 +--- .../jdbc/StoredProcExecutorTests.java | 57 ++++++++++--------- 3 files changed, 32 insertions(+), 41 deletions(-) diff --git a/build.gradle b/build.gradle index dfc9a5d852..317483f5d0 100644 --- a/build.gradle +++ b/build.gradle @@ -92,7 +92,6 @@ subprojects { subproject -> openJpaVersion = '2.3.0' pahoMqttClientVersion = '0.4.0' postgresVersion = '9.1-901-1.jdbc4' - powermockVersion = '1.5.4' romeVersion = '1.0.0' saajApiVersion = '1.3.5' saajImplVersion = '1.3.23' @@ -342,9 +341,6 @@ project('spring-integration-jdbc') { testCompile "org.apache.derby:derby:$derbyVersion" testCompile "org.apache.derby:derbyclient:$derbyVersion" - testCompile "org.powermock:powermock-module-junit4:$powermockVersion" - testCompile "org.powermock:powermock-api-mockito:$powermockVersion" - testCompile "postgresql:postgresql:$postgresVersion" testCompile "mysql:mysql-connector-java:$mysqlVersion" testCompile "commons-dbcp:commons-dbcp:$commonsDbcpVersion" diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/StoredProcExecutor.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/StoredProcExecutor.java index 64bb19ab4d..196d4dd789 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/StoredProcExecutor.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/StoredProcExecutor.java @@ -325,17 +325,7 @@ public class StoredProcExecutor implements BeanFactoryAware, InitializingBean { SqlParameterSource storedProcedureParameterSource = sqlParameterSourceFactory.createParameterSource(input); - return StoredProcExecutor.executeStoredProcedure(localSimpleJdbcCall, - storedProcedureParameterSource); - - } - - private static Map executeStoredProcedure(SimpleJdbcCallOperations simpleJdbcCallOperations, - SqlParameterSource storedProcedureParameterSource) { - - Map resultMap = simpleJdbcCallOperations.execute(storedProcedureParameterSource); - - return resultMap; + return localSimpleJdbcCall.execute(storedProcedureParameterSource); } diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java index 389ac97750..c45e2080f9 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,26 +29,27 @@ import javax.sql.DataSource; import org.apache.log4j.Logger; import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.integration.config.ExpressionFactoryBean; import org.springframework.integration.jdbc.storedproc.ProcedureParameter; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.test.util.TestUtils; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.SqlParameter; +import org.springframework.jdbc.core.simple.SimpleJdbcCall; +import org.springframework.jdbc.core.simple.SimpleJdbcCallOperations; +import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheStats; -@RunWith(PowerMockRunner.class) -@PrepareForTest({StoredProcExecutor.class}) -@PowerMockIgnore ({"org.apache.log4j.*"}) +/** + * @author Gunnar Hillert + * @author Artem Bilan + * @author Gary Russell + */ public class StoredProcExecutorTests { private static final Logger LOGGER = Logger.getLogger(StoredProcExecutorTests.class); @@ -315,11 +316,6 @@ public class StoredProcExecutorTests { final DataSource datasource = mock(DataSource.class); - PowerMockito.mockStatic(StoredProcExecutor.class); - - PowerMockito.spy(StoredProcExecutor.class); - PowerMockito.doReturn(null).when(StoredProcExecutor.class, "executeStoredProcedure", Mockito.any(), Mockito.any()); - final StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource); final ExpressionFactoryBean efb = new ExpressionFactoryBean("headers['stored_procedure_name']"); @@ -331,6 +327,8 @@ public class StoredProcExecutorTests { storedProcExecutor.afterPropertiesSet(); + this.mockTheOperationsCache(storedProcExecutor); + //This should work storedProcExecutor.executeStoredProcedure( @@ -345,7 +343,8 @@ public class StoredProcExecutorTests { MessageBuilder.withPayload("test") .setHeader("some_other_header", "123") .build()); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException e) { assertEquals("Unable to resolve Stored Procedure/Function name for the provided Expression 'headers['stored_procedure_name']'.", e.getMessage()); return; } @@ -359,11 +358,6 @@ public class StoredProcExecutorTests { final DataSource datasource = mock(DataSource.class); - PowerMockito.mockStatic(StoredProcExecutor.class); - - PowerMockito.spy(StoredProcExecutor.class); - PowerMockito.doReturn(null).when(StoredProcExecutor.class, "executeStoredProcedure", Mockito.any(), Mockito.any()); - final StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource); final ExpressionFactoryBean efb = new ExpressionFactoryBean("headers['stored_procedure_name']"); @@ -375,6 +369,8 @@ public class StoredProcExecutorTests { storedProcExecutor.afterPropertiesSet(); + this.mockTheOperationsCache(storedProcExecutor); + for (int i = 1; i <= 3; i++) { storedProcExecutor.executeStoredProcedure( MessageBuilder.withPayload("test") @@ -397,12 +393,8 @@ public class StoredProcExecutorTests { final DataSource datasource = mock(DataSource.class); - PowerMockito.mockStatic(StoredProcExecutor.class); - - PowerMockito.spy(StoredProcExecutor.class); - PowerMockito.doReturn(null).when(StoredProcExecutor.class, "executeStoredProcedure", Mockito.any(), Mockito.any()); - final StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource); + storedProcExecutor.setJdbcCallOperationsCacheSize(0); final ExpressionFactoryBean efb = new ExpressionFactoryBean("headers['stored_procedure_name']"); @@ -414,6 +406,8 @@ public class StoredProcExecutorTests { storedProcExecutor.afterPropertiesSet(); + this.mockTheOperationsCache(storedProcExecutor); + for (int i = 1; i <= 10; i++) { storedProcExecutor.executeStoredProcedure( MessageBuilder.withPayload("test") @@ -427,4 +421,15 @@ public class StoredProcExecutorTests { } + private void mockTheOperationsCache(final StoredProcExecutor storedProcExecutor) { + Object cache = TestUtils.getPropertyValue(storedProcExecutor, "jdbcCallOperationsCache.localCache"); + new DirectFieldAccessor(cache) + .setPropertyValue("defaultLoader", new CacheLoader() { + @Override + public SimpleJdbcCall load(String storedProcedureName) { + return mock(SimpleJdbcCall.class); + } + }); + } + }