This is an automated email from the ASF dual-hosted git repository. veithen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ws-axiom.git
The following commit(s) were added to refs/heads/master by this push: new 4988989af Use assertThrows 4988989af is described below commit 4988989afa869b830280857c831aff604bce96ad Author: Andreas Veithen <andreas.veit...@gmail.com> AuthorDate: Fri Nov 4 21:35:08 2022 +0000 Use assertThrows --- .../apache/axiom/attachments/AttachmentsTest.java | 97 ++++++---------------- .../blob/suite/TestGetInputStreamIllegalState.java | 9 +- .../suite/TestGetOutputStreamIllegalState.java | 9 +- .../axiom/blob/suite/TestGetSizeIllegalState.java | 9 +- .../axiom/blob/suite/TestReadFromIllegalState.java | 9 +- .../axiom/blob/suite/TestReadFromWithError.java | 11 +-- .../axiom/blob/suite/TestWriteToIllegalState.java | 9 +- .../axiom/blob/suite/TestWriteToWithError.java | 11 +-- .../org/apache/axiom/mime/ContentTypeTest.java | 14 ++-- .../java/org/apache/axiom/mime/MediaTypeTest.java | 15 +--- .../org/apache/axiom/om/util/StAXUtilsTest.java | 18 ++-- .../apache/axiom/util/base64/Base64UtilsTest.java | 36 ++------ .../axiom/util/stax/XMLStreamReaderUtilsTest.java | 23 ++--- .../dialect/IllegalStateExceptionTestCase.java | 15 ++-- .../TestCreateXMLEventWriterWithNullEncoding.java | 12 +-- .../TestCreateXMLStreamWriterWithNullEncoding.java | 12 +-- .../dialect/TestGetCharacterEncodingScheme.java | 9 +- .../axiom/util/stax/dialect/TestGetEncoding.java | 9 +- .../axiom/util/stax/dialect/TestGetVersion.java | 9 +- .../axiom/util/stax/dialect/TestIsStandalone.java | 9 +- .../axiom/util/stax/dialect/TestStandaloneSet.java | 9 +- .../TestWriteStartDocumentWithNullEncoding.java | 10 +-- 22 files changed, 106 insertions(+), 258 deletions(-) diff --git a/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java b/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java index 016e12f46..32ca8d6fe 100644 --- a/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java +++ b/axiom-api/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java @@ -19,6 +19,7 @@ package org.apache.axiom.attachments; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import java.io.BufferedReader; import java.io.ByteArrayInputStream; @@ -233,15 +234,10 @@ public class AttachmentsTest extends AbstractTestCase { public void testGetRootPartContentTypeWithContentIDMismatch() { String contentType = "multipart/related; boundary=\"" + MTOMSample.SAMPLE1.getBoundary() + "\"; type=\"text/xml\"; start=\"<wrong-content...@example.org>\""; - try { + assertThrows(MIMEException.class, () -> { Attachments attachments = new Attachments(MTOMSample.SAMPLE1.getInputStream(), contentType); attachments.getRootPartContentType(); - fail("Expected MIMEException"); - } catch (MIMEException ex) { - // OK, expected - } catch (Throwable ex) { - fail("Unexpected exception: " + ex.getClass().getName()); - } + }); } public void testGetIncomingAttachmentStreams() throws Exception { @@ -311,72 +307,44 @@ public class AttachmentsTest extends AbstractTestCase { while (is.read() != -1) ; } - public void testSimultaneousStreamAccess() throws Exception { - InputStream inStream; - Attachments attachments; - - inStream = MTOMSample.SAMPLE1.getInputStream(); - attachments = new Attachments(inStream, MTOMSample.SAMPLE1.getContentType()); + public void testSimultaneousStreamAccess1() throws Exception { + InputStream inStream = MTOMSample.SAMPLE1.getInputStream(); + Attachments attachments = new Attachments(inStream, MTOMSample.SAMPLE1.getContentType()); attachments.getDataHandler("2.urn:uuid:a3adbaee51a1a87b2a11443668160...@apache.org"); // This should throw an error - try { - attachments.getIncomingAttachmentStreams(); - fail("No exception caught when attempting to access datahandler and stream at the same time"); - } catch (IllegalStateException ise) { - // Nothing - } + assertThrows(IllegalStateException.class, attachments::getIncomingAttachmentStreams); inStream.close(); - + } + + public void testSimultaneousStreamAccess2() throws Exception { // Try the other way around. - inStream = MTOMSample.SAMPLE1.getInputStream(); - attachments = new Attachments(inStream, MTOMSample.SAMPLE1.getContentType()); + InputStream inStream = MTOMSample.SAMPLE1.getInputStream(); + Attachments attachments = new Attachments(inStream, MTOMSample.SAMPLE1.getContentType()); attachments.getIncomingAttachmentStreams(); // These should NOT throw error even though they are using part based access - try { - String contentType = attachments.getRootPartContentType(); - assertTrue(contentType.indexOf("application/xop+xml;") >=0); - assertTrue(contentType.indexOf("charset=UTF-8;") >=0); - assertTrue(contentType.indexOf("type=\"application/soap+xml\";") >=0); - } catch (IllegalStateException ise) { - fail("No exception expected when requesting SOAP part data"); - ise.printStackTrace(); - } + String contentType = attachments.getRootPartContentType(); + assertTrue(contentType.indexOf("application/xop+xml;") >=0); + assertTrue(contentType.indexOf("charset=UTF-8;") >=0); + assertTrue(contentType.indexOf("type=\"application/soap+xml\";") >=0); - try { - attachments.getRootPartInputStream(); - } catch (IllegalStateException ise) { - fail("No exception expected when requesting SOAP part data"); - } + attachments.getRootPartInputStream(); // These should throw an error - try { - attachments.getDataHandler("2.urn:uuid:a3adbaee51a1a87b2a11443668160...@apache.org"); - fail("No exception caught when attempting to access stream and datahandler at the same time"); - } catch (IllegalStateException ise) { - // Nothing - } + assertThrows(IllegalStateException.class, () -> + attachments.getDataHandler("2.urn:uuid:a3adbaee51a1a87b2a11443668160...@apache.org")); // Additionally, we also need to ensure mutual exclusion if someone // tries to access part data directly - try { - attachments.getAllContentIDs(); - fail("No exception caught when attempting to access stream and contentids list at the same time"); - } catch (IllegalStateException ise) { - // Nothing - } + assertThrows(IllegalStateException.class, attachments::getAllContentIDs); - try { - attachments.getDataHandler("2.urn:uuid:a3adbaee51a1a87b2a11443668160...@apache.org"); - fail("No exception caught when attempting to access stream and part at the same time"); - } catch (IllegalStateException ise) { - // Nothing - } + assertThrows(IllegalStateException.class, () -> + attachments.getDataHandler("2.urn:uuid:a3adbaee51a1a87b2a11443668160...@apache.org")); } public void testRemoveDataHandlerAfterParsing() { @@ -707,12 +675,7 @@ public class AttachmentsTest extends AbstractTestCase { } public void testGetAttachmentSpecTypeWithoutStream() { - try { - new Attachments().getAttachmentSpecType(); - fail("Expected OMException"); - } catch (OMException ex) { - // Expected - } + assertThrows(OMException.class, () -> new Attachments().getAttachmentSpecType()); } private void testGetSizeOnDataSource(boolean useFiles) throws Exception { @@ -748,12 +711,7 @@ public class AttachmentsTest extends AbstractTestCase { try { Attachments attachments = new Attachments(new ExceptionInputStream(in, 1050), MTOMSample.SAMPLE1.getContentType()); // TODO: decide what exception should be thrown exactly here - try { - attachments.getDataHandler("1.urn:uuid:a3adbaee51a1a87b2a11443668160...@apache.org"); - fail("Expected exception"); - } catch (MIMEException ex) { - // Expected - } + assertThrows(MIMEException.class, () -> attachments.getDataHandler("1.urn:uuid:a3adbaee51a1a87b2a11443668160...@apache.org")); } finally { in.close(); } @@ -765,12 +723,7 @@ public class AttachmentsTest extends AbstractTestCase { Attachments attachments = new Attachments(new ExceptionInputStream(in, 1500), MTOMSample.SAMPLE1.getContentType()); DataHandler dh = attachments.getDataHandler("1.urn:uuid:a3adbaee51a1a87b2a11443668160...@apache.org"); // TODO: decide what exception should be thrown exactly here - try { - dh.getInputStream(); - fail("Expected exception"); - } catch (MIMEException ex) { - // Expected - } + assertThrows(MIMEException.class, dh::getInputStream); } finally { in.close(); } diff --git a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetInputStreamIllegalState.java b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetInputStreamIllegalState.java index edd572668..6413cb4c4 100644 --- a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetInputStreamIllegalState.java +++ b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetInputStreamIllegalState.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.blob.suite; +import static org.junit.Assert.assertThrows; + import org.apache.axiom.blob.WritableBlob; import org.apache.axiom.blob.WritableBlobFactory; @@ -29,11 +31,6 @@ public class TestGetInputStreamIllegalState extends WritableBlobTestCase { @Override protected void runTest(WritableBlob blob) throws Throwable { - try { - blob.getInputStream(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, blob::getInputStream); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetOutputStreamIllegalState.java b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetOutputStreamIllegalState.java index 123b81d45..0b3b7f501 100644 --- a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetOutputStreamIllegalState.java +++ b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetOutputStreamIllegalState.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.blob.suite; +import static org.junit.Assert.assertThrows; + import org.apache.axiom.blob.WritableBlob; import org.apache.axiom.blob.WritableBlobFactory; @@ -29,11 +31,6 @@ public class TestGetOutputStreamIllegalState extends WritableBlobTestCase { @Override protected void runTest(WritableBlob blob) throws Throwable { - try { - blob.getOutputStream(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, blob::getOutputStream); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetSizeIllegalState.java b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetSizeIllegalState.java index c41df3c6f..fc621beb8 100644 --- a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetSizeIllegalState.java +++ b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestGetSizeIllegalState.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.blob.suite; +import static org.junit.Assert.assertThrows; + import org.apache.axiom.blob.WritableBlob; import org.apache.axiom.blob.WritableBlobFactory; @@ -29,11 +31,6 @@ public class TestGetSizeIllegalState extends WritableBlobTestCase { @Override protected void runTest(WritableBlob blob) throws Throwable { - try { - blob.getSize(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, blob::getSize); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestReadFromIllegalState.java b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestReadFromIllegalState.java index de6e7d694..3e7c80bbc 100644 --- a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestReadFromIllegalState.java +++ b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestReadFromIllegalState.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.blob.suite; +import static org.junit.Assert.assertThrows; + import org.apache.axiom.blob.WritableBlob; import org.apache.axiom.blob.WritableBlobFactory; import org.apache.commons.io.input.NullInputStream; @@ -30,11 +32,6 @@ public class TestReadFromIllegalState extends WritableBlobTestCase { @Override protected void runTest(WritableBlob blob) throws Throwable { - try { - blob.readFrom(new NullInputStream(0)); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, () -> blob.readFrom(new NullInputStream(0))); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestReadFromWithError.java b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestReadFromWithError.java index a8b7cfa9a..071af55a1 100644 --- a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestReadFromWithError.java +++ b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestReadFromWithError.java @@ -19,6 +19,7 @@ package org.apache.axiom.blob.suite; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import org.apache.axiom.blob.WritableBlob; import org.apache.axiom.blob.WritableBlobFactory; @@ -34,12 +35,8 @@ public class TestReadFromWithError extends WritableBlobTestCase { @Override protected void runTest(WritableBlob blob) throws Throwable { ExceptionInputStream in = new ExceptionInputStream(new NullInputStream(1000), 500); - try { - blob.readFrom(in); - fail("Expected StreamCopyException"); - } catch (StreamCopyException ex) { - assertThat(ex.getOperation()).isEqualTo(StreamCopyException.READ); - assertThat(ex.getCause()).isSameInstanceAs(in.getException()); - } + StreamCopyException ex = assertThrows(StreamCopyException.class, () -> blob.readFrom(in)); + assertThat(ex.getOperation()).isEqualTo(StreamCopyException.READ); + assertThat(ex.getCause()).isSameInstanceAs(in.getException()); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestWriteToIllegalState.java b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestWriteToIllegalState.java index 4ce7c25f8..07d741d14 100644 --- a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestWriteToIllegalState.java +++ b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestWriteToIllegalState.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.blob.suite; +import static org.junit.Assert.assertThrows; + import org.apache.axiom.blob.WritableBlob; import org.apache.axiom.blob.WritableBlobFactory; import org.apache.commons.io.output.NullOutputStream; @@ -30,11 +32,6 @@ public class TestWriteToIllegalState extends WritableBlobTestCase { @Override protected void runTest(WritableBlob blob) throws Throwable { - try { - blob.writeTo(NullOutputStream.NULL_OUTPUT_STREAM); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, () -> blob.writeTo(NullOutputStream.NULL_OUTPUT_STREAM)); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestWriteToWithError.java b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestWriteToWithError.java index ddc2304c7..5718f53b6 100644 --- a/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestWriteToWithError.java +++ b/axiom-api/src/test/java/org/apache/axiom/blob/suite/TestWriteToWithError.java @@ -25,6 +25,7 @@ import org.apache.axiom.blob.WritableBlobFactory; import org.apache.axiom.ext.io.StreamCopyException; import org.apache.axiom.testutils.io.ExceptionOutputStream; import org.apache.commons.io.input.NullInputStream; +import org.junit.Assert; public class TestWriteToWithError extends SizeSensitiveWritableBlobTestCase { public TestWriteToWithError(WritableBlobFactory<?> factory, int size) { @@ -35,12 +36,8 @@ public class TestWriteToWithError extends SizeSensitiveWritableBlobTestCase { protected void runTest(WritableBlob blob) throws Throwable { blob.readFrom(new NullInputStream(size)); ExceptionOutputStream out = new ExceptionOutputStream(size/2); - try { - blob.writeTo(out); - fail("Expected StreamCopyException"); - } catch (StreamCopyException ex) { - assertThat(ex.getOperation()).isEqualTo(StreamCopyException.WRITE); - assertThat(ex.getCause()).isSameInstanceAs(out.getException()); - } + StreamCopyException ex = Assert.assertThrows(StreamCopyException.class, () -> blob.writeTo(out)); + assertThat(ex.getOperation()).isEqualTo(StreamCopyException.WRITE); + assertThat(ex.getCause()).isSameInstanceAs(out.getException()); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java b/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java index 141e4a790..096616f01 100644 --- a/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java +++ b/axiom-api/src/test/java/org/apache/axiom/mime/ContentTypeTest.java @@ -81,31 +81,31 @@ public class ContentTypeTest extends TestCase { } public void testParseInvalid1() { - assertThrows(ParseException.class, () -> { new ContentType("text/xml; ?"); }); + assertThrows(ParseException.class, () -> new ContentType("text/xml; ?")); } public void testParseInvalid2() { - assertThrows(ParseException.class, () -> { new ContentType("text/"); }); + assertThrows(ParseException.class, () -> new ContentType("text/")); } public void testParseInvalid3() { - assertThrows(ParseException.class, () -> { new ContentType("text/xml; charset="); }); + assertThrows(ParseException.class, () -> new ContentType("text/xml; charset=")); } public void testParseInvalid4() { - assertThrows(ParseException.class, () -> { new ContentType("text/xml; charset=\"asc"); }); + assertThrows(ParseException.class, () -> new ContentType("text/xml; charset=\"asc")); } public void testParseInvalid5() { - assertThrows(ParseException.class, () -> { new ContentType("text/xml; param=\"test\\"); }); + assertThrows(ParseException.class, () -> new ContentType("text/xml; param=\"test\\")); } public void testParseInvalid6() { - assertThrows(ParseException.class, () -> { new ContentType("text/xml; param;"); }); + assertThrows(ParseException.class, () -> new ContentType("text/xml; param;")); } public void testParseInvalid7() { - assertThrows(ParseException.class, () -> { new ContentType("text/xml; param"); }); + assertThrows(ParseException.class, () -> new ContentType("text/xml; param")); } public void testToString() { diff --git a/axiom-api/src/test/java/org/apache/axiom/mime/MediaTypeTest.java b/axiom-api/src/test/java/org/apache/axiom/mime/MediaTypeTest.java index 514188990..41e586260 100644 --- a/axiom-api/src/test/java/org/apache/axiom/mime/MediaTypeTest.java +++ b/axiom-api/src/test/java/org/apache/axiom/mime/MediaTypeTest.java @@ -19,6 +19,7 @@ package org.apache.axiom.mime; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import java.text.ParseException; @@ -78,21 +79,11 @@ public class MediaTypeTest extends TestCase { } public void testParseInvalid1() { - try { - new MediaType("text/"); - fail("Expected ParseException"); - } catch (ParseException ex) { - // Expected - } + assertThrows(ParseException.class, () -> new MediaType("text/")); } public void testParseInvalid2() { - try { - new MediaType("text/xml;"); - fail("Expected ParseException"); - } catch (ParseException ex) { - // Expected - } + assertThrows(ParseException.class, () -> new MediaType("text/xml;")); } public void testIsXML() throws Exception { diff --git a/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java b/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java index b72315dfb..0c065b304 100644 --- a/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java +++ b/axiom-api/src/test/java/org/apache/axiom/om/util/StAXUtilsTest.java @@ -19,26 +19,20 @@ package org.apache.axiom.om.util; +import static org.junit.Assert.assertThrows; + import junit.framework.TestCase; public class StAXUtilsTest extends TestCase { public void testInputFactoryIsImmutable() throws Exception { - try { + assertThrows(IllegalStateException.class, () -> StAXUtils.getXMLInputFactory().setProperty("javax.xml.stream.isValidating", - Boolean.TRUE); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + Boolean.TRUE)); } public void testOutputFactoryIsImmutable() throws Exception { - try { + assertThrows(IllegalStateException.class, () -> StAXUtils.getXMLOutputFactory().setProperty("javax.xml.stream.isRepairingNamespaces", - Boolean.TRUE); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + Boolean.TRUE)); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/base64/Base64UtilsTest.java b/axiom-api/src/test/java/org/apache/axiom/util/base64/Base64UtilsTest.java index 8fb88817a..5ab4d15dd 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/base64/Base64UtilsTest.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/base64/Base64UtilsTest.java @@ -20,6 +20,7 @@ package org.apache.axiom.util.base64; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import java.util.Random; @@ -38,30 +39,15 @@ public class Base64UtilsTest extends TestCase { } public void testMissingPadding() { - try { - Base64Utils.decode("cw"); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException ex) { - // Expected - } + assertThrows(IllegalArgumentException.class, () -> Base64Utils.decode("cw")); } public void testTooMuchPadding() { - try { - Base64Utils.decode("cw==="); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException ex) { - // Expected - } + assertThrows(IllegalArgumentException.class, () -> Base64Utils.decode("cw===")); } public void testNonZeroRemainder() { - try { - Base64Utils.decode("//=="); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException ex) { - // Expected - } + assertThrows(IllegalArgumentException.class, () -> Base64Utils.decode("//==")); } public void testSpace() throws Exception{ @@ -71,20 +57,10 @@ public class Base64UtilsTest extends TestCase { } public void testInvalidCharacter() { - try { - Base64Utils.decode("//-/"); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException ex) { - // Expected - } + assertThrows(IllegalArgumentException.class, () -> Base64Utils.decode("//-/")); } public void testInvalidPadding() { - try { - Base64Utils.decode("//=/"); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException ex) { - // Expected - } + assertThrows(IllegalArgumentException.class, () -> Base64Utils.decode("//=/")); } } \ No newline at end of file diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java index 32183945b..89ed35693 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax; +import static org.junit.Assert.assertThrows; + import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -106,22 +108,16 @@ public class XMLStreamReaderUtilsTest extends TestCase { } private void testGetBlobFromElementWithUnexpectedContent(boolean useBlobReader) throws Exception { - XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<test>\n<child/>\n</test>")); - if (useBlobReader) { - reader = new XMLStreamReaderWithBlobReader(reader); - } + XMLStreamReader parentReader = StAXUtils.createXMLStreamReader(new StringReader("<test>\n<child/>\n</test>")); + XMLStreamReader reader = useBlobReader ? new XMLStreamReaderWithBlobReader(parentReader) : parentReader; try { reader.next(); // Check precondition assertTrue(reader.isStartElement()); - try { - XMLStreamReaderUtils.getBlobFromElement(reader); - fail("Expected XMLStreamException"); - } catch (XMLStreamException ex) { - // Expected - } + assertThrows(XMLStreamException.class, () -> + XMLStreamReaderUtils.getBlobFromElement(reader)); } finally { reader.close(); } @@ -225,11 +221,6 @@ public class XMLStreamReaderUtilsTest extends TestCase { XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<a>xxx<b>yyy</b>zzz</a>")); reader.next(); Reader in = XMLStreamReaderUtils.getElementTextAsStream(reader, false); - try { - IOUtils.toString(in); - fail("Expected exception"); - } catch (IOException ex) { - // Expected - } + assertThrows(IOException.class, () -> IOUtils.toString(in)); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/IllegalStateExceptionTestCase.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/IllegalStateExceptionTestCase.java index 039167647..be1793bd7 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/IllegalStateExceptionTestCase.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/IllegalStateExceptionTestCase.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; @@ -31,17 +33,10 @@ public abstract class IllegalStateExceptionTestCase extends EventSpecificTestCas @Override protected final void runTest(XMLStreamReader reader) throws Throwable { - boolean exception; - try { + if (expectException) { + assertThrows(IllegalStateException.class, () -> invoke(reader)); + } else { invoke(reader); - exception = false; - } catch (IllegalStateException ex) { - exception = true; - } - if (exception && !expectException) { - fail("Didn't expect IllegalStateException"); - } else if (!exception && expectException) { - fail("Expected IllegalStateException"); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestCreateXMLEventWriterWithNullEncoding.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestCreateXMLEventWriterWithNullEncoding.java index 4ae627b38..5f3c3406e 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestCreateXMLEventWriterWithNullEncoding.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestCreateXMLEventWriterWithNullEncoding.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import javax.xml.stream.XMLOutputFactory; public class TestCreateXMLEventWriterWithNullEncoding extends DialectTestCase { @@ -29,14 +31,6 @@ public class TestCreateXMLEventWriterWithNullEncoding extends DialectTestCase { protected void runTest() throws Throwable { XMLOutputFactory factory = staxImpl.newNormalizedXMLOutputFactory(); // This should cause an exception - try { - factory.createXMLEventWriter(System.out, null); - } catch (Throwable ex) { - // Expected - return; - } - // Attention here: since the fail method works by throwing an exception and we - // catch Throwable, it must be invoked outside of the catch block! - fail("Expected createXMLEventWriter to throw an exception"); + assertThrows(Throwable.class, () -> factory.createXMLEventWriter(System.out, null)); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestCreateXMLStreamWriterWithNullEncoding.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestCreateXMLStreamWriterWithNullEncoding.java index cec5c62a3..c656fe6ed 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestCreateXMLStreamWriterWithNullEncoding.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestCreateXMLStreamWriterWithNullEncoding.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import javax.xml.stream.XMLOutputFactory; public class TestCreateXMLStreamWriterWithNullEncoding extends DialectTestCase { @@ -29,14 +31,6 @@ public class TestCreateXMLStreamWriterWithNullEncoding extends DialectTestCase { protected void runTest() throws Throwable { XMLOutputFactory factory = staxImpl.newNormalizedXMLOutputFactory(); // This should cause an exception - try { - factory.createXMLStreamWriter(System.out, null); - } catch (Throwable ex) { - // Expected - return; - } - // Attention here: since the fail method works by throwing an exception and we - // catch Throwable, it must be invoked outside of the catch block! - fail("Expected createXMLStreamWriter to throw an exception"); + assertThrows(Throwable.class, () -> factory.createXMLStreamWriter(System.out, null)); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetCharacterEncodingScheme.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetCharacterEncodingScheme.java index a1b317739..e4d4e0208 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetCharacterEncodingScheme.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetCharacterEncodingScheme.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import java.io.ByteArrayInputStream; import javax.xml.stream.XMLInputFactory; @@ -35,12 +37,7 @@ public class TestGetCharacterEncodingScheme extends DialectTestCase { "<?xml version='1.0' encoding='iso-8859-15'?><root/>".getBytes("iso-8859-15"))); assertEquals("iso-8859-15", reader.getCharacterEncodingScheme()); reader.next(); - try { - reader.getCharacterEncodingScheme(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, reader::getCharacterEncodingScheme); reader.close(); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetEncoding.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetEncoding.java index 832ddbc7b..99c5a87cc 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetEncoding.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetEncoding.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import java.io.ByteArrayInputStream; import javax.xml.stream.XMLInputFactory; @@ -35,12 +37,7 @@ public class TestGetEncoding extends DialectTestCase { "<?xml version='1.0' encoding='iso-8859-15'?><root/>".getBytes("iso-8859-15"))); assertEquals("iso-8859-15", reader.getEncoding()); reader.next(); - try { - reader.getEncoding(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, reader::getEncoding); reader.close(); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetVersion.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetVersion.java index 8edfc42ec..b231a876e 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetVersion.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestGetVersion.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import java.io.StringReader; import javax.xml.stream.XMLInputFactory; @@ -35,12 +37,7 @@ public class TestGetVersion extends DialectTestCase { new StringReader("<?xml version='1.0'?><root/>")); assertEquals("1.0", reader.getVersion()); reader.next(); - try { - reader.getVersion(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, reader::getVersion); reader.close(); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestIsStandalone.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestIsStandalone.java index e1c4ce6d7..92c1c8395 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestIsStandalone.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestIsStandalone.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import java.io.StringReader; import javax.xml.stream.XMLInputFactory; @@ -35,12 +37,7 @@ public class TestIsStandalone extends DialectTestCase { new StringReader("<?xml version='1.0' standalone='no'?><root/>")); assertEquals(false, reader.isStandalone()); reader.next(); - try { - reader.isStandalone(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, reader::isStandalone); reader.close(); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestStandaloneSet.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestStandaloneSet.java index 829a82bde..afb6556b5 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestStandaloneSet.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestStandaloneSet.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import java.io.StringReader; import javax.xml.stream.XMLInputFactory; @@ -35,12 +37,7 @@ public class TestStandaloneSet extends DialectTestCase { new StringReader("<?xml version='1.0'?><root/>")); assertEquals(false, reader.standaloneSet()); reader.next(); - try { - reader.standaloneSet(); - fail("Expected IllegalStateException"); - } catch (IllegalStateException ex) { - // Expected - } + assertThrows(IllegalStateException.class, reader::standaloneSet); reader.close(); } } diff --git a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestWriteStartDocumentWithNullEncoding.java b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestWriteStartDocumentWithNullEncoding.java index b21b5619e..2bae7204f 100644 --- a/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestWriteStartDocumentWithNullEncoding.java +++ b/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/TestWriteStartDocumentWithNullEncoding.java @@ -18,6 +18,8 @@ */ package org.apache.axiom.util.stax.dialect; +import static org.junit.Assert.assertThrows; + import javax.xml.stream.XMLStreamWriter; public class TestWriteStartDocumentWithNullEncoding extends DialectTestCase { @@ -28,12 +30,6 @@ public class TestWriteStartDocumentWithNullEncoding extends DialectTestCase { @Override protected void runTest() throws Throwable { XMLStreamWriter writer = staxImpl.newNormalizedXMLOutputFactory().createXMLStreamWriter(System.out, "UTF-8"); - try { - writer.writeStartDocument(null, "1.0"); - } catch (Throwable ex) { - // Expected - return; - } - fail("Expected writeStartDocument to throw an exception"); + assertThrows(Throwable.class, () -> writer.writeStartDocument(null, "1.0")); } }