[tomcat] branch 7.0.x updated: SpotBugs - fix various exception class related issues

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[tomcat] branch 7.0.x updated: SpotBugs - fix various exception class related issues

markt
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/7.0.x by this push:
     new ed94503  SpotBugs - fix various exception class related issues
ed94503 is described below

commit ed94503f43f88eafb6a4c719d46ffe899313189a
Author: Mark Thomas <[hidden email]>
AuthorDate: Fri Oct 16 17:06:16 2020 +0100

    SpotBugs - fix various exception class related issues
   
    I'd prefer not to use static imports at all but JUnit uses them
    extensively and avoiding them in these instances means unnecessarily
    verbose code.
---
 res/checkstyle/checkstyle.xml                              |  5 +++--
 test/javax/el/TestBeanELResolver.java                      |  7 +++++--
 .../apache/catalina/core/TestSwallowAbortedUploads.java    | 11 +++++++----
 test/org/apache/catalina/realm/TestJNDIRealm.java          |  7 +++++--
 test/org/apache/catalina/startup/TestTomcat.java           |  7 +++++--
 .../apache/catalina/startup/TestWebappServiceLoader.java   |  7 +++++--
 test/org/apache/catalina/tribes/io/TestXByteBuffer.java    |  5 ++++-
 .../apache/jasper/compiler/TestELInterpreterFactory.java   | 14 ++++++++------
 8 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/res/checkstyle/checkstyle.xml b/res/checkstyle/checkstyle.xml
index 3bbcb43..77276d6 100644
--- a/res/checkstyle/checkstyle.xml
+++ b/res/checkstyle/checkstyle.xml
@@ -60,8 +60,9 @@
     <!-- Imports -->
     <module name="AvoidStarImport"/>
     <module name="AvoidStaticImport">
-        <property name="excludes"
-                  value="org.apache.catalina.startup.SimpleHttpClient.CRLF"/>
+        <property name="excludes" value="org.apache.catalina.startup.SimpleHttpClient.CRLF"/>
+        <property name="excludes" value="org.hamcrest.MatcherAssert.*"/>
+        <property name="excludes" value="org.hamcrest.core.IsInstanceOf.*"/>
     </module>
     <module name="IllegalImport">
         <property name="illegalPkgs" value="sun,junit.framework"/>
diff --git a/test/javax/el/TestBeanELResolver.java b/test/javax/el/TestBeanELResolver.java
index 825d7c3..cbd3797 100644
--- a/test/javax/el/TestBeanELResolver.java
+++ b/test/javax/el/TestBeanELResolver.java
@@ -21,6 +21,9 @@ import java.beans.PropertyDescriptor;
 import java.util.ArrayList;
 import java.util.Iterator;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -57,9 +60,9 @@ public class TestBeanELResolver {
         } catch (PropertyNotFoundException pnfe) {
             e = pnfe;
         }
-        Assert.assertTrue("Wrong exception type",
-                e instanceof PropertyNotFoundException);
+        assertThat("Wrong exception type", e, instanceOf(PropertyNotFoundException.class));
         String type = Bean.class.getName();
