Skip to content

Commit 29ebcfc

Browse files
committed
Add more process shortcuts to TextParser
1 parent c096110 commit 29ebcfc

3 files changed

Lines changed: 56 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1010
### Added
1111

1212
- Add collector-based static factory to TextParser
13+
- Add more process shortcuts to TextParser
1314

1415
## [0.0.33] - 2025-03-06
1516

java-io-base/src/main/java/nbbrd/io/text/TextParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.nio.charset.Charset;
1515
import java.nio.file.Files;
1616
import java.nio.file.Path;
17+
import java.util.List;
1718
import java.util.Optional;
1819
import java.util.function.Consumer;
1920
import java.util.function.Function;
@@ -47,6 +48,14 @@ public interface TextParser<T> {
4748
}
4849
}
4950

51+
default @NonNull T parseProcess(@NonNull ProcessBuilder processBuilder, @NonNull Charset encoding) throws IOException {
52+
return parseProcess(processBuilder.start(), encoding);
53+
}
54+
55+
default @NonNull T parseProcess(@NonNull List<String> command, @NonNull Charset encoding) throws IOException {
56+
return parseProcess(new ProcessBuilder(command), encoding);
57+
}
58+
5059
default @NonNull T parseResource(@NonNull Class<?> type, @NonNull String name, @NonNull Charset encoding) throws IOException {
5160
try (InputStream resource = Resource.newInputStream(type, name)) {
5261
return parseStream(resource, encoding);

java-io-base/src/test/java/nbbrd/io/text/TextParserTest.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import java.io.IOException;
1010
import java.io.Reader;
11-
import java.nio.charset.Charset;
1211
import java.nio.file.Path;
1312
import java.nio.file.Paths;
1413
import java.util.ArrayList;
@@ -19,7 +18,9 @@
1918
import java.util.stream.Stream;
2019

2120
import static _test.io.text.TextParserAssertions.assertTextParserCompliance;
21+
import static java.nio.charset.Charset.defaultCharset;
2222
import static java.nio.charset.StandardCharsets.UTF_8;
23+
import static java.util.Arrays.asList;
2324
import static java.util.Collections.singleton;
2425
import static java.util.Collections.singletonList;
2526
import static java.util.stream.Collectors.*;
@@ -139,21 +140,61 @@ public void testAsParserWithListener() {
139140

140141
@SuppressWarnings("DataFlowIssue")
141142
@Test
142-
public void testParseProcess() throws IOException {
143+
public void testParseProcessOfProcess() throws IOException {
143144
TextParser<Path> x = onParsingReader(TextParserTest::toPath);
144145

145146
assertThatNullPointerException()
146-
.isThrownBy(() -> x.parseProcess(null, UTF_8))
147+
.isThrownBy(() -> x.parseProcess((Process) null, UTF_8))
147148
.withMessageContaining("process");
148149

149150
switch (OS.NAME) {
150151
case WINDOWS:
151-
assertThat(x.parseProcess(new ProcessBuilder("where", "where").start(), Charset.defaultCharset())).exists();
152+
assertThat(x.parseProcess(new ProcessBuilder("where", "where").start(), defaultCharset())).exists();
152153
break;
153154
case LINUX:
154155
case MACOS:
155156
case SOLARIS:
156-
assertThat(x.parseProcess(new ProcessBuilder("which", "which").start(), Charset.defaultCharset())).exists();
157+
assertThat(x.parseProcess(new ProcessBuilder("which", "which").start(), defaultCharset())).exists();
158+
break;
159+
}
160+
}
161+
@SuppressWarnings("DataFlowIssue")
162+
@Test
163+
public void testParseProcessOfProcessBuilder() throws IOException {
164+
TextParser<Path> x = onParsingReader(TextParserTest::toPath);
165+
166+
assertThatNullPointerException()
167+
.isThrownBy(() -> x.parseProcess((ProcessBuilder) null, UTF_8))
168+
.withMessageContaining("processBuilder");
169+
170+
switch (OS.NAME) {
171+
case WINDOWS:
172+
assertThat(x.parseProcess(new ProcessBuilder("where", "where"), defaultCharset())).exists();
173+
break;
174+
case LINUX:
175+
case MACOS:
176+
case SOLARIS:
177+
assertThat(x.parseProcess(new ProcessBuilder("which", "which"), defaultCharset())).exists();
178+
break;
179+
}
180+
}
181+
@SuppressWarnings("DataFlowIssue")
182+
@Test
183+
public void testParseProcessOfCommand() throws IOException {
184+
TextParser<Path> x = onParsingReader(TextParserTest::toPath);
185+
186+
assertThatNullPointerException()
187+
.isThrownBy(() -> x.parseProcess((List<String>) null, UTF_8))
188+
.withMessageContaining("command");
189+
190+
switch (OS.NAME) {
191+
case WINDOWS:
192+
assertThat(x.parseProcess(asList("where", "where"), defaultCharset())).exists();
193+
break;
194+
case LINUX:
195+
case MACOS:
196+
case SOLARIS:
197+
assertThat(x.parseProcess(asList("which", "which"), defaultCharset())).exists();
157198
break;
158199
}
159200
}

0 commit comments

Comments
 (0)