InteractionTester(Class<T> interfaceType, Method method) { this.interfaceType = interfaceType; this.method = method; this.passedArgs = getParameterValues(method); this.returnValue = new FreshValueGenerator().generateFresh(method.getReturnType()); }
public void testAddSampleInstances_noInstance() { FreshValueGenerator generator = new FreshValueGenerator(); generator.addSampleInstances(String.class, ImmutableList.<String>of()); assertEquals( new FreshValueGenerator().generateFresh(String.class), generator.generateFresh(String.class)); }
Class<?> rawType = type.getRawType(); List<Object> samples = sampleInstances.get(rawType); Object sample = pickInstance(samples, null); if (sample != null) { return sample; return pickInstance(rawType.getEnumConstants(), null); Array.set(array, 0, generate(componentType)); return array; return invokeGeneratorMethod(emptyGenerate); } else { Object emptyInstance = invokeGeneratorMethod(emptyGenerate); emptyInstanceGenerated.put(type.getType(), freshness.get()); return emptyInstance; Object argValue = generate(paramType); if (argValue == null) { return invokeGeneratorMethod(generate, args.toArray()); return defaultGenerate(rawType);
private static <T> void testToString( Class<T> interfaceType, Function<? super T, ? extends T> wrapperFunction) { T proxy = new FreshValueGenerator().newFreshProxy(interfaceType); assertEquals( "toString() isn't properly forwarded", proxy.toString(), wrapperFunction.apply(proxy).toString()); }
InteractionTester(Class<T> interfaceType, Method method) { this.interfaceType = interfaceType; this.method = method; this.passedArgs = getParameterValues(method); this.returnValue = new FreshValueGenerator().generate(method.getReturnType()); }
public void testAddSampleInstances_noInstance() { FreshValueGenerator generator = new FreshValueGenerator(); generator.addSampleInstances(String.class, ImmutableList.<String>of()); assertEquals(new FreshValueGenerator().generate(String.class), generator.generate(String.class)); }
private static <T> void testToString( Class<T> interfaceType, Function<? super T, ? extends T> wrapperFunction) { T proxy = new FreshValueGenerator().newProxy(interfaceType); assertEquals("toString() isn't properly forwarded", proxy.toString(), wrapperFunction.apply(proxy).toString()); }
Class<?> rawType = type.getRawType(); List<Object> samples = sampleInstances.get(rawType); Object sample = nextInstance(samples, null); if (sample != null) { return sample; return nextInstance(rawType.getEnumConstants(), null); Array.set(array, 0, generate(componentType)); return array; Object argValue = generate(paramType); if (argValue == null) { return defaultGenerate(rawType);
@SuppressWarnings({"unchecked", "rawtypes"}) private FreshValueGenerator newFreshValueGenerator() { FreshValueGenerator generator = new FreshValueGenerator() { @Override Object interfaceMethodCalled(Class<?> interfaceType, Method method) { return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType()); } }; for (Entry<Class<?>, Collection<Object>> entry : distinctValues.asMap().entrySet()) { generator.addSampleInstances((Class) entry.getKey(), entry.getValue()); } return generator; }
@Nullable final <T> T generateFresh(Class<T> type) { return Primitives.wrap(type).cast(generateFresh(TypeToken.of(type))); }
final <T> T newFreshProxy(final Class<T> interfaceType) { T proxy = newProxy(interfaceType); freshness.incrementAndGet(); return proxy; }
/** * Returns a fresh instance for {@code type} if possible. The returned instance could be: * * <ul> * <li>exactly of the given type, including generic type parameters, such as {@code * ImmutableList<String>}; * <li>of the raw type; * <li>null if no value can be generated. * </ul> */ @Nullable final Object generateFresh(TypeToken<?> type) { Object generated = generate(type); if (generated != null) { freshness.incrementAndGet(); } return generated; }
@Override protected Object handleInvocation(Object proxy, Method method, Object[] args) { return interfaceMethodCalled(interfaceType, method); }
private static Object[] getParameterValues(Method method) { FreshValueGenerator paramValues = new FreshValueGenerator(); final List<Object> passedArgs = Lists.newArrayList(); for (Class<?> paramType : method.getParameterTypes()) { passedArgs.add(paramValues.generate(paramType)); } return passedArgs.toArray(); }
public void testAddSampleInstances_oneInstance() { FreshValueGenerator generator = new FreshValueGenerator(); generator.addSampleInstances(String.class, ImmutableList.of("a")); assertEquals("a", generator.generate(String.class)); assertEquals("a", generator.generate(String.class)); }
private static <T> void testEquals( Class<T> interfaceType, Function<? super T, ? extends T> wrapperFunction) { FreshValueGenerator generator = new FreshValueGenerator(); T instance = generator.newFreshProxy(interfaceType); new EqualsTester() .addEqualityGroup(wrapperFunction.apply(instance), wrapperFunction.apply(instance)) .addEqualityGroup(wrapperFunction.apply(generator.newFreshProxy(interfaceType))) // TODO: add an overload to EqualsTester to print custom error message? .testEquals(); }
private static <T> void testEquals( Class<T> interfaceType, Function<? super T, ? extends T> wrapperFunction) { FreshValueGenerator generator = new FreshValueGenerator(); T instance = generator.newProxy(interfaceType); new EqualsTester() .addEqualityGroup(wrapperFunction.apply(instance), wrapperFunction.apply(instance)) .addEqualityGroup(wrapperFunction.apply(generator.newProxy(interfaceType))) // TODO: add an overload to EqualsTester to print custom error message? .testEquals(); }
@SuppressWarnings({"unchecked", "rawtypes"}) private FreshValueGenerator newFreshValueGenerator() { FreshValueGenerator generator = new FreshValueGenerator() { @Override Object interfaceMethodCalled(Class<?> interfaceType, Method method) { return getDummyValue(TypeToken.of(interfaceType).method(method).getReturnType()); } }; for (Map.Entry<Class<?>, Collection<Object>> entry : distinctValues.asMap().entrySet()) { generator.addSampleInstances((Class) entry.getKey(), entry.getValue()); } return generator; }
for (int i = 0; i < params.size(); i++) { List<Object> newArgs = Lists.newArrayList(args); Object newArg = argGenerators.get(i).generateFresh(params.get(i).getType());
private <T> T defaultGenerate(Class<T> rawType) { if (rawType.isInterface()) { // always create a new proxy return newProxy(rawType); } return ArbitraryInstances.get(rawType); }