Skip to content

Commit e1faaad

Browse files
committed
Changed StringMatcherTest to use mocks
1 parent 75fc837 commit e1faaad

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

src/test/java/org/culturegraph/mf/stream/pipe/StringMatcherTest.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
*/
1616
package org.culturegraph.mf.stream.pipe;
1717

18-
import org.junit.Assert;
18+
import static org.mockito.Mockito.verify;
19+
20+
import org.culturegraph.mf.framework.ObjectReceiver;
21+
import org.junit.After;
22+
import org.junit.Before;
1923
import org.junit.Test;
24+
import org.mockito.Mock;
25+
import org.mockito.MockitoAnnotations;
2026

2127

2228
/**
@@ -25,26 +31,54 @@
2531
*/
2632
public final class StringMatcherTest {
2733

28-
@Test
29-
public void testMatcher() {
30-
final StringMatcher matcher = new StringMatcher();
31-
final ObjectBuffer<String> result = new ObjectBuffer<String>();
32-
33-
matcher.setReceiver(result);
34+
private static final String INPUT_STRING =
35+
"The pattern is not part of the input";
36+
37+
private StringMatcher matcher;
38+
39+
@Mock
40+
private ObjectReceiver<String> receiver;
41+
42+
@Before
43+
public void setup() {
44+
MockitoAnnotations.initMocks(this);
3445

46+
matcher = new StringMatcher();
47+
matcher.setReceiver(receiver);
48+
}
49+
50+
@After
51+
public void cleanup() {
52+
matcher.closeStream();
53+
}
54+
55+
@Test
56+
public void testShouldReplaceAllMatchesWithReplacementString() {
3557
matcher.setPattern("PLACEHOLDER");
3658
matcher.setReplacement("Karl");
37-
matcher.process("Hi PLACEHOLDER! -- Bye PLACEHOLDER!");
3859

60+
matcher.process("Hi PLACEHOLDER! -- Goodbye PLACEHOLDER!");
61+
62+
verify(receiver).process("Hi Karl! -- Goodbye Karl!");
63+
}
64+
65+
@Test
66+
public void testShouldHandleCaptureGroupReferencesInReplacementString() {
3967
matcher.setPattern("^([^ ]+) .*$");
4068
matcher.setReplacement("$1");
41-
matcher.process("important-bit this can be ignored");
4269

43-
matcher.closeStream();
70+
matcher.process("important-bit but this can be ignored");
4471

45-
Assert.assertEquals("Hi Karl! -- Bye Karl!", result.pop());
46-
Assert.assertEquals("important-bit", result.pop());
47-
Assert.assertNull(result.pop());
72+
verify(receiver).process("important-bit");
4873
}
49-
74+
75+
@Test
76+
public void testShouldRelayNonMatchingInput() {
77+
matcher.setPattern("Non-matching pattern");
78+
79+
matcher.process(INPUT_STRING);
80+
81+
verify(receiver).process(INPUT_STRING);
82+
}
83+
5084
}

0 commit comments

Comments
 (0)