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.assertNull; 022import static org.junit.Assert.assertTrue; 023 024import java.util.Arrays; 025import java.util.Iterator; 026 027import org.apache.commons.lang3.StringUtils; 028import org.apache.commons.proxy2.ObjectProvider; 029import org.apache.commons.proxy2.provider.BeanProvider; 030import org.apache.commons.proxy2.provider.ObjectProviderUtils; 031import org.junit.Test; 032 033public class StubBuilderTest extends AbstractStubTestCase 034{ 035 036 //---------------------------------------------------------------------------------------------------------------------- 037 // Other Methods 038 //---------------------------------------------------------------------------------------------------------------------- 039 040 @Override 041 protected StubInterface createProxy(Trainer<StubInterface> trainer) 042 { 043 return new StubBuilder<StubInterface>(proxyFactory, StubInterface.class).train(trainer).build(); 044 } 045 046 @Test 047 public void testWithConcreteTarget() 048 { 049 StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class, 050 new SimpleStub()); 051 builder.train(new Trainer<StubInterface>() 052 { 053 @Override 054 protected void train(StubInterface trainee) 055 { 056 when(trainee.one("Foo")).thenReturn("Bar"); 057 } 058 }); 059 StubInterface stub = builder.build(); 060 assertEquals("Bar", stub.one("Foo")); 061 } 062 063 @Test 064 public void testWithNoTargetAndNoInterceptors() 065 { 066 StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class); 067 StubInterface stub = builder.build(); 068 assertNull(stub.one("Whatever")); 069 } 070 071 @Test 072 public void testWithNoTargetWithInterceptor() 073 { 074 StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class); 075 builder.train(new Trainer<StubInterface>() 076 { 077 @Override 078 protected void train(StubInterface trainee) 079 { 080 when(trainee.one("Foo")).thenReturn("Bar"); 081 } 082 }); 083 StubInterface stub = builder.build(); 084 assertEquals("Bar", stub.one("Foo")); 085 } 086 087 @Test 088 public void testWithObjectProviderTarget() 089 { 090 StubBuilder<StubInterface> builder = new StubBuilder<StubInterface>(proxyFactory, StubInterface.class, 091 new BeanProvider<StubInterface>(SimpleStub.class)); 092 builder.train(new Trainer<StubInterface>() 093 { 094 @Override 095 protected void train(StubInterface trainee) 096 { 097 when(trainee.one("Foo")).thenReturn("Bar"); 098 } 099 }); 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}