diff --git a/src/main/java/com/github/javafaker/Commerce.java b/src/main/java/com/github/javafaker/Commerce.java index c609b7582..fb54fd13c 100644 --- a/src/main/java/com/github/javafaker/Commerce.java +++ b/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; @@ -45,13 +51,39 @@ public String material() { /** * 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 localLists=( List) 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() { diff --git a/src/test/java/com/github/javafaker/CommerceTest.java b/src/test/java/com/github/javafaker/CommerceTest.java index 4b982966e..ce1e34c35 100644 --- a/src/test/java/com/github/javafaker/CommerceTest.java +++ b/src/test/java/com/github/javafaker/CommerceTest.java @@ -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; @@ -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}")); } @@ -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}")); + } }