View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      // Fields
35      //----------------------------------------------------------------------------------------------------------------------
36  
37      private StubInterceptorBuilder builder;
38  
39      //----------------------------------------------------------------------------------------------------------------------
40      // Other Methods
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  }