Merge branch 'PascalSchumacher-add_commerce'

This commit is contained in:
Ricky
2016-04-25 07:31:10 +10:00
3 changed files with 92 additions and 0 deletions
@@ -0,0 +1,48 @@
package com.github.javafaker;
import java.util.SortedSet;
import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import com.github.javafaker.service.FakeValuesServiceInterface;
import com.github.javafaker.service.RandomService;
public class Commerce {
private final FakeValuesServiceInterface fakeValuesService;
private final RandomService randomService;
public Commerce(FakeValuesServiceInterface fakeValuesService, RandomService randomService) {
this.fakeValuesService = fakeValuesService;
this.randomService = randomService;
}
public String color() {
return fakeValuesService.fetchString("color.name");
}
public String department() {
int numberOfDepartments = Math.max(randomService.nextInt(4), 1);
SortedSet<String> departments = new TreeSet<String>();
while (departments.size() < numberOfDepartments) {
departments.add(fakeValuesService.fetchString("commerce.department"));
}
if (departments.size() > 1) {
String lastDepartment = departments.last();
return StringUtils.join(departments.headSet(lastDepartment), ", ") + " & " + lastDepartment;
} else {
return departments.first();
}
}
public String productName() {
return StringUtils.join(new String[] { fakeValuesService.fetchString("commerce.product_name.adjective"),
fakeValuesService.fetchString("commerce.product_name.material"),
fakeValuesService.fetchString("commerce.product_name.product") }, " ");
}
public String material() {
return fakeValuesService.fetchString("commerce.product_name.material");
}
}
@@ -30,6 +30,7 @@ public class Faker implements Resolver {
private final Business business;
private final Book book;
private final Color color;
private final Commerce commerce;
private final Company company;
private final Hacker hacker;
private final Options options;
@@ -68,6 +69,7 @@ public class Faker implements Resolver {
this.color = new Color(proxiedFakeValueService);
this.hacker = new Hacker(proxiedFakeValueService);
this.company = new Company(this, proxiedFakeValueService, randomService);
this.commerce = new Commerce(proxiedFakeValueService, randomService);
this.options = new Options(randomService);
this.code = new Code(randomService);
this.finance = new Finance(proxiedFakeValueService, randomService);
@@ -163,6 +165,10 @@ public class Faker implements Resolver {
return color;
}
public Commerce commerce() {
return commerce;
}
public Company company() {
return company;
}
@@ -0,0 +1,38 @@
package com.github.javafaker;
import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Before;
import org.junit.Test;
public class CommerceTest {
private Faker faker;
@Before
public void before() {
faker = new Faker();
}
@Test
public void testColor() {
assertThat(faker.commerce().color(), matchesRegularExpression("(\\w+ ?){1,2}"));
}
@Test
public void testDepartment() {
assertThat(faker.commerce().department(), matchesRegularExpression("(\\w+(, | & )?){1,3}"));
}
@Test
public void testProductName() {
assertThat(faker.commerce().productName(), matchesRegularExpression("(\\w+ ?){3,4}"));
}
@Test
public void testMaterial() {
assertThat(faker.commerce().material(), matchesRegularExpression("\\w+"));
}
}