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 14feb752 Don't hide stack traces from JUnit! 14feb752 is described below commit 14feb75241aa3e6040ae98bb9f6832fb88e159f3 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Sep 2 08:38:23 2024 -0400 Don't hide stack traces from JUnit! - Use assertThrows --- .../commons/beanutils2/BeanUtilsBean2TestCase.java | 8 +- .../commons/beanutils2/BeanUtilsBeanTestCase.java | 170 ++--- .../commons/beanutils2/DynaBeanUtilsTestCase.java | 87 +-- .../beanutils2/DynaPropertyUtilsTestCase.java | 783 +++++---------------- .../commons/beanutils2/LazyDynaBeanTestCase.java | 190 ++--- .../commons/beanutils2/LazyDynaClassTestCase.java | 43 +- .../commons/beanutils2/LazyDynaListTestCase.java | 22 +- 7 files changed, 306 insertions(+), 997 deletions(-) diff --git a/src/test/java/org/apache/commons/beanutils2/BeanUtilsBean2TestCase.java b/src/test/java/org/apache/commons/beanutils2/BeanUtilsBean2TestCase.java index bd8b7000..dff26b7c 100644 --- a/src/test/java/org/apache/commons/beanutils2/BeanUtilsBean2TestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/BeanUtilsBean2TestCase.java @@ -53,12 +53,8 @@ public class BeanUtilsBean2TestCase extends BeanUtilsBeanTestCase { */ @Override @Test - public void testCopyPropertyConvertToString() { - try { - BeanUtils.copyProperty(bean, "stringProperty", testUtilDate); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testCopyPropertyConvertToString() throws Exception { + BeanUtils.copyProperty(bean, "stringProperty", testUtilDate); assertEquals(testStringDate, bean.getStringProperty(), "java.util.Date --> String"); } diff --git a/src/test/java/org/apache/commons/beanutils2/BeanUtilsBeanTestCase.java b/src/test/java/org/apache/commons/beanutils2/BeanUtilsBeanTestCase.java index 1ffc7fea..24222a1c 100644 --- a/src/test/java/org/apache/commons/beanutils2/BeanUtilsBeanTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/BeanUtilsBeanTestCase.java @@ -186,16 +186,12 @@ public class BeanUtilsBeanTestCase { * Test the copyProperties() method from a DynaBean. */ @Test - public void testCopyPropertiesDynaBean() { + public void testCopyPropertiesDynaBean() throws Exception { // Set up an origin bean with customized properties final DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass(); DynaBean orig = null; - try { - orig = dynaClass.newInstance(); - } catch (final Exception e) { - fail("newInstance(): " + e); - } + orig = dynaClass.newInstance(); orig.set("booleanProperty", Boolean.FALSE); orig.set("byteProperty", Byte.valueOf((byte) 111)); orig.set("doubleProperty", Double.valueOf(333.33)); @@ -208,11 +204,7 @@ public class BeanUtilsBeanTestCase { orig.set("stringProperty", "Custom string"); // Copy the origin bean to our destination test bean - try { - BeanUtils.copyProperties(bean, orig); - } catch (final Exception e) { - fail("Threw exception: " + e); - } + BeanUtils.copyProperties(bean, orig); // Validate the results for scalar properties assertEquals(false, bean.getBooleanProperty(), "Copied boolean property"); @@ -248,7 +240,7 @@ public class BeanUtilsBeanTestCase { * Test copyProperties() when the origin is a {@code Map}. */ @Test - public void testCopyPropertiesMap() { + public void testCopyPropertiesMap() throws Exception { final Map<String, Object> map = new HashMap<>(); map.put("booleanProperty", "false"); @@ -262,11 +254,7 @@ public class BeanUtilsBeanTestCase { map.put("shortProperty", "555"); map.put("stringProperty", "New String Property"); - try { - BeanUtils.copyProperties(bean, map); - } catch (final Throwable t) { - fail("Threw " + t.toString()); - } + BeanUtils.copyProperties(bean, map); // Scalar properties assertEquals(false, bean.getBooleanProperty(), "booleanProperty"); @@ -298,7 +286,7 @@ public class BeanUtilsBeanTestCase { * Test the copyProperties() method from a standard JavaBean. */ @Test - public void testCopyPropertiesStandard() { + public void testCopyPropertiesStandard() throws Exception { // Set up an origin bean with customized properties final TestBean orig = new TestBean(); @@ -314,11 +302,7 @@ public class BeanUtilsBeanTestCase { orig.setStringProperty("Custom string"); // Copy the origin bean to our destination test bean - try { - BeanUtils.copyProperties(bean, orig); - } catch (final Exception e) { - fail("Threw exception: " + e); - } + BeanUtils.copyProperties(bean, orig); // Validate the results for scalar properties assertEquals(false, bean.getBooleanProperty(), "Copied boolean property"); @@ -375,12 +359,8 @@ public class BeanUtilsBeanTestCase { * Test {@code copyProperty()} conversion. */ @Test - public void testCopyPropertyConvert() { - try { - BeanUtils.copyProperty(bean, "dateProperty", testCalendar); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testCopyPropertyConvert() throws Exception { + BeanUtils.copyProperty(bean, "dateProperty", testCalendar); assertEquals(testUtilDate, bean.getDateProperty(), "Calendar --> java.util.Date"); } @@ -388,12 +368,8 @@ public class BeanUtilsBeanTestCase { * Test {@code copyProperty()} converting from a String. */ @Test - public void testCopyPropertyConvertFromString() { - try { - BeanUtils.copyProperty(bean, "dateProperty", testStringDate); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testCopyPropertyConvertFromString() throws Exception { + BeanUtils.copyProperty(bean, "dateProperty", testStringDate); assertEquals(testUtilDate, bean.getDateProperty(), "String --> java.util.Date"); } @@ -401,12 +377,8 @@ public class BeanUtilsBeanTestCase { * Test {@code copyProperty()} converting to a String. */ @Test - public void testCopyPropertyConvertToString() { - try { - BeanUtils.copyProperty(bean, "stringProperty", testUtilDate); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testCopyPropertyConvertToString() throws Exception { + BeanUtils.copyProperty(bean, "stringProperty", testUtilDate); assertEquals(testUtilDate.toString(), bean.getStringProperty(), "java.util.Date --> String"); } @@ -414,13 +386,9 @@ public class BeanUtilsBeanTestCase { * Test {@code copyProperty()} converting to a String. */ @Test - public void testCopyPropertyConvertToStringArray() { - try { - bean.setStringArray(null); - BeanUtils.copyProperty(bean, "stringArray", new java.util.Date[] { testUtilDate }); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testCopyPropertyConvertToStringArray() throws Exception { + bean.setStringArray(null); + BeanUtils.copyProperty(bean, "stringArray", new java.util.Date[] { testUtilDate }); assertEquals(1, bean.getStringArray().length, "java.util.Date[] --> String[] length"); assertEquals(testUtilDate.toString(), bean.getStringArray()[0], "java.util.Date[] --> String[] value "); } @@ -429,13 +397,9 @@ public class BeanUtilsBeanTestCase { * Test {@code copyProperty()} converting to a String on indexed property */ @Test - public void testCopyPropertyConvertToStringIndexed() { - try { - bean.setStringArray(new String[1]); - BeanUtils.copyProperty(bean, "stringArray[0]", testUtilDate); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testCopyPropertyConvertToStringIndexed() throws Exception { + bean.setStringArray(new String[1]); + BeanUtils.copyProperty(bean, "stringArray[0]", testUtilDate); assertEquals(testUtilDate.toString(), bean.getStringArray()[0], "java.util.Date --> String[]"); } @@ -723,14 +687,10 @@ public class BeanUtilsBeanTestCase { * Test {@code getArrayProperty()} converting to a String. */ @Test - public void testGetArrayPropertyDate() { + public void testGetArrayPropertyDate() throws Exception { String[] value = null; - try { - bean.setDateArrayProperty(new java.util.Date[] { testUtilDate }); - value = BeanUtils.getArrayProperty(bean, "dateArrayProperty"); - } catch (final Throwable t) { - fail("Threw " + t); - } + bean.setDateArrayProperty(new java.util.Date[] { testUtilDate }); + value = BeanUtils.getArrayProperty(bean, "dateArrayProperty"); assertEquals(1, value.length, "java.util.Date[] --> String[] length"); assertEquals(testUtilDate.toString(), value[0], "java.util.Date[] --> String[] value "); } @@ -780,14 +740,10 @@ public class BeanUtilsBeanTestCase { * Test {@code getArrayProperty()} converting to a String. */ @Test - public void testGetIndexedPropertyDate() { + public void testGetIndexedPropertyDate() throws Exception { String value = null; - try { - bean.setDateArrayProperty(new java.util.Date[] { testUtilDate }); - value = BeanUtils.getIndexedProperty(bean, "dateArrayProperty[0]"); - } catch (final Throwable t) { - fail("Threw " + t); - } + bean.setDateArrayProperty(new java.util.Date[] { testUtilDate }); + value = BeanUtils.getIndexedProperty(bean, "dateArrayProperty[0]"); assertEquals(testUtilDate.toString(), value, "java.util.Date[0] --> String"); } @@ -830,23 +786,17 @@ public class BeanUtilsBeanTestCase { * Test {@code getSimpleProperty()} converting to a String. */ @Test - public void testGetSimplePropertyDate() { + public void testGetSimplePropertyDate() throws Exception { String value = null; - try { - bean.setDateProperty(testUtilDate); - value = BeanUtils.getSimpleProperty(bean, "dateProperty"); - } catch (final Throwable t) { - fail("Threw " + t); - } + bean.setDateProperty(testUtilDate); + value = BeanUtils.getSimpleProperty(bean, "dateProperty"); assertEquals(testUtilDate.toString(), value, "java.util.Date --> String"); } @Test public void testMappedProperty() throws Exception { final MappedPropertyTestBean bean = new MappedPropertyTestBean(); - BeanUtils.setProperty(bean, "mapproperty(this.that.the-other)", "some.dotty.value"); - assertEquals("some.dotty.value", bean.getMapproperty("this.that.the-other"), "Mapped property set correctly"); } @@ -1027,22 +977,16 @@ public class BeanUtilsBeanTestCase { /* Do nothing */ } // make sure that this conversion has no been registered in the other instance - try { - - bean.setBooleanProperty(false); - utilsTwo.setProperty(bean, "booleanProperty", "true"); - assertEquals(bean.getBooleanProperty(), true, "Set property failed (3)"); - - } catch (final PassTestException e) { - fail("Registered converter is used by other instances"); - } + bean.setBooleanProperty(false); + utilsTwo.setProperty(bean, "booleanProperty", "true"); + assertEquals(bean.getBooleanProperty(), true, "Set property failed (3)"); } /** * Test setting a value out of a mapped Map */ @Test - public void testSetMappedMap() { + public void testSetMappedMap() throws Exception { final TestBean bean = new TestBean(); final Map<String, Object> map = new HashMap<>(); map.put("sub-key-1", "sub-value-1"); @@ -1051,11 +995,7 @@ public class BeanUtilsBeanTestCase { bean.getMapProperty().put("mappedMap", map); assertEquals("sub-value-3", ((Map<?, ?>) bean.getMapProperty().get("mappedMap")).get("sub-key-3"), "BEFORE"); - try { - BeanUtils.setProperty(bean, "mapProperty(mappedMap)(sub-key-3)", "SUB-KEY-3-UPDATED"); - } catch (final Throwable t) { - fail("Threw " + t + ""); - } + BeanUtils.setProperty(bean, "mapProperty(mappedMap)(sub-key-3)", "SUB-KEY-3-UPDATED"); assertEquals("SUB-KEY-3-UPDATED", ((Map<?, ?>) bean.getMapProperty().get("mappedMap")).get("sub-key-3"), "AFTER"); } @@ -1084,12 +1024,8 @@ public class BeanUtilsBeanTestCase { * Test {@code setProperty()} conversion. */ @Test - public void testSetPropertyConvert() { - try { - BeanUtils.setProperty(bean, "dateProperty", testCalendar); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testSetPropertyConvert() throws Exception { + BeanUtils.setProperty(bean, "dateProperty", testCalendar); assertEquals(testUtilDate, bean.getDateProperty(), "Calendar --> java.util.Date"); } @@ -1097,12 +1033,8 @@ public class BeanUtilsBeanTestCase { * Test {@code setProperty()} converting from a String. */ @Test - public void testSetPropertyConvertFromString() { - try { - BeanUtils.setProperty(bean, "dateProperty", testStringDate); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testSetPropertyConvertFromString() throws Exception { + BeanUtils.setProperty(bean, "dateProperty", testStringDate); assertEquals(testUtilDate, bean.getDateProperty(), "String --> java.util.Date"); } @@ -1110,12 +1042,8 @@ public class BeanUtilsBeanTestCase { * Test {@code setProperty()} converting to a String. */ @Test - public void testSetPropertyConvertToString() { - try { - BeanUtils.setProperty(bean, "stringProperty", testUtilDate); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testSetPropertyConvertToString() throws Exception { + BeanUtils.setProperty(bean, "stringProperty", testUtilDate); assertEquals(testUtilDate.toString(), bean.getStringProperty(), "java.util.Date --> String"); } @@ -1123,13 +1051,9 @@ public class BeanUtilsBeanTestCase { * Test {@code setProperty()} converting to a String array. */ @Test - public void testSetPropertyConvertToStringArray() { - try { - bean.setStringArray(null); - BeanUtils.setProperty(bean, "stringArray", new java.util.Date[] { testUtilDate }); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testSetPropertyConvertToStringArray() throws Exception { + bean.setStringArray(null); + BeanUtils.setProperty(bean, "stringArray", new java.util.Date[] { testUtilDate }); assertEquals(1, bean.getStringArray().length, "java.util.Date[] --> String[] length"); assertEquals(testUtilDate.toString(), bean.getStringArray()[0], "java.util.Date[] --> String[] value "); } @@ -1138,13 +1062,9 @@ public class BeanUtilsBeanTestCase { * Test {@code setProperty()} converting to a String on indexed property */ @Test - public void testSetPropertyConvertToStringIndexed() { - try { - bean.setStringArray(new String[1]); - BeanUtils.setProperty(bean, "stringArray[0]", testUtilDate); - } catch (final Throwable t) { - fail("Threw " + t); - } + public void testSetPropertyConvertToStringIndexed() throws Exception { + bean.setStringArray(new String[1]); + BeanUtils.setProperty(bean, "stringArray[0]", testUtilDate); assertEquals(testUtilDate.toString(), bean.getStringArray()[0], "java.util.Date --> String[]"); } diff --git a/src/test/java/org/apache/commons/beanutils2/DynaBeanUtilsTestCase.java b/src/test/java/org/apache/commons/beanutils2/DynaBeanUtilsTestCase.java index 752f63d9..bcdfb910 100644 --- a/src/test/java/org/apache/commons/beanutils2/DynaBeanUtilsTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/DynaBeanUtilsTestCase.java @@ -23,7 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; import java.util.ArrayList; import java.util.HashMap; @@ -47,7 +46,7 @@ public class DynaBeanUtilsTestCase { final int[] intArray = {}; final String[] stringArray = {}; - final DynaClass dynaClass = new BasicDynaClass("TestDynaClass", null, new DynaProperty[] { new DynaProperty("booleanProperty", Boolean.TYPE), + return new BasicDynaClass("TestDynaClass", null, new DynaProperty[] { new DynaProperty("booleanProperty", Boolean.TYPE), new DynaProperty("booleanSecond", Boolean.TYPE), new DynaProperty("byteProperty", Byte.TYPE), new DynaProperty("doubleProperty", Double.TYPE), new DynaProperty("dupProperty", stringArray.getClass()), new DynaProperty("floatProperty", Float.TYPE), new DynaProperty("intArray", intArray.getClass()), new DynaProperty("intIndexed", intArray.getClass()), @@ -56,7 +55,6 @@ public class DynaBeanUtilsTestCase { new DynaProperty("nested", TestBean.class), new DynaProperty("nullProperty", String.class), new DynaProperty("shortProperty", Short.TYPE), new DynaProperty("stringArray", stringArray.getClass()), new DynaProperty("stringIndexed", stringArray.getClass()), new DynaProperty("stringProperty", String.class), }); - return dynaClass; } @@ -168,16 +166,11 @@ public class DynaBeanUtilsTestCase { * Test the cloneBean() method from a DynaBean. */ @Test - public void testCloneDynaBean() { + public void testCloneDynaBean() throws Exception { // Set up an origin bean with customized properties final DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass(); - DynaBean orig = null; - try { - orig = dynaClass.newInstance(); - } catch (final Exception e) { - fail("newInstance(): " + e); - } + final DynaBean orig = dynaClass.newInstance(); orig.set("booleanProperty", Boolean.FALSE); orig.set("byteProperty", Byte.valueOf((byte) 111)); orig.set("doubleProperty", Double.valueOf(333.33)); @@ -190,24 +183,15 @@ public class DynaBeanUtilsTestCase { orig.set("stringProperty", "Custom string"); // Copy the origin bean to our destination test bean - DynaBean clonedBean = null; - try { - clonedBean = (DynaBean) BeanUtils.cloneBean(orig); - } catch (final Exception e) { - fail("Threw exception: " + e); - } + final DynaBean clonedBean = (DynaBean) BeanUtils.cloneBean(orig); // Validate the results for scalar properties - assertEquals(false, ((Boolean) clonedBean.get("booleanProperty")).booleanValue(), - "Cloned boolean property"); - assertEquals((byte) 111, ((Byte) clonedBean.get("byteProperty")).byteValue(), - "Cloned byte property"); - assertEquals(333.33, ((Double) clonedBean.get("doubleProperty")).doubleValue(), 0.005, - "Cloned double property"); + assertEquals(false, ((Boolean) clonedBean.get("booleanProperty")).booleanValue(), "Cloned boolean property"); + assertEquals((byte) 111, ((Byte) clonedBean.get("byteProperty")).byteValue(), "Cloned byte property"); + assertEquals(333.33, ((Double) clonedBean.get("doubleProperty")).doubleValue(), 0.005, "Cloned double property"); assertEquals(333, ((Integer) clonedBean.get("intProperty")).intValue(), "Cloned int property"); assertEquals(3333, ((Long) clonedBean.get("longProperty")).longValue(), "Cloned long property"); - assertEquals((short) 33, ((Short) clonedBean.get("shortProperty")).shortValue(), - "Cloned short property"); + assertEquals((short) 33, ((Short) clonedBean.get("shortProperty")).shortValue(), "Cloned short property"); assertEquals("Custom string", (String) clonedBean.get("stringProperty"), "Cloned string property"); // Validate the results for array properties @@ -235,16 +219,11 @@ public class DynaBeanUtilsTestCase { * Test the copyProperties() method from a DynaBean. */ @Test - public void testCopyPropertiesDynaBean() { + public void testCopyPropertiesDynaBean() throws Exception { // Set up an origin bean with customized properties final DynaClass dynaClass = DynaBeanUtilsTestCase.createDynaClass(); - DynaBean orig = null; - try { - orig = dynaClass.newInstance(); - } catch (final Exception e) { - fail("newInstance(): " + e); - } + final DynaBean orig = dynaClass.newInstance(); orig.set("booleanProperty", Boolean.FALSE); orig.set("byteProperty", Byte.valueOf((byte) 111)); orig.set("doubleProperty", Double.valueOf(333.33)); @@ -257,18 +236,12 @@ public class DynaBeanUtilsTestCase { orig.set("stringProperty", "Custom string"); // Copy the origin bean to our destination test bean - try { - BeanUtils.copyProperties(bean, orig); - } catch (final Exception e) { - fail("Threw exception: " + e); - } + BeanUtils.copyProperties(bean, orig); // Validate the results for scalar properties - assertEquals(false, ((Boolean) bean.get("booleanProperty")).booleanValue(), - "Copied boolean property"); + assertEquals(false, ((Boolean) bean.get("booleanProperty")).booleanValue(), "Copied boolean property"); assertEquals((byte) 111, ((Byte) bean.get("byteProperty")).byteValue(), "Copied byte property"); - assertEquals(333.33, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005, - "Copied double property"); + assertEquals(333.33, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005, "Copied double property"); assertEquals(333, ((Integer) bean.get("intProperty")).intValue(), "Copied int property"); assertEquals(3333, ((Long) bean.get("longProperty")).longValue(), "Copied long property"); assertEquals((short) 33, ((Short) bean.get("shortProperty")).shortValue(), "Copied short property"); @@ -299,7 +272,7 @@ public class DynaBeanUtilsTestCase { * Test copyProperties() when the origin is a {@code Map}. */ @Test - public void testCopyPropertiesMap() { + public void testCopyPropertiesMap() throws Exception { final Map<String, Object> map = new HashMap<>(); map.put("booleanProperty", "false"); @@ -313,18 +286,13 @@ public class DynaBeanUtilsTestCase { map.put("shortProperty", "555"); map.put("stringProperty", "New String Property"); - try { - BeanUtils.copyProperties(bean, map); - } catch (final Throwable t) { - fail("Threw " + t.toString()); - } + BeanUtils.copyProperties(bean, map); // Scalar properties assertEquals(false, ((Boolean) bean.get("booleanProperty")).booleanValue(), "booleanProperty"); assertEquals((byte) 111, ((Byte) bean.get("byteProperty")).byteValue(), "byteProperty"); assertEquals(333.0, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005, "doubleProperty"); - assertEquals((float) 222.0, ((Float) bean.get("floatProperty")).floatValue(), (float) 0.005, - "floatProperty"); + assertEquals((float) 222.0, ((Float) bean.get("floatProperty")).floatValue(), (float) 0.005, "floatProperty"); assertEquals(111, ((Integer) bean.get("intProperty")).intValue(), "intProperty"); assertEquals(444, ((Long) bean.get("longProperty")).longValue(), "longProperty"); assertEquals((short) 555, ((Short) bean.get("shortProperty")).shortValue(), "shortProperty"); @@ -350,7 +318,7 @@ public class DynaBeanUtilsTestCase { * Test the copyProperties() method from a standard JavaBean. */ @Test - public void testCopyPropertiesStandard() { + public void testCopyPropertiesStandard() throws Exception { // Set up an origin bean with customized properties final TestBean orig = new TestBean(); @@ -366,18 +334,12 @@ public class DynaBeanUtilsTestCase { orig.setStringProperty("Custom string"); // Copy the origin bean to our destination test bean - try { - BeanUtils.copyProperties(bean, orig); - } catch (final Exception e) { - fail("Threw exception: " + e); - } + BeanUtils.copyProperties(bean, orig); // Validate the results for scalar properties - assertEquals(false, ((Boolean) bean.get("booleanProperty")).booleanValue(), - "Copied boolean property"); + assertEquals(false, ((Boolean) bean.get("booleanProperty")).booleanValue(), "Copied boolean property"); assertEquals((byte) 111, ((Byte) bean.get("byteProperty")).byteValue(), "Copied byte property"); - assertEquals(333.33, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005, - "Copied double property"); + assertEquals(333.33, ((Double) bean.get("doubleProperty")).doubleValue(), 0.005, "Copied double property"); assertEquals(333, ((Integer) bean.get("intProperty")).intValue(), "Copied int property"); assertEquals(3333, ((Long) bean.get("longProperty")).longValue(), "Copied long property"); assertEquals((short) 33, ((Short) bean.get("shortProperty")).shortValue(), "Copied short property"); @@ -635,14 +597,9 @@ public class DynaBeanUtilsTestCase { * Test the describe() method. */ @Test - public void testDescribe() { + public void testDescribe() throws Exception { - Map<String, Object> map = null; - try { - map = PropertyUtils.describe(bean); - } catch (final Exception e) { - fail("Threw exception " + e); - } + final Map<String, Object> map = PropertyUtils.describe(bean); // Verify existence of all the properties that should be present for (final String describe : describes) { diff --git a/src/test/java/org/apache/commons/beanutils2/DynaPropertyUtilsTestCase.java b/src/test/java/org/apache/commons/beanutils2/DynaPropertyUtilsTestCase.java index 2ea8aa34..b4c48222 100644 --- a/src/test/java/org/apache/commons/beanutils2/DynaPropertyUtilsTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/DynaPropertyUtilsTestCase.java @@ -24,7 +24,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; 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.util.ArrayList; import java.util.HashMap; @@ -64,7 +63,7 @@ public class DynaPropertyUtilsTestCase { protected DynaClass createDynaClass() { final int[] intArray = {}; final String[] stringArray = {}; - final DynaClass dynaClass = new BasicDynaClass("TestDynaClass", null, new DynaProperty[] { new DynaProperty("booleanProperty", Boolean.TYPE), + return new BasicDynaClass("TestDynaClass", null, new DynaProperty[] { new DynaProperty("booleanProperty", Boolean.TYPE), new DynaProperty("booleanSecond", Boolean.TYPE), new DynaProperty("doubleProperty", Double.TYPE), new DynaProperty("dupProperty", stringArray.getClass()), new DynaProperty("floatProperty", Float.TYPE), new DynaProperty("intArray", intArray.getClass()), new DynaProperty("intIndexed", intArray.getClass()), @@ -73,7 +72,6 @@ public class DynaPropertyUtilsTestCase { new DynaProperty("mappedIntProperty", Map.class), new DynaProperty("nested", TestBean.class), new DynaProperty("nullProperty", String.class), new DynaProperty("shortProperty", Short.TYPE), new DynaProperty("stringArray", stringArray.getClass()), new DynaProperty("stringIndexed", stringArray.getClass()), new DynaProperty("stringProperty", String.class), }); - return dynaClass; } /** @@ -143,7 +141,7 @@ public class DynaPropertyUtilsTestCase { * Test copyProperties() when the origin is a {@code Map}. */ @Test - public void testCopyPropertiesMap() { + public void testCopyPropertiesMap() throws Exception { final Map<String, Object> map = new HashMap<>(); map.put("booleanProperty", Boolean.FALSE); map.put("doubleProperty", Double.valueOf(333.0)); @@ -154,11 +152,7 @@ public class DynaPropertyUtilsTestCase { map.put("longProperty", Long.valueOf(444)); map.put("shortProperty", Short.valueOf((short) 555)); map.put("stringProperty", "New String Property"); - try { - PropertyUtils.copyProperties(bean, map); - } catch (final Throwable t) { - fail("Threw " + t.toString()); - } + PropertyUtils.copyProperties(bean, map); // Scalar properties assertEquals(false, ((Boolean) bean.get("booleanProperty")).booleanValue(), "booleanProperty"); @@ -189,14 +183,9 @@ public class DynaPropertyUtilsTestCase { * Test the describe() method. */ @Test - public void testDescribe() { + public void testDescribe() throws Exception { - Map<String, Object> map = null; - try { - map = PropertyUtils.describe(bean); - } catch (final Exception e) { - fail("Threw exception " + e); - } + final Map<String, Object> map = PropertyUtils.describe(bean); // Verify existence of all the properties that should be present for (final String describe : describes) { @@ -240,204 +229,74 @@ public class DynaPropertyUtilsTestCase { * Positive and negative tests on getIndexedProperty valid arguments. */ @Test - public void testGetIndexedValues() { - + public void testGetIndexedValues() throws Exception { Object value = null; - // Use explicit key argument - for (int i = 0; i < 5; i++) { - - try { - value = PropertyUtils.getIndexedProperty(bean, "intArray", i); - assertNotNull(value, "intArray returned value " + i); - assertInstanceOf(Integer.class, value, "intArray returned Integer " + i); - assertEquals(i * 10, ((Integer) value).intValue(), "intArray returned correct " + i); - } catch (final Throwable t) { - fail("intArray " + i + " threw " + t); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "intIndexed", i); - assertNotNull(value, "intIndexed returned value " + i); - assertInstanceOf(Integer.class, value, "intIndexed returned Integer " + i); - assertEquals(i * 10, ((Integer) value).intValue(), "intIndexed returned correct " + i); - } catch (final Throwable t) { - fail("intIndexed " + i + " threw " + t); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "listIndexed", i); - assertNotNull(value, "listIndexed returned value " + i); - assertInstanceOf(String.class, value, "list returned String " + i); - assertEquals("String " + i, (String) value, "listIndexed returned correct " + i); - } catch (final Throwable t) { - fail("listIndexed " + i + " threw " + t); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "stringArray", i); - assertNotNull(value, "stringArray returned value " + i); - assertInstanceOf(String.class, value, "stringArray returned String " + i); - assertEquals("String " + i, (String) value, "stringArray returned correct " + i); - } catch (final Throwable t) { - fail("stringArray " + i + " threw " + t); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "stringIndexed", i); - assertNotNull(value, "stringIndexed returned value " + i); - assertInstanceOf(String.class, value, "stringIndexed returned String " + i); - assertEquals("String " + i, (String) value, "stringIndexed returned correct " + i); - } catch (final Throwable t) { - fail("stringIndexed " + i + " threw " + t); - } - + value = PropertyUtils.getIndexedProperty(bean, "intArray", i); + assertNotNull(value, "intArray returned value " + i); + assertInstanceOf(Integer.class, value, "intArray returned Integer " + i); + assertEquals(i * 10, ((Integer) value).intValue(), "intArray returned correct " + i); + + value = PropertyUtils.getIndexedProperty(bean, "intIndexed", i); + assertNotNull(value, "intIndexed returned value " + i); + assertInstanceOf(Integer.class, value, "intIndexed returned Integer " + i); + assertEquals(i * 10, ((Integer) value).intValue(), "intIndexed returned correct " + i); + + value = PropertyUtils.getIndexedProperty(bean, "listIndexed", i); + assertNotNull(value, "listIndexed returned value " + i); + assertInstanceOf(String.class, value, "list returned String " + i); + assertEquals("String " + i, (String) value, "listIndexed returned correct " + i); + + value = PropertyUtils.getIndexedProperty(bean, "stringArray", i); + assertNotNull(value, "stringArray returned value " + i); + assertInstanceOf(String.class, value, "stringArray returned String " + i); + assertEquals("String " + i, (String) value, "stringArray returned correct " + i); + + value = PropertyUtils.getIndexedProperty(bean, "stringIndexed", i); + assertNotNull(value, "stringIndexed returned value " + i); + assertInstanceOf(String.class, value, "stringIndexed returned String " + i); + assertEquals("String " + i, (String) value, "stringIndexed returned correct " + i); } - // Use key expression - for (int i = 0; i < 5; i++) { + value = PropertyUtils.getIndexedProperty(bean, "intArray[" + i + "]"); + assertNotNull(value, "intArray returned value " + i); + assertInstanceOf(Integer.class, value, "intArray returned Integer " + i); + assertEquals(i * 10, ((Integer) value).intValue(), "intArray returned correct " + i); - try { - value = PropertyUtils.getIndexedProperty(bean, "intArray[" + i + "]"); - assertNotNull(value, "intArray returned value " + i); - assertInstanceOf(Integer.class, value, "intArray returned Integer " + i); - assertEquals(i * 10, ((Integer) value).intValue(), "intArray returned correct " + i); - } catch (final Throwable t) { - fail("intArray " + i + " threw " + t); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "intIndexed[" + i + "]"); - assertNotNull(value, "intIndexed returned value " + i); - assertInstanceOf(Integer.class, value, "intIndexed returned Integer " + i); - assertEquals(i * 10, ((Integer) value).intValue(), "intIndexed returned correct " + i); - } catch (final Throwable t) { - fail("intIndexed " + i + " threw " + t); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "listIndexed[" + i + "]"); - assertNotNull(value, "listIndexed returned value " + i); - assertInstanceOf(String.class, value, "listIndexed returned String " + i); - assertEquals("String " + i, (String) value, "listIndexed returned correct " + i); - } catch (final Throwable t) { - fail("listIndexed " + i + " threw " + t); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "stringArray[" + i + "]"); - assertNotNull(value, "stringArray returned value " + i); - assertInstanceOf(String.class, value, "stringArray returned String " + i); - assertEquals("String " + i, (String) value, "stringArray returned correct " + i); - } catch (final Throwable t) { - fail("stringArray " + i + " threw " + t); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "stringIndexed[" + i + "]"); - assertNotNull(value, "stringIndexed returned value " + i); - assertInstanceOf(String.class, value, "stringIndexed returned String " + i); - assertEquals("String " + i, (String) value, "stringIndexed returned correct " + i); - } catch (final Throwable t) { - fail("stringIndexed " + i + " threw " + t); - } - - } - - // Index out of bounds tests + value = PropertyUtils.getIndexedProperty(bean, "intIndexed[" + i + "]"); + assertNotNull(value, "intIndexed returned value " + i); + assertInstanceOf(Integer.class, value, "intIndexed returned Integer " + i); + assertEquals(i * 10, ((Integer) value).intValue(), "intIndexed returned correct " + i); - try { - value = PropertyUtils.getIndexedProperty(bean, "intArray", -1); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } + value = PropertyUtils.getIndexedProperty(bean, "listIndexed[" + i + "]"); + assertNotNull(value, "listIndexed returned value " + i); + assertInstanceOf(String.class, value, "listIndexed returned String " + i); + assertEquals("String " + i, (String) value, "listIndexed returned correct " + i); - try { - value = PropertyUtils.getIndexedProperty(bean, "intArray", 5); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } + value = PropertyUtils.getIndexedProperty(bean, "stringArray[" + i + "]"); + assertNotNull(value, "stringArray returned value " + i); + assertInstanceOf(String.class, value, "stringArray returned String " + i); + assertEquals("String " + i, (String) value, "stringArray returned correct " + i); - try { - value = PropertyUtils.getIndexedProperty(bean, "intIndexed", -1); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "intIndexed", 5); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "listIndexed", -1); - fail("Should have thrown IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of IndexOutOfBoundsException"); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "listIndexed", 5); - fail("Should have thrown IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of IndexOutOfBoundsException"); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "stringArray", -1); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "stringArray", 5); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "stringIndexed", -1); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - value = PropertyUtils.getIndexedProperty(bean, "stringIndexed", 5); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); + value = PropertyUtils.getIndexedProperty(bean, "stringIndexed[" + i + "]"); + assertNotNull(value, "stringIndexed returned value " + i); + assertInstanceOf(String.class, value, "stringIndexed returned String " + i); + assertEquals("String " + i, (String) value, "stringIndexed returned correct " + i); } + // Index out of bounds tests + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "intArray", -1)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "intArray", 5)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "intIndexed", -1)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "intIndexed", 5)); + assertThrows(IndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "listIndexed", -1)); + assertThrows(IndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "listIndexed", 5)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "stringArray", -1)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "stringArray", 5)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "stringIndexed", -1)); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.getIndexedProperty(bean, "stringIndexed", 5)); } /** @@ -460,137 +319,68 @@ public class DynaPropertyUtilsTestCase { * Test getting mapped values with periods in the key. */ @Test - public void testGetMappedPeriods() { - + public void testGetMappedPeriods() throws Exception { bean.set("mappedProperty", "key.with.a.dot", "Special Value"); assertEquals("Special Value", (String) bean.get("mappedProperty", "key.with.a.dot"), "Can retrieve directly"); - try { - assertEquals("Special Value", PropertyUtils.getMappedProperty(bean, "mappedProperty", "key.with.a.dot"), "Can retrieve via getMappedProperty"); - } catch (final Exception e) { - fail("Thew exception: " + e); - } - try { - assertEquals("Special Value", PropertyUtils.getNestedProperty(bean, "mappedProperty(key.with.a.dot)"), "Can retrieve via getNestedProperty"); - } catch (final Exception e) { - fail("Thew exception: " + e); - } + assertEquals("Special Value", PropertyUtils.getMappedProperty(bean, "mappedProperty", "key.with.a.dot"), "Can retrieve via getMappedProperty"); + assertEquals("Special Value", PropertyUtils.getNestedProperty(bean, "mappedProperty(key.with.a.dot)"), "Can retrieve via getNestedProperty"); bean.set("mappedObjects", "nested.property", new TestBean()); assertNotNull(bean.get("mappedObjects", "nested.property"), "Can retrieve directly"); - try { - assertEquals("This is a string", PropertyUtils.getNestedProperty(bean, "mappedObjects(nested.property).stringProperty"), "Can retrieve nested"); - } catch (final Exception e) { - fail("Thew exception: " + e); - } - + assertEquals("This is a string", PropertyUtils.getNestedProperty(bean, "mappedObjects(nested.property).stringProperty"), "Can retrieve nested"); } /** * Test getting mapped values with slashes in the key. This is different from periods because slashes are not syntactically significant. */ @Test - public void testGetMappedSlashes() { + public void testGetMappedSlashes() throws Exception { bean.set("mappedProperty", "key/with/a/slash", "Special Value"); assertEquals("Special Value", bean.get("mappedProperty", "key/with/a/slash"), "Can retrieve directly"); - try { - assertEquals("Special Value", PropertyUtils.getMappedProperty(bean, "mappedProperty", "key/with/a/slash"), "Can retrieve via getMappedProperty"); - } catch (final Exception e) { - fail("Thew exception: " + e); - } - try { - assertEquals("Special Value", PropertyUtils.getNestedProperty(bean, "mappedProperty(key/with/a/slash)"), "Can retrieve via getNestedProperty"); - } catch (final Exception e) { - fail("Thew exception: " + e); - } - + assertEquals("Special Value", PropertyUtils.getMappedProperty(bean, "mappedProperty", "key/with/a/slash"), "Can retrieve via getMappedProperty"); + assertEquals("Special Value", PropertyUtils.getNestedProperty(bean, "mappedProperty(key/with/a/slash)"), "Can retrieve via getNestedProperty"); bean.set("mappedObjects", "nested/property", new TestBean()); assertNotNull(bean.get("mappedObjects", "nested/property"), "Can retrieve directly"); - try { - assertEquals("This is a string", PropertyUtils.getNestedProperty(bean, "mappedObjects(nested/property).stringProperty"), "Can retrieve nested"); - } catch (final Exception e) { - fail("Thew exception: " + e); - } - + assertEquals("This is a string", PropertyUtils.getNestedProperty(bean, "mappedObjects(nested/property).stringProperty"), "Can retrieve nested"); } /** * Positive and negative tests on getMappedProperty valid arguments. */ @Test - public void testGetMappedValues() { - + public void testGetMappedValues() throws Exception { Object value = null; - // Use explicit key argument + value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "First Key"); + assertEquals("First Value", value, "Can find first value"); - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "First Key"); - assertEquals("First Value", value, "Can find first value"); - } catch (final Throwable t) { - fail("Finding first value threw " + t); - } - - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "Second Key"); - assertEquals("Second Value", value, "Can find second value"); - } catch (final Throwable t) { - fail("Finding second value threw " + t); - } + value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "Second Key"); + assertEquals("Second Value", value, "Can find second value"); - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "Third Key"); - assertNull(value, "Can not find third value"); - } catch (final Throwable t) { - fail("Finding third value threw " + t); - } + value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "Third Key"); + assertNull(value, "Can not find third value"); // Use key expression with parentheses + value = PropertyUtils.getMappedProperty(bean, "mappedProperty(First Key)"); + assertEquals("First Value", value, "Can find first value"); - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty(First Key)"); - assertEquals("First Value", value, "Can find first value"); - } catch (final Throwable t) { - fail("Finding first value threw " + t); - } + value = PropertyUtils.getMappedProperty(bean, "mappedProperty(Second Key)"); + assertEquals("Second Value", value, "Can find second value"); - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty(Second Key)"); - assertEquals("Second Value", value, "Can find second value"); - } catch (final Throwable t) { - fail("Finding second value threw " + t); - } - - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty(Third Key)"); - assertNull(value, "Can not find third value"); - } catch (final Throwable t) { - fail("Finding third value threw " + t); - } + value = PropertyUtils.getMappedProperty(bean, "mappedProperty(Third Key)"); + assertNull(value, "Can not find third value"); // Use key expression with dotted syntax - try { - value = PropertyUtils.getNestedProperty(bean, "mapProperty.First Key"); - assertEquals("First Value", value, "Can find first value"); - } catch (final Throwable t) { - fail("Finding first value threw " + t); - } - - try { - value = PropertyUtils.getNestedProperty(bean, "mapProperty.Second Key"); - assertEquals("Second Value", value, "Can find second value"); - } catch (final Throwable t) { - fail("Finding second value threw " + t); - } + value = PropertyUtils.getNestedProperty(bean, "mapProperty.First Key"); + assertEquals("First Value", value, "Can find first value"); - try { - value = PropertyUtils.getNestedProperty(bean, "mapProperty.Third Key"); - assertNull(value, "Can not find third value"); - } catch (final Throwable t) { - fail("Finding third value threw " + t); - } + value = PropertyUtils.getNestedProperty(bean, "mapProperty.Second Key"); + assertEquals("Second Value", value, "Can find second value"); + value = PropertyUtils.getNestedProperty(bean, "mapProperty.Third Key"); + assertNull(value, "Can not find third value"); } /** @@ -783,13 +573,7 @@ public class DynaPropertyUtilsTestCase { */ @Test public void testGetSimpleNested() throws Exception { - try { - PropertyUtils.getSimpleProperty(bean, "nested.stringProperty"); - fail("Should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - // Correct result for this test - } - + assertThrows(IllegalArgumentException.class, () -> PropertyUtils.getSimpleProperty(bean, "nested.stringProperty")); } /** @@ -819,14 +603,7 @@ public class DynaPropertyUtilsTestCase { */ @Test public void testGetSimpleUnknown() throws Exception { - try { - PropertyUtils.getSimpleProperty(bean, "unknown"); - fail("Should have thrown NoSuchMethodException"); - } catch (final NoSuchMethodException e) { - // Correct result for this test - assertEquals("Unknown property 'unknown' on dynaclass '" + bean.getDynaClass() + "'", e.getMessage()); - } - + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.getSimpleProperty(bean, "unknown")); } /** @@ -854,206 +631,81 @@ public class DynaPropertyUtilsTestCase { * Positive and negative tests on setIndexedProperty valid arguments. */ @Test - public void testSetIndexedValues() { - + public void testSetIndexedValues() throws Exception { Object value = null; - // Use explicit index argument - - try { - PropertyUtils.setIndexedProperty(bean, "intArray", 0, Integer.valueOf(1)); - value = PropertyUtils.getIndexedProperty(bean, "intArray", 0); - assertNotNull(value, "Returned new value 0"); - assertInstanceOf(Integer.class, value, "Returned Integer new value 0"); - assertEquals(1, ((Integer) value).intValue(), "Returned correct new value 0"); - } catch (final Throwable t) { - fail("Threw " + t); - } - - try { - PropertyUtils.setIndexedProperty(bean, "intIndexed", 1, Integer.valueOf(11)); - value = PropertyUtils.getIndexedProperty(bean, "intIndexed", 1); - assertNotNull(value, "Returned new value 1"); - assertInstanceOf(Integer.class, value, "Returned Integer new value 1"); - assertEquals(11, ((Integer) value).intValue(), "Returned correct new value 1"); - } catch (final Throwable t) { - fail("Threw " + t); - } - - try { - PropertyUtils.setIndexedProperty(bean, "listIndexed", 2, "New Value 2"); - value = PropertyUtils.getIndexedProperty(bean, "listIndexed", 2); - assertNotNull(value, "Returned new value 2"); - assertInstanceOf(String.class, value, "Returned String new value 2"); - assertEquals("New Value 2", (String) value, "Returned correct new value 2"); - } catch (final Throwable t) { - fail("Threw " + t); - } - - try { - PropertyUtils.setIndexedProperty(bean, "stringArray", 2, "New Value 2"); - value = PropertyUtils.getIndexedProperty(bean, "stringArray", 2); - assertNotNull(value, "Returned new value 2"); - assertInstanceOf(String.class, value, "Returned String new value 2"); - assertEquals("New Value 2", (String) value, "Returned correct new value 2"); - } catch (final Throwable t) { - fail("Threw " + t); - } - - try { - PropertyUtils.setIndexedProperty(bean, "stringArray", 3, "New Value 3"); - value = PropertyUtils.getIndexedProperty(bean, "stringArray", 3); - assertNotNull(value, "Returned new value 3"); - assertInstanceOf(String.class, value, "Returned String new value 3"); - assertEquals("New Value 3", (String) value, "Returned correct new value 3"); - } catch (final Throwable t) { - fail("Threw " + t); - } + PropertyUtils.setIndexedProperty(bean, "intArray", 0, Integer.valueOf(1)); + value = PropertyUtils.getIndexedProperty(bean, "intArray", 0); + assertNotNull(value, "Returned new value 0"); + assertInstanceOf(Integer.class, value, "Returned Integer new value 0"); + assertEquals(1, ((Integer) value).intValue(), "Returned correct new value 0"); + + PropertyUtils.setIndexedProperty(bean, "intIndexed", 1, Integer.valueOf(11)); + value = PropertyUtils.getIndexedProperty(bean, "intIndexed", 1); + assertNotNull(value, "Returned new value 1"); + assertInstanceOf(Integer.class, value, "Returned Integer new value 1"); + assertEquals(11, ((Integer) value).intValue(), "Returned correct new value 1"); + + PropertyUtils.setIndexedProperty(bean, "listIndexed", 2, "New Value 2"); + value = PropertyUtils.getIndexedProperty(bean, "listIndexed", 2); + assertNotNull(value, "Returned new value 2"); + assertInstanceOf(String.class, value, "Returned String new value 2"); + assertEquals("New Value 2", (String) value, "Returned correct new value 2"); + + PropertyUtils.setIndexedProperty(bean, "stringArray", 2, "New Value 2"); + value = PropertyUtils.getIndexedProperty(bean, "stringArray", 2); + assertNotNull(value, "Returned new value 2"); + assertInstanceOf(String.class, value, "Returned String new value 2"); + assertEquals("New Value 2", (String) value, "Returned correct new value 2"); + + PropertyUtils.setIndexedProperty(bean, "stringArray", 3, "New Value 3"); + value = PropertyUtils.getIndexedProperty(bean, "stringArray", 3); + assertNotNull(value, "Returned new value 3"); + assertInstanceOf(String.class, value, "Returned String new value 3"); + assertEquals("New Value 3", (String) value, "Returned correct new value 3"); // Use index expression - - try { - PropertyUtils.setIndexedProperty(bean, "intArray[4]", Integer.valueOf(1)); - value = PropertyUtils.getIndexedProperty(bean, "intArray[4]"); - assertNotNull(value, "Returned new value 4"); - assertInstanceOf(Integer.class, value, "Returned Integer new value 4"); - assertEquals(1, ((Integer) value).intValue(), "Returned correct new value 4"); - } catch (final Throwable t) { - fail("Threw " + t); - } - - try { - PropertyUtils.setIndexedProperty(bean, "intIndexed[3]", Integer.valueOf(11)); - value = PropertyUtils.getIndexedProperty(bean, "intIndexed[3]"); - assertNotNull(value, "Returned new value 5"); - assertInstanceOf(Integer.class, value, "Returned Integer new value 5"); - assertEquals(11, ((Integer) value).intValue(), "Returned correct new value 5"); - } catch (final Throwable t) { - fail("Threw " + t); - } - - try { - PropertyUtils.setIndexedProperty(bean, "listIndexed[1]", "New Value 2"); - value = PropertyUtils.getIndexedProperty(bean, "listIndexed[1]"); - assertNotNull(value, "Returned new value 6"); - assertInstanceOf(String.class, value, "Returned String new value 6"); - assertEquals("New Value 2", (String) value, "Returned correct new value 6"); - } catch (final Throwable t) { - fail("Threw " + t); - } - - try { - PropertyUtils.setIndexedProperty(bean, "stringArray[1]", "New Value 2"); - value = PropertyUtils.getIndexedProperty(bean, "stringArray[2]"); - assertNotNull(value, "Returned new value 6"); - assertInstanceOf(String.class, value, "Returned String new value 6"); - assertEquals("New Value 2", (String) value, "Returned correct new value 6"); - } catch (final Throwable t) { - fail("Threw " + t); - } - - try { - PropertyUtils.setIndexedProperty(bean, "stringArray[0]", "New Value 3"); - value = PropertyUtils.getIndexedProperty(bean, "stringArray[0]"); - assertNotNull(value, "Returned new value 7"); - assertInstanceOf(String.class, value, "Returned String new value 7"); - assertEquals("New Value 3", (String) value, "Returned correct new value 7"); - } catch (final Throwable t) { - fail("Threw " + t); - } + PropertyUtils.setIndexedProperty(bean, "intArray[4]", Integer.valueOf(1)); + value = PropertyUtils.getIndexedProperty(bean, "intArray[4]"); + assertNotNull(value, "Returned new value 4"); + assertInstanceOf(Integer.class, value, "Returned Integer new value 4"); + assertEquals(1, ((Integer) value).intValue(), "Returned correct new value 4"); + + PropertyUtils.setIndexedProperty(bean, "intIndexed[3]", Integer.valueOf(11)); + value = PropertyUtils.getIndexedProperty(bean, "intIndexed[3]"); + assertNotNull(value, "Returned new value 5"); + assertInstanceOf(Integer.class, value, "Returned Integer new value 5"); + assertEquals(11, ((Integer) value).intValue(), "Returned correct new value 5"); + + PropertyUtils.setIndexedProperty(bean, "listIndexed[1]", "New Value 2"); + value = PropertyUtils.getIndexedProperty(bean, "listIndexed[1]"); + assertNotNull(value, "Returned new value 6"); + assertInstanceOf(String.class, value, "Returned String new value 6"); + assertEquals("New Value 2", (String) value, "Returned correct new value 6"); + + PropertyUtils.setIndexedProperty(bean, "stringArray[1]", "New Value 2"); + value = PropertyUtils.getIndexedProperty(bean, "stringArray[2]"); + assertNotNull(value, "Returned new value 6"); + assertInstanceOf(String.class, value, "Returned String new value 6"); + assertEquals("New Value 2", (String) value, "Returned correct new value 6"); + + PropertyUtils.setIndexedProperty(bean, "stringArray[0]", "New Value 3"); + value = PropertyUtils.getIndexedProperty(bean, "stringArray[0]"); + assertNotNull(value, "Returned new value 7"); + assertInstanceOf(String.class, value, "Returned String new value 7"); + assertEquals("New Value 3", (String) value, "Returned correct new value 7"); // Index out of bounds tests - - try { - PropertyUtils.setIndexedProperty(bean, "intArray", -1, Integer.valueOf(0)); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "intArray", 5, Integer.valueOf(0)); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "intIndexed", -1, Integer.valueOf(0)); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "intIndexed", 5, Integer.valueOf(0)); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "listIndexed", 5, "New String"); - fail("Should have thrown IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of IndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "listIndexed", -1, "New String"); - fail("Should have thrown IndexOutOfBoundsException"); - } catch (final IndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of IndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "stringArray", -1, "New String"); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "stringArray", 5, "New String"); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "stringIndexed", -1, "New String"); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - - try { - PropertyUtils.setIndexedProperty(bean, "stringIndexed", 5, "New String"); - fail("Should have thrown ArrayIndexOutOfBoundsException"); - } catch (final ArrayIndexOutOfBoundsException t) { - // Expected results - } catch (final Throwable t) { - fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException"); - } - + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "intArray", -1, Integer.valueOf(0))); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "intArray", 5, Integer.valueOf(0))); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "intIndexed", -1, Integer.valueOf(0))); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "intIndexed", 5, Integer.valueOf(0))); + assertThrows(IndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "listIndexed", 5, "New String")); + assertThrows(IndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "listIndexed", -1, "New String")); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "stringArray", -1, "New String")); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "stringArray", 5, "New String")); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "stringIndexed", -1, "New String")); + assertThrows(ArrayIndexOutOfBoundsException.class, () -> PropertyUtils.setIndexedProperty(bean, "stringIndexed", 5, "New String")); } /** @@ -1075,76 +727,35 @@ public class DynaPropertyUtilsTestCase { * Positive and negative tests on setMappedProperty valid arguments. */ @Test - public void testSetMappedValues() { - + public void testSetMappedValues() throws Exception { Object value = null; - // Use explicit key argument + value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "Fourth Key"); + assertNull(value, "Can not find fourth value"); - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "Fourth Key"); - assertNull(value, "Can not find fourth value"); - } catch (final Throwable t) { - fail("Finding fourth value threw " + t); - } + PropertyUtils.setMappedProperty(bean, "mappedProperty", "Fourth Key", "Fourth Value"); - try { - PropertyUtils.setMappedProperty(bean, "mappedProperty", "Fourth Key", "Fourth Value"); - } catch (final Throwable t) { - fail("Setting fourth value threw " + t); - } - - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "Fourth Key"); - assertEquals("Fourth Value", value, "Can find fourth value"); - } catch (final Throwable t) { - fail("Finding fourth value threw " + t); - } + value = PropertyUtils.getMappedProperty(bean, "mappedProperty", "Fourth Key"); + assertEquals("Fourth Value", value, "Can find fourth value"); // Use key expression with parentheses + value = PropertyUtils.getMappedProperty(bean, "mappedProperty(Fifth Key)"); + assertNull(value, "Can not find fifth value"); - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty(Fifth Key)"); - assertNull(value, "Can not find fifth value"); - } catch (final Throwable t) { - fail("Finding fifth value threw " + t); - } - - try { - PropertyUtils.setMappedProperty(bean, "mappedProperty(Fifth Key)", "Fifth Value"); - } catch (final Throwable t) { - fail("Setting fifth value threw " + t); - } + PropertyUtils.setMappedProperty(bean, "mappedProperty(Fifth Key)", "Fifth Value"); - try { - value = PropertyUtils.getMappedProperty(bean, "mappedProperty(Fifth Key)"); - assertEquals("Fifth Value", value, "Can find fifth value"); - } catch (final Throwable t) { - fail("Finding fifth value threw " + t); - } + value = PropertyUtils.getMappedProperty(bean, "mappedProperty(Fifth Key)"); + assertEquals("Fifth Value", value, "Can find fifth value"); // Use key expression with dotted expression - try { - value = PropertyUtils.getNestedProperty(bean, "mapProperty.Sixth Key"); - assertNull(value, "Can not find sixth value"); - } catch (final Throwable t) { - fail("Finding fifth value threw " + t); - } - - try { - PropertyUtils.setNestedProperty(bean, "mapProperty.Sixth Key", "Sixth Value"); - } catch (final Throwable t) { - fail("Setting sixth value threw " + t); - } + value = PropertyUtils.getNestedProperty(bean, "mapProperty.Sixth Key"); + assertNull(value, "Can not find sixth value"); - try { - value = PropertyUtils.getNestedProperty(bean, "mapProperty.Sixth Key"); - assertEquals("Sixth Value", value, "Can find sixth value"); - } catch (final Throwable t) { - fail("Finding sixth value threw " + t); - } + PropertyUtils.setNestedProperty(bean, "mapProperty.Sixth Key", "Sixth Value"); + value = PropertyUtils.getNestedProperty(bean, "mapProperty.Sixth Key"); + assertEquals("Sixth Value", value, "Can find sixth value"); } /** @@ -1216,15 +827,8 @@ public class DynaPropertyUtilsTestCase { */ @Test public void testSetNestedReadOnly() throws Exception { - try { - final String oldValue = nested.getWriteOnlyPropertyValue(); - final String newValue = oldValue + " Extra Value"; - PropertyUtils.setNestedProperty(bean, "nested.readOnlyProperty", newValue); - fail("Should have thrown NoSuchMethodException"); - } catch (final NoSuchMethodException e) { - // Correct result for this test - } - + assertThrows(NoSuchMethodException.class, + () -> PropertyUtils.setNestedProperty(bean, "nested.readOnlyProperty", nested.getWriteOnlyPropertyValue() + " Extra Value")); } /** @@ -1255,14 +859,7 @@ public class DynaPropertyUtilsTestCase { */ @Test public void testSetNestedUnknown() throws Exception { - try { - final String newValue = "New String Value"; - PropertyUtils.setNestedProperty(bean, "nested.unknown", newValue); - fail("Should have thrown NoSuchMethodException"); - } catch (final NoSuchMethodException e) { - // Correct result for this test - } - + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.setNestedProperty(bean, "nested.unknown", "New String Value")); } /** @@ -1323,12 +920,7 @@ public class DynaPropertyUtilsTestCase { */ @Test public void testSetSimpleIndexed() throws Exception { - try { - PropertyUtils.setSimpleProperty(bean, "stringIndexed[0]", "New String Value"); - fail("Should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - // Correct result for this test - } + assertThrows(IllegalArgumentException.class, () -> PropertyUtils.setSimpleProperty(bean, "stringIndexed[0]", "New String Value")); } /** @@ -1358,12 +950,7 @@ public class DynaPropertyUtilsTestCase { */ @Test public void testSetSimpleNested() throws Exception { - try { - PropertyUtils.setSimpleProperty(bean, "nested.stringProperty", "New String Value"); - fail("Should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException e) { - // Correct result for this test - } + assertThrows(IllegalArgumentException.class, () -> PropertyUtils.setSimpleProperty(bean, "nested.stringProperty", "New String Value")); } /** @@ -1394,15 +981,7 @@ public class DynaPropertyUtilsTestCase { */ @Test public void testSetSimpleUnknown() throws Exception { - try { - final String newValue = "New String Value"; - PropertyUtils.setSimpleProperty(bean, "unknown", newValue); - fail("Should have thrown NoSuchMethodException"); - } catch (final NoSuchMethodException e) { - // Correct result for this test - assertEquals("Unknown property 'unknown' on dynaclass '" + bean.getDynaClass() + "'", e.getMessage()); - } - + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.setSimpleProperty(bean, "unknown", "New String Value")); } } diff --git a/src/test/java/org/apache/commons/beanutils2/LazyDynaBeanTestCase.java b/src/test/java/org/apache/commons/beanutils2/LazyDynaBeanTestCase.java index f2635959..c38e114b 100644 --- a/src/test/java/org/apache/commons/beanutils2/LazyDynaBeanTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/LazyDynaBeanTestCase.java @@ -20,10 +20,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +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.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; @@ -86,23 +85,19 @@ public class LazyDynaBeanTestCase { // Add a DynaProperty of type String[] dynaClass.add(testProperty, objectArray.getClass()); - assertEquals(objectArray.getClass(), dynaClass.getDynaProperty(testProperty).getType(), - "Check Indexed Property exists"); - assertEquals(objectArray.getClass(), bean.get(testProperty).getClass(), - "Check Indexed Property is correct type"); + assertEquals(objectArray.getClass(), dynaClass.getDynaProperty(testProperty).getType(), "Check Indexed Property exists"); + assertEquals(objectArray.getClass(), bean.get(testProperty).getClass(), "Check Indexed Property is correct type"); // Retrieving from Array should initialize DynaBean for (int i = index; i >= 0; i--) { - assertEquals(LazyDynaMap.class, bean.get(testProperty, index).getClass(), - "Check Array Components initialized"); + assertEquals(LazyDynaMap.class, bean.get(testProperty, index).getClass(), "Check Array Components initialized"); } dynaClass.add(testPropertyB, objectArray.getClass()); final LazyDynaMap newMap = new LazyDynaMap(); newMap.set(testPropertyB, testString2); bean.set(testPropertyA, index, newMap); - assertEquals(testString2, ((DynaBean) bean.get(testPropertyA, index)).get(testPropertyB), - "Check Indexed Value is correct(a)"); + assertEquals(testString2, ((DynaBean) bean.get(testPropertyA, index)).get(testPropertyB), "Check Indexed Value is correct(a)"); } @@ -114,12 +109,7 @@ public class LazyDynaBeanTestCase { final int index = 3; dynaClass.add(testProperty, String.class); assertFalse(dynaClass.getDynaProperty(testProperty).isIndexed(), "Check Property is not indexed"); - try { - bean.set(testProperty, index, testString1); - fail("set(property, index, value) should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - // expected result - } + assertThrows(IllegalArgumentException.class, () -> bean.set(testProperty, index, testString1)); } /** @@ -137,25 +127,20 @@ public class LazyDynaBeanTestCase { // Add a 'LinkedList' property to the DynaClass dynaClass.add(testProperty, LinkedList.class); assertTrue(dynaClass.getDynaProperty(testProperty).isIndexed(), "Check Property is indexed"); - assertEquals(LinkedList.class, dynaClass.getDynaProperty(testProperty).getType(), - "Check Property is correct type"); + assertEquals(LinkedList.class, dynaClass.getDynaProperty(testProperty).getType(), "Check Property is correct type"); assertEquals(LinkedList.class, bean.get(testProperty).getClass(), "Check Property type is correct"); // Set the property, should instantiate a new LinkedList and set appropriate indexed value bean.set(testProperty, index, testString1); assertEquals(LinkedList.class, bean.get(testProperty).getClass(), "Check Property type is correct"); assertEquals(testString1, bean.get(testProperty, index), "Check First Indexed Value is correct"); - assertEquals(Integer.valueOf(index + 1), - Integer.valueOf(((LinkedList<?>) bean.get(testProperty)).size()), - "Check First Array length is correct"); + assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((LinkedList<?>) bean.get(testProperty)).size()), "Check First Array length is correct"); // Set a second indexed value, should automatically grow the LinkedList and set appropriate indexed value index += 2; bean.set(testProperty, index, testInteger1); assertEquals(testInteger1, bean.get(testProperty, index), "Check Second Indexed Value is correct"); - assertEquals(Integer.valueOf(index + 1), - Integer.valueOf(((LinkedList<?>) bean.get(testProperty)).size()), - "Check Second Array length is correct"); + assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((LinkedList<?>) bean.get(testProperty)).size()), "Check Second Array length is correct"); } /** @@ -173,30 +158,23 @@ public class LazyDynaBeanTestCase { // Add a DynaProperty of type String[] dynaClass.add(testProperty, objectArray.getClass()); - assertEquals(objectArray.getClass(), dynaClass.getDynaProperty(testProperty).getType(), - "Check Indexed Property exists"); - assertEquals(objectArray.getClass(), bean.get(testProperty).getClass(), - "Check Indexed Property is correct type"); + assertEquals(objectArray.getClass(), dynaClass.getDynaProperty(testProperty).getType(), "Check Indexed Property exists"); + assertEquals(objectArray.getClass(), bean.get(testProperty).getClass(), "Check Indexed Property is correct type"); // Set an indexed value bean.set(testProperty, index, testString1); assertNotNull(bean.get(testProperty), "Check Indexed Property is not null"); - assertEquals(objectArray.getClass(), bean.get(testProperty).getClass(), - "Check Indexed Property is correct type"); + assertEquals(objectArray.getClass(), bean.get(testProperty).getClass(), "Check Indexed Property is correct type"); assertEquals(testString1, bean.get(testProperty, index), "Check First Indexed Value is correct(a)"); - assertEquals(testString1, ((String[]) bean.get(testProperty))[index], - "Check First Indexed Value is correct(b)"); - assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((String[]) bean.get(testProperty)).length), - "Check Array length is correct"); + assertEquals(testString1, ((String[]) bean.get(testProperty))[index], "Check First Indexed Value is correct(b)"); + assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((String[]) bean.get(testProperty)).length), "Check Array length is correct"); // Set a second indexed value, should automatically grow the String[] and set appropriate indexed value index += 2; bean.set(testProperty, index, testString2); assertEquals(testString2, bean.get(testProperty, index), "Check Second Indexed Value is correct(a)"); - assertEquals(testString2, ((String[]) bean.get(testProperty))[index], - "Check Second Indexed Value is correct(b)"); - assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((String[]) bean.get(testProperty)).length), - "Check Second Array length is correct"); + assertEquals(testString2, ((String[]) bean.get(testProperty))[index], "Check Second Indexed Value is correct(b)"); + assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((String[]) bean.get(testProperty)).length), "Check Second Array length is correct"); } /** @@ -214,31 +192,23 @@ public class LazyDynaBeanTestCase { // Add a DynaProperty of type int[] dynaClass.add(testProperty, primitiveArray.getClass()); - assertEquals(primitiveArray.getClass(), dynaClass.getDynaProperty(testProperty).getType(), - "Check Indexed Property exists"); - assertEquals(primitiveArray.getClass(), bean.get(testProperty).getClass(), - "Check Indexed Property is correct type"); + assertEquals(primitiveArray.getClass(), dynaClass.getDynaProperty(testProperty).getType(), "Check Indexed Property exists"); + assertEquals(primitiveArray.getClass(), bean.get(testProperty).getClass(), "Check Indexed Property is correct type"); // Set an indexed value bean.set(testProperty, index, testInteger1); assertNotNull(bean.get(testProperty), "Check Indexed Property is not null"); - assertEquals(primitiveArray.getClass(), bean.get(testProperty).getClass(), - "Check Indexed Property is correct type"); + assertEquals(primitiveArray.getClass(), bean.get(testProperty).getClass(), "Check Indexed Property is correct type"); assertEquals(testInteger1, bean.get(testProperty, index), "Check First Indexed Value is correct(a)"); - assertEquals(testInteger1, Integer.valueOf(((int[]) bean.get(testProperty))[index]), - "Check First Indexed Value is correct(b)"); - assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((int[]) bean.get(testProperty)).length), - "Check Array length is correct"); + assertEquals(testInteger1, Integer.valueOf(((int[]) bean.get(testProperty))[index]), "Check First Indexed Value is correct(b)"); + assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((int[]) bean.get(testProperty)).length), "Check Array length is correct"); // Set a second indexed value, should automatically grow the int[] and set appropriate indexed value index += 2; bean.set(testProperty, index, testInteger2); - assertEquals(testInteger2, bean.get(testProperty, index), - "Check Second Indexed Value is correct(a)"); - assertEquals(testInteger2, Integer.valueOf(((int[]) bean.get(testProperty))[index]), - "Check Second Indexed Value is correct(b)"); - assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((int[]) bean.get(testProperty)).length), - "Check Second Array length is correct"); + assertEquals(testInteger2, bean.get(testProperty, index), "Check Second Indexed Value is correct(a)"); + assertEquals(testInteger2, Integer.valueOf(((int[]) bean.get(testProperty))[index]), "Check Second Indexed Value is correct(b)"); + assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((int[]) bean.get(testProperty)).length), "Check Second Array length is correct"); } @@ -258,20 +228,15 @@ public class LazyDynaBeanTestCase { // Set the property, should create new ArrayList and set appropriate indexed value bean.set(testProperty, index, testInteger1); assertNotNull(bean.get(testProperty), "Check Indexed Property is not null"); - assertEquals(ArrayList.class, bean.get(testProperty).getClass(), - "Check Indexed Property is correct type"); + assertEquals(ArrayList.class, bean.get(testProperty).getClass(), "Check Indexed Property is correct type"); assertEquals(testInteger1, bean.get(testProperty, index), "Check First Indexed Value is correct"); - assertEquals(Integer.valueOf(index + 1), - Integer.valueOf(((ArrayList<?>) bean.get(testProperty)).size()), - "Check First Array length is correct"); + assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((ArrayList<?>) bean.get(testProperty)).size()), "Check First Array length is correct"); // Set a second indexed value, should automatically grow the ArrayList and set appropriate indexed value index += 2; bean.set(testProperty, index, testString1); assertEquals(testString1, bean.get(testProperty, index), "Check Second Indexed Value is correct"); - assertEquals(Integer.valueOf(index + 1), - Integer.valueOf(((ArrayList<?>) bean.get(testProperty)).size()), - "Check Second Array length is correct"); + assertEquals(Integer.valueOf(index + 1), Integer.valueOf(((ArrayList<?>) bean.get(testProperty)).size()), "Check Second Array length is correct"); } /** @@ -279,49 +244,30 @@ public class LazyDynaBeanTestCase { */ @Test public void testIndexedPropertyRestricted() { - final int index = 3; - // Set the MutableDyanClass to 'restricted' (i.e. no new properties cab be added dynaClass.setRestricted(true); assertTrue(dynaClass.isRestricted(), "Check MutableDynaClass is restricted"); - // Check the property & value doesn't exist assertNull(dynaClass.getDynaProperty(testProperty), "Check Property doesn't exist"); assertNull(bean.get(testProperty), "Check Value is null"); - // Set the property - should fail because property doesn't exist and MutableDynaClass is restricted - try { - bean.set(testProperty, index, testInteger1); - fail( - "expected IllegalArgumentException trying to add new property to restricted MutableDynaClass"); - } catch (final IllegalArgumentException expected) { - // expected result - } - + assertThrows(IllegalArgumentException.class, () -> bean.set(testProperty, index, testInteger1)); } /** * Test Setting an 'Indexed' Property using PropertyUtils */ @Test - public void testIndexedPropertyUtils() { - + public void testIndexedPropertyUtils() throws Exception { final int index = 3; dynaClass.setReturnNull(false); - // Check the property & value doesn't exist assertFalse(dynaClass.isDynaProperty(testProperty), "Check Indexed Property doesn't exist"); assertNull(bean.get(testProperty), "Check Indexed Property is null"); assertNull(bean.get(testProperty, index), "Check Indexed value is null"); - // Use PropertyUtils to set the indexed value - try { - PropertyUtils.setProperty(bean, testProperty + "[" + index + "]", testString1); - } catch (final NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { - fail("testIndexedPropertyUtils threw " + ex); - } - + PropertyUtils.setProperty(bean, testProperty + "[" + index + "]", testString1); // Check property value correctly set assertEquals(testString1, bean.get(testProperty, index), "Check Indexed Bean Value is correct"); @@ -334,12 +280,7 @@ public class LazyDynaBeanTestCase { public void testMappedInvalidType() { dynaClass.add(testProperty, String.class); assertFalse(dynaClass.getDynaProperty(testProperty).isMapped(), "Check Property is not mapped"); - try { - bean.set(testProperty, testKey, testInteger1); - fail("set(property, key, value) should have thrown IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - // expected result - } + assertThrows(IllegalArgumentException.class, () -> bean.set(testProperty, testKey, testInteger1)); } /** @@ -356,17 +297,13 @@ public class LazyDynaBeanTestCase { // Set a new mapped property - should add new HashMap property and set the mapped value bean.set(testProperty, testKey, testInteger1); assertEquals(HashMap.class, bean.get(testProperty).getClass(), "Check Mapped Property exists"); - assertEquals(testInteger1, bean.get(testProperty, testKey), - "Check First Mapped Value is correct(a)"); - assertEquals(testInteger1, ((HashMap<?, ?>) bean.get(testProperty)).get(testKey), - "Check First Mapped Value is correct(b)"); + assertEquals(testInteger1, bean.get(testProperty, testKey), "Check First Mapped Value is correct(a)"); + assertEquals(testInteger1, ((HashMap<?, ?>) bean.get(testProperty)).get(testKey), "Check First Mapped Value is correct(b)"); // Set the property again - should set the new value bean.set(testProperty, testKey, testInteger2); - assertEquals(testInteger2, bean.get(testProperty, testKey), - "Check Second Mapped Value is correct(a)"); - assertEquals(testInteger2, ((HashMap<?, ?>) bean.get(testProperty)).get(testKey), - "Check Second Mapped Value is correct(b)"); + assertEquals(testInteger2, bean.get(testProperty, testKey), "Check Second Mapped Value is correct(a)"); + assertEquals(testInteger2, ((HashMap<?, ?>) bean.get(testProperty)).get(testKey), "Check Second Mapped Value is correct(b)"); } /** @@ -374,24 +311,14 @@ public class LazyDynaBeanTestCase { */ @Test public void testMappedPropertyRestricted() { - // Set the MutableDyanClass to 'restricted' (i.e. no new properties cab be added dynaClass.setRestricted(true); assertTrue(dynaClass.isRestricted(), "Check MutableDynaClass is restricted"); - // Check the property & value doesn't exist assertNull(dynaClass.getDynaProperty(testProperty), "Check Property doesn't exist"); assertNull(bean.get(testProperty), "Check Value is null"); - // Set the property - should fail because property doesn't exist and MutableDynaClass is restricted - try { - bean.set(testProperty, testKey, testInteger1); - fail( - "expected IllegalArgumentException trying to add new property to restricted MutableDynaClass"); - } catch (final IllegalArgumentException expected) { - // expected result - } - + assertThrows(IllegalArgumentException.class, () -> bean.set(testProperty, testKey, testInteger1)); } /** @@ -399,39 +326,30 @@ public class LazyDynaBeanTestCase { */ @Test public void testMappedPropertyTreeMap() { - // Check the property & value doesn't exist assertNull(dynaClass.getDynaProperty(testProperty), "Check Mapped Property doesn't exist"); - // Add a 'TreeMap' property to the DynaClass dynaClass.add(testProperty, TreeMap.class); assertTrue(dynaClass.getDynaProperty(testProperty).isMapped(), "Check Property is mapped"); - assertEquals(TreeMap.class, dynaClass.getDynaProperty(testProperty).getType(), - "Check Property is correct type"); + assertEquals(TreeMap.class, dynaClass.getDynaProperty(testProperty).getType(), "Check Property is correct type"); assertEquals(TreeMap.class, bean.get(testProperty).getClass(), "Check Mapped Property exists"); // assertNull("Check mapped property is null", bean.get(testProperty)); - // Set a new mapped property - should instantiate a new TreeMap property and set the mapped value bean.set(testProperty, testKey, testInteger1); assertEquals(TreeMap.class, bean.get(testProperty).getClass(), "Check Mapped Property exists"); - assertEquals(testInteger1, bean.get(testProperty, testKey), - "Check First Mapped Value is correct(a)"); - assertEquals(testInteger1, ((TreeMap<?, ?>) bean.get(testProperty)).get(testKey), - "Check First Mapped Value is correct(b)"); - + assertEquals(testInteger1, bean.get(testProperty, testKey), "Check First Mapped Value is correct(a)"); + assertEquals(testInteger1, ((TreeMap<?, ?>) bean.get(testProperty)).get(testKey), "Check First Mapped Value is correct(b)"); // Set the property again - should set the new value bean.set(testProperty, testKey, testInteger2); - assertEquals(testInteger2, bean.get(testProperty, testKey), - "Check Second Mapped Value is correct(a)"); - assertEquals(testInteger2, ((TreeMap<?, ?>) bean.get(testProperty)).get(testKey), - "Check Second Mapped Value is correct(b)"); + assertEquals(testInteger2, bean.get(testProperty, testKey), "Check Second Mapped Value is correct(a)"); + assertEquals(testInteger2, ((TreeMap<?, ?>) bean.get(testProperty)).get(testKey), "Check Second Mapped Value is correct(b)"); } /** * Test Setting a 'Mapped' Property using PropertyUtils */ @Test - public void testMappedPropertyUtils() { + public void testMappedPropertyUtils() throws Exception { dynaClass.setReturnNull(false); @@ -441,11 +359,7 @@ public class LazyDynaBeanTestCase { assertNull(bean.get(testProperty, testKey), "Check Mapped Value is null"); // Set the mapped property using PropertyUtils - try { - PropertyUtils.setProperty(bean, testProperty + "(" + testKey + ")", testString1); - } catch (final NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { - fail("testIndexedPropertyUtils threw " + ex); - } + PropertyUtils.setProperty(bean, testProperty + "(" + testKey + ")", testString1); // Check property value correctly set assertEquals(testString1, bean.get(testProperty, testKey), "Check Mapped Bean Value is correct"); @@ -481,20 +395,14 @@ public class LazyDynaBeanTestCase { // Set a new property - should add new property and set value bean.set(testProperty, testInteger1); assertEquals(testInteger1, bean.get(testProperty), "Check First Value is correct"); - assertEquals(Integer.class, dynaClass.getDynaProperty(testProperty).getType(), - "Check Property type is correct"); + assertEquals(Integer.class, dynaClass.getDynaProperty(testProperty).getType(), "Check Property type is correct"); // Set the property again - should set the new value bean.set(testProperty, testInteger2); assertEquals(testInteger2, bean.get(testProperty), "Check Second Value is correct"); // Set the property again - with a different type, should fail - try { - bean.set(testProperty, testString1); - fail("expected ConversionException trying to set an Integer property to a String"); - } catch (final ConversionException expected) { - // expected result - } + assertThrows(ConversionException.class, () -> bean.set(testProperty, testString1)); } @@ -513,13 +421,7 @@ public class LazyDynaBeanTestCase { assertNull(bean.get(testProperty), "Check Value is null"); // Set the property - should fail because property doesn't exist and MutableDynaClass is restricted - try { - bean.set(testProperty, testString1); - fail("expected IllegalArgumentException trying to add new property to restricted DynaClass"); - } catch (final IllegalArgumentException expected) { - // expected result - } - + assertThrows(IllegalArgumentException.class, () -> bean.set(testProperty, testString1)); } } \ No newline at end of file diff --git a/src/test/java/org/apache/commons/beanutils2/LazyDynaClassTestCase.java b/src/test/java/org/apache/commons/beanutils2/LazyDynaClassTestCase.java index 81031d3e..49fbcf07 100644 --- a/src/test/java/org/apache/commons/beanutils2/LazyDynaClassTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/LazyDynaClassTestCase.java @@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; 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 org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -82,12 +81,7 @@ public class LazyDynaClassTestCase { */ @Test public void testAddProperty3() { - try { - dynaClass.add(testProperty, String.class, true, true); - fail("add(name, type, readable, writable) did not throw UnsupportedOperationException"); - } catch (final UnsupportedOperationException expected) { - // expected result - } + assertThrows(UnsupportedOperationException.class, () -> dynaClass.add(testProperty, String.class, true, true)); } /** @@ -111,12 +105,7 @@ public class LazyDynaClassTestCase { */ @Test public void testAddPropertyNullName3() { - try { - dynaClass.add(null, String.class, true, true); - fail("add(name, type, readable, writable) did not throw UnsupportedOperationException"); - } catch (final UnsupportedOperationException expected) { - // expected result - } + assertThrows(UnsupportedOperationException.class, () -> dynaClass.add(null, String.class, true, true)); } /** @@ -126,12 +115,7 @@ public class LazyDynaClassTestCase { public void testAddPropertyRestricted1() { dynaClass.setRestricted(true); assertTrue(dynaClass.isRestricted(), "MutableDynaClass is restricted"); - try { - dynaClass.add(testProperty); - fail("add(name) did not throw IllegalStateException"); - } catch (final IllegalStateException expected) { - // expected result - } + assertThrows(IllegalStateException.class, () -> dynaClass.add(testProperty)); } /** @@ -141,12 +125,7 @@ public class LazyDynaClassTestCase { public void testAddPropertyRestricted2() { dynaClass.setRestricted(true); assertTrue(dynaClass.isRestricted(), "MutableDynaClass is restricted"); - try { - dynaClass.add(testProperty, String.class); - fail("add(name, type) did not throw IllegalStateException"); - } catch (final IllegalStateException expected) { - // expected result - } + assertThrows(IllegalStateException.class, () -> dynaClass.add(testProperty, String.class)); } /** @@ -156,12 +135,7 @@ public class LazyDynaClassTestCase { public void testAddPropertyRestricted3() { dynaClass.setRestricted(true); assertTrue(dynaClass.isRestricted(), "MutableDynaClass is restricted"); - try { - dynaClass.add(testProperty, String.class, true, true); - fail("add(name, type, readable, writable) did not throw UnsupportedOperationException"); - } catch (final UnsupportedOperationException t) { - // expected result - } + assertThrows(UnsupportedOperationException.class, () -> dynaClass.add(testProperty, String.class, true, true)); } /** @@ -228,11 +202,6 @@ public class LazyDynaClassTestCase { assertTrue(dynaClass.isDynaProperty(testProperty), "Property exists"); dynaClass.setRestricted(true); assertTrue(dynaClass.isRestricted(), "MutableDynaClass is restricted"); - try { - dynaClass.remove(testProperty); - fail("remove property when MutableDynaClassis restricted did not throw IllegalStateException"); - } catch (final IllegalStateException expected) { - // expected result - } + assertThrows(IllegalStateException.class, () -> dynaClass.remove(testProperty)); } } \ No newline at end of file diff --git a/src/test/java/org/apache/commons/beanutils2/LazyDynaListTestCase.java b/src/test/java/org/apache/commons/beanutils2/LazyDynaListTestCase.java index cd9908f2..3d45958f 100644 --- a/src/test/java/org/apache/commons/beanutils2/LazyDynaListTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/LazyDynaListTestCase.java @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import java.io.ByteArrayInputStream; @@ -84,12 +85,7 @@ public class LazyDynaListTestCase { assertEquals(4, dynaArray.length, "10. Array Size Wrong"); // Test fail if different type added - try { - list.add(2, wrongBean); - fail("Expected IllegalArgumentException"); - } catch (final IllegalArgumentException ignore) { - // expected result - } + assertThrows(IllegalArgumentException.class, () -> list.add(2, wrongBean)); // find a String property to set final String testProperty = findStringProperty(testDynaClass); @@ -197,12 +193,7 @@ public class LazyDynaListTestCase { assertEquals(3, mapArray.length, "14. Array Size Wrong"); // Test fail if different type added - try { - list.add(2, wrongBean); - fail("Expected IllegalArgumentException"); - } catch (final IllegalArgumentException ignore) { - // expected result - } + assertThrows(IllegalArgumentException.class, () -> list.add(2, wrongBean)); } @@ -246,12 +237,7 @@ public class LazyDynaListTestCase { assertEquals(2, pojoArray.length, "14. Array Size Wrong"); // Test fail if different type added - try { - list.add(2, wrongBean); - fail("Expected IllegalArgumentException"); - } catch (final IllegalArgumentException ignore) { - // expected result - } + assertThrows(IllegalArgumentException.class, () -> list.add(2, wrongBean)); }