Skip to content

Commit

Permalink
Merge 38aa166 into 1850b03
Browse files Browse the repository at this point in the history
  • Loading branch information
bianzheng123 committed May 31, 2020
2 parents 1850b03 + 38aa166 commit ad550d5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/github/javafaker/PhoneNumber.java
Expand Up @@ -11,10 +11,18 @@ public String cellPhone() {
return faker.numerify(faker.fakeValuesService().resolve("cell_phone.formats", this, faker));
}

public String cellPhone(String target) {
return faker.numerify(target);
}

public String phoneNumber() {
return faker.numerify(faker.fakeValuesService().resolve("phone_number.formats", this, faker));
}

public String phoneNumber(String target) {
return faker.numerify(target);
}

public String extension() {
return subscriberNumber();
}
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/com/github/javafaker/Relationships.java
Expand Up @@ -46,15 +46,9 @@ public String any() {
Method runMethod = methods[indx];
Relationships relationships = new Relationships(faker);
return (String)runMethod.invoke(relationships);
} catch (SecurityException e) {
throw new RuntimeException("SecurityException: " + e.getMessage());
} catch (IllegalArgumentException e) {
throw new RuntimeException("IllegalArgumentException: " + e.getMessage());
} catch (IllegalAccessException e) {
throw new RuntimeException("IllegalAccessException: " + e.getMessage());
} catch (InvocationTargetException e) {
throw new RuntimeException("InvocationTargetException: " + e.getMessage());
}
} catch (Exception e) {
throw new RuntimeException("Exception: " + e.getMessage());
}
}

}
43 changes: 33 additions & 10 deletions src/main/java/com/github/javafaker/service/FakeValuesService.java
Expand Up @@ -21,6 +21,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.mifmif.common.regex.Generex;

public class FakeValuesService {

private static final Pattern EXPRESSION_PATTERN = Pattern.compile("#\\{([a-z0-9A-Z_.]+)\\s?((?:,?'([^']+)')*)\\}");
Expand Down Expand Up @@ -183,6 +185,7 @@ 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++) {
Expand All @@ -204,22 +207,44 @@ public Object fetchObject(String key) {
/**
* 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".
* Support regrular expression like '[2-4]'
* For example, the string "ABC##EFG[2-4]" could be replaced with a string like "ABC99EFG3".
*
* @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));
boolean isMatch = Pattern.matches(".*\\[.*", numberString);

if (isMatch) {
for (int i = 0; i < numberString.length(); i++) {
if (numberString.charAt(i) == '#') {
sb.append("\\d");
}
else if (numberString.charAt(i) == '?') {
sb.append("[?]");
} else {
sb.append(numberString.charAt(i));

}
}
}

return sb.toString();
String result = sb.toString();
Generex generex = new Generex(result);
String randomStr = generex.random();
return randomStr;
} else {
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();
}
}

/**
Expand Down Expand Up @@ -303,11 +328,9 @@ private String letterHelper(int baseChar, String letterString) {
*/
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);
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/github/javafaker/PhoneNumberTest.java
Expand Up @@ -48,4 +48,21 @@ public void testSubscriberNumber() {
public void testSubscriberNumberWithLength() {
assertThat(faker.phoneNumber().subscriberNumber(10), matchesRegularExpression("\\d{10}"));
}

@Test
public void testPhoneNumberRegex() {
String number = faker.phoneNumber().phoneNumber("[2-4]#####");
assertThat(number, matchesRegularExpression("[2-4]\\d\\d\\d\\d\\d"));
number = faker.phoneNumber().phoneNumber("[2-4][3-7]#####");
assertThat(number, matchesRegularExpression("[2-4][3-7]\\d\\d\\d\\d\\d"));
}

@Test
public void testCellPhoneRegex() {
String number = faker.phoneNumber().cellPhone("[5-9]#-##-##");
assertThat(number, matchesRegularExpression("[5-9]\\d-\\d\\d-\\d\\d"));

number = faker.phoneNumber().cellPhone("[3-7][7-9]-##-##");
assertThat(number, matchesRegularExpression("[3-7][7-9]-\\d\\d-\\d\\d"));
}
}
Expand Up @@ -67,5 +67,6 @@ public void testIntInRange() {
@Test
public void testHex() {
assertThat(randomService.hex(8), matchesRegularExpression("^[0-9A-F]{8}$"));
assertThat(randomService.hex(), matchesRegularExpression("^[0-9A-F]{8}$"));
}
}

0 comments on commit ad550d5

Please sign in to comment.