-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathFakeValuesService.java
More file actions
606 lines (538 loc) · 22.1 KB
/
FakeValuesService.java
File metadata and controls
606 lines (538 loc) · 22.1 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
package com.github.javafaker.service;
import com.github.javafaker.Address;
import com.github.javafaker.Faker;
import com.github.javafaker.Name;
import com.github.javafaker.service.files.EnFile;
import com.mifmif.common.regex.Generex;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Locale;
public class FakeValuesService {
private static final Pattern EXPRESSION_PATTERN = Pattern.compile("#\\{([a-z0-9A-Z_.]+)\\s?((?:,?'([^']+)')*)\\}");
private static final Pattern EXPRESSION_ARGUMENTS_PATTERN = Pattern.compile("(?:'(.*?)')");
private final Logger log = Logger.getLogger("faker");
private final List<FakeValuesInterface> fakeValuesList;
private final RandomService randomService;
/**
* <p>
* Resolves YAML file using the most specific path first based on language and country code.
* 'en_US' would resolve in the following order:
* <ol>
* <li>/en-US.yml</li>
* <li>/en.yml</li>
* </ol>
* The search is case-insensitive, so the following will all resolve correctly. Also, either a hyphen or
* an underscore can be used when constructing a {@link Locale} instance. This is legacy behavior and not
* condoned, but it will work.
* <ul>
* <li>EN_US</li>
* <li>En-Us</li>
* <li>eN_uS</li>
* </ul>
* </p>
*
* @param locale
* @param randomService
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public FakeValuesService(Locale locale, RandomService randomService) {
if (locale == null) {
throw new IllegalArgumentException("locale is required");
}
this.randomService = randomService;
locale = normalizeLocale(locale);
final List<Locale> locales = localeChain(locale);
final List<FakeValuesInterface> all = new ArrayList(locales.size());
for (final Locale l : locales) {
boolean isEnglish = l.equals(Locale.ENGLISH);
if (isEnglish) {
FakeValuesGrouping fakeValuesGrouping = new FakeValuesGrouping();
for (EnFile file : EnFile.getFiles()) {
fakeValuesGrouping.add(new FakeValues(l, file.getFile(), file.getPath()));
}
all.add(fakeValuesGrouping);
} else {
all.add(new FakeValues(l));
}
}
this.fakeValuesList = Collections.unmodifiableList(all);
}
/**
* Convert the specified locale into a chain of locales used for message resolution. For example:
* <p>
* {@link Locale#FRANCE} (fr_FR) -> [ fr_FR, anotherTest, en ]
*
* @return a list of {@link Locale} instances
*/
protected List<Locale> localeChain(Locale from) {
if (Locale.ENGLISH.equals(from)) {
return Collections.singletonList(Locale.ENGLISH);
}
final Locale normalized = normalizeLocale(from);
final List<Locale> chain = new ArrayList<Locale>(3);
chain.add(normalized);
if (!"".equals(normalized.getCountry()) && !Locale.ENGLISH.getLanguage().equals(normalized.getLanguage())) {
chain.add(new Locale(normalized.getLanguage()));
}
chain.add(Locale.ENGLISH); // default
return chain;
}
/**
* @return a proper {@link Locale} instance with language and country code set regardless of how
* it was instantiated. new Locale("pt-br") will be normalized to a locale constructed
* with new Locale("pt","BR").
*/
private Locale normalizeLocale(Locale locale) {
final String[] parts = locale.toString().split("[-_]");
if (parts.length == 1) {
return new Locale(parts[0]);
} else {
return new Locale(parts[0], parts[1]);
}
}
/**
* Fetch a random value from an array item specified by the key
*
* @param key
* @return
*/
public Object fetch(String key) {
List<Object> valuesArray = (List) fetchObject(key);
return valuesArray == null ? null : valuesArray.get(randomService.nextInt(valuesArray.size()));
}
/**
* Same as {@link #fetch(String)} except this casts the result into a String.
*
* @param key
* @return
*/
public String fetchString(String key) {
return (String) fetch(key);
}
/**
* Safely fetches a key.
* <p>
* If the value is null, it will return an empty string.
* <p>
* If it is a list, it will assume it is a list of strings and select a random value from it.
* <p>
* If the retrieved value is an slash encoded regular expression such as {@code /[a-b]/} then
* the regex will be converted to a regexify expression and returned (ex. {@code #regexify '[a-b]'})
* <p>
* Otherwise it will just return the value as a string.
*
* @param key the key to fetch from the YML structure.
* @param defaultIfNull the value to return if the fetched value is null
* @return see above
*/
@SuppressWarnings("unchecked")
public String safeFetch(String key, String defaultIfNull) {
Object o = fetchObject(key);
if (o == null) {
return defaultIfNull;
}
if (o instanceof List) {
List<String> values = (List<String>) o;
if (values.size() == 0) {
return defaultIfNull;
}
return values.get(randomService.nextInt(values.size()));
} else if (isSlashDelimitedRegex(o.toString())) {
return String.format("#{regexify '%s'}", trimRegexSlashes(o.toString()));
} else {
return (String) o;
}
}
/**
* Return the object selected by the key from yaml file.
*
* @param key key contains path to an object. Path segment is separated by
* dot. E.g. name.first_name
* @return
*/
@SuppressWarnings("unchecked")
public Object fetchObject(String key) {
String[] path = key.split("\\.");
Object result = null;
for (FakeValuesInterface fakeValuesInterface : fakeValuesList) {
Object currentValue = fakeValuesInterface;
for (int p = 0; currentValue != null && p < path.length; p++) {
String currentPath = path[p];
if (currentValue instanceof Map) {
currentValue = ((Map) currentValue).get(currentPath);
} else {
currentValue = ((FakeValuesInterface) currentValue).get(currentPath);
}
}
result = currentValue;
if (result != null) {
break;
}
}
return result;
}
/**
* Returns a string with the '#' characters in the parameter replaced with random digits between 0-9 inclusive.
* <p/>
* For example, the string "ABC##EFG" could be replaced with a string like "ABC99EFG".
*
* @param numberString
* @return
*/
public String numerify(String numberString) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numberString.length(); i++) {
if (numberString.charAt(i) == '#') {
sb.append(randomService.nextInt(10));
} else {
sb.append(numberString.charAt(i));
}
}
return sb.toString();
}
/**
* Applies both a {@link #numerify(String)} and a {@link #letterify(String)}
* over the incoming string.
*
* @param string
* @return
*/
public String bothify(String string) {
return letterify(numerify(string));
}
/**
* Applies both a {@link #numerify(String)} and a {@link #letterify(String, boolean)}
* over the incoming string.
*
* @param string
* @param isUpper
* @return
*/
public String bothify(String string, boolean isUpper) {
return letterify(numerify(string), isUpper);
}
/**
* Generates a String that matches the given regular expression.
*/
public String regexify(String regex) {
Generex generex = new Generex(regex);
generex.setSeed(randomService.nextLong());
return generex.random();
}
/**
* Returns a string with the '?' characters in the parameter replaced with random alphabetic
* characters.
* <p/>
* For example, the string "12??34" could be replaced with a string like "12AB34".
*
* @param letterString
* @return
*/
public String letterify(String letterString) {
return this.letterify(letterString, false);
}
/**
* Returns a string with the '?' characters in the parameter replaced with random alphabetic
* characters.
* <p/>
* For example, the string "12??34" could be replaced with a string like "12AB34".
*
* @param letterString
* @param isUpper specifies whether or not letters should be upper case
* @return
*/
public String letterify(String letterString, boolean isUpper) {
return letterHelper(isUpper ? 65 : 97, letterString); // from ascii table
}
private String letterHelper(int baseChar, String letterString) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < letterString.length(); i++) {
if (letterString.charAt(i) == '?') {
sb.append((char) (baseChar + randomService.nextInt(26))); // a-z
} else {
sb.append(letterString.charAt(i));
}
}
return sb.toString();
}
/**
* Resolves a key to a method on an object.
* <p>
* #{hello} with result in a method call to current.hello();
* <p>
* #{Person.hello_someone} will result in a method call to person.helloSomeone();
*/
public String resolve(String key, Object current, Faker root) {
final String expression = safeFetch(key, null);
if (expression == null) {
throw new RuntimeException(key + " resulted in null expression");
}
return resolveExpression(expression, current, root);
}
/**
* resolves an expression using the current faker.
*
* @param expression
* @param faker
* @return
*/
public String expression(String expression, Faker faker) {
return resolveExpression(expression, null, faker);
}
/**
* <p>processes a expression in the style #{X.y} using the current objects as the 'current' location
* within the yml file (or the {@link Faker} object hierarchy as it were).
* </p>
* <p>
* #{Address.streetName} would get resolved to {@link Faker#address()}'s {@link Address#streetName()}
* </p>
* <p>
* #{address.street} would get resolved to the YAML > locale: faker: address: street:
* </p>
* <p>
* Combinations are supported as well: "#{x} #{y}"
* </p>
* <p>
* Recursive templates are supported. if "#{x}" resolves to "#{Address.streetName}" then "#{x}" resolves to
* {@link Faker#address()}'s {@link Address#streetName()}.
* </p>
*/
protected String resolveExpression(String expression, Object current, Faker root) {
final Matcher matcher = EXPRESSION_PATTERN.matcher(expression);
String result = expression;
while (matcher.find()) {
final String escapedDirective = matcher.group(0);
final String directive = matcher.group(1);
final String arguments = matcher.group(2);
final Matcher argsMatcher = EXPRESSION_ARGUMENTS_PATTERN.matcher(arguments);
List<String> args = new ArrayList<String>();
while (argsMatcher.find()) {
args.add(argsMatcher.group(1));
}
// resolve the expression and reprocess it to handle recursive templates
String resolved = resolveExpression(directive, args, current, root);
if (resolved == null) {
throw new RuntimeException("Unable to resolve " + escapedDirective + " directive.");
}
resolved = resolveExpression(resolved, current, root);
result = StringUtils.replaceOnce(result, escapedDirective, resolved);
}
return result;
}
/**
* <h1>Search Order</h1>
* <ul>
* <li>Search for methods on the current object</li>
* <li>local keys in Yaml File</li>
* <li>Search for methods on faker child objects</li>
* <li>Search for keys in yaml file by transforming object reference to yaml reference</li>
* </ul>
*
* @return null if unable to resolve
*/
private String resolveExpression(String directive, List<String> args, Object current, Faker root) {
// name.name (resolve locally)
// Name.first_name (resolve to faker.name().firstName())
final String simpleDirective = (isDotDirective(directive) || current == null)
? directive
: classNameToYamlName(current) + "." + directive;
String resolved = null;
// resolve method references on CURRENT object like #{number_between '1','10'} on Number or
// #{ssn_valid} on IdNumber
if (!isDotDirective(directive)) {
resolved = resolveFromMethodOn(current, directive, args);
}
// simple fetch of a value from the yaml file. the directive may have been mutated
// such that if the current yml object is car: and directive is #{wheel} then
// car.wheel will be looked up in the YAML file.
if (resolved == null) {
resolved = safeFetch(simpleDirective, null);
}
// resolve method references on faker object like #{regexify '[a-z]'}
if (resolved == null && !isDotDirective(directive)) {
resolved = resolveFromMethodOn(root, directive, args);
}
// Resolve Faker Object method references like #{ClassName.method_name}
if (resolved == null && isDotDirective(directive)) {
resolved = resolveFakerObjectAndMethod(root, directive, args);
}
// last ditch effort. Due to Ruby's dynamic nature, something like 'Address.street_title' will resolve
// because 'street_title' is a dynamic method on the Address object. We can't do this in Java so we go
// thru the normal resolution above, but if we will can't resolve it, we once again do a 'safeFetch' as we
// did first but FIRST we change the Object reference Class.method_name with a yml style internal refernce ->
// class.method_name (lowercase)
if (resolved == null && isDotDirective(directive)) {
resolved = safeFetch(javaNameToYamlName(simpleDirective), null);
}
return resolved;
}
/**
* @param expression input expression
* @return true if s is non null and is a slash delimited regex (ex. {@code /[ab]/})
*/
private boolean isSlashDelimitedRegex(String expression) {
return expression != null && expression.startsWith("/") && expression.endsWith("/");
}
/**
* Given a {@code slashDelimitedRegex} such as {@code /[ab]/}, removes the slashes and returns only {@code [ab]}
*
* @param slashDelimitedRegex a non null slash delimited regex (ex. {@code /[ab]/})
* @return the regex without the slashes (ex. {@code [ab]})
*/
private String trimRegexSlashes(String slashDelimitedRegex) {
return slashDelimitedRegex.substring(1, slashDelimitedRegex.length() - 1);
}
private boolean isDotDirective(String directive) {
return directive.contains(".");
}
/**
* @return a yaml style name from the classname of the supplied object (PhoneNumber => phone_number)
*/
private String classNameToYamlName(Object current) {
return javaNameToYamlName(current.getClass().getSimpleName());
}
/**
* @return a yaml style name like 'phone_number' from a java style name like 'PhoneNumber'
*/
private String javaNameToYamlName(String expression) {
return expression.replaceAll("([A-Z])", "_$1")
.substring(1)
.toLowerCase(Locale.ROOT);
}
/**
* Given a directive like 'firstName', attempts to resolve it to a method. For example if obj is an instance of
* {@link Name} then this method would return {@link Name#firstName()}. Returns null if the directive is nested
* (i.e. has a '.') or the method doesn't exist on the <em>obj</em> object.
*/
private String resolveFromMethodOn(Object obj, String directive, List<String> args) {
if (obj == null) {
return null;
}
try {
final MethodAndCoercedArgs accessor = accessor(obj, directive, args);
return (accessor == null)
? null
: string(accessor.invoke(obj));
} catch (Exception e) {
log.log(Level.FINE, "Can't call " + directive + " on " + obj, e);
return null;
}
}
/**
* Accepts a {@link Faker} instance and a name.firstName style 'key' which is resolved to the return value of:
* {@link Faker#name()}'s {@link Name#firstName()} method.
*
* @throws RuntimeException if there's a problem invoking the method or it doesn't exist.
*/
private String resolveFakerObjectAndMethod(Faker faker, String key, List<String> args) {
final String[] classAndMethod = key.split("\\.", 2);
try {
String fakerMethodName = classAndMethod[0].replaceAll("_", "");
MethodAndCoercedArgs fakerAccessor = accessor(faker, fakerMethodName, Collections.<String>emptyList());
if (fakerAccessor == null) {
log.fine("Can't find top level faker object named " + fakerMethodName + ".");
return null;
}
Object objectWithMethodToInvoke = fakerAccessor.invoke(faker);
String nestedMethodName = classAndMethod[1].replaceAll("_", "");
final MethodAndCoercedArgs accessor = accessor(objectWithMethodToInvoke, classAndMethod[1].replaceAll("_", ""), args);
if (accessor == null) {
throw new Exception("Can't find method on "
+ objectWithMethodToInvoke.getClass().getSimpleName()
+ " called " + nestedMethodName + ".");
}
return string(accessor.invoke(objectWithMethodToInvoke));
} catch (Exception e) {
log.fine(e.getMessage());
return null;
}
}
/**
* Find an accessor by name ignoring case.
*/
private MethodAndCoercedArgs accessor(Object onObject, String name, List<String> args) {
log.log(Level.FINE, "Find accessor named " + name + " on " + onObject.getClass().getSimpleName() + " with args " + args);
for (Method m : onObject.getClass().getMethods()) {
if (m.getName().equalsIgnoreCase(name)
&& m.getParameterTypes().length == args.size()) {
final List<Object> coercedArguments = coerceArguments(m, args);
if (coercedArguments != null) {
return new MethodAndCoercedArgs(m, coercedArguments);
}
}
}
if (name.contains("_")) {
return accessor(onObject, name.replaceAll("_", ""), args);
}
return null;
}
/**
* Coerce arguments in <em>args</em> into the appropriate types (if possible) for the parameter arguments
* to <em>accessor</em>.
*
* @return array of coerced values if successful, null otherwise
* @throws Exception if unable to coerce
*/
private List<Object> coerceArguments(Method accessor, List<String> args) {
final List<Object> coerced = new ArrayList<Object>();
for (int i = 0; i < accessor.getParameterTypes().length; i++) {
Class<?> toType = ClassUtils.primitiveToWrapper(accessor.getParameterTypes()[i]);
try {
if (toType.isEnum()) {
Method method = toType.getMethod( "valueOf", String.class );
String enumArg = args.get( i ).substring( args.get( i ).indexOf( "." ) + 1 );
Object coercedArg = method.invoke( null, enumArg );
coerced.add( coercedArg );
} else {
final Constructor<?> ctor = toType.getConstructor(String.class);
final Object coercedArgument = ctor.newInstance(args.get(i));
coerced.add(coercedArgument);
}
} catch (Exception e) {
log.fine("Unable to coerce " + args.get(i) + " to " + toType.getSimpleName() + " via " + toType.getSimpleName() + "(String) constructor.");
return null;
}
}
return coerced;
}
private String string(Object obj) {
return (obj == null) ? null : obj.toString();
}
/**
* simple wrapper class around an accessor and a list of coerced arguments.
* this is useful as we get to find the method and coerce the arguments in one
* shot, returning both when successful. This saves us from doing it more than once (coercing args).
*/
private static class MethodAndCoercedArgs {
private final Method method;
private final List<Object> coerced;
private MethodAndCoercedArgs(Method m, List<Object> coerced) {
this.method = requireNonNull(m, "method cannot be null");
this.coerced = requireNonNull(coerced, "coerced arguments cannot be null");
}
private Object invoke(Object on) throws InvocationTargetException, IllegalAccessException {
return method.invoke(on, coerced.toArray());
}
/**
* source level precludes me from using Objects.requireNonNull
*/
private <T> T requireNonNull(T instance, String messageIfNull) {
if (instance == null) {
throw new NullPointerException(messageIfNull);
}
return instance;
}
}
}