|
15 | 15 | */
|
16 | 16 | package org.culturegraph.mf.stream.pipe;
|
17 | 17 |
|
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; |
19 | 23 | import org.junit.Test;
|
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.MockitoAnnotations; |
20 | 26 |
|
21 | 27 |
|
22 | 28 | /**
|
|
25 | 31 | */
|
26 | 32 | public final class StringMatcherTest {
|
27 | 33 |
|
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); |
34 | 45 |
|
| 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() { |
35 | 57 | matcher.setPattern("PLACEHOLDER");
|
36 | 58 | matcher.setReplacement("Karl");
|
37 |
| - matcher.process("Hi PLACEHOLDER! -- Bye PLACEHOLDER!"); |
38 | 59 |
|
| 60 | + matcher.process("Hi PLACEHOLDER! -- Goodbye PLACEHOLDER!"); |
| 61 | + |
| 62 | + verify(receiver).process("Hi Karl! -- Goodbye Karl!"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testShouldHandleCaptureGroupReferencesInReplacementString() { |
39 | 67 | matcher.setPattern("^([^ ]+) .*$");
|
40 | 68 | matcher.setReplacement("$1");
|
41 |
| - matcher.process("important-bit this can be ignored"); |
42 | 69 |
|
43 |
| - matcher.closeStream(); |
| 70 | + matcher.process("important-bit but this can be ignored"); |
44 | 71 |
|
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"); |
48 | 73 | }
|
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 | + |
50 | 84 | }
|
0 commit comments