Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.github.anhem.testpopulator;
import com.github.anhem.testpopulator.config.PopulateConfig;
import com.github.anhem.testpopulator.config.Strategy;
import com.github.anhem.testpopulator.exception.PopulateException;
import com.github.anhem.testpopulator.internal.carrier.ClassCarrier;
import com.github.anhem.testpopulator.internal.carrier.CollectionCarrier;
import com.github.anhem.testpopulator.internal.carrier.TypeCarrier;
import com.github.anhem.testpopulator.internal.object.ObjectFactory;
import com.github.anhem.testpopulator.internal.object.ObjectFactoryImpl;
import com.github.anhem.testpopulator.internal.object.ObjectFactoryVoid;
import com.github.anhem.testpopulator.internal.value.ValueFactory;
import java.lang.reflect.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static com.github.anhem.testpopulator.config.BuilderPattern.IMMUTABLES;
import static com.github.anhem.testpopulator.config.BuilderPattern.LOMBOK;
import static com.github.anhem.testpopulator.config.Strategy.*;
import static com.github.anhem.testpopulator.internal.carrier.CollectionCarrier.initialize;
import static com.github.anhem.testpopulator.internal.util.ImmutablesUtil.getImmutablesGeneratedClass;
import static com.github.anhem.testpopulator.internal.util.ImmutablesUtil.getMethodsForImmutablesBuilder;
import static com.github.anhem.testpopulator.internal.util.LombokUtil.calculateExpectedChildren;
import static com.github.anhem.testpopulator.internal.util.LombokUtil.getMethodsForLombokBuilderGroupedByInvokeOrder;
import static com.github.anhem.testpopulator.internal.util.PopulateUtil.*;
import static java.lang.String.format;
/**
* Factory for creating populated objects from classes
*/
public class PopulateFactory {
static final String MISSING_COLLECTION_TYPE = "Failed to find type for collection %s";
static final String NO_MATCHING_STRATEGY = "Unable to populate %s. No matching strategy found. Tried with %s. Try another strategy or override population for this class";
static final String FAILED_TO_SET_FIELD = "Failed to set field %s in object of class %s";
static final String FAILED_TO_CALL_METHOD = "Failed to call method %s in object of class %s";
static final String FAILED_TO_CREATE_OBJECT = "Failed to create object of %s using %s strategy";
static final String FAILED_TO_CREATE_COLLECTION = "Failed to create and populate collection %s";
public static final String BUILD_METHOD = "build";
public static final String BUILDER_METHOD = "builder";
private final PopulateConfig populateConfig;
private final ValueFactory valueFactory;
/**
* Create new instance of PopulateFactory with default configuration
*/
public PopulateFactory() {
this(PopulateConfig.builder().build());
}
/**
* Create new instance of PopulateFactory
*
* @param populateConfig configuration properties for PopulateFactory
*/
public PopulateFactory(PopulateConfig populateConfig) {
this.populateConfig = populateConfig;
this.valueFactory = new ValueFactory(populateConfig.useRandomValues(), populateConfig.getOverridePopulate());
}
/**
* Call to create a fully populated object from a class
*
* @param clazz Class that should be populated
* @return object of clazz
*/
public T populate(Class clazz) {
ObjectFactory objectFactory = populateConfig.isObjectFactoryEnabled() ? new ObjectFactoryImpl(populateConfig) : new ObjectFactoryVoid();
T t = populateWithOverrides(initialize(clazz, objectFactory));
objectFactory.writeToFile();
return t;
}
private T populateWithOverrides(ClassCarrier classCarrier) {
Class clazz = classCarrier.getClazz();
if (valueFactory.hasType(clazz)) {
T value = valueFactory.createValue(clazz);
classCarrier.getObjectFactory().value(value);
return value;
}
if (populateConfig.isNullOnCircularDependency() && !isJavaBaseClass(clazz) && !classCarrier.addVisited()) {
classCarrier.getObjectFactory().nullValue(clazz);
return null;
}
if (isCollectionCarrier(classCarrier)) {
return continuePopulateForCollection((CollectionCarrier) classCarrier);
}
if (clazz.isArray()) {
return continuePopulateForArray(classCarrier);
}
return continuePopulateWithStrategies(classCarrier);
}
@SuppressWarnings("unchecked")
private T continuePopulateForArray(ClassCarrier classCarrier) {
Class componentType = classCarrier.getClazz().getComponentType();
classCarrier.getObjectFactory().array(componentType);
Object value = populateWithOverrides(classCarrier.toClassCarrier(componentType));
Object array = Array.newInstance(componentType, 1);
Array.set(array, 0, value);
return (T) array;
}
private T continuePopulateForCollection(CollectionCarrier classCarrier) {
try {
if (isMap(classCarrier.getClazz())) {
return continuePopulateForMap(classCarrier);
}
if (isSet(classCarrier.getClazz())) {
return continuePopulateForSet(classCarrier);
}
if (isCollection(classCarrier.getClazz())) {
return continuePopulateForList(classCarrier);
}
} catch (Exception e) {
throw new PopulateException(format(FAILED_TO_CREATE_COLLECTION, classCarrier.getClazz().getTypeName()), e);
}
throw new PopulateException(format(MISSING_COLLECTION_TYPE, classCarrier.getClazz().getTypeName()));
}
@SuppressWarnings("unchecked")
private T continuePopulateForMap(CollectionCarrier classCarrier) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
if (hasConstructors(classCarrier)) {
classCarrier.getObjectFactory().map(classCarrier.getClazz());
Map