1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.proxy2.stub;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25
26 import org.apache.commons.proxy2.Interceptor;
27 import org.apache.commons.proxy2.invoker.NullInvoker;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 public class StubInterceptorBuilderTest extends AbstractStubTestCase
32 {
33
34
35
36
37 private StubInterceptorBuilder builder;
38
39
40
41
42
43 @Before
44 public void initialize()
45 {
46 builder = new StubInterceptorBuilder(proxyFactory);
47 }
48
49 @Override
50 protected StubInterface createProxy(Trainer<StubInterface> trainer)
51 {
52 Interceptor interceptor = builder.train(trainer).build();
53 return proxyFactory.createInterceptorProxy(
54 proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, StubInterface.class), interceptor,
55 StubInterface.class);
56 }
57
58 @Test
59 public void testWithNestedAnnotations()
60 {
61 Interceptor interceptor = builder.train(new Trainer<RetentionWrapper>()
62 {
63 @Override
64 protected void train(RetentionWrapper trainee)
65 {
66
67 when(trainee.value()).thenStub(new Trainer<Retention>()
68 {
69 @Override
70 protected void train(Retention trainee)
71 {
72 when(trainee.value()).thenReturn(RetentionPolicy.RUNTIME);
73 }
74 });
75 }
76 }).build();
77 RetentionWrapper wrapper = proxyFactory.createInterceptorProxy(
78 proxyFactory.createInvokerProxy(NullInvoker.INSTANCE), interceptor, RetentionWrapper.class);
79 assertNotNull(wrapper.value());
80 assertEquals(RetentionPolicy.RUNTIME, wrapper.value().value());
81 }
82
83 @Test
84 public void testWithSimpleAnnotations()
85 {
86 Interceptor interceptor = builder.train(new Trainer<Retention>()
87 {
88 @Override
89 protected void train(Retention trainee)
90 {
91 when(trainee.value()).thenReturn(RetentionPolicy.RUNTIME);
92 }
93 }).build();
94 Retention wrapper = proxyFactory.createInterceptorProxy(proxyFactory.createInvokerProxy(NullInvoker.INSTANCE),
95 interceptor, Retention.class);
96 assertEquals(RetentionPolicy.RUNTIME, wrapper.value());
97 }
98
99 }