autofixture.publicinterface.Fixture Maven / Gradle / Ivy
package autofixture.publicinterface;
import autofixture.generators.composition.DefaultGeneratorsFactory;
import autofixture.generators.objects.implementationdetails.ConcreteInstanceType;
import autofixture.implementationdetails.CollectionFactory;
import autofixture.implementationdetails.MapBasedRecursionGuard;
import autofixture.interfaces.*;
import com.google.common.primitives.Primitives;
import com.google.common.reflect.TypeToken;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static autofixture.generators.objects.implementationdetails.TypeAssertions.assertIsNotParameterized;
public class Fixture implements FixtureContract {
public static final int MINIMUM_VALUE_THAT_COULD_MEAN_MANY = 3;
private final DefaultGeneratorsFactory generatorsFactory;
private final RecursionGuard recursionGuard;
private final GeneratorsPipeline instanceGenerators;
private final int arbitraryRecursionDepth = 5;
private int repeatCount = MINIMUM_VALUE_THAT_COULD_MEAN_MANY;
public Fixture() {
recursionGuard = new MapBasedRecursionGuard(arbitraryRecursionDepth);
generatorsFactory = new DefaultGeneratorsFactory(new DefaultInlineGeneratorsFactory());
instanceGenerators = generatorsFactory.createBuiltinGenerators(recursionGuard);
}
@Override
public T create(final Class clazz) {
assertIsNotParameterized(clazz,
getParameterizedClassAssertionMessage(clazz, "create"));
return this.create(TypeToken.of(Primitives.wrap(clazz)));
}
@Override
public T create(final TypeToken typeToken) {
return create(new ConcreteInstanceType<>(typeToken));
}
public T create(final InstanceOf typeToken) {
return create((TypeToken) (typeToken));
}
public T createDummy(final Class clazz) {
assertIsNotParameterized(clazz,
getParameterizedClassAssertionMessage(clazz, "createDummy"));
return this.createDummy(TypeToken.of(Primitives.wrap(clazz)));
}
public T createDummy(final TypeToken typeToken) {
return createDummy(new ConcreteInstanceType<>(typeToken));
}
public T createDummy(final InstanceOf typeToken) {
return createDummy((TypeToken)(typeToken));
}
public T freeze(final TypeToken clazz) {
final T value = create(clazz);
inject(value);
return value;
}
public T freeze(final InstanceOf clazz) {
return freeze((TypeToken)clazz);
}
public T freeze(final Class clazz) {
assertIsNotParameterized(clazz,
getParameterizedClassAssertionMessage(clazz, "freeze"));
return freeze(TypeToken.of(Primitives.wrap(clazz)));
}
public void register(final InstanceGenerator instanceGenerator) {
instanceGenerators.registerCustomization(instanceGenerator);
}
public void clearCustomizations() {
instanceGenerators.clearCustomizations();
}
@Override
public T create(final InstanceType instanceType) {
return instanceGenerators.generateInstanceOf(instanceType, this);
}
@Override
public T createDummy(final InstanceType instanceType) {
return instanceGenerators.generateEmptyInstanceOf(instanceType, this);
}
@Override
public Collection createMany(final TypeToken type) {
final List manyObjects = Arrays.asList(createArray(type));
return manyObjects;
}
public Collection createMany(final InstanceOf type) {
return createMany((TypeToken)type);
}
@Override
public T[] createArray(final TypeToken type) {
final T[] array = CollectionFactory.createArray(type, repeatCount);
for (int i = 0; i < repeatCount; ++i) {
Array.set(array, i, create(type));
}
return array;
}
public T[] createArray(final InstanceOf type) {
return createArray((TypeToken)type);
}
@Override
public Collection super T> createMany(final InstanceType type) {
return createMany(type.getToken());
}
@Override
public int getRepeatCount() {
return repeatCount;
}
public void setRepeatCount(final int repeatCount) {
this.repeatCount = repeatCount;
}
public void inject(final T injectedValue) {
register(new InstanceGenerator() {
@Override
public boolean appliesTo(final InstanceType instanceType) {
return instanceType.isSameAsThatOf(injectedValue);
}
@Override
public T2 next(final InstanceType instanceType, final FixtureContract fixture) {
return (T2) injectedValue;
}
@Override
public T2 nextEmpty(final InstanceType instanceType, final FixtureContract fixture) {
return (T2) injectedValue;
}
@Override
public void setOmittingAutoProperties(final boolean isOn) {
}
});
}
public T create(final TypeToken type, final InlineConstrainedGenerator generator) {
return generator.next(type, this);
}
public T create(final InstanceOf type, final InlineConstrainedGenerator generator) {
return create((TypeToken)type, generator);
}
@Override
public T create(final InlineInstanceGenerator inlineGenerator) {
return inlineGenerator.next(this);
}
public String create(final String prefix) {
return prefix + create(String.class);
}
public void setOmittingAutoProperties(final boolean isOn) {
instanceGenerators.setOmittingAutoProperties(isOn);
}
public void setRecursionDepth(final int depth) {
this.recursionGuard.setMaxDepth(depth);
}
private String getParameterizedClassAssertionMessage(final Class clazz, final String methodName) {
return clazz.getSimpleName() + " is a generic class, " +
"which cannot be instantiated using " + methodName + "(Class) method. " +
"It should be created using "+ methodName + "(InstanceOf) method like this (example for List class): " +
methodName + "(new InstanceOf>() {}); " +
"(note the {} brackets - they are mandatory!)";
}
}