/*
 * Copyright 2012-2017 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.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.test.mock.mockito;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.SimpleCacheResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 * Test {@link SpyBean} when mixed with Spring AOP.
 *
 * @author Phillip Webb
 * @see <a href="https://github.com/spring-projects/spring-boot/issues/5837">5837</a>
 */
@RunWith(SpringRunner.class)
public class SpyBeanWithAopProxyTests {

	@SpyBean
	private DateService dateService;

	@Test
	public void verifyShouldUseProxyTarget() throws Exception {
		Long d1 = this.dateService.getDate(false);
		Thread.sleep(200);
		Long d2 = this.dateService.getDate(false);
		assertThat(d1).isEqualTo(d2);
		verify(this.dateService, times(1)).getDate(false);
		verify(this.dateService, times(1)).getDate(matchesFalse());
		verify(this.dateService, times(1)).getDate(matchesAnyBoolean());
	}

	private boolean matchesFalse() {
		if (isTestingMockito1()) {
			Method method = ReflectionUtils.findMethod(
					ClassUtils.resolveClassName("org.mockito.Matchers", null), "eq",
					Boolean.TYPE);
			return (boolean) ReflectionUtils.invokeMethod(method, null, false);
		}
		return ArgumentMatchers.eq(false);
	}

	private boolean matchesAnyBoolean() {
		if (isTestingMockito1()) {
			Method method = ReflectionUtils.findMethod(
					ClassUtils.resolveClassName("org.mockito.Matchers", null),
					"anyBoolean");
			return (boolean) ReflectionUtils.invokeMethod(method, null);
		}
		return ArgumentMatchers.anyBoolean();
	}

	private boolean isTestingMockito1() {
		return ClassUtils.isPresent("org.mockito.ReturnValues", null);
	}

	@Configuration
	@EnableCaching(proxyTargetClass = true)
	@Import(DateService.class)
	static class Config {

		@Bean
		public CacheResolver cacheResolver(CacheManager cacheManager) {
			SimpleCacheResolver resolver = new SimpleCacheResolver();
			resolver.setCacheManager(cacheManager);
			return resolver;
		}

		@Bean
		public ConcurrentMapCacheManager cacheManager() {
			ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager();
			cacheManager.setCacheNames(Arrays.asList("test"));
			return cacheManager;
		}

	}

	@Service
	static class DateService {

		@Cacheable(cacheNames = "test")
		public Long getDate(boolean arg) {
			return System.nanoTime();
		}

	}

}

@@ -17,17 +17,17 @@
 2012-201
-7
+8
  the ori
@@ -672,41 +672,8 @@
 o;%0A%0A
-import java.lang.reflect.Method;%0A
 impo
@@ -753,45 +753,8 @@
 ith;
-%0Aimport org.mockito.ArgumentMatchers;
 %0A%0Aim
@@ -1388,158 +1388,167 @@
 er;%0A
+%0A
 import 
-org.springframework.util.ClassUtils;%0Aimport org.springframework.util.ReflectionUtils;%0A%0Aimport static org.assertj.core.api.Assertions.assertThat
+static org.assertj.core.api.Assertions.assertThat;%0Aimport static org.mockito.ArgumentMatchers.anyBoolean;%0Aimport static org.mockito.ArgumentMatchers.eq
 ;%0Aim
@@ -2226,29 +2226,24 @@
 getDate(
-matchesF
+eq(f
 alse
-(
 ));%0A%09%09ve
@@ -2287,762 +2287,20 @@
 ate(
-matchesAnyBoolean());%0A%09%7D%0A%0A%09private boolean matchesFalse() %7B%0A%09%09if (isTestingMockito1()) %7B%0A%09%09%09Method method = ReflectionUtils.findMethod(%0A%09%09%09%09%09ClassUtils.resolveClassName(%22org.mockito.Matchers%22, null), %22eq%22,%0A%09%09%09%09%09Boolean.TYPE);%0A%09%09%09return (boolean) ReflectionUtils.invokeMethod(method, null, false);%0A%09%09%7D%0A%09%09return ArgumentMatchers.eq(false);%0A%09%7D%0A%0A%09private boolean matchesAnyBoolean() %7B%0A%09%09if (isTestingMockito1()) %7B%0A%09%09%09Method method = ReflectionUtils.findMethod(%0A%09%09%09%09%09ClassUtils.resolveClassName(%22org.mockito.Matchers%22, null),%0A%09%09%09%09%09%22anyBoolean%22);%0A%09%09%09return (boolean) ReflectionUtils.invokeMethod(method, null);%0A%09%09%7D%0A%09%09return ArgumentMatchers.anyBoolean();%0A%09%7D%0A%0A%09private boolean isTestingMockito1() %7B%0A%09%09return ClassUtils.isPresent(%22org.mockito.ReturnValues%22, null
+anyBoolean()
 );%0A%09

Remove Mockito 1 leftover