├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── vcs.xml ├── misc.xml ├── compiler.xml ├── modules.xml ├── gradle.xml ├── modules │ ├── fritterfactory_main.iml │ └── fritterfactory_test.iml └── uiDesigner.xml ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .travis.yml ├── notes.md ├── src ├── test │ └── java │ │ └── one │ │ └── equinox │ │ └── fritterfactory │ │ └── test │ │ ├── model │ │ ├── SampleEnum.java │ │ ├── MyModelWithEnum.java │ │ ├── MyModelMold.java │ │ ├── MyModelWithExtends.java │ │ ├── MyModelMoldWithProviders.java │ │ ├── SampleValues.java │ │ └── MyModel.java │ │ ├── ModelWithNoDefaultConstructorTests.java │ │ ├── FritterFactoryTestModelWithEnum.java │ │ ├── FritterFactoryTestModelWithExtends.java │ │ ├── FritterFactoryTestWithMapMold.java │ │ ├── FritterFactoryTest.java │ │ ├── FritterFactoryTestWithMold.java │ │ └── FritterFactoryTestWithSubmodel.java └── main │ └── java │ └── one │ └── equinox │ └── fritterfactory │ ├── providers │ ├── primitives │ │ ├── StringProvider.java │ │ ├── LongProvider.java │ │ ├── FloatProvider.java │ │ ├── DoubleProvider.java │ │ ├── BooleanProvider.java │ │ └── IntegerProvider.java │ ├── lorem │ │ ├── CityProvider.java │ │ ├── CountryProvider.java │ │ ├── LastNameProvider.java │ │ ├── FirstNameProvider.java │ │ └── WordProvider.java │ ├── fritterproviders │ │ ├── FritterProvider.java │ │ └── ModelFritterProvider.java │ ├── basic │ │ ├── DateProvider.java │ │ └── CalendarProvider.java │ ├── ListItemProvider.java │ ├── ModelProvider.java │ └── images │ │ └── PersonImageProvider.java │ ├── mold │ ├── Mold.java │ ├── ClassMold.java │ ├── MapMold.java │ └── InstanceMold.java │ ├── annotation │ └── FritterIgnoreField.java │ ├── util │ ├── RandomFactory.java │ └── ListedMap.java │ ├── FritterFactoryException.java │ ├── ExistingModels.java │ ├── ProviderFactory.java │ ├── ReflectionUtil.java │ └── FritterFactory.java ├── fritterfactory.iml ├── .gitignore ├── gradlew.bat ├── gradlew └── README.md /.idea/.name: -------------------------------------------------------------------------------- 1 | fritterfactory -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'fritterfactory' 2 | 3 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/equinox-one/fritterfactory/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | - openjdk8 5 | 6 | after_success: 7 | - ./gradlew cobertura coveralls 8 | -------------------------------------------------------------------------------- /notes.md: -------------------------------------------------------------------------------- 1 | 2 | #deploy to bintray 3 | ./gradlew clean build bintrayUpload -PbintrayUser=BINTRAY_USERNAME -PbintrayKey=BINTRAY_KEY -PdryRun=false -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/model/SampleEnum.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test.model; 2 | 3 | public enum SampleEnum { 4 | VALUE1, VALUE2, VALUE3 5 | } 6 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/primitives/StringProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.primitives; 2 | 3 | import one.equinox.fritterfactory.providers.lorem.WordProvider; 4 | 5 | 6 | public class StringProvider extends WordProvider { 7 | } 8 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/model/MyModelWithEnum.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test.model; 2 | 3 | public class MyModelWithEnum extends MyModel { 4 | SampleEnum sampleEnum; 5 | 6 | public SampleEnum getSampleEnum() { 7 | return sampleEnum; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/mold/Mold.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.mold; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | 6 | import java.lang.reflect.Field; 7 | 8 | public interface Mold { 9 | Object getMoldValue(FritterFactory factory, Field filed) throws Exception; 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/model/MyModelMold.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test.model; 2 | 3 | public class MyModelMold{ 4 | public String stringVal = SampleValues.SAMPLE_STRING; 5 | public int intVal = SampleValues.SAMPLE_INTEGER; 6 | public Integer integerVal = SampleValues.SAMPLE_INTEGER; 7 | } -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/mold/ClassMold.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.mold; 2 | 3 | 4 | import one.equinox.fritterfactory.ReflectionUtil; 5 | 6 | public class ClassMold extends InstanceMold{ 7 | public ClassMold(Class moldInstance) { 8 | super(ReflectionUtil.newInstance(moldInstance)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/model/MyModelWithExtends.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test.model; 2 | 3 | import one.equinox.symbols.Symbolize; 4 | 5 | @Symbolize 6 | public class MyModelWithExtends extends MyModel { 7 | String anotherAttribute; 8 | 9 | public String getAnotherAttribute() { 10 | return anotherAttribute; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/lorem/CityProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.lorem; 2 | 3 | import com.thedeanda.lorem.LoremIpsum; 4 | 5 | import javax.inject.Provider; 6 | 7 | 8 | public class CityProvider implements Provider { 9 | @Override 10 | public String get() { 11 | return LoremIpsum.getInstance().getCity(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/fritterproviders/FritterProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.fritterproviders; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | 6 | import javax.inject.Provider; 7 | 8 | public interface FritterProvider { 9 | public T get(FritterFactory factory); 10 | public Provider getProvider(FritterFactory factory); 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/lorem/CountryProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.lorem; 2 | 3 | import com.thedeanda.lorem.LoremIpsum; 4 | 5 | import javax.inject.Provider; 6 | 7 | 8 | public class CountryProvider implements Provider { 9 | @Override 10 | public String get() { 11 | return LoremIpsum.getInstance().getCountry(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/lorem/LastNameProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.lorem; 2 | 3 | import com.thedeanda.lorem.LoremIpsum; 4 | 5 | import javax.inject.Provider; 6 | 7 | 8 | public class LastNameProvider implements Provider { 9 | @Override 10 | public String get() { 11 | return LoremIpsum.getInstance().getLastName(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/lorem/FirstNameProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.lorem; 2 | 3 | import com.thedeanda.lorem.LoremIpsum; 4 | 5 | import javax.inject.Provider; 6 | 7 | 8 | public class FirstNameProvider implements Provider { 9 | @Override 10 | public String get() { 11 | return LoremIpsum.getInstance().getFirstName(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/model/MyModelMoldWithProviders.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test.model; 2 | 3 | import javax.inject.Provider; 4 | 5 | public class MyModelMoldWithProviders { 6 | public Provider stringVal = new SampleValues.DummyStringProvider(); 7 | public Provider intVal = new SampleValues.DummyIntegerProvider(); 8 | public Provider integerVal = new SampleValues.DummyIntegerProvider(); 9 | } -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/primitives/LongProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.primitives; 2 | 3 | import one.equinox.fritterfactory.util.RandomFactory; 4 | 5 | import javax.inject.Provider; 6 | import java.util.Random; 7 | 8 | 9 | public class LongProvider implements Provider { 10 | Random random = new RandomFactory().get(); 11 | 12 | @Override 13 | public Long get() { 14 | return random.nextLong(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/primitives/FloatProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.primitives; 2 | 3 | import one.equinox.fritterfactory.util.RandomFactory; 4 | 5 | import javax.inject.Provider; 6 | import java.util.Random; 7 | 8 | 9 | public class FloatProvider implements Provider { 10 | Random random = new RandomFactory().get(); 11 | 12 | @Override 13 | public Float get() { 14 | return random.nextFloat(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/annotation/FritterIgnoreField.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.annotation; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * Annotates a field that should be ignored by Fritter 10 | */ 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target(ElementType.FIELD) 13 | public @interface FritterIgnoreField { 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/primitives/DoubleProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.primitives; 2 | 3 | import one.equinox.fritterfactory.util.RandomFactory; 4 | 5 | import java.util.Random; 6 | 7 | import javax.inject.Provider; 8 | 9 | 10 | public class DoubleProvider implements Provider { 11 | Random random = new RandomFactory().get(); 12 | 13 | @Override 14 | public Double get() { 15 | return random.nextDouble(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/primitives/BooleanProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.primitives; 2 | 3 | import one.equinox.fritterfactory.util.RandomFactory; 4 | 5 | import java.util.Random; 6 | 7 | import javax.inject.Provider; 8 | 9 | 10 | public class BooleanProvider implements Provider { 11 | Random random = new RandomFactory().get(); 12 | 13 | @Override 14 | public Boolean get() { 15 | return random.nextBoolean(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/basic/DateProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.basic; 2 | 3 | import one.equinox.fritterfactory.util.RandomFactory; 4 | 5 | import javax.inject.Provider; 6 | import java.util.Date; 7 | import java.util.Random; 8 | 9 | 10 | public class DateProvider implements Provider { 11 | Random random = new RandomFactory().get(); 12 | 13 | @Override 14 | public Date get() { 15 | return new Date(random.nextLong()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/ListItemProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers; 2 | 3 | import one.equinox.fritterfactory.util.RandomFactory; 4 | 5 | import java.util.List; 6 | 7 | import javax.inject.Provider; 8 | 9 | 10 | public class ListItemProvider implements Provider { 11 | List items; 12 | 13 | public ListItemProvider(List items) { 14 | this.items = items; 15 | } 16 | 17 | @Override 18 | public T get() { 19 | int position = new RandomFactory().get().nextInt(items.size()); 20 | return items.get(position); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/util/RandomFactory.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.util; 2 | 3 | import java.util.Date; 4 | import java.util.Random; 5 | 6 | import javax.inject.Provider; 7 | 8 | /** 9 | * Created by mateuyabar on 15/12/15. 10 | */ 11 | public class RandomFactory implements Provider{ 12 | private static Random random; 13 | 14 | public RandomFactory(){ 15 | if(random==null) { 16 | random = new Random(); 17 | random.setSeed(new Date().getTime()); 18 | } 19 | } 20 | 21 | @Override 22 | public Random get() { 23 | return random; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/basic/CalendarProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.basic; 2 | 3 | import one.equinox.fritterfactory.util.RandomFactory; 4 | 5 | import javax.inject.Provider; 6 | import java.util.Calendar; 7 | import java.util.Date; 8 | import java.util.GregorianCalendar; 9 | import java.util.Random; 10 | 11 | 12 | public class CalendarProvider implements Provider { 13 | Random random = new RandomFactory().get(); 14 | 15 | @Override 16 | public Calendar get() { 17 | Calendar calendar = Calendar.getInstance(); 18 | calendar.setTime(new Date(random.nextLong())); 19 | return calendar; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/lorem/WordProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.lorem; 2 | 3 | import com.thedeanda.lorem.LoremIpsum; 4 | 5 | import javax.inject.Provider; 6 | 7 | 8 | public class WordProvider implements Provider { 9 | int max, min; 10 | public WordProvider() { 11 | this(1,1); 12 | } 13 | 14 | public WordProvider(int count) { 15 | this(count,count); 16 | } 17 | 18 | public WordProvider(int min, int max) { 19 | this.min = min; 20 | this.max = max; 21 | } 22 | 23 | @Override 24 | public String get() { 25 | return LoremIpsum.getInstance().getWords(min, max); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/model/SampleValues.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test.model; 2 | 3 | 4 | import javax.inject.Provider; 5 | 6 | public class SampleValues { 7 | public static final String SAMPLE_STRING = "qwertyuio"; 8 | public static final int SAMPLE_INTEGER = 200; 9 | 10 | public static class DummyStringProvider implements Provider{ 11 | @Override 12 | public String get() { 13 | return SAMPLE_STRING; 14 | } 15 | } 16 | 17 | public static class DummyIntegerProvider implements Provider{ 18 | @Override 19 | public Integer get() { 20 | return SAMPLE_INTEGER; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/primitives/IntegerProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.primitives; 2 | 3 | import one.equinox.fritterfactory.util.RandomFactory; 4 | 5 | import java.util.Random; 6 | 7 | import javax.inject.Provider; 8 | 9 | 10 | public class IntegerProvider implements Provider { 11 | Random random = new RandomFactory().get(); 12 | 13 | int min, max; 14 | 15 | public IntegerProvider(){ 16 | this(0, 100000); 17 | } 18 | 19 | public IntegerProvider(int min, int max) { 20 | this.min = min; 21 | this.max = max; 22 | } 23 | 24 | @Override 25 | public Integer get() { 26 | return min + random.nextInt(max-min); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /fritterfactory.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/FritterFactoryException.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory; 2 | 3 | /** 4 | * Created by mateuyabar on 17/12/15. 5 | */ 6 | public class FritterFactoryException extends RuntimeException { 7 | public FritterFactoryException() { 8 | } 9 | 10 | public FritterFactoryException(String message) { 11 | super(message); 12 | } 13 | 14 | public FritterFactoryException(String message, Throwable cause) { 15 | super(message, cause); 16 | } 17 | 18 | public FritterFactoryException(Throwable cause) { 19 | super(cause); 20 | } 21 | 22 | public FritterFactoryException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 23 | super(message, cause, enableSuppression, writableStackTrace); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/fritterproviders/ModelFritterProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.fritterproviders; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.mold.Mold; 6 | import one.equinox.fritterfactory.providers.ModelProvider; 7 | 8 | import javax.inject.Provider; 9 | 10 | public class ModelFritterProvider implements FritterProvider{ 11 | Class clazz; 12 | Mold mold; 13 | 14 | public ModelFritterProvider(Class clazz, Mold mold) { 15 | this.clazz = clazz; 16 | this.mold = mold; 17 | } 18 | 19 | public T get(FritterFactory factory){ 20 | return factory.build(clazz, mold); 21 | } 22 | 23 | public Provider getProvider(FritterFactory factory){ 24 | return new ModelProvider(factory, clazz, mold); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/util/ListedMap.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.util; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Hashtable; 5 | import java.util.List; 6 | 7 | /** 8 | * 9 | * Map which its value is a List. This Map has added functionallity add, which 10 | * enables to add a value to the set of the key. 11 | * 12 | */ 13 | public class ListedMap extends Hashtable> { 14 | private static final long serialVersionUID = 5210326981038218066L; 15 | 16 | public void add(K key, V value) { 17 | if (get(key) == null) { 18 | super.put(key, createEntry()); 19 | } 20 | get(key).add(value); 21 | } 22 | 23 | public void addAll(K key, List value) { 24 | for(int i=0; i createEntry() { 30 | return new ArrayList(); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/ModelProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers; 2 | 3 | import one.equinox.fritterfactory.FritterFactory; 4 | import one.equinox.fritterfactory.mold.Mold; 5 | 6 | import javax.inject.Provider; 7 | 8 | public class ModelProvider implements Provider{ 9 | FritterFactory fritterFactory; 10 | Class clazz; 11 | Mold mold; 12 | 13 | public ModelProvider(FritterFactory fritterFactory, Class clazz) { 14 | this(fritterFactory, clazz, null); 15 | } 16 | 17 | 18 | 19 | public ModelProvider(FritterFactory fritterFactory, Class clazz, Mold mold) { 20 | this.clazz = clazz; 21 | this.fritterFactory = fritterFactory; 22 | this.mold = mold; 23 | } 24 | 25 | @Override 26 | public T get() { 27 | return fritterFactory.buildModel(clazz, mold); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/mold/MapMold.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.mold; 2 | 3 | import one.equinox.fritterfactory.FritterFactory; 4 | 5 | import java.lang.reflect.Field; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | 10 | public class MapMold implements Mold{ 11 | Map attributes = new HashMap(); 12 | 13 | public MapMold(){} 14 | 15 | public MapMold(Map attributes) { 16 | this.attributes = attributes; 17 | } 18 | 19 | public MapMold put(String string, Object value){ 20 | attributes.put(string, value); 21 | return this; 22 | } 23 | 24 | @Override 25 | public Object getMoldValue(FritterFactory factory, Field field) throws Exception { 26 | Object moldFieldValue = attributes.get(field.getName()); 27 | if(moldFieldValue==null) 28 | return null; 29 | return InstanceMold.getValueFromProperty(factory, moldFieldValue); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/providers/images/PersonImageProvider.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.providers.images; 2 | 3 | import one.equinox.fritterfactory.providers.ListItemProvider; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by mateuyabar on 18/12/15. 10 | */ 11 | public class PersonImageProvider extends ListItemProvider { 12 | private static List images; 13 | 14 | private static List getImages(){ 15 | if(images == null){ 16 | images = new ArrayList(); 17 | for(int i=0; i<50; ++i){ 18 | String manUrl = "http://api.randomuser.me/portraits/men/"+i+".jpg"; 19 | String womenUrl = "http://api.randomuser.me/portraits/women/"+i+".jpg"; 20 | images.add(manUrl); 21 | images.add(womenUrl); 22 | } 23 | } 24 | return images; 25 | } 26 | 27 | public PersonImageProvider() { 28 | super(getImages()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.idea/modules/fritterfactory_main.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/ExistingModels.java: -------------------------------------------------------------------------------- 1 | //package com.mateuyabar.fritterfactory; 2 | // 3 | //import ListedMap; 4 | //import RandomFactory; 5 | // 6 | //import java.util.List; 7 | // 8 | //import javax.inject.Provider; 9 | // 10 | // 11 | //public class ExistingModels { 12 | // ListedMap, Object> generatedModels; 13 | // 14 | // public void add(Object model){ 15 | // generatedModels.add(model.getClass(), model); 16 | // } 17 | // 18 | // public void addAll(List models){ 19 | // generatedModels.addAll(models.get(0).getClass(), models); 20 | // } 21 | // 22 | // public Provider getProvider(Class clazz){ 23 | // return new ExistingModelsProvider(clazz); 24 | // } 25 | // 26 | // private class ExistingModelsProvider implements Provider{ 27 | // Class clazz; 28 | // 29 | // public ExistingModelsProvider(Class clazz) { 30 | // this.clazz = clazz; 31 | // } 32 | // 33 | // @Override 34 | // public T get() { 35 | // List existing = (List) generatedModels.get(clazz); 36 | // int position = new RandomFactory().get().nextInt(existing.size()); 37 | // return existing.get(position); 38 | // } 39 | // } 40 | //} 41 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/ModelWithNoDefaultConstructorTests.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.FritterFactoryException; 6 | import org.junit.Rule; 7 | import org.junit.Test; 8 | import org.junit.rules.ExpectedException; 9 | 10 | import java.util.List; 11 | 12 | import static org.junit.Assert.assertNotNull; 13 | 14 | public class ModelWithNoDefaultConstructorTests { 15 | @Rule public final ExpectedException exception = ExpectedException.none(); 16 | 17 | FritterFactory factory = new FritterFactory(); 18 | 19 | @Test 20 | public void privateConstructorModel(){ 21 | List list = factory.buildList(PrivateConstructorModel.class); 22 | assertNotNull(list); 23 | } 24 | 25 | @Test 26 | public void noDefaultConstructor(){ 27 | exception.expect(FritterFactoryException.class); 28 | List list = factory.buildList(NoDefaultConstructor.class); 29 | } 30 | 31 | public static class PrivateConstructorModel{ 32 | private PrivateConstructorModel(){} 33 | } 34 | 35 | public static class NoDefaultConstructor{ 36 | private NoDefaultConstructor(int someValue){} 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/model/MyModel.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test.model; 2 | 3 | import one.equinox.fritterfactory.annotation.FritterIgnoreField; 4 | import one.equinox.symbols.Symbolize; 5 | 6 | @Symbolize 7 | public class MyModel { 8 | @FritterIgnoreField 9 | String ignoredStringVal; 10 | 11 | @FritterIgnoreField 12 | final String finalIgnoredString; 13 | 14 | MyModel() { 15 | finalStringVal = null; 16 | finalIgnoredString = null; 17 | } 18 | 19 | final String finalStringVal; 20 | String stringVal; 21 | int intVal; 22 | Integer integerVal; 23 | 24 | 25 | public String getStringVal() { 26 | return stringVal; 27 | } 28 | public int getIntVal() { 29 | return intVal; 30 | } 31 | public Integer getIntegerVal() { 32 | return integerVal; 33 | } 34 | public String getIgnoredStringVal() { return ignoredStringVal; } 35 | public String getFinalIgnoredString() { return finalIgnoredString; } 36 | public String getFinalStringVal() { return finalStringVal; } 37 | 38 | public static String sNoVal; 39 | 40 | @FritterIgnoreField 41 | public static String sIgnoredString; 42 | 43 | public static String sVal = "My String"; 44 | public static final String fVal = "My final String"; 45 | 46 | public static final String fNullVal = null; 47 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea/workspace.xml 2 | .idea/libraries 3 | /out/ 4 | /.gradle 5 | /build 6 | 7 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 8 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 9 | 10 | # User-specific stuff 11 | .idea/**/workspace.xml 12 | .idea/**/tasks.xml 13 | .idea/**/usage.statistics.xml 14 | .idea/**/dictionaries 15 | .idea/**/shelf 16 | 17 | # Generated files 18 | .idea/**/contentModel.xml 19 | 20 | # Sensitive or high-churn files 21 | .idea/**/dataSources/ 22 | .idea/**/dataSources.ids 23 | .idea/**/dataSources.local.xml 24 | .idea/**/sqlDataSources.xml 25 | .idea/**/dynamic.xml 26 | .idea/**/uiDesigner.xml 27 | .idea/**/dbnavigator.xml 28 | 29 | # Gradle 30 | .idea/**/gradle.xml 31 | .idea/**/libraries 32 | 33 | # Gradle and Maven with auto-import 34 | # When using Gradle or Maven with auto-import, you should exclude module files, 35 | # since they will be recreated, and may cause churn. Uncomment if using 36 | # auto-import. 37 | # .idea/modules.xml 38 | # .idea/*.iml 39 | # .idea/modules 40 | 41 | # CMake 42 | cmake-build-*/ 43 | 44 | # Mongo Explorer plugin 45 | .idea/**/mongoSettings.xml 46 | 47 | # File-based project format 48 | *.iws 49 | 50 | # IntelliJ 51 | out/ 52 | 53 | # mpeltonen/sbt-idea plugin 54 | .idea_modules/ 55 | 56 | # JIRA plugin 57 | atlassian-ide-plugin.xml 58 | 59 | # Cursive Clojure plugin 60 | .idea/replstate.xml 61 | 62 | # Crashlytics plugin (for Android Studio and IntelliJ) 63 | com_crashlytics_export_strings.xml 64 | crashlytics.properties 65 | crashlytics-build.properties 66 | fabric.properties 67 | 68 | # Editor-based Rest Client 69 | .idea/httpRequests 70 | 71 | # Android studio 3.1+ serialized cache file 72 | .idea/caches/build_file_checksums.ser 73 | 74 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/ProviderFactory.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory; 2 | 3 | import one.equinox.fritterfactory.providers.basic.CalendarProvider; 4 | import one.equinox.fritterfactory.providers.basic.DateProvider; 5 | import one.equinox.fritterfactory.providers.primitives.*; 6 | import one.equinox.fritterfactory.providers.lorem.WordProvider; 7 | 8 | import java.util.Calendar; 9 | import java.util.Date; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import javax.inject.Provider; 14 | 15 | 16 | public class ProviderFactory { 17 | Map, Provider> providers = new HashMap, Provider>(); 18 | 19 | public Provider get(Class clazz){ 20 | return (Provider) providers.get(clazz); 21 | } 22 | 23 | public void addProvider(Class clazz, Provider provider){ 24 | providers.put(clazz, provider); 25 | } 26 | 27 | public static class DefaultProviderFactory extends ProviderFactory{ 28 | public DefaultProviderFactory (){ 29 | //addProvider(String.class, new StringProvider()); 30 | addProvider(String.class, new WordProvider()); 31 | addProvider(Integer.class, new IntegerProvider()); 32 | addProvider(int.class, new IntegerProvider()); 33 | addProvider(Long.class, new LongProvider()); 34 | addProvider(long.class, new LongProvider()); 35 | addProvider(Double.class, new DoubleProvider()); 36 | addProvider(double.class, new DoubleProvider()); 37 | addProvider(Float.class, new FloatProvider()); 38 | addProvider(float.class, new FloatProvider()); 39 | addProvider(Boolean.class, new BooleanProvider()); 40 | addProvider(boolean.class, new BooleanProvider()); 41 | 42 | 43 | addProvider(Date.class, new DateProvider()); 44 | addProvider(Calendar.class, new CalendarProvider()); 45 | 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/FritterFactoryTestModelWithEnum.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.mold.MapMold; 6 | import one.equinox.fritterfactory.mold.Mold; 7 | import one.equinox.fritterfactory.test.model.MyModelWithEnum; 8 | import one.equinox.fritterfactory.test.model.MyModelWithExtends; 9 | import org.junit.Test; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | public class FritterFactoryTestModelWithEnum { 14 | FritterFactory fritterFactory = new FritterFactory(); 15 | 16 | @Test 17 | public void buildModel() throws Exception { 18 | MyModelWithEnum model = fritterFactory.build(MyModelWithEnum.class); 19 | assertNotNull(model.getStringVal()); 20 | assertNotNull(model.getSampleEnum()); 21 | assertNotNull(model.getIntegerVal()); 22 | assertNotEquals(model.getIntVal(),0); 23 | 24 | } 25 | 26 | // @Test 27 | // public void buildModelWithMapMold() throws Exception { 28 | // Mold mapMold = new MapMold() 29 | // .put(MyModelWithExtendsSymbols.ANOTHER_ATTRIBUTE, FritterFactoryTestWithMapMold.STRING_VAL) 30 | // .put(MyModelWithExtendsSymbols.STRING_VAL, FritterFactoryTestWithMapMold.STRING_VAL) 31 | // .put(MyModelWithExtendsSymbols.INT_VAL, FritterFactoryTestWithMapMold.INTVAL) 32 | // .put(MyModelWithExtendsSymbols.INTEGER_VAL, FritterFactoryTestWithMapMold.INTEGER_VAL); 33 | // MyModelWithExtends model = fritterFactory.build(MyModelWithExtends.class, mapMold); 34 | // assertEquals(model.getStringVal(), FritterFactoryTestWithMapMold.STRING_VAL); 35 | // assertEquals(model.getAnotherAttribute(), FritterFactoryTestWithMapMold.STRING_VAL); 36 | // assertEquals(model.getIntVal(), FritterFactoryTestWithMapMold.INTVAL); 37 | // assertEquals(model.getIntegerVal(), (Integer) FritterFactoryTestWithMapMold.INTEGER_VAL); 38 | // } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/mold/InstanceMold.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.mold; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.providers.fritterproviders.FritterProvider; 6 | import one.equinox.fritterfactory.ReflectionUtil; 7 | 8 | import java.lang.reflect.Field; 9 | 10 | import javax.inject.Provider; 11 | 12 | public class InstanceMold implements Mold { 13 | Object moldInstance; 14 | 15 | public InstanceMold(Object moldInstance) { 16 | this.moldInstance = moldInstance; 17 | } 18 | 19 | /** 20 | * Given an object, it returns the value its value on a given field. 21 | * If the value is a provider it gets the value of the provider. 22 | * If not found, returns null 23 | * 24 | * @param factory fritter factory 25 | * @param field filed to look for 26 | * @return value of the field 27 | * @throws Exception 28 | */ 29 | public Object getMoldValue(FritterFactory factory, Field field) throws Exception{ 30 | if(moldInstance==null) 31 | return null; 32 | Class moldClass = moldInstance.getClass(); 33 | Field moldField = ReflectionUtil.getStoredField(moldClass, field.getName()); 34 | if(moldField==null) 35 | return null; 36 | moldField.setAccessible(true); 37 | Object moldFieldValue = moldField.get(moldInstance); 38 | 39 | return getValueFromProperty(factory, moldFieldValue); 40 | } 41 | 42 | 43 | protected static Object getValueFromProperty(FritterFactory factory, Object moldFieldValue){ 44 | if(moldFieldValue instanceof Provider){ 45 | //its a provider, we generate a new value 46 | moldFieldValue = ((Provider)moldFieldValue).get(); 47 | } else if (moldFieldValue instanceof FritterProvider) { 48 | //its a fritter provider, we generate a new value 49 | moldFieldValue = ((FritterProvider) moldFieldValue).get(factory); 50 | } 51 | return moldFieldValue; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/FritterFactoryTestModelWithExtends.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.mold.MapMold; 6 | import one.equinox.fritterfactory.mold.Mold; 7 | import one.equinox.fritterfactory.test.model.MyModel; 8 | import one.equinox.fritterfactory.test.model.MyModelWithExtends; 9 | import org.junit.Test; 10 | import one.equinox.fritterfactory.test.model.symbols.MyModelWithExtendsSymbols; 11 | 12 | import java.util.List; 13 | 14 | import static org.junit.Assert.assertEquals; 15 | import static org.junit.Assert.assertNotEquals; 16 | import static org.junit.Assert.assertNotNull; 17 | 18 | public class FritterFactoryTestModelWithExtends { 19 | FritterFactory fritterFactory = new FritterFactory(); 20 | 21 | @Test 22 | public void buildModel() throws Exception { 23 | MyModelWithExtends model = fritterFactory.build(MyModelWithExtends.class); 24 | assertNotNull(model.getStringVal()); 25 | assertNotNull(model.getAnotherAttribute()); 26 | assertNotNull(model.getIntegerVal()); 27 | assertNotEquals(model.getIntVal(),0); 28 | 29 | } 30 | 31 | @Test 32 | public void buildModelWithMapMold() throws Exception { 33 | Mold mapMold = new MapMold() 34 | .put(MyModelWithExtendsSymbols.ANOTHER_ATTRIBUTE, FritterFactoryTestWithMapMold.STRING_VAL) 35 | .put(MyModelWithExtendsSymbols.STRING_VAL, FritterFactoryTestWithMapMold.STRING_VAL) 36 | .put(MyModelWithExtendsSymbols.INT_VAL, FritterFactoryTestWithMapMold.INTVAL) 37 | .put(MyModelWithExtendsSymbols.INTEGER_VAL, FritterFactoryTestWithMapMold.INTEGER_VAL); 38 | MyModelWithExtends model = fritterFactory.build(MyModelWithExtends.class, mapMold); 39 | assertEquals(model.getStringVal(), FritterFactoryTestWithMapMold.STRING_VAL); 40 | assertEquals(model.getAnotherAttribute(), FritterFactoryTestWithMapMold.STRING_VAL); 41 | assertEquals(model.getIntVal(), FritterFactoryTestWithMapMold.INTVAL); 42 | assertEquals(model.getIntegerVal(), (Integer) FritterFactoryTestWithMapMold.INTEGER_VAL); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/FritterFactoryTestWithMapMold.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.mold.MapMold; 6 | import one.equinox.fritterfactory.mold.Mold; 7 | import one.equinox.fritterfactory.providers.primitives.IntegerProvider; 8 | import one.equinox.fritterfactory.providers.primitives.StringProvider; 9 | import one.equinox.fritterfactory.test.model.MyModel; 10 | import org.junit.Test; 11 | 12 | import static org.junit.Assert.assertEquals; 13 | 14 | import one.equinox.fritterfactory.test.model.symbols.MyModelSymbols; 15 | 16 | public class FritterFactoryTestWithMapMold { 17 | FritterFactory fritterFactory = new FritterFactory(); 18 | 19 | public static final String stringVal = "stringVal"; 20 | public static final String intVal = "intVal"; 21 | public static final String integerVal = "integerVal"; 22 | 23 | private static final StringProvider stringProvider = new StringProvider(); 24 | public static final String STRING_VAL = stringProvider.get(); 25 | public static final int INTVAL = new IntegerProvider().get(); 26 | public static final Integer INTEGER_VAL = new IntegerProvider().get(); 27 | 28 | 29 | 30 | 31 | @Test 32 | public void buildModelWithMapMold() throws Exception { 33 | Mold mapMold = new MapMold() 34 | .put(MyModelSymbols.STRING_VAL, STRING_VAL) 35 | .put(MyModelSymbols.INT_VAL, INTVAL) 36 | .put(MyModelSymbols.INTEGER_VAL, INTEGER_VAL); 37 | MyModel model = fritterFactory.build(MyModel.class, mapMold); 38 | assertEquals(model.getStringVal(), STRING_VAL); 39 | assertEquals(model.getIntVal(), INTVAL); 40 | assertEquals(model.getIntegerVal(), (Integer) INTEGER_VAL); 41 | } 42 | 43 | @Test 44 | public void buildModelWithMapMoldWithProviders() throws Exception { 45 | int staticInt = 5000; 46 | IntegerProvider intProvider = new IntegerProvider(staticInt, staticInt+1); 47 | Mold mapMold = new MapMold().put(intVal, intProvider); 48 | 49 | MyModel model = fritterFactory.build(MyModel.class, mapMold); 50 | assertEquals(model.getIntVal(), staticInt); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/FritterFactoryTest.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.test.model.MyModel; 6 | 7 | import org.junit.Test; 8 | 9 | import java.util.List; 10 | 11 | import static org.junit.Assert.assertEquals; 12 | import static org.junit.Assert.assertNotEquals; 13 | import static org.junit.Assert.assertNotNull; 14 | import static org.junit.Assert.assertNull; 15 | 16 | public class FritterFactoryTest { 17 | FritterFactory fritterFactory = new FritterFactory(); 18 | 19 | @Test 20 | public void buildModelFillsModel() throws Exception { 21 | MyModel model = fritterFactory.build(MyModel.class); 22 | assertNotNull(model.getStringVal()); 23 | assertNotNull(model.getIntegerVal()); 24 | assertNotEquals(model.getIntVal(),0); 25 | assertNotNull(model.getFinalStringVal()); 26 | 27 | assertNotNull(MyModel.sNoVal); 28 | 29 | // Make sure that non-null (or final) statics are not overwritten 30 | assertEquals(MyModel.sVal, "My String"); 31 | assertEquals(MyModel.fVal, "My final String"); 32 | assertNull(MyModel.fNullVal); 33 | 34 | // Assert ignored fields are still null 35 | assertNull(model.getIgnoredStringVal()); 36 | assertNull(model.getFinalIgnoredString()); 37 | assertNull(MyModel.sIgnoredString); 38 | } 39 | 40 | @Test 41 | public void buildListModelFillsModel() throws Exception { 42 | List models = fritterFactory.buildList(MyModel.class, 10); 43 | assertEquals(models.size(), 10); 44 | for(MyModel model : models){ 45 | assertNotNull(model.getStringVal()); 46 | assertNotNull(model.getIntegerVal()); 47 | assertNotEquals(model.getIntVal(), 0); 48 | assertNotNull(model.getFinalStringVal()); 49 | } 50 | } 51 | 52 | @Test 53 | public void buildModelShouldRandomizeValues() throws Exception { 54 | MyModel model = fritterFactory.build(MyModel.class); 55 | MyModel model2 = fritterFactory.build(MyModel.class); 56 | //may fail if unlucky 57 | assertNotEquals(model.getStringVal(), model2.getStringVal()); 58 | assertNotEquals(model.getIntegerVal(), model2.getIntegerVal()); 59 | assertNotEquals(model.getIntVal(), model2.getIntVal()); 60 | assertNotEquals(model.getFinalStringVal(), model2.getFinalStringVal()); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/FritterFactoryTestWithMold.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.mold.ClassMold; 6 | import one.equinox.fritterfactory.mold.InstanceMold; 7 | import one.equinox.fritterfactory.providers.ModelProvider; 8 | import one.equinox.fritterfactory.providers.primitives.StringProvider; 9 | import one.equinox.fritterfactory.test.model.MyModel; 10 | import one.equinox.fritterfactory.test.model.MyModelMold; 11 | import one.equinox.fritterfactory.test.model.MyModelMoldWithProviders; 12 | 13 | import org.junit.Test; 14 | 15 | import static one.equinox.fritterfactory.test.model.SampleValues.SAMPLE_INTEGER; 16 | import static one.equinox.fritterfactory.test.model.SampleValues.SAMPLE_STRING; 17 | import static org.junit.Assert.assertEquals; 18 | 19 | public class FritterFactoryTestWithMold { 20 | FritterFactory fritterFactory = new FritterFactory(); 21 | 22 | @Test 23 | public void buildModelWithMoldClass() throws Exception { 24 | MyModel model = fritterFactory.build(MyModel.class, new ClassMold(MyModelMold.class)); 25 | assertEquals(model.getStringVal(), SAMPLE_STRING); 26 | assertEquals(model.getIntVal(), SAMPLE_INTEGER); 27 | assertEquals(model.getIntegerVal(), (Integer) SAMPLE_INTEGER); 28 | } 29 | 30 | @Test 31 | public void buildModelWithMoldInstance() throws Exception { 32 | MyModelMold mold = new MyModelMold(); 33 | mold.stringVal = new StringProvider().get(); 34 | 35 | MyModel model = fritterFactory.build(MyModel.class, new InstanceMold(mold)); 36 | assertEquals(model.getStringVal(), mold.stringVal); 37 | assertEquals(model.getIntVal(), SAMPLE_INTEGER); 38 | assertEquals(model.getIntegerVal(), (Integer) SAMPLE_INTEGER); 39 | } 40 | 41 | @Test 42 | public void buildModelWithMoldAsProvider() throws Exception { 43 | FritterFactory fritterFactory = new FritterFactory(); 44 | MyModelMold mold = new MyModelMold(); 45 | InstanceMold intanceMold = new InstanceMold(mold); 46 | 47 | mold.stringVal = new StringProvider().get(); 48 | fritterFactory.addProvider(MyModel.class, new ModelProvider(fritterFactory, MyModel.class, intanceMold)); 49 | 50 | MyModel model = fritterFactory.build(MyModel.class, intanceMold); 51 | assertEquals(model.getStringVal(), mold.stringVal); 52 | assertEquals(model.getIntVal(), SAMPLE_INTEGER); 53 | assertEquals(model.getIntegerVal(), (Integer) SAMPLE_INTEGER); 54 | } 55 | 56 | @Test 57 | public void buildModelWithMoldClassWithProviders() throws Exception { 58 | MyModel model = fritterFactory.build(MyModel.class, new ClassMold(MyModelMoldWithProviders.class)); 59 | assertEquals(model.getStringVal(), SAMPLE_STRING); 60 | assertEquals(model.getIntVal(), SAMPLE_INTEGER); 61 | assertEquals(model.getIntegerVal(), (Integer) SAMPLE_INTEGER); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/ReflectionUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Mateu Yabar Valles (http://mateuyabar.com) 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as 6 | * published by the Free Software Foundation, either version 2 of the 7 | * License, or (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public 15 | * License along with this program. If not, see 16 | * . 17 | */ 18 | 19 | package one.equinox.fritterfactory; 20 | 21 | 22 | import one.equinox.fritterfactory.annotation.FritterIgnoreField; 23 | 24 | import java.lang.reflect.Field; 25 | import java.lang.reflect.Modifier; 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | 29 | public class ReflectionUtil { 30 | 31 | /** 32 | * Returns the declared fields including fields form parent classes that are not transient neither synthetic 33 | * @param type class type 34 | * @return declared fields 35 | */ 36 | public static Field[] getStoredFields(Class type) { 37 | List fields = new ArrayList(); 38 | for (Class c = type; c != null; c = c.getSuperclass()) { 39 | Field[] innerFields = c.getDeclaredFields(); 40 | for(Field field:innerFields){ 41 | if(!field.isSynthetic() && !isTransient(field) && !isIgnored(field) && !isStaticFinal(field)){ 42 | if (isStatic(field) && !hasNullValue(field)) { 43 | // Skip static field with a non-null value 44 | continue; 45 | } 46 | 47 | fields.add(field); 48 | } 49 | } 50 | } 51 | return fields.toArray(new Field[]{}); 52 | } 53 | 54 | public static Field getStoredField(Class type, String name) { 55 | List fields = new ArrayList(); 56 | for (Class c = type; c != null; c = c.getSuperclass()) { 57 | Field[] innerFields = c.getDeclaredFields(); 58 | for(Field field:innerFields){ 59 | if(!field.isSynthetic() && !isTransient(field) && field.getName().equals(name)){ 60 | return field; 61 | } 62 | } 63 | } 64 | return null; 65 | } 66 | 67 | private static boolean isTransient(Field field){ 68 | return Modifier.isTransient(field.getModifiers()); 69 | } 70 | 71 | private static boolean isIgnored(Field field) { 72 | return field.isAnnotationPresent(FritterIgnoreField.class); 73 | } 74 | 75 | private static boolean isStatic(Field field) { 76 | return Modifier.isStatic(field.getModifiers()); 77 | } 78 | 79 | private static boolean isStaticFinal(Field field) { 80 | int modifiers = field.getModifiers(); 81 | return Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers); 82 | } 83 | 84 | private static boolean hasNullValue(Field field) { 85 | field.setAccessible(true); 86 | try { 87 | return (null == field.get(field.getClass())); 88 | } catch (Throwable t) { 89 | return true; 90 | } 91 | } 92 | 93 | public static T newInstance(Class clazz) throws FritterFactoryException { 94 | try{ 95 | return clazz.newInstance(); 96 | } catch (Exception e){ 97 | throw new FritterFactoryException(e); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /.idea/modules/fritterfactory_test.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/main/java/one/equinox/fritterfactory/FritterFactory.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory; 2 | 3 | 4 | import one.equinox.fritterfactory.mold.Mold; 5 | import one.equinox.fritterfactory.providers.ModelProvider; 6 | 7 | import java.lang.reflect.Constructor; 8 | import java.lang.reflect.Field; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | import javax.inject.Provider; 13 | 14 | public class FritterFactory { 15 | int defaultListSize = 10; 16 | ProviderFactory providers; 17 | 18 | public FritterFactory(){ 19 | this(new ProviderFactory.DefaultProviderFactory()); 20 | } 21 | 22 | public FritterFactory(ProviderFactory providers){ 23 | this.providers = providers; 24 | } 25 | 26 | public void addProvider(Class clazz, Provider provider){ 27 | providers.addProvider(clazz, provider); 28 | } 29 | 30 | public T build(Class modelClass) throws FritterFactoryException { 31 | return build(modelClass, null); 32 | } 33 | 34 | public T build(Class modelClass, Mold mold) throws FritterFactoryException { 35 | Provider provider = providers.get(modelClass); 36 | if(mold==null && provider!=null) 37 | return provider.get(); 38 | else 39 | return buildModel(modelClass, mold); 40 | } 41 | 42 | public T buildModel(Class modelClass, Mold mold) throws FritterFactoryException { 43 | T result; 44 | try { 45 | Constructor constructor = modelClass.getDeclaredConstructor(); 46 | constructor.setAccessible(true); 47 | result = constructor.newInstance(); 48 | } catch (Exception e){ 49 | throw new FritterFactoryException(modelClass.getName()+" does not define default constructor required by Fritter Factoy",e); 50 | } 51 | try{ 52 | for (Field field : ReflectionUtil.getStoredFields(modelClass)) { 53 | field.setAccessible(true); 54 | Object value = getMoldValue(mold, field); 55 | if (value == null) 56 | value = generateValue(field); 57 | field.set(result, value); 58 | } 59 | return result; 60 | } catch (Exception e){ 61 | throw new FritterFactoryException(e); 62 | } 63 | } 64 | 65 | public List buildList(Class modelClass) throws FritterFactoryException { 66 | return buildList(modelClass, defaultListSize); 67 | } 68 | 69 | public List buildList(Class modelClass, int size) throws FritterFactoryException { 70 | return buildList(modelClass, size, null); 71 | } 72 | 73 | public List buildList(Class modelClass, int size, Mold mold) throws FritterFactoryException { 74 | List result = new ArrayList(); 75 | for(int i=0; i type = field.getType(); 101 | Provider provider = providers.get(type); 102 | if(provider==null){ 103 | if(type.isEnum()){ 104 | return type.getEnumConstants()[0]; 105 | } 106 | //We hope it is a submodel 107 | provider = getProvider(type); 108 | } 109 | return provider.get(); 110 | } 111 | 112 | public int getDefaultListSize() { 113 | return defaultListSize; 114 | } 115 | 116 | public void setDefaultListSize(int defaultListSize) { 117 | this.defaultListSize = defaultListSize; 118 | } 119 | 120 | private Provider getProvider(Class clazz){ 121 | return new ModelProvider(this, clazz); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/test/java/one/equinox/fritterfactory/test/FritterFactoryTestWithSubmodel.java: -------------------------------------------------------------------------------- 1 | package one.equinox.fritterfactory.test; 2 | 3 | 4 | import one.equinox.fritterfactory.FritterFactory; 5 | import one.equinox.fritterfactory.mold.ClassMold; 6 | import one.equinox.fritterfactory.providers.ListItemProvider; 7 | import one.equinox.fritterfactory.providers.ModelProvider; 8 | import one.equinox.fritterfactory.providers.fritterproviders.FritterProvider; 9 | import one.equinox.fritterfactory.providers.fritterproviders.ModelFritterProvider; 10 | import one.equinox.fritterfactory.providers.primitives.StringProvider; 11 | 12 | import org.junit.Test; 13 | 14 | import java.util.List; 15 | 16 | import javax.inject.Provider; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | import static org.junit.Assert.assertNotNull; 20 | import static org.junit.Assert.assertTrue; 21 | 22 | public class FritterFactoryTestWithSubmodel { 23 | private static final StringProvider stringProvider = new StringProvider(); 24 | public static final String SUBMODEL_NAME = stringProvider.get(); 25 | public static final String MODEL_MOLD_NAME = stringProvider.get(); 26 | public static final String SUBMODEL_MOLD_STATIC_MODEL_NAME = stringProvider.get(); 27 | 28 | 29 | 30 | @Test 31 | public void testSubModelWithoutMolds(){ 32 | FritterFactory fritterFactory = new FritterFactory(); 33 | SubModel subModel = fritterFactory.build(SubModel.class); 34 | assertNotNull(subModel); 35 | assertNotNull(subModel.model); 36 | assertNotNull(subModel.name); 37 | assertNotNull(subModel.model.name); 38 | } 39 | 40 | @Test 41 | public void testSubModelWithFixedSubmodels(){ 42 | FritterFactory fritterFactory = new FritterFactory(); 43 | List models = fritterFactory.buildList(Model.class, 2); 44 | Provider modelProvider = new ListItemProvider(models); 45 | fritterFactory.addProvider(Model.class, modelProvider); 46 | List subModels = fritterFactory.buildList(SubModel.class, 30); 47 | for(SubModel subModel : subModels){ 48 | assertNotNull(subModel); 49 | assertNotNull(subModel.model); 50 | assertNotNull(subModel.name); 51 | assertNotNull(subModel.model.name); 52 | assertTrue(models.contains(subModel.model)); 53 | } 54 | 55 | } 56 | 57 | @Test 58 | public void testSubModelAddingMoldPorviderWithStaticSubmodel(){ 59 | FritterFactory fritterFactory = new FritterFactory(); 60 | fritterFactory.addProvider(SubModel.class, new ModelProvider(fritterFactory, SubModel.class, new ClassMold(SubModelMoldSaticModel.class))); 61 | SubModel subModel = fritterFactory.build(SubModel.class); 62 | 63 | assertNotNull(subModel); 64 | assertNotNull(subModel.model); 65 | assertNotNull(subModel.name); 66 | assertEquals(subModel.name, SUBMODEL_NAME); 67 | assertNotNull(subModel.model.name); 68 | assertEquals(subModel.model.name, SUBMODEL_MOLD_STATIC_MODEL_NAME); 69 | } 70 | 71 | @Test 72 | public void testSubModelAddingMoldPorviderWithSubmodelFritterProvider(){ 73 | FritterFactory fritterFactory = new FritterFactory(); 74 | fritterFactory.addProvider(SubModel.class, new ModelProvider(fritterFactory, SubModel.class, new ClassMold(SubModelMoldSaticModel.class))); 75 | SubModel subModel = fritterFactory.build(SubModel.class); 76 | 77 | assertNotNull(subModel); 78 | assertNotNull(subModel.model); 79 | assertNotNull(subModel.name); 80 | assertEquals(subModel.name, SUBMODEL_NAME); 81 | assertNotNull(subModel.model.name); 82 | assertEquals(subModel.model.name, SUBMODEL_MOLD_STATIC_MODEL_NAME); 83 | } 84 | 85 | 86 | 87 | public static class Model{ 88 | public Model(){} 89 | public Model(String name) { 90 | this.name = name; 91 | } 92 | public String name; 93 | } 94 | public static class SubModel{ 95 | public String name; 96 | public Model model; 97 | } 98 | public static class ModelMold{ 99 | public String name = MODEL_MOLD_NAME; 100 | } 101 | public static class SubModelMoldSaticModel{ 102 | String name = SUBMODEL_NAME; 103 | Model model = new Model(SUBMODEL_MOLD_STATIC_MODEL_NAME); 104 | } 105 | public static class SubModelMold{ 106 | String name = SUBMODEL_NAME; 107 | FritterProvider model = new ModelFritterProvider(Model.class, new ClassMold(ModelMold.class)); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/equinox-one/fritterfactory.svg?branch=master)](https://travis-ci.org/equinox-one/fritterfactory) 2 | [![Coverage Status](https://coveralls.io/repos/equinox-one/fritterfactory/badge.svg?branch=master&service=github)](https://coveralls.io/github/equinox-one/fritterfactory?branch=master) 3 | [![Download](https://api.bintray.com/packages/equinox-one/maven/fritterfactory/images/download.svg) ](https://bintray.com/equinox-one/maven/fritterfactory/_latestVersion) 4 | 5 | # Fritter Factory 6 | Library to automatically populate models, to be used in automatic tests or user made tests. 7 | 8 | When performing automatic test we need to create models constantly. 9 | Moreover when using hand made test, we may need to populate an application with fake data. 10 | Fritter Factory has been developed to solve this problems. 11 | 12 | # Android friendly 13 | Fritter Factory is Android Friendly: it does not use any reflection methods not implemented in the Android libraries. Therefore, it can be used in Android applications without any problem. 14 | 15 | # Show me an example 16 | Let's imagine that we have the following model 17 | 18 | ```java 19 | public class Person { 20 | String name; 21 | String surname; 22 | int age; 23 | Adress adress; 24 | String description; 25 | Category category; 26 | String image; 27 | final String country; // fritter will populate this field 28 | @FritterIgnoreField 29 | String memberId; // fritter will leave this field alone 30 | 31 | static String CATEGORY; // fritter will populate this field 32 | static String TAG = 1; // fritter won't touch static field with a non-null value 33 | } 34 | ``` 35 | 36 | We can create a person with all the attributes set with the following: 37 | 38 | ```java 39 | FritterFactory fritterFactory = new FritterFactory(); 40 | List persons = fritterFactory.buildList(Person.class, 3); 41 | ``` 42 | 43 | 44 | # Show me a better example 45 | 46 | That's not all! FritterFactory allows the generated models to be configurable. In this example we will use [Symbols] but its not mandatory: 47 | 48 | ```java 49 | /** 50 | * Print 3 persons, using specific providers defined by a mold. 51 | */ 52 | public void sampleWithMolds(){ 53 | FritterFactory fritterFactory = createFactoryWithMolds(); 54 | List persons = fritterFactory.buildList(Person.class, 3); 55 | System.out.println(persons); 56 | } 57 | 58 | /** 59 | * Creates a FritterFactory with defined providers for Person and Adress. 60 | * @return 61 | */ 62 | public FritterFactory createFactoryWithMolds(){ 63 | FritterFactory fritterFactory = new FritterFactory(); 64 | fritterFactory.addProvider(Person.class, createPersonProvider(fritterFactory)); 65 | fritterFactory.addProvider(Adress.class, createAdressProvider(fritterFactory)); 66 | return fritterFactory; 67 | } 68 | 69 | public Provider createPersonProvider(FritterFactory fritterFactory){ 70 | MapMold personMold = new MapMold(); 71 | personMold.put(PersonSymbols.NAME, new FirstNameProvider()); 72 | personMold.put(PersonSymbols.SURNAME, new FirstNameProvider()); 73 | personMold.put(PersonSymbols.AGE, new IntegerProvider(0,110)); 74 | personMold.put(PersonSymbols.DESCRIPTION, new WordProvider(50, 100)); 75 | personMold.put(PersonSymbols.IMAGE, new PersonImageProvider()); 76 | return new ModelProvider(fritterFactory, Person.class, personMold); 77 | } 78 | 79 | public Provider createAdressProvider(FritterFactory fritterFactory){ 80 | MapMold adressMold = new MapMold(); 81 | adressMold.put(AdressSymbols.STREET, new WordProvider(1, 3)); 82 | adressMold.put(AdressSymbols.CITY, new CityProvider()); 83 | adressMold.put(AdressSymbols.COUNTRY, new CountryProvider()); 84 | return new ModelProvider(fritterFactory, Adress.class, adressMold); 85 | } 86 | ``` 87 | 88 | # Get full Example 89 | 90 | You can download the [full example at github]. 91 | 92 | # Which types can be automatically populated. 93 | 94 | By default, Fritter Factory is created using the DefaultProviderFactory. This allows to create the following random values: 95 | 96 | - String.class 97 | - Integer.class 98 | - int.class 99 | - Long.class 100 | - long.class 101 | - Double.class 102 | - double.class 103 | - Float.class 104 | - float.class 105 | - Boolean.class 106 | - boolean.class 107 | - Date.class 108 | - Calendar.class 109 | 110 | # Adding providers 111 | 112 | It can also create complex models that use this sub types. However we may need to create our own providers in some cases. As an example, lets imagine that we use Joda time classes and we have a model that uses LocalDate. 113 | We will need to create a provider for this type like the following: 114 | 115 | ```java 116 | public class LocalDateProvider implements Provider { 117 | Random random = new RandomFactory().get(); 118 | 119 | @Override 120 | public LocalDate get() { 121 | return new LocalDate(random.nextLong()); 122 | } 123 | } 124 | ``` 125 | 126 | and add it to the Fritter Factory 127 | 128 | ```java 129 | fritterFactory.addProvider(LocalDate.class, new LocalDateProvider()); 130 | ``` 131 | 132 | # Default constructor required 133 | 134 | One last thing. To create new instances of your classes, FritterFactory needs to find a default constructor (one with no parameters). 135 | This constructor can be private (if desired), but must exist. 136 | 137 | # Download 138 | 139 | Grab via Gradle: 140 | ```groovy 141 | repositories { jcenter() } 142 | 143 | compile 'one.equinox.fritterfactory:fritterfactory:+' 144 | ``` 145 | or via Maven: 146 | ```xml 147 | 148 | jcenter 149 | http://jcenter.bintray.com 150 | 151 | 152 | 153 | one.equinox.fritterfactory 154 | fritterfactory 155 | + 156 | 157 | ``` 158 | 159 | # What's next 160 | Some ideas on how to improve this library 161 | - Create more default providers for other types. 162 | - Use annotation processing to create mold classes instead of using [Symbols]. 163 | 164 | License 165 | ======= 166 | 167 | Copyright 2016 Equinox.one 168 | 169 | Licensed under the Apache License, Version 2.0 (the "License"); 170 | you may not use this file except in compliance with the License. 171 | You may obtain a copy of the License at 172 | 173 | http://www.apache.org/licenses/LICENSE-2.0 174 | 175 | Unless required by applicable law or agreed to in writing, software 176 | distributed under the License is distributed on an "AS IS" BASIS, 177 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 178 | See the License for the specific language governing permissions and 179 | limitations under the License. 180 | 181 | 182 | 183 | [Symbols]: https://github.com/equinox-one/symbols 184 | [full example at github]: https://github.com/equinox-one/fritterfactory-example -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | --------------------------------------------------------------------------------