Skip to content

Commit 87e6728

Browse files
committed
Update tests to user StringInputStream's builder.
1 parent 8d0f1b9 commit 87e6728

12 files changed

Lines changed: 54 additions & 50 deletions

src/test/java/org/apache/commons/io/CopyUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void copy_inputStreamToWriterWithEncoding() throws Exception {
101101
final String inDataStr = "data";
102102
final String charsetName = StandardCharsets.UTF_8.name();
103103
final StringWriter writer = new StringWriter();
104-
CopyUtils.copy(new StringInputStream(inDataStr, charsetName), writer, charsetName);
104+
CopyUtils.copy(new StringInputStream.Builder().setString(inDataStr).setCharset(charsetName).get(), writer, charsetName);
105105
assertEquals(inDataStr, writer.toString());
106106
}
107107

src/test/java/org/apache/commons/io/DemuxInputStreamTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private String getOutput(final String threadName) {
140140
}
141141

142142
private void startReader(final String name, final String data, final DemuxInputStream demux) {
143-
final InputStream input = new StringInputStream(data);
143+
final InputStream input = new StringInputStream.Builder().setString(data).get();
144144
final ReaderThread thread = new ReaderThread(name, input, demux);
145145
threadMap.put(name, thread);
146146
}

src/test/java/org/apache/commons/io/IOUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ public void testReadFully_InputStream_ByteArray() throws Exception {
966966

967967
@Test
968968
public void testReadFully_InputStream_Offset() throws Exception {
969-
final StringInputStream stream = new StringInputStream("abcd1234", StandardCharsets.UTF_8);
969+
final StringInputStream stream = new StringInputStream.Builder().setString("abcd1234").setCharset(StandardCharsets.UTF_8).get();
970970
final byte[] buffer = "wx00000000".getBytes(StandardCharsets.UTF_8);
971971
IOUtils.readFully(stream, buffer, 2, 8);
972972
assertEquals("wxabcd1234", new String(buffer, 0, buffer.length, StandardCharsets.UTF_8));

src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ private InputStream createUtf8Input(final byte[] baseData, final boolean addBOM)
152152
return new ByteArrayInputStream(data);
153153
}
154154

155-
private boolean doesSaxSupportCharacterSet(final String charSetName) throws ParserConfigurationException, SAXException, IOException {
155+
private boolean doesSaxSupportCharacterSet(final String charsetName) throws ParserConfigurationException, SAXException, IOException {
156156
final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
157-
try (StringInputStream byteStream = new StringInputStream("<?xml version=\"1.0\" encoding=\"" + charSetName + "\"?><Z/>", charSetName)) {
157+
try (StringInputStream byteStream = new StringInputStream.Builder().setString("<?xml version=\"1.0\" encoding=\"" + charsetName + "\"?><Z/>")
158+
.setCharset(charsetName).get()) {
158159
final InputSource is = new InputSource(byteStream);
159-
is.setEncoding(charSetName);
160+
is.setEncoding(charsetName);
160161
documentBuilder.parse(is);
161162
} catch (final SAXParseException e) {
162-
if (e.getMessage().contains(charSetName)) {
163+
if (e.getMessage().contains(charsetName)) {
163164
return false;
164165
}
165166
}

src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class CountingInputStreamTest {
3333
@Test
3434
public void testCounting() throws Exception {
3535
final String text = "A piece of text";
36-
try (CountingInputStream cis = new CountingInputStream(new StringInputStream(text))) {
36+
try (CountingInputStream cis = new CountingInputStream(new StringInputStream.Builder().setString(text).get())) {
3737

3838
// have to declare this larger as we're going to read
3939
// off the end of the stream and input stream seems
@@ -149,7 +149,7 @@ public void testResetting() throws Exception {
149149
@Test
150150
public void testSkipping() throws IOException {
151151
final String text = "Hello World!";
152-
try (CountingInputStream cis = new CountingInputStream(new StringInputStream(text))) {
152+
try (CountingInputStream cis = new CountingInputStream(new StringInputStream.Builder().setString(text).get())) {
153153

154154
assertEquals(6, cis.skip(6));
155155
assertEquals(6, cis.getCount());

src/test/java/org/apache/commons/io/input/StringInputStream.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,56 @@
2121
import java.io.StringReader;
2222
import java.nio.charset.Charset;
2323

24+
import org.apache.commons.io.build.AbstractStreamBuilder;
25+
2426
/**
2527
* An {@link InputStream} on a String.
2628
*
27-
* @since 2.12.0
29+
* @since 2.13.0
2830
*/
2931
public class StringInputStream extends ReaderInputStream {
3032

3133
/**
32-
* Creates a new instance on a String.
33-
*
34-
* @param source The source string, MUST not be null.
35-
* @return A new instance.
34+
* Builds a new {@link ReaderInputStream} instance.
35+
* <p>
36+
* For example:
37+
* </p>
38+
* <pre>{@code
39+
* ReaderInputStream s = ReaderInputStream.builder()
40+
* .setString("String")
41+
* .setCharsetEncoder(Charset.defaultCharset())
42+
* .get()}
43+
* </pre>
44+
* <p>
3645
*/
37-
public static StringInputStream on(final String source) {
38-
return new StringInputStream(source);
39-
}
46+
public static class Builder extends AbstractStreamBuilder<StringInputStream, Builder> {
4047

41-
/**
42-
* Creates a new instance on the empty String.
43-
*/
44-
public StringInputStream() {
45-
this("", Charset.defaultCharset());
46-
}
48+
private String string;
4749

48-
/**
49-
* Creates a new instance on a String.
50-
*
51-
* @param source The source string, MUST not be null.
52-
*/
53-
public StringInputStream(final String source) {
54-
this(source, Charset.defaultCharset());
55-
}
50+
/**
51+
* Constructs a new instance.
52+
*
53+
* Only uses the String and Charset aspects of this builder.
54+
* @throws UnsupportedOperationException if the origin cannot be converted to a Reader.
55+
*/
56+
@Override
57+
public StringInputStream get() {
58+
return new StringInputStream(string, getCharset());
59+
}
5660

57-
/**
58-
* Creates a new instance on a String for a Charset.
59-
*
60-
* @param source The source string, MUST not be null.
61-
* @param charset The source charset, MUST not be null.
62-
*/
63-
public StringInputStream(final String source, final Charset charset) {
64-
super(new StringReader(source), charset);
65-
}
61+
public Builder setString(final String string) {
62+
this.string = string;
63+
return this;
64+
}
6665

66+
}
6767
/**
68-
* Creates a new instance on a String and for a Charset.
68+
* Creates a new instance on a String for a Charset.
6969
*
7070
* @param source The source string, MUST not be null.
7171
* @param charset The source charset, MUST not be null.
7272
*/
73-
public StringInputStream(final String source, final String charset) {
73+
private StringInputStream(final String source, final Charset charset) {
7474
super(new StringReader(source), charset);
7575
}
7676

src/test/java/org/apache/commons/io/input/StringInputStreamTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.nio.charset.Charset;
2424

2525
import org.apache.commons.io.IOUtils;
26+
import org.apache.commons.io.input.StringInputStream.Builder;
2627
import org.junit.jupiter.api.Test;
2728

2829
/**
@@ -32,14 +33,14 @@ public class StringInputStreamTest {
3233

3334
@Test
3435
public void testStringConstructorString() throws IOException {
35-
try (StringInputStream input = StringInputStream.on("01")) {
36+
try (StringInputStream input = new Builder().setString("01").get()) {
3637
assertEquals("01", IOUtils.toString(input, Charset.defaultCharset()));
3738
}
3839
}
3940

4041
@Test
4142
public void testStringConstructorStringCharset() throws IOException {
42-
try (StringInputStream input = new StringInputStream("01", Charset.defaultCharset())) {
43+
try (StringInputStream input = new Builder().setString("01").setCharset(Charset.defaultCharset()).get()) {
4344
assertEquals("01", IOUtils.toString(input, Charset.defaultCharset()));
4445
}
4546
}

src/test/java/org/apache/commons/io/input/UncheckedFilterInputStreamTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public class UncheckedFilterInputStreamTest {
4141
@SuppressWarnings("resource")
4242
@BeforeEach
4343
public void beforeEach() {
44-
stringInputStream = UncheckedFilterInputStream.builder().setInputStream(new BufferedInputStream(new StringInputStream("01"))).get();
44+
stringInputStream = UncheckedFilterInputStream.builder().setInputStream(new BufferedInputStream(new StringInputStream.Builder().setString("01").get()))
45+
.get();
4546
exception = new IOException("test exception");
4647
brokenInputStream = UncheckedFilterInputStream.builder().setInputStream(new BrokenInputStream(exception)).get();
4748
}

src/test/java/org/apache/commons/io/input/WindowsLineEndingInputStreamTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ private String roundtrip(final String msg) throws IOException {
5656
}
5757

5858
private String roundtrip(final String msg, final boolean ensure) throws IOException {
59-
try (WindowsLineEndingInputStream lf = new WindowsLineEndingInputStream(new StringInputStream(msg, StandardCharsets.UTF_8), ensure)) {
59+
try (WindowsLineEndingInputStream lf = new WindowsLineEndingInputStream(
60+
new StringInputStream.Builder().setString(msg).setCharset(StandardCharsets.UTF_8).get(), ensure)) {
6061
final byte[] buf = new byte[100];
6162
final int read = lf.read(buf);
6263
return new String(buf, 0, read, StandardCharsets.UTF_8);

src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public void testHttp() throws Exception {
379379
public void testHttpContent() throws Exception {
380380
final String encoding = UTF_8;
381381
final String xml = getXML("no-bom", XML3, encoding, encoding);
382-
try (XmlStreamReader xmlReader = new XmlStreamReader(new StringInputStream(xml, encoding))) {
382+
try (XmlStreamReader xmlReader = new XmlStreamReader(new StringInputStream.Builder().setString(xml).setCharset(encoding).get())) {
383383
assertEquals(xmlReader.getEncoding(), encoding, "Check encoding");
384384
assertEquals(xml, IOUtils.toString(xmlReader), "Check content");
385385
}
@@ -505,7 +505,7 @@ protected void testRawBomValid(final String encoding) throws Exception {
505505
public void testRawContent() throws Exception {
506506
final String encoding = UTF_8;
507507
final String xml = getXML("no-bom", XML3, encoding, encoding);
508-
try (XmlStreamReader xmlReader = new XmlStreamReader(new StringInputStream(xml, encoding))) {
508+
try (XmlStreamReader xmlReader = new XmlStreamReader(new StringInputStream.Builder().setString(xml).setCharset(encoding).get())) {
509509
assertEquals(xmlReader.getEncoding(), encoding, "Check encoding");
510510
assertEquals(xml, IOUtils.toString(xmlReader), "Check content");
511511
}

0 commit comments

Comments
 (0)