+        @SuppressWarnings("null") // Not possible due to test above
         String msg = e.getMessage();
         Assert.assertTrue("No reference to type [" + type +
                 "] where property cannot be found in [" + msg + "]",
diff --git a/test/org/apache/catalina/core/TestSwallowAbortedUploads.java b/test/org/apache/catalina/core/TestSwallowAbortedUploads.java
index 450d9fc..ba45c93 100644
--- a/test/org/apache/catalina/core/TestSwallowAbortedUploads.java
+++ b/test/org/apache/catalina/core/TestSwallowAbortedUploads.java
@@ -34,6 +34,9 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.Part;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -127,8 +130,8 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest {
         log.info("Limited, swallow disabled");
         AbortedUploadClient client = new AbortedUploadClient();
         Exception ex = doAbortedUploadTest(client, true, false);
-        Assert.assertTrue("Limited upload with swallow disabled does not generate client exception",
-                   ex instanceof java.net.SocketException);
+        assertThat("Limited upload with swallow disabled does not generate client exception",
+                   ex, instanceOf(java.net.SocketException.class));
         client.reset();
     }
 
@@ -173,8 +176,8 @@ public class TestSwallowAbortedUploads extends TomcatBaseTest {
         log.info("Aborted (413), swallow disabled");
         AbortedPOSTClient client = new AbortedPOSTClient();
         Exception ex = doAbortedPOSTTest(client, HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, false);
-        Assert.assertTrue("Limited upload with swallow disabled does not generate client exception",
-                   ex instanceof java.net.SocketException);
+        assertThat("Limited upload with swallow disabled does not generate client exception",
+                ex, instanceOf(java.net.SocketException.class));
         client.reset();
     }
 
diff --git a/test/org/apache/catalina/realm/TestJNDIRealm.java b/test/org/apache/catalina/realm/TestJNDIRealm.java
index 9174b72..4986969 100644
--- a/test/org/apache/catalina/realm/TestJNDIRealm.java
+++ b/test/org/apache/catalina/realm/TestJNDIRealm.java
@@ -28,6 +28,9 @@ import javax.naming.directory.InitialDirContext;
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -86,7 +89,7 @@ public class TestJNDIRealm {
                 realm.authenticate(USER, expectedResponse, NONCE, null, null, null, REALM, HA2);
 
         // THEN
-        Assert.assertTrue(principal instanceof GenericPrincipal);
+        assertThat(principal, instanceOf(GenericPrincipal.class));
         Assert.assertEquals(PASSWORD, ((GenericPrincipal)principal).getPassword());
     }
 
@@ -103,7 +106,7 @@ public class TestJNDIRealm {
                 realm.authenticate(USER, expectedResponse, NONCE, null, null, null, REALM, HA2);
 
         // THEN
-        Assert.assertTrue(principal instanceof GenericPrincipal);
+        assertThat(principal, instanceOf(GenericPrincipal.class));
         Assert.assertEquals(ha1(), ((GenericPrincipal)principal).getPassword());
     }
 
diff --git a/test/org/apache/catalina/startup/TestTomcat.java b/test/org/apache/catalina/startup/TestTomcat.java
index c59efcd..16ffd47 100644
--- a/test/org/apache/catalina/startup/TestTomcat.java
+++ b/test/org/apache/catalina/startup/TestTomcat.java
@@ -38,6 +38,9 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -638,7 +641,7 @@ public class TestTomcat extends TomcatBaseTest {
             tomcat.start();
             Assert.fail();
         } catch (Throwable t) {
-            Assert.assertTrue(getRootCause(t) instanceof LifecycleException);
+            assertThat(getRootCause(t), instanceOf(LifecycleException.class));
         }
     }
 
@@ -656,7 +659,7 @@ public class TestTomcat extends TomcatBaseTest {
             tomcat.start();
             Assert.fail();
         } catch (Throwable t) {
-            Assert.assertTrue(getRootCause(t) instanceof MultiThrowable);
+            assertThat(getRootCause(t), instanceOf(MultiThrowable.class));
         }
     }
 
