All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.praegus.fitnesse.symbols.Fake Maven / Gradle / Ivy

There is a newer version: 2.0.24
Show newest version
package nl.praegus.fitnesse.symbols;

import fitnesse.wikitext.parser.Matcher;
import fitnesse.wikitext.parser.Maybe;
import fitnesse.wikitext.parser.Parser;
import fitnesse.wikitext.parser.Rule;
import fitnesse.wikitext.parser.Symbol;
import fitnesse.wikitext.parser.SymbolType;
import fitnesse.wikitext.parser.Translation;
import fitnesse.wikitext.parser.Translator;
import net.datafaker.Faker;

import java.util.List;
import java.util.Locale;

public class Fake extends SymbolType implements Rule, Translation {
    private static final String CATEGORY = "Category";
    private static final String ITEM = "Item";
    private static final String LOCALE = "Locale";


    public Fake() {
        super("Fake");
        wikiMatcher(new Matcher().string("!Fake"));
        wikiRule(this);
        htmlTranslation(this);
    }


    @Override
    public Maybe parse(Symbol current, Parser parser) {
        Maybe result = storeParenthesisContent(current, parser, CATEGORY);
        if (!result.isNothing()) {
            result = storeParenthesisContent(current, parser, ITEM);
            if (!result.isNothing()) {
                result = storeParenthesisContent(current, parser, LOCALE);
            }
        }
        return result;
    }

    @Override
    public String toTarget(Translator translator, Symbol symbol) {
        Faker faker = new Faker(new Locale(symbol.findProperty(LOCALE, "en")));
        String result;
        try {
            Object category = Faker.class.getMethod(symbol.findProperty(CATEGORY, "")).invoke(faker);
            result = category.getClass().getDeclaredMethod(symbol.findProperty(ITEM, "")).invoke(category).toString();
        } catch (Exception e) {
            result = "ERROR_FAKING_DATA";
            e.printStackTrace();
        }
        return result;
    }

    protected Maybe storeParenthesisContent(Symbol current, Parser parser, String key) {
        Maybe result = new Maybe<>(current);
        List lookAhead = parser.peek(new SymbolType[]{SymbolType.Whitespace, SymbolType.OpenParenthesis});
        if (!lookAhead.isEmpty()) {
            parser.moveNext(2);
            Maybe format = parser.parseToAsString(SymbolType.CloseParenthesis);
            if (format.isNothing()) {
                result = Symbol.nothing;
            }
            current.putProperty(key, format.getValue());
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy