Skip to content

Commit b969826

Browse files
committed
Merge pull request #166 from cboehme/line-splitter-improvements
Improvements to LineSplitter
2 parents 524473b + 34aed80 commit b969826

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

src/main/java/org/culturegraph/mf/stream/pipe/LineSplitter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import org.culturegraph.mf.framework.DefaultObjectPipe;
2121
import org.culturegraph.mf.framework.ObjectReceiver;
22+
import org.culturegraph.mf.framework.annotations.Description;
23+
import org.culturegraph.mf.framework.annotations.In;
24+
import org.culturegraph.mf.framework.annotations.Out;
2225

2326

2427
/**
@@ -27,7 +30,10 @@
2730
* @author Christoph Böhme
2831
*
2932
*/
30-
public final class LineSplitter
33+
@Description("Splits a string at new lines and sends each line to the receiver.")
34+
@In(String.class)
35+
@Out(String.class)
36+
public final class LineSplitter
3137
extends DefaultObjectPipe<String, ObjectReceiver<String>> {
3238

3339
private static final char NEWLINE = '\n';
@@ -37,7 +43,7 @@ public final class LineSplitter
3743
@Override
3844
public void process(final String lines) {
3945
assert !isClosed();
40-
for (String record : LINE_PATTERN.split(lines)) {
46+
for (final String record : LINE_PATTERN.split(lines)) {
4147
getReceiver().process(record);
4248
}
4349
}

src/main/resources/flux-commands.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ draw-uniform-sample org.culturegraph.mf.stream.pipe.UniformSampler
9696
catch-object-exception org.culturegraph.mf.stream.pipe.ObjectExceptionCatcher
9797

9898
normalize-utf8 org.culturegraph.mf.stream.pipe.Utf8Normalizer
99+
split-lines org.culturegraph.mf.stream.pipe.LineSplitter
99100

100101
morph org.culturegraph.mf.morph.Metamorph
101102
filter org.culturegraph.mf.stream.pipe.Filter
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2013 Christoph Böhme
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.stream.pipe;
17+
18+
import static org.mockito.Mockito.inOrder;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.verifyNoMoreInteractions;
21+
22+
import org.culturegraph.mf.framework.ObjectReceiver;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.mockito.InOrder;
26+
import org.mockito.Mock;
27+
import org.mockito.MockitoAnnotations;
28+
29+
/**
30+
* Tests for class {@link LineSplitter}.
31+
*
32+
* @author Christoph Böhme
33+
*
34+
*/
35+
public final class LineSplitterTest {
36+
37+
private static final String PART1 = "One";
38+
private static final String PART2 = "Two";
39+
private static final String PART3 = "Three";
40+
41+
private LineSplitter lineSplitter;
42+
43+
@Mock
44+
private ObjectReceiver<String> receiver;
45+
46+
@Before
47+
public void setup() {
48+
MockitoAnnotations.initMocks(this);
49+
lineSplitter = new LineSplitter();
50+
lineSplitter.setReceiver(receiver);
51+
}
52+
53+
@Test
54+
public void shouldSplitInputStringAtNewLines() {
55+
lineSplitter.process(PART1 + "\n" + PART2 + "\n" + PART3);
56+
57+
final InOrder ordered = inOrder(receiver);
58+
ordered.verify(receiver).process(PART1);
59+
ordered.verify(receiver).process(PART2);
60+
ordered.verify(receiver).process(PART3);
61+
ordered.verifyNoMoreInteractions();
62+
}
63+
64+
@Test
65+
public void shouldPassInputWithoutNewLinesUnchanged() {
66+
lineSplitter.process(PART1);
67+
68+
verify(receiver).process(PART1);
69+
verifyNoMoreInteractions(receiver);
70+
}
71+
72+
@Test
73+
public void shouldOutputEmptyStringsForSequencesOfNewLines() {
74+
lineSplitter.process(PART1 + "\n\n" + PART2);
75+
76+
final InOrder ordered = inOrder(receiver);
77+
ordered.verify(receiver).process(PART1);
78+
ordered.verify(receiver).process("");
79+
ordered.verify(receiver).process(PART2);
80+
ordered.verifyNoMoreInteractions();
81+
}
82+
83+
@Test
84+
public void shouldOutputEmptyStringForNewLinesAtStartOfTheInput() {
85+
lineSplitter.process("\n" + PART1);
86+
87+
final InOrder ordered = inOrder(receiver);
88+
ordered.verify(receiver).process("");
89+
ordered.verify(receiver).process(PART1);
90+
ordered.verifyNoMoreInteractions();
91+
}
92+
93+
@Test
94+
public void shouldNotOutputEmptyStringForNewLinesAtEndOfTheInput() {
95+
lineSplitter.process(PART1 + "\n");
96+
97+
verify(receiver).process(PART1);
98+
verifyNoMoreInteractions(receiver);
99+
}
100+
101+
}

0 commit comments

Comments
 (0)