Fritter Factory

Additional

Language
Java
Version
N/A
Created
Dec 18, 2015
Updated
Jan 14, 2020 (Retired)
Owner
Equinox.one (equinox-one)
Contributor
Mateu Y (mateuy-dev)
1
Activity
Badge
Generate
Download
Source code

Advertisement

Fritter Factory

Library to automatically populate models, to be used in automatic tests or user made tests.

When performing automatic test we need to create models constantly. Moreover when using hand made test, we may need to populate an application with fake data. Fritter Factory has been developed to solve this problems.

Android friendly

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.

Show me an example

Let's imagine that we have the following model

public class Person {
    String name;
    String surname;
    int age;
    Adress adress;
    String description;
    Category category;
    String image;
    final String country; // fritter will populate this field
    @FritterIgnoreField
    String memberId; // fritter will leave this field alone
    
    static String CATEGORY; // fritter will populate this field
    static String TAG = 1; // fritter won't touch static field with a non-null value
}

We can create a person with all the attributes set with the following:

FritterFactory fritterFactory = new FritterFactory();
List<Person> persons = fritterFactory.buildList(Person.class, 3);

Show me a better example

That's not all! FritterFactory allows the generated models to be configurable. In this example we will use Symbols but its not mandatory:

    /**
     * Print 3 persons, using specific providers defined by a mold.
     */
    public void sampleWithMolds(){
        FritterFactory fritterFactory = createFactoryWithMolds();
        List<Person> persons = fritterFactory.buildList(Person.class, 3);
        System.out.println(persons);
    }

    /**
     * Creates a FritterFactory with defined providers for Person and Adress.
     * @return
     */
    public FritterFactory createFactoryWithMolds(){
        FritterFactory fritterFactory = new FritterFactory();
        fritterFactory.addProvider(Person.class, createPersonProvider(fritterFactory));
        fritterFactory.addProvider(Adress.class, createAdressProvider(fritterFactory));
        return fritterFactory;
    }

    public Provider<Person> createPersonProvider(FritterFactory fritterFactory){
        MapMold personMold = new MapMold();
        personMold.put(PersonSymbols.NAME, new FirstNameProvider());
        personMold.put(PersonSymbols.SURNAME, new FirstNameProvider());
        personMold.put(PersonSymbols.AGE, new IntegerProvider(0,110));
        personMold.put(PersonSymbols.DESCRIPTION, new WordProvider(50, 100));
        personMold.put(PersonSymbols.IMAGE, new PersonImageProvider());
        return new ModelProvider<Person>(fritterFactory, Person.class, personMold);
    }

    public Provider<Adress> createAdressProvider(FritterFactory fritterFactory){
        MapMold adressMold = new MapMold();
        adressMold.put(AdressSymbols.STREET, new WordProvider(1, 3));
        adressMold.put(AdressSymbols.CITY, new CityProvider());
        adressMold.put(AdressSymbols.COUNTRY, new CountryProvider());
        return new ModelProvider<Adress>(fritterFactory, Adress.class, adressMold);
    }

Get full Example

You can download the full example at github.

Which types can be automatically populated.

By default, Fritter Factory is created using the DefaultProviderFactory. This allows to create the following random values:

  • String.class
  • Integer.class
  • int.class
  • Long.class
  • long.class
  • Double.class
  • double.class
  • Float.class
  • float.class
  • Boolean.class
  • boolean.class
  • Date.class
  • Calendar.class

Adding providers

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. We will need to create a provider for this type like the following:

public class LocalDateProvider implements Provider<LocalDate> {
    Random random = new RandomFactory().get();

    @Override
    public LocalDate get() {
        return new LocalDate(random.nextLong());
    }
}

and add it to the Fritter Factory

fritterFactory.addProvider(LocalDate.class, new LocalDateProvider());

Default constructor required

One last thing. To create new instances of your classes, FritterFactory needs to find a default constructor (one with no parameters). This constructor can be private (if desired), but must exist.

Download

Grab via Gradle:

repositories { jcenter() }

compile 'one.equinox.fritterfactory:fritterfactory:+'

or via Maven:

<repository>
  <id>jcenter</id>
  <url>http://jcenter.bintray.com</url>
</repository>

<dependency>
    <groupId>one.equinox.fritterfactory</groupId>
    <artifactId>fritterfactory</artifactId>
    <version>+</version>
</dependency>

What's next

Some ideas on how to improve this library

  • Create more default providers for other types.
  • Use annotation processing to create mold classes instead of using Symbols.

License

 Copyright 2016 Equinox.one

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.