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.assertNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.Arrays;
25  import java.util.Iterator;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.commons.proxy2.ObjectProvider;
29  import org.apache.commons.proxy2.provider.BeanProvider;
30  import org.apache.commons.proxy2.provider.ObjectProviderUtils;
31  import org.junit.Test;
32  
33  public class StubBuilderTest extends AbstractStubTestCase
34  {
35  
36      //----------------------------------------------------------------------------------------------------------------------
37      // Other Methods
38      //----------------------------------------------------------------------------------------------------------------------
39  
40      @Override
41      protected StubInterface createProxy(Trainer<StubInterface> trainer)
42      {
43          return new StubBuilder<StubInterface>(proxyFactory, StubInterface.class).train(trainer).build();
44      }
45  
46      @Test
47      public void testWithConcreteTarget()
48      {
49          StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class,
50                  new SimpleStub());
51          builder.train(new Trainer<StubInterface>()
52          {
53              @Override
54              protected void train(StubInterface trainee)
55              {
56                  when(trainee.one("Foo")).thenReturn("Bar");
57              }
58          });
59          StubInterface stub = builder.build();
60          assertEquals("Bar", stub.one("Foo"));
61      }
62  
63      @Test
64      public void testWithNoTargetAndNoInterceptors()
65      {
66          StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class);
67          StubInterface stub = builder.build();
68          assertNull(stub.one("Whatever"));
69      }
70  
71      @Test
72      public void testWithNoTargetWithInterceptor()
73      {
74          StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class);
75          builder.train(new Trainer<StubInterface>()
76          {
77              @Override
78              protected void train(StubInterface trainee)
79              {
80                  when(trainee.one("Foo")).thenReturn("Bar");
81              }
82          });
83          StubInterface stub = builder.build();
84          assertEquals("Bar", stub.one("Foo"));
85      }
86  
87      @Test
88      public void testWithObjectProviderTarget()
89      {
90          StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class,
91                  new BeanProvider<StubInterface>(SimpleStub.class));
92          builder.train(new Trainer<StubInterface>()
93          {
94              @Override
95              protected void train(StubInterface trainee)
96              {
97                  when(trainee.one("Foo")).thenReturn("Bar");
98              }
99          });
100         StubInterface stub = builder.build();
101         assertEquals("Bar", stub.one("Foo"));
102     }
103 
104     @Test
105     public void testAdditionalInterfaces()
106     {
107         StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class,
108                 ObjectProviderUtils.constant(new SimpleStub()));
109         builder.train(new Trainer<Iterable<String>>()
110         {
111 
112             @Override
113             protected void train(Iterable<String> trainee)
114             {
115                 when(trainee.iterator()).thenAnswer(new ObjectProvider<Iterator<String>>()
116                 {
117                     private static final long serialVersionUID = 1L;
118 
119                     @Override
120                     public Iterator<String> getObject()
121                     {
122                         return Arrays.asList("foo", "bar", "baz").iterator();
123                     }
124                 });
125             }
126         });
127         builder.addProxyTypes(Cloneable.class, Marker.class);
128         StubInterface stub = builder.build();
129         assertTrue(stub instanceof Iterable<?>);
130         assertTrue(stub instanceof Cloneable);
131         assertTrue(stub instanceof Marker);
132     }
133 
134     //----------------------------------------------------------------------------------------------------------------------
135     // Inner Classes
136     //----------------------------------------------------------------------------------------------------------------------
137 
138     private static class SimpleStub implements StubInterface
139     {
140         @Override
141         public String one(String value)
142         {
143             return value;
144         }
145 
146         @Override
147         public String three(String arg1, String arg2)
148         {
149             return arg1 + arg2;
150         }
151 
152         @Override
153         public String two(String value)
154         {
155             return StringUtils.repeat(value, 2);
156         }
157 
158         @Override
159         public byte[] byteArray()
160         {
161             return new byte[] { 1, 2, 3 };
162         }
163 
164         @Override
165         public char[] charArray()
166         {
167             return new char[] { '1', '2', '3' };
168         }
169 
170         @Override
171         public short[] shortArray()
172         {
173             return new short[] { 1, 2, 3 };
174         }
175 
176         @Override
177         public int[] intArray()
178         {
179             return new int[] { 1, 2, 3 };
180         }
181 
182         @Override
183         public long[] longArray()
184         {
185             return new long[] { 1, 2, 3 };
186         }
187 
188         @Override
189         public float[] floatArray()
190         {
191             return new float[] { 1.0f, 2.0f, 3.0f };
192         }
193 
194         @Override
195         public double[] doubleArray()
196         {
197             return new double[] { 1.0, 2.0, 3.0 };
198         }
199 
200         @Override
201         public boolean[] booleanArray()
202         {
203             return new boolean[] { true, false, true };
204         }
205 
206         @Override
207         public String[] stringArray()
208         {
209             return new String[] { "One", "Two", "Three" };
210         }
211 
212         @Override
213         public String arrayParameter(String... strings)
214         {
215             return StringUtils.join(strings, ", ");
216         }
217 
218         @Override
219         public void voidMethod(String arg)
220         {
221 
222         }
223 
224         @Override
225         public StubInterface stub()
226         {
227             return null;
228         }
229 
230         @Override
231         public StubInterface[] stubs()
232         {
233             return new StubInterface[0];
234         }
235     }
236 
237     public interface Marker
238     {
239     }
240 }