diff --git a/test/org/apache/catalina/startup/TestWebappServiceLoader.java b/test/org/apache/catalina/startup/TestWebappServiceLoader.java
index dddf9dc..824bfcb 100644
--- a/test/org/apache/catalina/startup/TestWebappServiceLoader.java
+++ b/test/org/apache/catalina/startup/TestWebappServiceLoader.java
@@ -27,6 +27,9 @@ import java.util.List;
 import javax.servlet.ServletContainerInitializer;
 import javax.servlet.ServletContext;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -159,7 +162,7 @@ public class TestWebappServiceLoader {
         try {
             loader.loadServices(ServletContainerInitializer.class, names);
         } catch (IOException e) {
-            Assert.assertTrue(e.getCause() instanceof ClassCastException);
+            assertThat(e.getCause(), instanceOf(ClassCastException.class));
         } finally {
             control.verify();
         }
@@ -178,7 +181,7 @@ public class TestWebappServiceLoader {
         try {
             loader.loadServices(ServletContainerInitializer.class, names);
         } catch (IOException e) {
-            Assert.assertTrue(e.getCause() instanceof InstantiationException);
+            assertThat(e.getCause(), instanceOf(InstantiationException.class));
         } finally {
             control.verify();
         }
diff --git a/test/org/apache/catalina/tribes/io/TestXByteBuffer.java b/test/org/apache/catalina/tribes/io/TestXByteBuffer.java
index 5bbb7c2..cf7ff86 100644
--- a/test/org/apache/catalina/tribes/io/TestXByteBuffer.java
+++ b/test/org/apache/catalina/tribes/io/TestXByteBuffer.java
@@ -16,6 +16,9 @@
  */
 package org.apache.catalina.tribes.io;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -32,7 +35,7 @@ public class TestXByteBuffer {
         String test = "This is as test.";
         byte[] msg = XByteBuffer.serialize(test);
         Object obj = XByteBuffer.deserialize(msg);
-        Assert.assertTrue(obj instanceof String);
+        assertThat(obj, instanceOf(String.class));
         Assert.assertEquals(test, obj);
     }
 }
diff --git a/test/org/apache/jasper/compiler/TestELInterpreterFactory.java b/test/org/apache/jasper/compiler/TestELInterpreterFactory.java
index 1b5a8fe..0b46440 100644
--- a/test/org/apache/jasper/compiler/TestELInterpreterFactory.java
+++ b/test/org/apache/jasper/compiler/TestELInterpreterFactory.java
@@ -22,6 +22,9 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -53,10 +56,9 @@ public class TestELInterpreterFactory extends TomcatBaseTest {
 
         ServletContext context = ctx.getServletContext();
 
-        ELInterpreter interpreter =
-                ELInterpreterFactory.getELInterpreter(context);
+        ELInterpreter interpreter = ELInterpreterFactory.getELInterpreter(context);
         Assert.assertNotNull(interpreter);
-        Assert.assertTrue(interpreter instanceof DefaultELInterpreter);
+        assertThat(interpreter, instanceOf(DefaultELInterpreter.class));
 
         context.removeAttribute(ELInterpreter.class.getName());
 
@@ -64,7 +66,7 @@ public class TestELInterpreterFactory extends TomcatBaseTest {
                 SimpleELInterpreter.class.getName());
         interpreter = ELInterpreterFactory.getELInterpreter(context);
         Assert.assertNotNull(interpreter);
-        Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
+        assertThat(interpreter, instanceOf(SimpleELInterpreter.class));
 
         context.removeAttribute(ELInterpreter.class.getName());
 
@@ -72,7 +74,7 @@ public class TestELInterpreterFactory extends TomcatBaseTest {
         context.setAttribute(ELInterpreter.class.getName(), simpleInterpreter);
         interpreter = ELInterpreterFactory.getELInterpreter(context);
         Assert.assertNotNull(interpreter);
-        Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
+        assertThat(interpreter, instanceOf(SimpleELInterpreter.class));
         Assert.assertTrue(interpreter == simpleInterpreter);
 
         context.removeAttribute(ELInterpreter.class.getName());
@@ -83,7 +85,7 @@ public class TestELInterpreterFactory extends TomcatBaseTest {
 
         interpreter = ELInterpreterFactory.getELInterpreter(ctx.getServletContext());
         Assert.assertNotNull(interpreter);
-        Assert.assertTrue(interpreter instanceof SimpleELInterpreter);
+        assertThat(interpreter, instanceOf(SimpleELInterpreter.class));
     }
 
     public static class Bug54239Listener implements ServletContextListener {


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]