-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathRandomServiceTest.java
More file actions
108 lines (86 loc) · 3.25 KB
/
Copy pathRandomServiceTest.java
File metadata and controls
108 lines (86 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.github.javafaker.service;
import com.github.javafaker.AbstractFakerTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.CombinableMatcher.both;
import static org.junit.Assert.assertThat;
/**
* @author pmiklos
*
*/
@RunWith(Parameterized.class)
public class RandomServiceTest extends AbstractFakerTest {
private RandomService randomService;
public RandomServiceTest(String ignoredTitle, RandomService service) {
this.randomService = service;
}
@Parameterized.Parameters(name = "Created via {0}")
public static Collection<Object[]> data() {
Object[][] data = new Object[][]{
{"RandomService(Random)", new RandomService(new Random())},
{"RandomService()", new RandomService()}
};
return Arrays.asList(data);
}
@Test(expected = IllegalArgumentException.class)
public void testPositiveBoundariesOnly() {
randomService.nextLong(0L);
}
@Test
public void testLongWithinBoundary() {
assertThat(randomService.nextLong(1), is(0L));
for (int i = 1; i < 10; i++) {
assertThat(randomService.nextLong(2), lessThan(2L));
}
}
@Test
public void testLongMaxBoundary() {
assertThat(randomService.nextLong(Long.MAX_VALUE), greaterThan(0L));
assertThat(randomService.nextLong(Long.MAX_VALUE), lessThan(Long.MAX_VALUE));
}
@Test
public void testIntInRange() {
for (int i = 1; i < 100; i++) {
assertThat(randomService.nextInt(-5, 5), both(lessThanOrEqualTo(5)).and(greaterThanOrEqualTo(-5)));
}
}
@Test
public void testHex() {
assertThat(randomService.hex(8), matchesRegularExpression("^[0-9A-F]{8}$"));
}
@Test
public void testDefaultHex() {
assertThat(randomService.hex(), matchesRegularExpression("^[0-9A-F]{8}$"));
}
@Test
public void testRandomString() {
assertThat(randomService.randomString(8), matchesRegularExpression("^[a-zA-Z0-9]{8}$"));
}
@Test
public void testRandomStringRandomLength() {
String randomString = randomService.randomString();
assertThat(randomString, matchesRegularExpression("^[a-zA-Z0-9]{1,255}$"));
}
@Test
public void testRandomStringWithMinMaxLength() {
String randomString = randomService.randomString(10, 30);
assertThat(randomString, matchesRegularExpression("^[a-zA-Z0-9]{10,30}$"));
}
@Test
public void testRandomStringWithMinMaxLengthAndSpecificCharacters() {
String randomString = randomService.randomString(10, 30, "abc123".toCharArray());
assertThat(randomString, matchesRegularExpression("^[abc123]{10,30}$"));
}
@Test
public void testRandomStringWithLengthAndSpecificCharacters() {
String randomString = randomService.randomString(35, "abc123".toCharArray());
assertThat(randomString, matchesRegularExpression("^[abc123]{35}$"));
}
}