Skip to content

Commit 73beb31

Browse files
authored
Refactoring (#1569)
* [refactor] simplify CommerceTest the `decimalSeparator` is always '.' - by inlining it, we make the regex in test much simpler. * [refactor] fix IDE warnings in ProviderGenerator * [refactor] remove redundant suppression in Range.java * [refactor] Use FileSystems.getSeparator() instead of 'System.getProperty' * [refactor] mark test fields final to avoid the potential risk that some test might modify the field value thus affecting other tests. * [refactor] simplify FakeValuesGroupingTest initialize test fields right in the field declaration line. The makes unneeded `@BeforeEach` method.
1 parent cee4f6d commit 73beb31

File tree

14 files changed

+47
-56
lines changed

14 files changed

+47
-56
lines changed

src/main/java/net/datafaker/providers/base/File.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.datafaker.providers.base;
22

3+
import java.nio.file.FileSystems;
4+
35
/**
46
* @since 0.8.0
57
*/
@@ -22,7 +24,7 @@ public String fileName() {
2224
}
2325

2426
public String fileName(String dirOrNull, String nameOrNull, String extensionOrNull, String separatorOrNull) {
25-
final String sep = separatorOrNull == null ? System.getProperty("file.separator") : separatorOrNull;
27+
final String sep = separatorOrNull == null ? FileSystems.getDefault().getSeparator() : separatorOrNull;
2628
final String dir = dirOrNull == null ? faker.internet().slug() : dirOrNull;
2729
final String name = nameOrNull == null ? faker.lorem().word().toLowerCase(faker.getContext().getLocale()) : nameOrNull;
2830
final String ext = extensionOrNull == null ? extension() : extensionOrNull;

src/main/java/net/datafaker/service/FakeValuesGrouping.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.Locale;
1010
import java.util.Map;
1111

12-
public class FakeValuesGrouping implements FakeValuesInterface {
12+
public final class FakeValuesGrouping implements FakeValuesInterface {
1313
private static final FakeValuesGrouping ENGLISH_FAKE_VALUE_GROUPING = new FakeValuesGrouping();
1414
private final Map<String, Collection<FakeValuesInterface>> fakeValues = new HashMap<>();
1515

@@ -19,6 +19,13 @@ public class FakeValuesGrouping implements FakeValuesInterface {
1919
});
2020
}
2121

22+
public FakeValuesGrouping() {
23+
}
24+
25+
public FakeValuesGrouping(FakeValues values) {
26+
add(values);
27+
}
28+
2229
public void add(FakeValuesInterface fakeValue) {
2330
if (fakeValue instanceof FakeValues) {
2431
((FakeValues) fakeValue).getPaths().forEach(p ->

src/main/java/net/datafaker/service/Range.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public static <T extends Comparable<T>> Range<T> exclusiveInclusive(T from, T to
5858
return new Range<>(new Bound<>(from, End.EXCLUSIVE), new Bound<>(to, End.INCLUSIVE));
5959
}
6060

61-
@SuppressWarnings("unchecked")
6261
public <V extends Comparable<V>> Range<V> cast(Function<T, V> caster) {
6362
return new Range<>(
6463
new Bound<>(caster.apply(from.value), from.end),

src/test/java/net/datafaker/providers/base/CommerceTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
import org.junit.jupiter.api.Test;
44

5-
import java.text.DecimalFormatSymbols;
6-
import java.util.List;
75
import java.util.Collection;
6+
import java.util.List;
87

8+
import static java.lang.Float.parseFloat;
99
import static org.assertj.core.api.Assertions.assertThat;
1010

1111
class CommerceTest extends BaseFakerTest<BaseFaker> {
1212

13-
private final char decimalSeparator = new DecimalFormatSymbols(getFaker().getContext().getLocale()).getDecimalSeparator();
14-
1513
private static final String CAPITALIZED_WORD_REGEX = "[A-Z][a-z]+";
1614

1715
private static final String PROMOTION_CODE_REGEX = CAPITALIZED_WORD_REGEX + "(-" + CAPITALIZED_WORD_REGEX + ")*";
@@ -37,12 +35,20 @@ protected Collection<TestSpec> providerListTest() {
3735

3836
@Test
3937
void testPrice() {
40-
assertThat(commerce.price()).matches("\\d{1,3}\\" + decimalSeparator + "\\d{2}");
38+
String price = commerce.price();
39+
assertThat(price)
40+
.as("random price between 0.00 and 100.00")
41+
.matches("\\d{1,3}\\.\\d{2}");
42+
assertThat(parseFloat(price)).isBetween(0f, 100f);
4143
}
4244

4345
@Test
4446
void testPriceMinMax() {
45-
assertThat(commerce.price(100, 1000)).matches("\\d{3,4}\\" + decimalSeparator + "\\d{2}");
47+
String price = commerce.price(100, 1000);
48+
assertThat(price)
49+
.as("random price between 100.00 and 1000.00")
50+
.matches("\\d{3,4}\\.\\d{2}");
51+
assertThat(parseFloat(price)).isBetween(100f, 1000f);
4652
}
4753

4854
@Test

src/test/java/net/datafaker/providers/base/ComputerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class ComputerTest extends BaseFakerTest<BaseFaker> {
1111

12-
Computer computer = faker.computer();
12+
private final Computer computer = faker.computer();
1313

1414
@Test
1515
void testOperatingSystem() {

src/test/java/net/datafaker/providers/base/CountryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class CountryTest extends BaseFakerTest<BaseFaker> {
1212

13-
Country country = faker.country();
13+
private final Country country = faker.country();
1414

1515
@RepeatedTest(10)
1616
void testFlag() {

src/test/java/net/datafaker/providers/base/NationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class NationTest extends BaseFakerTest<BaseFaker> {
88

9-
Nation nation = faker.nation();
9+
private final Nation nation = faker.nation();
1010

1111
@Test
1212
void nationality() {

src/test/java/net/datafaker/providers/base/RandomFakerTest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package net.datafaker.providers.base;
22

3-
import org.junit.jupiter.api.BeforeEach;
43
import org.junit.jupiter.api.Test;
54

65
import java.util.Random;
@@ -10,14 +9,8 @@
109
class RandomFakerTest extends BaseFakerTest<BaseFaker> {
1110

1211
private static final int CONSTANT_SEED_VALUE = 10;
13-
private BaseFaker faker;
14-
private Random random;
15-
16-
@BeforeEach
17-
final void before() {
18-
random = new Random();
19-
faker = new BaseFaker(random);
20-
}
12+
private final Random random = new Random();
13+
private final BaseFaker faker = new BaseFaker(random);
2114

2215
@Test
2316
void testNumerifyRandomnessCanBeControlled() {

src/test/java/net/datafaker/providers/base/ScienceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class ScienceTest extends BaseFakerTest<BaseFaker> {
1111

12-
Science science = faker.science();
12+
private final Science science = faker.science();
1313

1414
@Override
1515
protected Collection<TestSpec> providerListTest() {

src/test/java/net/datafaker/providers/base/SipTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class SipTest extends BaseFakerTest<BaseFaker> {
88

9-
Sip sip = faker.sip();
9+
private final Sip sip = faker.sip();
1010

1111
@Test
1212
void method_returnUpperCaseWithMinimum3Chars() {

0 commit comments

Comments
 (0)