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-beanutils.git
The following commit(s) were added to refs/heads/master by this push: new 70d40e76 Don't hide stack traces from JUnit! 70d40e76 is described below commit 70d40e76922d80e7470042950a3e218f7656d997 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Sep 2 09:55:25 2024 -0400 Don't hide stack traces from JUnit! - Use assertThrows() - Use more precise exceptions than Exception --- .../commons/beanutils2/WrapDynaBeanTestCase.java | 77 ++++------------------ .../commons/beanutils2/bugs/Jira157TestCase.java | 30 ++------- .../commons/beanutils2/bugs/Jira273TestCase.java | 61 ++++------------- .../commons/beanutils2/bugs/Jira298TestCase.java | 31 ++------- .../commons/beanutils2/bugs/Jira339TestCase.java | 25 ++----- 5 files changed, 43 insertions(+), 181 deletions(-) diff --git a/src/test/java/org/apache/commons/beanutils2/WrapDynaBeanTestCase.java b/src/test/java/org/apache/commons/beanutils2/WrapDynaBeanTestCase.java index 5c2af465..f403a888 100644 --- a/src/test/java/org/apache/commons/beanutils2/WrapDynaBeanTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/WrapDynaBeanTestCase.java @@ -18,13 +18,10 @@ package org.apache.commons.beanutils2; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertSame; 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; @@ -143,20 +140,10 @@ public class WrapDynaBeanTestCase extends BasicDynaBeanTestCase { public void testIndexedProperties() { // Invalid getter - try { - bean.get("invalidProperty", 0); - fail("Invalid get should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException t) { - // Expected result - } + assertThrows(IllegalArgumentException.class, () -> bean.get("invalidProperty", 0)); // Invalid setter - try { - bean.set("invalidProperty", 0, "XYZ"); - fail("Invalid set should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException t) { - // Expected result - } + assertThrows(IllegalArgumentException.class, () -> bean.set("invalidProperty", 0, "XYZ")); // Set up initial Value String testValue = "Original Value"; @@ -166,15 +153,10 @@ public class WrapDynaBeanTestCase extends BasicDynaBeanTestCase { assertEquals(testValue, instance.getStringIndexed(0), "Check String property"); // Test Valid Get & Set - try { - testValue = "Some new value"; - bean.set(testProperty, 0, testValue); - assertEquals(testValue, instance.getStringIndexed(0), "Test Set"); - assertEquals(testValue, bean.get(testProperty, 0), "Test Get"); - } catch (final IllegalArgumentException t) { - fail("Get threw exception: " + t); - } - + testValue = "Some new value"; + bean.set(testProperty, 0, testValue); + assertEquals(testValue, instance.getStringIndexed(0), "Test Set"); + assertEquals(testValue, bean.get(testProperty, 0), "Test Get"); } /** @@ -208,25 +190,8 @@ public class WrapDynaBeanTestCase extends BasicDynaBeanTestCase { @Override @Test public void testMappedContains() { - - try { - assertTrue(bean.contains("mappedProperty", "First Key"), "Can see first key"); - fail("Should have thrown UnsupportedOperationException"); - } catch (final UnsupportedOperationException t) { - // Expected result - } catch (final Throwable t) { - fail("Exception: " + t); - } - - try { - assertFalse(bean.contains("mappedProperty", "Unknown Key"), "Can not see unknown key"); - fail("Should have thrown UnsupportedOperationException"); - } catch (final UnsupportedOperationException t) { - // Expected result - } catch (final Throwable t) { - fail("Exception: " + t); - } - + assertThrows(UnsupportedOperationException.class, () -> bean.contains("mappedProperty", "First Key")); + assertThrows(UnsupportedOperationException.class, () -> bean.contains("mappedProperty", "Unknown Key")); } /** @@ -236,29 +201,11 @@ public class WrapDynaBeanTestCase extends BasicDynaBeanTestCase { @Test public void testMappedRemove() { - try { - assertTrue(bean.contains("mappedProperty", "First Key"), "Can see first key"); - bean.remove("mappedProperty", "First Key"); - fail("Should have thrown UnsupportedOperationException"); - // Assert.assertTrue("Can not see first key", - // !bean.contains("mappedProperty", "First Key")); - } catch (final UnsupportedOperationException t) { - // Expected result - } catch (final Throwable t) { - fail("Exception: " + t); - } + assertThrows(UnsupportedOperationException.class, () -> bean.contains("mappedProperty", "First Key")); + assertThrows(UnsupportedOperationException.class, () -> bean.remove("mappedProperty", "First Key")); - try { - assertFalse(bean.contains("mappedProperty", "Unknown Key"), "Can not see unknown key"); - bean.remove("mappedProperty", "Unknown Key"); - fail("Should have thrown UnsupportedOperationException"); - // Assert.assertTrue("Can not see unknown key", - // !bean.contains("mappedProperty", "Unknown Key")); - } catch (final UnsupportedOperationException t) { - // Expected result - } catch (final Throwable t) { - fail("Exception: " + t); - } + assertThrows(UnsupportedOperationException.class, () -> bean.contains("mappedProperty", "Unknown Key")); + assertThrows(UnsupportedOperationException.class, () -> bean.remove("mappedProperty", "Unknown Key")); } diff --git a/src/test/java/org/apache/commons/beanutils2/bugs/Jira157TestCase.java b/src/test/java/org/apache/commons/beanutils2/bugs/Jira157TestCase.java index 27fbc4a3..b8db6b00 100644 --- a/src/test/java/org/apache/commons/beanutils2/bugs/Jira157TestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/bugs/Jira157TestCase.java @@ -18,7 +18,6 @@ package org.apache.commons.beanutils2.bugs; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.io.Serializable; import java.util.Map; @@ -94,15 +93,10 @@ public class Jira157TestCase { * See Jira issue# BEANUTILS-157. */ @Test - public void testIssue_BEANUTILS_157_BeanUtils_Describe_Bean() { + public void testIssue_BEANUTILS_157_BeanUtils_Describe_Bean() throws Exception { final Object bean = new FooBar(); Map<String, String> result = null; - try { - result = BeanUtils.describe(bean); - } catch (final Throwable t) { - LOG.error("Describe Bean: " + t.getMessage(), t); - fail("Describe Bean Threw exception: " + t); - } + result = BeanUtils.describe(bean); assertEquals(2, result.size(), "Check Size"); assertTrue(result.containsKey("class"), "Class"); assertTrue(result.containsKey("publicFoo"), "publicFoo Key"); @@ -115,7 +109,7 @@ public class Jira157TestCase { * See Jira issue# BEANUTILS-157. */ @Test - public void testIssue_BEANUTILS_157_BeanUtils_Describe_Interface() { + public void testIssue_BEANUTILS_157_BeanUtils_Describe_Interface() throws Exception { final Object bean = new XY() { @Override public String getX() { @@ -127,13 +121,7 @@ public class Jira157TestCase { return "y-value"; } }; - Map<String, String> result = null; - try { - result = BeanUtils.describe(bean); - } catch (final Throwable t) { - LOG.error("Describe Interface: " + t.getMessage(), t); - fail("Describe Interface Threw exception: " + t); - } + final Map<String, String> result = BeanUtils.describe(bean); assertEquals(3, result.size(), "Check Size"); assertTrue(result.containsKey("class"), "Class"); assertTrue(result.containsKey("x"), "X Key"); @@ -148,7 +136,7 @@ public class Jira157TestCase { * See Jira issue# BEANUTILS-157. */ @Test - public void testIssue_BEANUTILS_157_BeanUtils_Describe_Serializable() { + public void testIssue_BEANUTILS_157_BeanUtils_Describe_Serializable() throws Exception { final Object bean = new Serializable() { private static final long serialVersionUID = 1L; @@ -162,13 +150,7 @@ public class Jira157TestCase { return "y-value"; } }; - Map<String, String> result = null; - try { - result = BeanUtils.describe(bean); - } catch (final Throwable t) { - LOG.error("Describe Serializable: " + t.getMessage(), t); - fail("Describe Serializable Threw exception: " + t); - } + final Map<String, String> result = BeanUtils.describe(bean); assertEquals(1, result.size(), "Check Size"); assertTrue(result.containsKey("class"), "Class"); } diff --git a/src/test/java/org/apache/commons/beanutils2/bugs/Jira273TestCase.java b/src/test/java/org/apache/commons/beanutils2/bugs/Jira273TestCase.java index fdb05d6d..167814a2 100644 --- a/src/test/java/org/apache/commons/beanutils2/bugs/Jira273TestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/bugs/Jira273TestCase.java @@ -17,7 +17,6 @@ package org.apache.commons.beanutils2.bugs; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; import org.apache.commons.beanutils2.PropertyUtils; import org.apache.commons.beanutils2.bugs.other.Jira273BeanFactory; @@ -58,15 +57,9 @@ public class Jira273TestCase { * Test with an anonymous class that inherits a public method of a public class. */ @Test - public void testIssue_BEANUTILS_273_AnonymousNotOverridden() { + public void testIssue_BEANUTILS_273_AnonymousNotOverridden() throws Exception { final Object bean = Jira273BeanFactory.createAnonymousNotOverridden(); - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "beanValue"); - } catch (final Throwable t) { - LOG.error("AnonymousNotOverridden: " + t.getMessage(), t); - fail("AnonymousNotOverridden Threw exception: " + t); - } + Object result = PropertyUtils.getProperty(bean, "beanValue"); assertEquals("PublicBeanWithMethod", result); } @@ -74,15 +67,9 @@ public class Jira273TestCase { * Test with an anonymous class that overrides a public method of a public class. */ @Test - public void testIssue_BEANUTILS_273_AnonymousOverridden() { + public void testIssue_BEANUTILS_273_AnonymousOverridden() throws Exception { final Object bean = Jira273BeanFactory.createAnonymousOverridden(); - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "beanValue"); - } catch (final Throwable t) { - LOG.error("AnonymousOverridden: " + t.getMessage(), t); - fail("AnonymousOverridden Threw exception: " + t); - } + Object result = PropertyUtils.getProperty(bean, "beanValue"); assertEquals("AnonymousOverridden", result); } @@ -90,15 +77,9 @@ public class Jira273TestCase { * Test with an private class that inherits a public method of a "grand parent" public class. */ @Test - public void testIssue_BEANUTILS_273_PrivatePrivatePublicNotOverridden() { + public void testIssue_BEANUTILS_273_PrivatePrivatePublicNotOverridden() throws Exception { final Object bean = Jira273BeanFactory.createPrivatePrivatePublicNotOverridden(); - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "beanValue"); - } catch (final Throwable t) { - LOG.error("PrivatePrivatePublicNotOverridden: " + t.getMessage(), t); - fail("PrivatePrivatePublicNotOverridden Threw exception: " + t); - } + Object result = PropertyUtils.getProperty(bean, "beanValue"); assertEquals("PublicBeanWithMethod", result); } @@ -106,15 +87,9 @@ public class Jira273TestCase { * Test with an private class that overrides a public method of a "grand parent" public class. */ @Test - public void testIssue_BEANUTILS_273_PrivatePrivatePublicOverridden() { + public void testIssue_BEANUTILS_273_PrivatePrivatePublicOverridden() throws Exception { final Object bean = Jira273BeanFactory.createPrivatePrivatePublicOverridden(); - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "beanValue"); - } catch (final Throwable t) { - LOG.error("PrivatePrivatePublicOverridden: " + t.getMessage(), t); - fail("PrivatePrivatePublicOverridden Threw exception: " + t); - } + Object result = PropertyUtils.getProperty(bean, "beanValue"); assertEquals("PrivatePrivatePublicOverridden", result); } @@ -122,15 +97,9 @@ public class Jira273TestCase { * Test with an private class that inherits a public method of a public class. */ @Test - public void testIssue_BEANUTILS_273_PrivatePublicNotOverridden() { + public void testIssue_BEANUTILS_273_PrivatePublicNotOverridden() throws Exception { final Object bean = Jira273BeanFactory.createPrivatePublicNotOverridden(); - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "beanValue"); - } catch (final Throwable t) { - LOG.error("PrivatePublicNotOverridden: " + t.getMessage(), t); - fail("PrivatePublicNotOverridden Threw exception: " + t); - } + Object result = PropertyUtils.getProperty(bean, "beanValue"); assertEquals("PublicBeanWithMethod", result); } @@ -138,15 +107,9 @@ public class Jira273TestCase { * Test with an private class that overrides a public method of a public class. */ @Test - public void testIssue_BEANUTILS_273_PrivatePublicOverridden() { + public void testIssue_BEANUTILS_273_PrivatePublicOverridden() throws Exception { final Object bean = Jira273BeanFactory.createPrivatePublicOverridden(); - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "beanValue"); - } catch (final Throwable t) { - LOG.error("PrivatePublicOverridden: " + t.getMessage(), t); - fail("PrivatePublicOverridden Threw exception: " + t); - } + Object result = PropertyUtils.getProperty(bean, "beanValue"); assertEquals("PrivatePublicOverridden", result); } } diff --git a/src/test/java/org/apache/commons/beanutils2/bugs/Jira298TestCase.java b/src/test/java/org/apache/commons/beanutils2/bugs/Jira298TestCase.java index dfc47481..804bf737 100644 --- a/src/test/java/org/apache/commons/beanutils2/bugs/Jira298TestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/bugs/Jira298TestCase.java @@ -17,7 +17,6 @@ package org.apache.commons.beanutils2.bugs; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; import java.lang.reflect.Method; @@ -60,16 +59,11 @@ public class Jira298TestCase { * Test {@link MethodUtils#getAccessibleMethod(Class, Method)} */ @Test - public void testIssue_BEANUTILS_298_MethodUtils_getAccessibleMethod() { + public void testIssue_BEANUTILS_298_MethodUtils_getAccessibleMethod() throws Exception { final Object bean = Jira298BeanFactory.createImplX(); Object result = null; - try { - final Method m2 = MethodUtils.getAccessibleMethod(bean.getClass(), "getName", new Class[0]); - result = m2.invoke(bean); - } catch (final Throwable t) { - LOG.error("Failed: " + t.getMessage(), t); - fail("Threw exception: " + t); - } + final Method m2 = MethodUtils.getAccessibleMethod(bean.getClass(), "getName", new Class[0]); + result = m2.invoke(bean); assertEquals("BaseX name value", result); } @@ -77,15 +71,9 @@ public class Jira298TestCase { * Test {@link PropertyUtils#getProperty(Object, String)} */ @Test - public void testIssue_BEANUTILS_298_PropertyUtils_getProperty() { + public void testIssue_BEANUTILS_298_PropertyUtils_getProperty() throws Exception { final Object bean = Jira298BeanFactory.createImplX(); - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "name"); - } catch (final Throwable t) { - LOG.error("Failed: " + t.getMessage(), t); - fail("Threw exception: " + t); - } + Object result = PropertyUtils.getProperty(bean, "name"); assertEquals("BaseX name value", result); } @@ -93,15 +81,10 @@ public class Jira298TestCase { * Test {@link PropertyUtils#setProperty(Object, String, Object)} */ @Test - public void testIssue_BEANUTILS_298_PropertyUtils_setProperty() { + public void testIssue_BEANUTILS_298_PropertyUtils_setProperty() throws Exception { final Object bean = Jira298BeanFactory.createImplX(); assertEquals("BaseX name value", ((IX) bean).getName()); - try { - PropertyUtils.setProperty(bean, "name", "new name"); - } catch (final Throwable t) { - LOG.error("Failed: " + t.getMessage(), t); - fail("Threw exception: " + t); - } + PropertyUtils.setProperty(bean, "name", "new name"); assertEquals("new name", ((IX) bean).getName()); } } diff --git a/src/test/java/org/apache/commons/beanutils2/bugs/Jira339TestCase.java b/src/test/java/org/apache/commons/beanutils2/bugs/Jira339TestCase.java index 639d52f3..b7d46ef1 100644 --- a/src/test/java/org/apache/commons/beanutils2/bugs/Jira339TestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/bugs/Jira339TestCase.java @@ -17,7 +17,6 @@ package org.apache.commons.beanutils2.bugs; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.fail; import java.util.Comparator; import java.util.HashMap; @@ -86,17 +85,11 @@ public class Jira339TestCase { * Test {@link BeanUtils#populate(Object, Map)} */ @Test - public void testIssue_BEANUTILS_331_BeanUtilsBean_populate() { - + public void testIssue_BEANUTILS_331_BeanUtilsBean_populate() throws Exception { final TestBean bean = new TestBean(); - try { - final Map<String, Object> properties = new HashMap<>(); - properties.put("comparator", null); - BeanUtils.populate(bean, properties); - } catch (final Throwable t) { - LOG.error("Failed: " + t.getMessage(), t); - fail("Threw exception: " + t); - } + final Map<String, Object> properties = new HashMap<>(); + properties.put("comparator", null); + BeanUtils.populate(bean, properties); assertNull(bean.getComparator(), "TestBean comparator should be null"); } @@ -104,15 +97,9 @@ public class Jira339TestCase { * Test {@link PropertyUtils#setProperty(Object, String, Object)} */ @Test - public void testIssue_BEANUTILS_339_BeanUtilsBean_setProperty() { - + public void testIssue_BEANUTILS_339_BeanUtilsBean_setProperty() throws Exception { final TestBean bean = new TestBean(); - try { - BeanUtils.setProperty(bean, "comparator", null); - } catch (final Throwable t) { - LOG.error("Failed: " + t.getMessage(), t); - fail("Threw exception: " + t); - } + BeanUtils.setProperty(bean, "comparator", null); assertNull(bean.getComparator(), "TestBean comparator should be null"); } }