autofixture.publicinterface.Fixture Maven / Gradle / Ivy
package autofixture.publicinterface;
import autofixture.interfaces.InstanceGenerator;
import autofixture.implementationdetails.CollectionFactory;
import autofixture.generators.objects.implementationdetails.ConcreteInstanceType;
import autofixture.implementationdetails.MapBasedRecursionGuard;
import autofixture.interfaces.*;
import autofixture.generators.DefaultGeneratorsFactory;
import autofixture.interfaces.InlineConstrainedGenerator;
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;
public class Fixture implements FixtureContract {
public static final int MINIMUM_VALUE_THAT_COULD_MEAN_MANY = 3;
private final DefaultGeneratorsFactory generatorsFactory = new DefaultGeneratorsFactory();
private final RecursionGuard recursionGuard;
private final GeneratorsPipeline instanceGenerators;
private final int arbitraryRecursionDepth = 5;
private final GeneratorsPipeline dummyGenerators;
private int repeatCount = MINIMUM_VALUE_THAT_COULD_MEAN_MANY;
public Fixture() {
dummyGenerators = generatorsFactory.createDummyGenerators();
recursionGuard = new MapBasedRecursionGuard(arbitraryRecursionDepth, dummyGenerators);
instanceGenerators = generatorsFactory.createBuiltinGenerators(recursionGuard);
}
@Override
public T create(final Class clazz) {
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) {
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) {
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.executeFor(instanceType, this);
}
public T createDummy(final InstanceType instanceType) {
return dummyGenerators.executeFor(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 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);
}
}