Skip to content

Commit

Permalink
Merge d6960e2 into 1850b03
Browse files Browse the repository at this point in the history
  • Loading branch information
HcPlu committed May 8, 2020
2 parents 1850b03 + d6960e2 commit 685738e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
48 changes: 40 additions & 8 deletions src/main/java/com/github/javafaker/Commerce.java
@@ -1,8 +1,14 @@
package com.github.javafaker;

import com.github.javafaker.service.FakeValuesInterface;
import com.github.javafaker.service.FakeValuesService;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.SortedSet;
import java.util.TreeSet;

Expand Down Expand Up @@ -32,26 +38,52 @@ public String department() {
}

public String productName() {
return StringUtils.join(new String[] {
faker.fakeValuesService().resolve("commerce.product_name.adjective", this,faker),
faker.fakeValuesService().resolve("commerce.product_name.material", this, faker),
faker.fakeValuesService().resolve("commerce.product_name.product", this, faker) }, " ");
return StringUtils.join(new String[] {
faker.fakeValuesService().resolve("commerce.product_name.adjective", this,faker),
faker.fakeValuesService().resolve("commerce.product_name.material", this, faker),
faker.fakeValuesService().resolve("commerce.product_name.product", this, faker) }, " ");
}

public String material() {
return faker.fakeValuesService().resolve("commerce.product_name.material", this, faker);
}

/**
* Generate a random price between 0.00 and 100.00
* Generate a random price between 0.00 and 100.00
*/
public String price() {
public String price() throws IllegalAccessException, NoSuchFieldException {
return price(0, 100);
}

public String price(double min, double max) {
/**
* Generate a random price between min and max, the format depends on locale
* @param min the smaller number, also the inclusive lower bound of generated number
* @param max the larger number, also the exclusive upper bound of generated number
* @return A generated number between min and max, its format depends on locale
* @throws IllegalAccessException An exception happens when get fields.
*/
public String price(double min, double max) throws IllegalAccessException, NoSuchFieldException {
double price = min + (faker.random().nextDouble() * (max - min));
return new DecimalFormat("#0.00").format(price);
Locale locale = new Locale("en");
Field fakeValuesServiceField=Faker.class.getDeclaredField("fakeValuesService");
fakeValuesServiceField.setAccessible(true);
FakeValuesService fakeValuesService=(FakeValuesService) fakeValuesServiceField.get(faker);
Field fakeValuesListField=fakeValuesService.getClass().getDeclaredField("fakeValuesList");
fakeValuesListField.setAccessible(true);
List<FakeValuesInterface> localLists=( List<FakeValuesInterface>) fakeValuesListField.get(fakeValuesService);
for(FakeValuesInterface currentInterface : localLists){
if (currentInterface.getClass().getSimpleName().equals("FakeValues")){
Field localeField = currentInterface.getClass().getDeclaredField("locale");
localeField.setAccessible(true);
locale=(Locale) localeField.get(currentInterface);
localeField.setAccessible(false);
}
}
fakeValuesListField.setAccessible(false);
fakeValuesServiceField.setAccessible(false);
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
decimalFormat.applyPattern("#0.00");
return decimalFormat.format(price);
}

public String promotionCode() {
Expand Down
22 changes: 20 additions & 2 deletions src/test/java/com/github/javafaker/CommerceTest.java
Expand Up @@ -3,6 +3,7 @@
import org.junit.Test;

import java.text.DecimalFormatSymbols;
import java.util.Locale;

import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression;
import static org.junit.Assert.assertThat;
Expand Down Expand Up @@ -36,12 +37,12 @@ public void testMaterial() {
}

@Test
public void testPrice() {
public void testPrice() throws IllegalAccessException, NoSuchFieldException {
assertThat(faker.commerce().price(), matchesRegularExpression("\\d{1,3}\\" + decimalSeparator + "\\d{2}"));
}

@Test
public void testPriceMinMax() {
public void testPriceMinMax() throws IllegalAccessException, NoSuchFieldException {
assertThat(faker.commerce().price(100, 1000), matchesRegularExpression("\\d{3,4}\\" + decimalSeparator + "\\d{2}"));
}

Expand All @@ -54,4 +55,21 @@ public void testPromotionCode() {
public void testPromotionCodeDigits() {
assertThat(faker.commerce().promotionCode(3), matchesRegularExpression(PROMOTION_CODE_REGEX + PROMOTION_CODE_REGEX + "\\d{3}"));
}
@Test
public void testPriceNotExist() throws NoSuchFieldException, IllegalAccessException {
Faker faker = new Faker(new Locale("PPP"));
assertThat(faker.commerce().price(), matchesRegularExpression("\\d{1,3}\\" + "." + "\\d{2}"));
}

@Test
public void testPriceChangedLocal() throws NoSuchFieldException, IllegalAccessException{
Faker faker = new Faker(new Locale("da"));
assertThat(faker.commerce().price(), matchesRegularExpression("\\d{1,3}\\" + "," + "\\d{2}"));
}

@Test
public void testPriceDefaultLocal() throws NoSuchFieldException, IllegalAccessException{
Faker faker = new Faker();
assertThat(faker.commerce().price(), matchesRegularExpression("\\d{1,3}\\" + "." + "\\d{2}"));
}
}

0 comments on commit 685738e

Please sign in to comment.