Git Product home page Git Product logo

dummy4j's Introduction

Dummy4j

Code Soapbox logo

Build Quality Gate Status Maintainability Rating Coverage Technical Debt Vulnerabilities Bugs GitHub code size in bytes GitHub

Maven Central javadoc

Dummy4j is an easy to use dummy data generator library for Java, designed for extensibility.

Dummy4j can be used in all projects using Java 8+.

Dummy4j

Getting started

Add the following dependency to your pom.xml:

<!-- https://mvnrepository.com/artifact/dev.codesoapbox/dummy4j -->
<dependency>
  <groupId>dev.codesoapbox</groupId>
  <artifactId>dummy4j</artifactId>
  <version>0.11.0</version>
</dependency>

Now you can start using Dummy4j:

Dummy4j dummy = new Dummy4j();

String randomCountry = dummy.nation().country();

The default configuration of Dummy4j uses a file-based definition provider which reads data definitions from .yml files inside the resources/dummy4j folder. Additionally, the default locale is en.

Documentation

Reference documentation for Dummy4j is available at:
https://daniel-frak.github.io/dummy4j

API documentation for Dummy4j is available at:
https://javadoc.io/doc/dev.codesoapbox/dummy4j

Motivation

Generating dummy data greatly simplifies development and testing of software. While many such generators exist, none of those for Java seem to support easy extensibility, which is crucial for many use cases.

While the ideas behind Dummy4j were first presented as a proposal for Java Faker, it turned out that it would be easier to write an entirely new library.

Dummy4j focuses on ease of use, clean code and effortless extensibility, while still taking inspiration from Ruby's Faker gem.

Why extensibility is important

Not all data is going to be useful to the general public and not all data can be publicly shared due to company policies. Therefore, it is imperative a dummy data library allows for easy and maintainable extension of its data definitions.

Furthermore, edge cases may exist where the functionality of the library must be altered. It is easy to imagine wanting to store data definitions in a central private repository from which the library would load them during instantiation. The classes themselves must, therefore, be clean, well documented and extensible.

Out-of-the-box dummies

While you can easily add your own dummy data definitions, the following are available out of the box:

Usage examples:

Dummy4j dummy = new Dummy4j();

String fullName = dummy.name().fullName();

String researchPaperTitle = dummy.researchPaper().title();

String sixSentenceParagraph = dummy.lorem().paragraph(6);

String romanNumeralsBetweenOneAndFifteen = dummy.numerals().roman(1, 15);

int betweenFiveAndTenInclusive = dummy.number().nextInt(5, 10);

boolean trueOrFalse = dummy.nextBoolean();

LocalDate birthdayBetween18And35 = dummy.dateAndTime().birthday(18, 35);

LocalDateTime upTo100YearsInThePast = dummy.dateAndTime().past(100, ChronoUnit.YEARS);

LocalDateTime upTo25YearsFrom1800 = dummy.dateAndTime().after(
        LocalDateTime.parse("1800-01-01T00:00:00"), 25, ChronoUnit.YEARS);

MyEnum randomEnum = dummy.nextEnum(MyEnum.class);

String thisValueMightBeNull = dummy.chance(1, 3, () -> "hello");

String thisValueWillBeUnique = dummy.unique().value("fullNameGroup", () -> dummy.name().fullName());

List<String> fiveNames = dummy.listOf(5, () -> dummy.name().fullName());

Set<String> fourCities = dummy.setOf(4, () -> dummy.address().city());

String nameOrCity = dummy.oneOf(() -> dummy.name().fullName(), () -> dummy.address().city());

String elementFromArray = dummy.oneOf(new String[]{ "one", "two", "three" });

String elementFromVarArgs = dummy.oneOf("one", "two", "three");

String elementFromCollection = dummy.oneOf(Arrays.asList("one", "two", "three"));

dummy.unique().within(() -> dummy.name().fullName(), name -> {
    // These names will only be unique within the context of this consumer
    List<String> tenLocallyUniqueNames = dummy.listOf(10, name);
});

List<String> tenLocallyUniqueNames = dummy.unique().of(() -> dummy.name().fullName(), 
                name -> dummy.listOf(10, name));

// This resolves an expression, finding the appropriate values in yml files.
// Normally, this should be hidden away under a custom method
// (e.g. customDummy.maleAndFemaleNames()).
String resolvedExpression = dummy4j.expressionResolver()
        .resolve("#{name.male_first_name}, #{name.female_first_name}");

// This finds and lists all methods containing the word "title": 
String searchResult = dummy4j.find("title");

Goals and contributing

Please read the contributing guidelines if you are considering contributing to this project.

The goal of Dummy4j is to become the most versatile, extensible and easy to use dummy data generation library. To that end, all contributions are welcome - whether it's Pull Requests, architectural proposals or simply issues, feel free to share them as only a continued conversation around the use of this tool can lead to innovation.

While care will always be taken to keep Dummy4j backwards compatible, some breaking changes might prove beneficial enough to the project to include them in new releases. However, even in those cases the decision will not be taken lightly and all possible measures will be taken to ensure a smooth transition to a new version of the library.

Finally, if you disagree with any details of how this project is maintained, feel free to create an issue!

dummy4j's People

Contributors

actions-user avatar daniel-frak avatar little-pinecone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

little-pinecone

dummy4j's Issues

MissingLocaleException when using Dummy4j in a Spring Boot jar

Example application: efficient-mvp-example

After building application with the following command:

mvn clean package && java -jar -Dspring.profiles.active=dev,init target/efficient-mvp-example-0.0.1-SNAPSHOT.jar 

and making the http://localhost:8080/api/init?seed=1&how-many-books=100 request I'm getting MissingLocaleException.

The logs show:
Loading definitions from: [BOOT-INF/classes/dummy4j/en/book.yml]
instead of
Loading definitions from: [dummy4j/en/book.yml, โ€ฆ]

Adding another condition specifically for Spring Boot projects in YamlFileLoader::isInAllowedPath seems to fix the problem, but there must be a better way that doesn't require hardcoding:

    private boolean isInAllowedPath(String resource, List<String> paths) {
        boolean isInAllowedPath = false;
        for (String path : paths) {
            if (resource.startsWith(path) || resource.startsWith("BOOT-INF/classes/" + path)) {
                isInAllowedPath = true;
                break;
            }
        }

        return isInAllowedPath;
    }

Useful links:
Resource pattern filter for complete resource path #148

Demo project for Dummy4j

A demo project presenting:

  1. Functionality (basic usage)
  2. Modules (selected, with configurable builders)
  3. Expression resolver (advanced usage)
  4. Commerce usage example

Builders - enable reuse through with/without parity

We should allow all builders to be reusable by 'undoing' any methods called on them. In this case I mean that everywhere we have a withoutX, like in CreditCardNumberBuilder and CreditCardBuilder (withoutFormatting), we should also have a withFormatting method.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.