Skip to content

Commit 2c13fc8

Browse files
IO-665 ensure that passing a null InputStream results in NPE with tests (#112)
* ensure that passing a null InputStream results in NPE with tests * remove stray import from bad autocomplete * per review, fix formating for lamda () -> * per review, protect other constructors
1 parent 6a78ef8 commit 2c13fc8

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/main/java/org/apache/commons/io/input/XmlStreamReader.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.net.URLConnection;
3131
import java.text.MessageFormat;
3232
import java.util.Locale;
33+
import java.util.Objects;
3334
import java.util.regex.Matcher;
3435
import java.util.regex.Pattern;
3536

@@ -135,7 +136,7 @@ public String getDefaultEncoding() {
135136
* @throws IOException thrown if there is a problem reading the file.
136137
*/
137138
public XmlStreamReader(final File file) throws IOException {
138-
this(new FileInputStream(file));
139+
this(new FileInputStream(Objects.requireNonNull(file)));
139140
}
140141

141142
/**
@@ -214,6 +215,7 @@ public XmlStreamReader(final InputStream inputStream, final boolean lenient) thr
214215
*/
215216
public XmlStreamReader(final InputStream inputStream, final boolean lenient, final String defaultEncoding)
216217
throws IOException {
218+
Objects.requireNonNull(inputStream, "inputStream");
217219
this.defaultEncoding = defaultEncoding;
218220
final BOMInputStream bom = new BOMInputStream(new BufferedInputStream(inputStream, BUFFER_SIZE), false, BOMS);
219221
final BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES);
@@ -239,7 +241,7 @@ public XmlStreamReader(final InputStream inputStream, final boolean lenient, fin
239241
* the URL.
240242
*/
241243
public XmlStreamReader(final URL url) throws IOException {
242-
this(url.openConnection(), null);
244+
this(Objects.requireNonNull(url, "url").openConnection(), null);
243245
}
244246

245247
/**
@@ -262,6 +264,7 @@ public XmlStreamReader(final URL url) throws IOException {
262264
* the URLConnection.
263265
*/
264266
public XmlStreamReader(final URLConnection conn, final String defaultEncoding) throws IOException {
267+
Objects.requireNonNull(conn, "conm");
265268
this.defaultEncoding = defaultEncoding;
266269
final boolean lenient = true;
267270
final String contentType = conn.getContentType();
@@ -334,6 +337,7 @@ public XmlStreamReader(final InputStream inputStream, final String httpContentTy
334337
*/
335338
public XmlStreamReader(final InputStream inputStream, final String httpContentType,
336339
final boolean lenient, final String defaultEncoding) throws IOException {
340+
Objects.requireNonNull(inputStream, "inputStream");
337341
this.defaultEncoding = defaultEncoding;
338342
final BOMInputStream bom = new BOMInputStream(new BufferedInputStream(inputStream, BUFFER_SIZE), false, BOMS);
339343
final BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES);

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
package org.apache.commons.io.input;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2021
import static org.junit.jupiter.api.Assertions.assertTrue;
2122
import static org.junit.jupiter.api.Assertions.fail;
2223

2324
import java.io.ByteArrayInputStream;
2425
import java.io.ByteArrayOutputStream;
26+
import java.io.File;
2527
import java.io.IOException;
2628
import java.io.InputStream;
2729
import java.io.OutputStreamWriter;
2830
import java.io.Writer;
31+
import java.net.URL;
32+
import java.net.URLConnection;
2933
import java.text.MessageFormat;
3034
import java.util.HashMap;
3135
import java.util.Map;
@@ -78,6 +82,26 @@ protected void _testRawNoBomInvalid(final String encoding) throws Exception {
7882
}
7983
}
8084

85+
@Test
86+
protected void testNullFileInput() throws IOException {
87+
assertThrows(NullPointerException.class, () -> new XmlStreamReader((File)null));
88+
}
89+
90+
@Test
91+
protected void testNullInputStreamInput() throws IOException {
92+
assertThrows(NullPointerException.class, () -> new XmlStreamReader((InputStream) null));
93+
}
94+
95+
@Test
96+
protected void testNullURLInput() throws IOException {
97+
assertThrows(NullPointerException.class, () -> new XmlStreamReader((URL)null));
98+
}
99+
100+
@Test
101+
protected void testNullURLConnectionInput() throws IOException {
102+
assertThrows(NullPointerException.class, () -> new XmlStreamReader((URLConnection)null, "US-ASCII"));
103+
}
104+
81105
@Test
82106
public void testRawNoBomUsAscii() throws Exception {
83107
_testRawNoBomValid("US-ASCII");

0 commit comments

Comments
 (0)