Tests for annotation lookups in interfaces (currently ignored for CGLIB proxies)
Issue: SPR-15271
Issue: SPR-14949
Issue: SPR-14322
(cherry picked from commit d003f66)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -16,12 +16,12 @@
|
||||
|
||||
package org.springframework.cache;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
@@ -58,8 +58,9 @@ public class CacheReproTests {
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
@Test
|
||||
public void spr11124() throws Exception {
|
||||
public void spr11124MultipleAnnotations() throws Exception {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr11124Config.class);
|
||||
Spr11124Service bean = context.getBean(Spr11124Service.class);
|
||||
bean.single(2);
|
||||
@@ -70,7 +71,7 @@ public class CacheReproTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void spr11249() throws Exception {
|
||||
public void spr11249PrimitiveVarargs() throws Exception {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr11249Config.class);
|
||||
Spr11249Service bean = context.getBean(Spr11249Service.class);
|
||||
Object result = bean.doSomething("op", 2, 3);
|
||||
@@ -128,8 +129,8 @@ public class CacheReproTests {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr13081Config.class);
|
||||
Spr13081Service bean = context.getBean(Spr13081Service.class);
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
thrown.expectMessage(MyCacheResolver.class.getName());
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage(MyCacheResolver.class.getName());
|
||||
bean.getSimple(null);
|
||||
}
|
||||
|
||||
@@ -167,6 +168,31 @@ public class CacheReproTests {
|
||||
assertSame(tb2, cache.get("tb1").get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void spr15271FindsOnInterfaceWithInterfaceProxy() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr15271ConfigA.class);
|
||||
Spr15271Interface bean = context.getBean(Spr15271Interface.class);
|
||||
Cache cache = context.getBean(CacheManager.class).getCache("itemCache");
|
||||
|
||||
TestBean tb = new TestBean("tb1");
|
||||
bean.insertItem(tb);
|
||||
assertSame(tb, bean.findById("tb1").get());
|
||||
assertSame(tb, cache.get("tb1").get());
|
||||
}
|
||||
|
||||
@Test @Ignore // TODO
|
||||
public void spr15271FindsOnInterfaceWithCglibProxy() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr15271ConfigB.class);
|
||||
Spr15271Interface bean = context.getBean(Spr15271Interface.class);
|
||||
Cache cache = context.getBean(CacheManager.class).getCache("itemCache");
|
||||
|
||||
TestBean tb = new TestBean("tb1");
|
||||
bean.insertItem(tb);
|
||||
assertSame(tb, bean.findById("tb1").get());
|
||||
assertSame(tb, cache.get("tb1").get());
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public static class Spr11124Config {
|
||||
@@ -251,7 +277,7 @@ public class CacheReproTests {
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
SimpleCacheManager cacheManager = new SimpleCacheManager();
|
||||
cacheManager.setCaches(Arrays.asList(cache()));
|
||||
cacheManager.setCaches(Collections.singletonList(cache()));
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
@@ -358,6 +384,7 @@ public class CacheReproTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class Spr14853Service {
|
||||
|
||||
@Cacheable(value = "itemCache", sync = true)
|
||||
@@ -372,6 +399,7 @@ public class CacheReproTests {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public static class Spr14853Config {
|
||||
@@ -387,4 +415,60 @@ public class CacheReproTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface Spr15271Interface {
|
||||
|
||||
@Cacheable(value = "itemCache", sync = true)
|
||||
Optional<TestBean> findById(String id);
|
||||
|
||||
@CachePut(cacheNames = "itemCache", key = "#item.name")
|
||||
TestBean insertItem(TestBean item);
|
||||
}
|
||||
|
||||
|
||||
public static class Spr15271Service implements Spr15271Interface {
|
||||
|
||||
@Override
|
||||
public Optional<TestBean> findById(String id) {
|
||||
return Optional.of(new TestBean(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TestBean insertItem(TestBean item) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableCaching
|
||||
public static class Spr15271ConfigA {
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
return new ConcurrentMapCacheManager();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Spr15271Interface service() {
|
||||
return new Spr15271Service();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableCaching(proxyTargetClass = true)
|
||||
public static class Spr15271ConfigB {
|
||||
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
return new ConcurrentMapCacheManager();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Spr15271Interface service() {
|
||||
return new Spr15271Service();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-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.
|
||||
@@ -25,6 +25,7 @@ import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
@@ -196,8 +197,32 @@ public class EnableAsyncTests {
|
||||
|
||||
asyncBean.fail();
|
||||
Thread.sleep(500);
|
||||
Method m = ReflectionUtils.findMethod(AsyncBean.class, "fail");
|
||||
exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class);
|
||||
Method method = ReflectionUtils.findMethod(AsyncBean.class, "fail");
|
||||
exceptionHandler.assertCalledWith(method, UnsupportedOperationException.class);
|
||||
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void spr14949FindsOnInterfaceWithInterfaceProxy() throws InterruptedException {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigA.class);
|
||||
|
||||
AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class);
|
||||
asyncBean.work();
|
||||
Thread.sleep(500);
|
||||
assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-"));
|
||||
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Test @Ignore // TODO
|
||||
public void spr14949FindsOnInterfaceWithCglibProxy() throws InterruptedException {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigB.class);
|
||||
|
||||
AsyncInterface asyncBean = ctx.getBean(AsyncInterface.class);
|
||||
asyncBean.work();
|
||||
Thread.sleep(500);
|
||||
assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-"));
|
||||
|
||||
ctx.close();
|
||||
}
|
||||
@@ -207,22 +232,22 @@ public class EnableAsyncTests {
|
||||
|
||||
@Async
|
||||
public Future<Thread> work0() {
|
||||
return new AsyncResult<Thread>(Thread.currentThread());
|
||||
return new AsyncResult<>(Thread.currentThread());
|
||||
}
|
||||
|
||||
@Async("e1")
|
||||
public Future<Thread> work() {
|
||||
return new AsyncResult<Thread>(Thread.currentThread());
|
||||
return new AsyncResult<>(Thread.currentThread());
|
||||
}
|
||||
|
||||
@Async("otherExecutor")
|
||||
public Future<Thread> work2() {
|
||||
return new AsyncResult<Thread>(Thread.currentThread());
|
||||
return new AsyncResult<>(Thread.currentThread());
|
||||
}
|
||||
|
||||
@Async("e2")
|
||||
public Future<Thread> work3() {
|
||||
return new AsyncResult<Thread>(Thread.currentThread());
|
||||
return new AsyncResult<>(Thread.currentThread());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,6 +358,28 @@ public class EnableAsyncTests {
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableAsync
|
||||
static class AsyncWithExecutorQualifiedByNameConfig {
|
||||
|
||||
@Bean
|
||||
public AsyncBeanWithExecutorQualifiedByName asyncBean() {
|
||||
return new AsyncBeanWithExecutorQualifiedByName();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Executor e1() {
|
||||
return new ThreadPoolTaskExecutor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Qualifier("e2")
|
||||
public Executor otherExecutor() {
|
||||
return new ThreadPoolTaskExecutor();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableAsync
|
||||
static class CustomExecutorAsyncConfig implements AsyncConfigurer {
|
||||
@@ -362,24 +409,75 @@ public class EnableAsyncTests {
|
||||
}
|
||||
|
||||
|
||||
public interface AsyncInterface {
|
||||
|
||||
@Async
|
||||
void work();
|
||||
|
||||
Thread getThreadOfExecution();
|
||||
}
|
||||
|
||||
|
||||
public static class AsyncService implements AsyncInterface {
|
||||
|
||||
private Thread threadOfExecution;
|
||||
|
||||
@Override
|
||||
public void work() {
|
||||
this.threadOfExecution = Thread.currentThread();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Thread getThreadOfExecution() {
|
||||
return threadOfExecution;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableAsync
|
||||
static class AsyncWithExecutorQualifiedByNameConfig {
|
||||
static class Spr14949ConfigA implements AsyncConfigurer {
|
||||
|
||||
@Bean
|
||||
public AsyncBeanWithExecutorQualifiedByName asyncBean() {
|
||||
return new AsyncBeanWithExecutorQualifiedByName();
|
||||
public AsyncInterface asyncBean() {
|
||||
return new AsyncService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Executor e1() {
|
||||
return new ThreadPoolTaskExecutor();
|
||||
@Override
|
||||
public Executor getAsyncExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setThreadNamePrefix("Custom-");
|
||||
executor.initialize();
|
||||
return executor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableAsync(proxyTargetClass = true)
|
||||
static class Spr14949ConfigB implements AsyncConfigurer {
|
||||
|
||||
@Bean
|
||||
@Qualifier("e2")
|
||||
public Executor otherExecutor() {
|
||||
return new ThreadPoolTaskExecutor();
|
||||
public AsyncInterface asyncBean() {
|
||||
return new AsyncService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Executor getAsyncExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setThreadNamePrefix("Custom-");
|
||||
executor.initialize();
|
||||
return executor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user