001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.proxy2.stub; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022 023import java.lang.annotation.Retention; 024import java.lang.annotation.RetentionPolicy; 025 026import org.apache.commons.proxy2.Interceptor; 027import org.apache.commons.proxy2.invoker.NullInvoker; 028import org.junit.Before; 029import org.junit.Test; 030 031public class StubInterceptorBuilderTest extends AbstractStubTestCase 032{ 033 //---------------------------------------------------------------------------------------------------------------------- 034 // Fields 035 //---------------------------------------------------------------------------------------------------------------------- 036 037 private StubInterceptorBuilder builder; 038 039 //---------------------------------------------------------------------------------------------------------------------- 040 // Other Methods 041 //---------------------------------------------------------------------------------------------------------------------- 042 043 @Before 044 public void initialize() 045 { 046 builder = new StubInterceptorBuilder(proxyFactory); 047 } 048 049 @Override 050 protected StubInterface createProxy(Trainer<StubInterface> trainer) 051 { 052 Interceptor interceptor = builder.train(trainer).build(); 053 return proxyFactory.createInterceptorProxy( 054 proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, StubInterface.class), interceptor, 055 StubInterface.class); 056 } 057 058 @Test 059 public void testWithNestedAnnotations() 060 { 061 Interceptor interceptor = builder.train(new Trainer<RetentionWrapper>() 062 { 063 @Override 064 protected void train(RetentionWrapper trainee) 065 { 066 067 when(trainee.value()).thenStub(new Trainer<Retention>() 068 { 069 @Override 070 protected void train(Retention trainee) 071 { 072 when(trainee.value()).thenReturn(RetentionPolicy.RUNTIME); 073 } 074 }); 075 } 076 }).build(); 077 RetentionWrapper wrapper = proxyFactory.createInterceptorProxy( 078 proxyFactory.createInvokerProxy(NullInvoker.INSTANCE), interceptor, RetentionWrapper.class); 079 assertNotNull(wrapper.value()); 080 assertEquals(RetentionPolicy.RUNTIME, wrapper.value().value()); 081 } 082 083 @Test 084 public void testWithSimpleAnnotations() 085 { 086 Interceptor interceptor = builder.train(new Trainer<Retention>() 087 { 088 @Override 089 protected void train(Retention trainee) 090 { 091 when(trainee.value()).thenReturn(RetentionPolicy.RUNTIME); 092 } 093 }).build(); 094 Retention wrapper = proxyFactory.createInterceptorProxy(proxyFactory.createInvokerProxy(NullInvoker.INSTANCE), 095 interceptor, Retention.class); 096 assertEquals(RetentionPolicy.RUNTIME, wrapper.value()); 097 } 098 099}