Class FakeValuesService

java.lang.Object
net.datafaker.service.FakeValuesService

public class FakeValuesService extends Object
  • Field Details

  • Constructor Details

    • FakeValuesService

      public FakeValuesService()
  • Method Details

    • updateFakeValuesInterfaceMap

      public void updateFakeValuesInterfaceMap(List<SingletonLocale> locales)
    • addPath

      public void addPath(Locale locale, Path path)
      Allows adding paths to files with custom data. Data should be in YAML format.
      Parameters:
      locale - the locale for which a path is going to be added.
      path - path to a file with YAML structure
      Throws:
      IllegalArgumentException - in case of invalid path
    • addUrl

      public void addUrl(Locale locale, URL url)
      Allows adding urls of files with custom data. Data should be in YAML format.
      Parameters:
      locale - the locale for which an url is going to be added.
      url - url of a file with YAML structure
      Throws:
      IllegalArgumentException - in case of invalid url
    • fetch

      public Object fetch(String key, FakerContext context)
      Fetch a random value from an array item specified by the key
    • fetchString

      public String fetchString(String key, FakerContext context)
      Same as fetch(String, FakerContext) except this casts the result into a String.
    • safeFetch

      public String safeFetch(String key, FakerContext context, String defaultIfNull)
      Safely fetches a key.

      If the value is null, it will return an empty string.

      If it is a list, it will assume it is a list of strings and select a random value from it.

      If the retrieved value is an slash encoded regular expression such as /[a-b]/ then the regex will be converted to a regexify expression and returned (ex. #regexify '[a-b]')

      Otherwise it will just return the value as a string.

      Parameters:
      key - the key to fetch from the YML structure.
      defaultIfNull - the value to return if the fetched value is null
      Returns:
      see above
    • fetchObject

      public Object fetchObject(String key, FakerContext context)
      Return the object selected by the key from yaml file.
      Parameters:
      key - key contains path to an object. Path segment is separated by dot. E.g. name.first_name
    • numerify

      public String numerify(String numberString, FakerContext context)
      Returns a string with the '#' characters in the parameter replaced with random digits between 0-9 inclusive or random digits in the range from 1-9 when Ø (not zero) is used.

      For example, the string "ABC##EFG" could be replaced with a string like "ABC99EFG".

    • bothify

      public String bothify(String string, FakerContext context)
      Applies both a numerify(String, FakerContext) and a letterify(String, FakerContext) over the incoming string.
    • bothify

      public String bothify(String input, FakerContext context, boolean isUpper)
      Applies both a numerify(String, FakerContext) and a letterify(String, FakerContext, boolean) over the incoming string.
    • regexify

      public String regexify(String regex, FakerContext context)
      Generates a String that matches the given regular expression.
    • examplify

      public String examplify(String example, FakerContext context)
      Generates a String by example. The output string will have the same pattern as the input string.
    • letterify

      public String letterify(String letterString, FakerContext context)
      Returns a string with the '?' characters in the parameter replaced with random alphabetic characters.

      For example, the string "12??34" could be replaced with a string like "12AB34".

    • letterify

      public String letterify(String letterString, FakerContext context, boolean isUpper)
      Returns a string with the '?' characters in the parameter replaced with random alphabetic characters.

      For example, the string "12??34" could be replaced with a string like "12AB34".

    • templatify

      public String templatify(String letterString, char char2replace, FakerContext context, String... options)
      Returns a string with the char2replace characters in the parameter replaced with random alphabetic characters from options
    • templatify

      public String templatify(String letterString, Map<Character,String[]> optionsMap, FakerContext context)
      Returns a string with the optionsMap.getKeys() characters in the parameter replaced with random alphabetic characters from corresponding optionsMap.values()
    • resolve

      public String resolve(String key, Object current, BaseFaker root, FakerContext context)
      Resolves a key to a method on an object or throws an exception.

      #{hello} with result in a method call to current.hello();

      #{Person.hello_someone} will result in a method call to person.helloSomeone();

    • resolve

      public String resolve(String key, AbstractProvider<?> provider, FakerContext context)
    • resolve

      public String resolve(String key, Object current, ProviderRegistration root, Supplier<String> exceptionMessage, FakerContext context)
      Resolves a key to a method on an object or throws an exception with specified message.

      #{hello} with result in a method call to current.hello();

      #{Person.hello_someone} will result in a method call to person.helloSomeone();

    • expression

      public String expression(String expression, BaseFaker faker, FakerContext context)
      Resolves an expression using the current faker.
    • fileExpression

      public String fileExpression(Path path, BaseFaker faker, FakerContext context)
      Resolves an expression in file using the current faker.
    • csv

      public String csv(int limit, String... columnExpressions)
      Generates csv based on input column expressions and number of lines. This method uses default separator, quote and always prints header.
    • csv

      public String csv(String delimiter, char quote, boolean withHeader, int limit, String... columnExpressions)
      Generates csv based on input.
    • json

      public String json(String... fieldExpressions)
      Generates json based on input.
    • jsona

      public String jsona(String... fieldExpressions)
      Generates json based on input.
    • resolveExpression

      protected String resolveExpression(String expression, Object current, ProviderRegistration root, FakerContext context)
      processes a expression in the style #{X.y} using the current objects as the 'current' location within the yml file (or the BaseFaker object hierarchy as it were).

      #{Address.streetName} would get resolved to BaseProviders.address()'s Address.streetName() #{address.street} would get resolved to the YAML like locale: faker: address: street: Combinations are supported as well: "#{x} #{y}"

      Recursive templates are supported. if "#{x}" resolves to "#{Address.streetName}" then "#{x}" resolves to BaseProviders.address()'s Address.streetName().

    • primitiveToWrapper

      public static Class<?> primitiveToWrapper(Class<?> cls)