Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/main/java/com/github/javafaker/Commerce.java
Original file line number Diff line number Diff line change
@@ -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 @@ -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<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
Original file line number Diff line number Diff line change
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}"));
}
}