This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new 2c13fc8 IO-665 ensure that passing a null InputStream results in NPE
with tests (#112)
2c13fc8 is described below
commit 2c13fc8148128d1a1f19f28b71acad262f24d4a3
Author: Otto Fowler <[email protected]>
AuthorDate: Mon Apr 13 21:30:45 2020 -0400
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
---
.../apache/commons/io/input/XmlStreamReader.java | 8 ++++++--
.../commons/io/input/XmlStreamReaderTest.java | 24 ++++++++++++++++++++++
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
index a69b923..2ecc38e 100644
--- a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
+++ b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java
@@ -30,6 +30,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.text.MessageFormat;
import java.util.Locale;
+import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -135,7 +136,7 @@ public class XmlStreamReader extends Reader {
* @throws IOException thrown if there is a problem reading the file.
*/
public XmlStreamReader(final File file) throws IOException {
- this(new FileInputStream(file));
+ this(new FileInputStream(Objects.requireNonNull(file)));
}
/**
@@ -214,6 +215,7 @@ public class XmlStreamReader extends Reader {
*/
public XmlStreamReader(final InputStream inputStream, final boolean
lenient, final String defaultEncoding)
throws IOException {
+ Objects.requireNonNull(inputStream, "inputStream");
this.defaultEncoding = defaultEncoding;
final BOMInputStream bom = new BOMInputStream(new
BufferedInputStream(inputStream, BUFFER_SIZE), false, BOMS);
final BOMInputStream pis = new BOMInputStream(bom, true,
XML_GUESS_BYTES);
@@ -239,7 +241,7 @@ public class XmlStreamReader extends Reader {
* the URL.
*/
public XmlStreamReader(final URL url) throws IOException {
- this(url.openConnection(), null);
+ this(Objects.requireNonNull(url, "url").openConnection(), null);
}
/**
@@ -262,6 +264,7 @@ public class XmlStreamReader extends Reader {
* the URLConnection.
*/
public XmlStreamReader(final URLConnection conn, final String
defaultEncoding) throws IOException {
+ Objects.requireNonNull(conn, "conm");
this.defaultEncoding = defaultEncoding;
final boolean lenient = true;
final String contentType = conn.getContentType();
@@ -334,6 +337,7 @@ public class XmlStreamReader extends Reader {
*/
public XmlStreamReader(final InputStream inputStream, final String
httpContentType,
final boolean lenient, final String defaultEncoding) throws
IOException {
+ Objects.requireNonNull(inputStream, "inputStream");
this.defaultEncoding = defaultEncoding;
final BOMInputStream bom = new BOMInputStream(new
BufferedInputStream(inputStream, BUFFER_SIZE), false, BOMS);
final BOMInputStream pis = new BOMInputStream(bom, true,
XML_GUESS_BYTES);
diff --git a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
index 019729f..443e5ce 100644
--- a/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
@@ -17,15 +17,19 @@
package org.apache.commons.io.input;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
+import java.net.URL;
+import java.net.URLConnection;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
@@ -79,6 +83,26 @@ public class XmlStreamReaderTest {
}
@Test
+ protected void testNullFileInput() throws IOException {
+ assertThrows(NullPointerException.class, () -> new
XmlStreamReader((File)null));
+ }
+
+ @Test
+ protected void testNullInputStreamInput() throws IOException {
+ assertThrows(NullPointerException.class, () -> new
XmlStreamReader((InputStream) null));
+ }
+
+ @Test
+ protected void testNullURLInput() throws IOException {
+ assertThrows(NullPointerException.class, () -> new
XmlStreamReader((URL)null));
+ }
+
+ @Test
+ protected void testNullURLConnectionInput() throws IOException {
+ assertThrows(NullPointerException.class, () -> new
XmlStreamReader((URLConnection)null, "US-ASCII"));
+ }
+
+ @Test
public void testRawNoBomUsAscii() throws Exception {
_testRawNoBomValid("US-ASCII");
}