15
15
*/
16
16
package org .culturegraph .mf .stream .pipe ;
17
17
18
- import java .util .Arrays ;
19
-
20
- import org .culturegraph .mf .stream .pipe .ObjectBuffer ;
21
- import org .culturegraph .mf .stream .pipe .UniformSampler ;
22
- import org .junit .Assert ;
23
- import org .junit .Test ;
24
- import org .junit .runner .RunWith ;
25
- import org .junit .runners .Parameterized ;
26
- import org .junit .runners .Parameterized .Parameters ;
18
+ import static org .mockito .Mockito .inOrder ;
19
+
20
+ import java .util .Arrays ;
21
+
22
+ import org .culturegraph .mf .framework .ObjectReceiver ;
23
+ import org .junit .Before ;
24
+ import org .junit .Test ;
25
+ import org .junit .runner .RunWith ;
26
+ import org .junit .runners .Parameterized ;
27
+ import org .junit .runners .Parameterized .Parameters ;
28
+ import org .mockito .InOrder ;
29
+ import org .mockito .Mock ;
30
+ import org .mockito .MockitoAnnotations ;
27
31
28
32
29
33
/**
35
39
@ RunWith (Parameterized .class )
36
40
public final class UniformSamplerTest {
37
41
38
- private static final long SEED = 1 ; // Use a fixed random seed to make the test repeatable.
42
+ private static final long SEED = 1 ; // Use a fixed random seed to make the test repeatable
39
43
private static final int SAMPLE_SIZE = 5 ;
40
44
41
45
private static final int LARGE_SET = 100 ;
42
46
private static final int SMALL_SET = 3 ;
47
+
48
+ private UniformSampler <String > sampler ;
49
+
50
+ @ Mock
51
+ private ObjectReceiver <String > receiver ;
43
52
44
53
private final int setSize ;
45
54
private final String [] expected ;
@@ -56,24 +65,28 @@ public static Iterable<Object[]> data() {
56
65
{ SMALL_SET , new String [] { "0" , "1" , "2" } },
57
66
});
58
67
}
68
+
69
+ @ Before
70
+ public void setup () {
71
+ MockitoAnnotations .initMocks (this );
72
+
73
+ sampler = new UniformSampler <String >(SAMPLE_SIZE );
74
+ sampler .setSeed (SEED );
75
+ sampler .setReceiver (receiver );
76
+ }
59
77
60
78
@ Test
61
- public void test () {
62
- final UniformSampler <String > sampler = new UniformSampler <String >(SAMPLE_SIZE );
63
- sampler .setSeed (SEED );
64
- final ObjectBuffer <String > buffer = new ObjectBuffer <String >();
65
-
66
- sampler .setReceiver (buffer );
67
-
79
+ public void testShouldEmitARandomSubsetOfTheInputObjects () {
68
80
for (int i = 0 ; i < setSize ; ++i ) {
69
81
sampler .process (Integer .toString (i ));
70
82
}
71
83
sampler .closeStream ();
72
-
84
+
85
+ final InOrder ordered = inOrder (receiver );
73
86
for (int i = 0 ; i < expected .length ; ++i ) {
74
- Assert . assertEquals ( expected [i ], buffer . pop () );
75
- }
76
- Assert . assertNull ( "The sample contains more than " + expected . length + " records" , buffer . pop ());
87
+ ordered . verify ( receiver ). process ( expected [i ]);
88
+ }
89
+ ordered . verify ( receiver ). closeStream ();
77
90
}
78
91
79
92
}
0 commit comments