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 f7b8863 Add and use a package-private factory for UnsupportedOperationException to provide exception messages with a consistent formatting. f7b8863 is described below commit f7b886324d4d49396d322673bfbc4eba959a3161 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Thu Jan 21 09:53:49 2021 -0500 Add and use a package-private factory for UnsupportedOperationException to provide exception messages with a consistent formatting. --- .../java/org/apache/commons/io/LineIterator.java | 2 +- .../apache/commons/io/input/NullInputStream.java | 4 +- .../org/apache/commons/io/input/NullReader.java | 4 +- .../commons/io/input/SwappedDataInputStream.java | 4 +- .../io/input/UnixLineEndingInputStream.java | 2 +- .../io/input/UnsupportedOperationExceptions.java | 58 ++++++++++++++++++++++ .../io/input/WindowsLineEndingInputStream.java | 2 +- .../commons/io/input/NullInputStreamTest.java | 7 ++- .../apache/commons/io/input/NullReaderTest.java | 7 ++- 9 files changed, 77 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/apache/commons/io/LineIterator.java b/src/main/java/org/apache/commons/io/LineIterator.java index 49c3a01..94f86e0 100644 --- a/src/main/java/org/apache/commons/io/LineIterator.java +++ b/src/main/java/org/apache/commons/io/LineIterator.java @@ -168,7 +168,7 @@ public class LineIterator implements Iterator<String>, Closeable { */ @Override public void remove() { - throw new UnsupportedOperationException("Remove unsupported on LineIterator"); + throw new UnsupportedOperationException("remove not supported"); } //----------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/io/input/NullInputStream.java b/src/main/java/org/apache/commons/io/input/NullInputStream.java index ce28f2c..f66f5cb 100644 --- a/src/main/java/org/apache/commons/io/input/NullInputStream.java +++ b/src/main/java/org/apache/commons/io/input/NullInputStream.java @@ -167,7 +167,7 @@ public class NullInputStream extends InputStream { @Override public synchronized void mark(final int readlimit) { if (!markSupported) { - throw new UnsupportedOperationException("Mark not supported"); + throw UnsupportedOperationExceptions.mark(); } mark = position; this.readlimit = readlimit; @@ -263,7 +263,7 @@ public class NullInputStream extends InputStream { @Override public synchronized void reset() throws IOException { if (!markSupported) { - throw new UnsupportedOperationException("Mark not supported"); + throw UnsupportedOperationExceptions.reset(); } if (mark < 0) { throw new IOException("No position has been marked"); diff --git a/src/main/java/org/apache/commons/io/input/NullReader.java b/src/main/java/org/apache/commons/io/input/NullReader.java index b2cc429..82b4724 100644 --- a/src/main/java/org/apache/commons/io/input/NullReader.java +++ b/src/main/java/org/apache/commons/io/input/NullReader.java @@ -152,7 +152,7 @@ public class NullReader extends Reader { @Override public synchronized void mark(final int readlimit) { if (!markSupported) { - throw new UnsupportedOperationException("Mark not supported"); + throw UnsupportedOperationExceptions.mark(); } mark = position; this.readlimit = readlimit; @@ -248,7 +248,7 @@ public class NullReader extends Reader { @Override public synchronized void reset() throws IOException { if (!markSupported) { - throw new UnsupportedOperationException("Mark not supported"); + throw UnsupportedOperationExceptions.reset(); } if (mark < 0) { throw new IOException("No position has been marked"); diff --git a/src/main/java/org/apache/commons/io/input/SwappedDataInputStream.java b/src/main/java/org/apache/commons/io/input/SwappedDataInputStream.java index d70ce92..86e0286 100644 --- a/src/main/java/org/apache/commons/io/input/SwappedDataInputStream.java +++ b/src/main/java/org/apache/commons/io/input/SwappedDataInputStream.java @@ -162,7 +162,7 @@ public class SwappedDataInputStream extends ProxyInputStream implements DataInpu */ @Override public String readLine() throws IOException, EOFException { - throw new UnsupportedOperationException("Operation not supported: readLine()"); + throw UnsupportedOperationExceptions.method("readLine"); } /** @@ -222,7 +222,7 @@ public class SwappedDataInputStream extends ProxyInputStream implements DataInpu */ @Override public String readUTF() throws IOException, EOFException { - throw new UnsupportedOperationException("Operation not supported: readUTF()"); + throw UnsupportedOperationExceptions.method("readUTF"); } /** diff --git a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java index 42d72d4..e4bfe71 100644 --- a/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java +++ b/src/main/java/org/apache/commons/io/input/UnixLineEndingInputStream.java @@ -122,6 +122,6 @@ public class UnixLineEndingInputStream extends InputStream { */ @Override public synchronized void mark(final int readlimit) { - throw new UnsupportedOperationException("Mark not supported"); + throw UnsupportedOperationExceptions.mark(); } } diff --git a/src/main/java/org/apache/commons/io/input/UnsupportedOperationExceptions.java b/src/main/java/org/apache/commons/io/input/UnsupportedOperationExceptions.java new file mode 100644 index 0000000..acc213d --- /dev/null +++ b/src/main/java/org/apache/commons/io/input/UnsupportedOperationExceptions.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.io.input; + +/** + * Package-private factory for {@link UnsupportedOperationException} to provide messages with consistent formatting. + * + * TODO Consider making this public and use from LineIterator. + */ +class UnsupportedOperationExceptions { + + private static final String MARK_RESET = "mark/reset"; + + /** + * Creates a new instance of UnsupportedOperationException for a {@code mark} method. + * + * @return a new instance of UnsupportedOperationException + */ + static UnsupportedOperationException mark() { + // Use the same message as in java.io.InputStream.reset() in OpenJDK 8.0.275-1. + return method(MARK_RESET); + } + + /** + * Creates a new instance of UnsupportedOperationException for the given unsupported a {@code method} name. + * + * @param method A method name + * @return a new instance of UnsupportedOperationException + */ + static UnsupportedOperationException method(final String method) { + return new UnsupportedOperationException(method + " not supported"); + } + + /** + * Creates a new instance of UnsupportedOperationException for a {@code reset} method. + * + * @return a new instance of UnsupportedOperationException + */ + static UnsupportedOperationException reset() { + // Use the same message as in java.io.InputStream.reset() in OpenJDK 8.0.275-1. + return method(MARK_RESET); + } +} diff --git a/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java b/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java index b557026..653ceab 100644 --- a/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java +++ b/src/main/java/org/apache/commons/io/input/WindowsLineEndingInputStream.java @@ -130,6 +130,6 @@ public class WindowsLineEndingInputStream extends InputStream { */ @Override public synchronized void mark(final int readlimit) { - throw new UnsupportedOperationException("Mark not supported"); + throw UnsupportedOperationExceptions.mark(); } } diff --git a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java index b886024..4ad1d35 100644 --- a/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/NullInputStreamTest.java @@ -33,6 +33,9 @@ import org.junit.jupiter.api.Test; */ public class NullInputStreamTest { + // Use the same message as in java.io.InputStream.reset() in OpenJDK 8.0.275-1. + private static final String MARK_RESET_NOT_SUPPORTED = "mark/reset not supported"; + @Test public void testRead() throws Exception { final int size = 5; @@ -178,14 +181,14 @@ public class NullInputStreamTest { input.mark(5); fail("mark() should throw UnsupportedOperationException"); } catch (final UnsupportedOperationException e) { - assertEquals("Mark not supported", e.getMessage(), "mark() error message"); + assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "mark() error message"); } try { input.reset(); fail("reset() should throw UnsupportedOperationException"); } catch (final UnsupportedOperationException e) { - assertEquals("Mark not supported", e.getMessage(), "reset() error message"); + assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "reset() error message"); } input.close(); } diff --git a/src/test/java/org/apache/commons/io/input/NullReaderTest.java b/src/test/java/org/apache/commons/io/input/NullReaderTest.java index 9782ef5..d1458f4 100644 --- a/src/test/java/org/apache/commons/io/input/NullReaderTest.java +++ b/src/test/java/org/apache/commons/io/input/NullReaderTest.java @@ -33,6 +33,9 @@ import org.junit.jupiter.api.Test; */ public class NullReaderTest { + // Use the same message as in java.io.InputStream.reset() in OpenJDK 8.0.275-1. + private static final String MARK_RESET_NOT_SUPPORTED = "mark/reset not supported"; + @Test public void testRead() throws Exception { final int size = 5; @@ -175,14 +178,14 @@ public class NullReaderTest { reader.mark(5); fail("mark() should throw UnsupportedOperationException"); } catch (final UnsupportedOperationException e) { - assertEquals("Mark not supported", e.getMessage(), "mark() error message"); + assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "mark() error message"); } try { reader.reset(); fail("reset() should throw UnsupportedOperationException"); } catch (final UnsupportedOperationException e) { - assertEquals("Mark not supported", e.getMessage(), "reset() error message"); + assertEquals(MARK_RESET_NOT_SUPPORTED, e.getMessage(), "reset() error message"); } reader.close(); }