From 732c154680b7356599bb0ad1c36544acafed924d Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Mon, 19 Jul 2021 12:55:53 +0200 Subject: [PATCH 1/2] test: update to junit5 --- pom.xml | 20 +- .../java/org/apache/commons/beanutils2/A.java | 34 -- .../beanutils2/MethodUtilsTestCase.java | 26 +- .../beanutils2/bugs/Jira18TestCase.java | 198 ++------- .../beanutils2/bugs/Jira61TestCase.java | 419 +++++------------- 5 files changed, 180 insertions(+), 517 deletions(-) delete mode 100644 src/test/java/org/apache/commons/beanutils2/A.java diff --git a/pom.xml b/pom.xml index ad76c5c70..240f8e18e 100644 --- a/pom.xml +++ b/pom.xml @@ -202,7 +202,7 @@ ggregory at apache.org https://www.garygregory.com The Apache Software Foundation - https://www.apache.org/ + https://www.apache.org/ PMC Member @@ -344,12 +344,24 @@ 3.2.1 test + + org.junit.jupiter + junit-jupiter + 5.7.2 + test + junit junit 4.13.2 test + + org.junit.vintage + junit-vintage-engine + 5.7.2 + test + @@ -522,9 +534,9 @@ - true diff --git a/src/test/java/org/apache/commons/beanutils2/A.java b/src/test/java/org/apache/commons/beanutils2/A.java deleted file mode 100644 index 627609935..000000000 --- a/src/test/java/org/apache/commons/beanutils2/A.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.commons.beanutils2; - -import java.io.OutputStream; - -/** - *

Class used in MethodUtils test

- * - */ -public class A { - - boolean called = false; - - public void foo(final OutputStream os) - { - called = true; - } -} diff --git a/src/test/java/org/apache/commons/beanutils2/MethodUtilsTestCase.java b/src/test/java/org/apache/commons/beanutils2/MethodUtilsTestCase.java index 5834b98f4..cb14dc0bb 100644 --- a/src/test/java/org/apache/commons/beanutils2/MethodUtilsTestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/MethodUtilsTestCase.java @@ -16,8 +16,8 @@ */ package org.apache.commons.beanutils2; -import java.io.OutputStream; -import java.io.PrintStream; +import static org.junit.jupiter.api.Assertions.assertAll; + import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -570,16 +570,18 @@ public void testPublicSub() throws Exception { } public void testParentMethod() throws Exception { - final OutputStream os = new PrintStream(System.out); - final PrintStream ps = new PrintStream(System.out); - - A a = new A(); - MethodUtils.invokeMethod(a, "foo", os); - assertTrue("Method Invoked(1)", a.called); - - a = new A(); - MethodUtils.invokeMethod(a, "foo", ps); - assertTrue("Method Invoked(2)", a.called); + final String a = "A"; + + assertAll( + () -> { + final String actual1 = (String) MethodUtils.invokeMethod(a, "toLowerCase", null); + assertEquals("a", actual1); + }, + () -> { + final char actual2 = (char) MethodUtils.invokeMethod(a, "charAt", 0); + assertEquals('A', actual2); + } + ); } /** diff --git a/src/test/java/org/apache/commons/beanutils2/bugs/Jira18TestCase.java b/src/test/java/org/apache/commons/beanutils2/bugs/Jira18TestCase.java index 6fe925f97..df40eb137 100644 --- a/src/test/java/org/apache/commons/beanutils2/bugs/Jira18TestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/bugs/Jira18TestCase.java @@ -16,14 +16,13 @@ */ package org.apache.commons.beanutils2.bugs; +import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertFalse; + import org.apache.commons.beanutils2.PropertyUtils; import org.apache.commons.beanutils2.bugs.other.Jira18BeanFactory; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; /** * Test case for Jira issue# BEANUTILS-18. @@ -32,251 +31,120 @@ * * @see https://issues.apache.org/jira/browse/BEANUTILS-18 */ -public class Jira18TestCase extends TestCase { - - private final Log log = LogFactory.getLog(Jira18TestCase.class); - private Object bean; - - /** - * Create a test case with the specified name. - * - * @param name The name of the test - */ - public Jira18TestCase(final String name) { - super(name); - } - - /** - * Run the Test. - * - * @param args Arguments - */ - public static void main(final String[] args) { - junit.textui.TestRunner.run(suite()); - } +public class Jira18TestCase { - /** - * Create a test suite for this test. - * - * @return a test suite - */ - public static Test suite() { - return new TestSuite(Jira18TestCase.class); - } + private static Object bean; - /** - * Sets up. - * - * @throws java.lang.Exception - */ - @Override - protected void setUp() throws Exception { - super.setUp(); + @BeforeAll + public static void beforeAll() { bean = Jira18BeanFactory.createBean(); } - /** - * Tear Down. - * - * @throws java.lang.Exception - */ - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - /** * Test {@link PropertyUtils#isReadable(Object, String)} * for simple properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_isReadable() { - boolean result = false; - try { - result = PropertyUtils.isReadable(bean, "simple"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertFalse("PropertyUtils.isReadable(bean, \"simple\") returned true", result); + assertFalse(PropertyUtils.isReadable(bean, "simple")); } /** * Test {@link PropertyUtils#isWriteable(Object, String)} * for simple properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable() { - boolean result = false; - try { - result = PropertyUtils.isWriteable(bean, "simple"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertFalse("PropertyUtils.isWriteable(bean, \"simple\") returned true", result); + assertFalse(PropertyUtils.isWriteable(bean, "simple")); } /** * Test {@link PropertyUtils#isReadable(Object, String)} * for indexed properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Indexed() { - boolean result = false; - try { - result = PropertyUtils.isReadable(bean, "indexed"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertFalse("PropertyUtils.isReadable(bean, \"indexed\") returned true", result); + assertFalse(PropertyUtils.isReadable(bean, "indexed")); } /** * Test {@link PropertyUtils#isWriteable(Object, String)} * for indexed properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Indexed() { - boolean result = false; - try { - result = PropertyUtils.isWriteable(bean, "indexed"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertFalse("PropertyUtils.isWriteable(bean, \"indexed\") returned true", result); + assertFalse(PropertyUtils.isWriteable(bean, "indexed")); } /** * Test {@link PropertyUtils#isReadable(Object, String)} * for Mapped properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Mapped() { - boolean result = false; - try { - result = PropertyUtils.isReadable(bean, "mapped"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertFalse("PropertyUtils.isReadable(bean, \"mapped\") returned true", result); + assertFalse(PropertyUtils.isReadable(bean, "mapped")); } /** * Test {@link PropertyUtils#isWriteable(Object, String)} * for Mapped properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Mapped() { - boolean result = false; - try { - result = PropertyUtils.isWriteable(bean, "mapped"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertFalse("PropertyUtils.isWriteable(bean, \"mapped\") returned true", result); + assertFalse(PropertyUtils.isWriteable(bean, "mapped")); } /** * Test {@link PropertyUtils#getProperty(Object, String)} * for simple properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_getProperty() { - boolean threwNoSuchMethodException = false; - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "simple"); - } catch (final NoSuchMethodException ex) { - threwNoSuchMethodException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException); + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.getProperty(bean, "simple")); } /** * Test {@link PropertyUtils#setProperty(Object, String, Object)} * for simple properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_setProperty() { - boolean threwNoSuchMethodException = false; - try { - PropertyUtils.setProperty(bean, "simple", "BAR"); - } catch (final NoSuchMethodException ex) { - threwNoSuchMethodException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException); + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.setProperty(bean, "simple", "BAR")); } /** * Test {@link PropertyUtils#getProperty(Object, String)} * for indexed properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Indexed() { - boolean threwNoSuchMethodException = false; - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "indexed[0]"); - } catch (final NoSuchMethodException ex) { - threwNoSuchMethodException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException); + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.getProperty(bean, "indexed[0]")); } /** * Test {@link PropertyUtils#setProperty(Object, String, Object)} * for indexed properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Indexed() { - boolean threwNoSuchMethodException = false; - try { - PropertyUtils.setProperty(bean, "indexed[0]", "BAR"); - } catch (final NoSuchMethodException ex) { - threwNoSuchMethodException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException); + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.setProperty(bean, "indexed[0]", "BAR")); } /** * Test {@link PropertyUtils#getProperty(Object, String)} * for mapped properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Mapped() { - boolean threwNoSuchMethodException = false; - Object result = null; - try { - result = PropertyUtils.getProperty(bean, "mapped(foo-key)"); - } catch (final NoSuchMethodException ex) { - threwNoSuchMethodException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertTrue("Expected NoSuchMethodException but returned '" + result + "'", threwNoSuchMethodException); + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.getProperty(bean, "mapped(foo-key)")); } /** * Test {@link PropertyUtils#setProperty(Object, String, Object)} * for mapped properties. */ + @Test public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Mapped() { - boolean threwNoSuchMethodException = false; - try { - PropertyUtils.setProperty(bean, "mapped(foo-key)", "BAR"); - } catch (final NoSuchMethodException ex) { - threwNoSuchMethodException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("Threw exception: " + t); - } - assertTrue("Expected NoSuchMethodException", threwNoSuchMethodException); + assertThrows(NoSuchMethodException.class, () -> PropertyUtils.setProperty(bean, "mapped(foo-key)", "BAR")); } } diff --git a/src/test/java/org/apache/commons/beanutils2/bugs/Jira61TestCase.java b/src/test/java/org/apache/commons/beanutils2/bugs/Jira61TestCase.java index 2ded8ade8..4c09a1055 100644 --- a/src/test/java/org/apache/commons/beanutils2/bugs/Jira61TestCase.java +++ b/src/test/java/org/apache/commons/beanutils2/bugs/Jira61TestCase.java @@ -16,6 +16,13 @@ */ package org.apache.commons.beanutils2.bugs; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + import java.util.HashMap; import java.util.Map; @@ -24,12 +31,8 @@ import org.apache.commons.beanutils2.WrapDynaBean; import org.apache.commons.beanutils2.bugs.other.Jira61BeanFactory; import org.apache.commons.beanutils2.bugs.other.Jira61BeanFactory.TestBean; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; /** * Test case for Jira issue# BEANUTILS-61. @@ -42,425 +45,237 @@ * * @see https://issues.apache.org/jira/browse/BEANUTILS-61 */ -public class Jira61TestCase extends TestCase { - - private final Log log = LogFactory.getLog(Jira61TestCase.class); - private Jira61BeanFactory.TestBean testBean; - private WrapDynaBean wrapDynaBean; - - /** - * Create a test case with the specified name. - * - * @param name The name of the test - */ - public Jira61TestCase(final String name) { - super(name); - } - - /** - * Run the Test. - * - * @param args Arguments - */ - public static void main(final String[] args) { - junit.textui.TestRunner.run(suite()); - } +public class Jira61TestCase { - /** - * Create a test suite for this test. - * - * @return a test suite - */ - public static Test suite() { - return new TestSuite(Jira61TestCase.class); - } + private static Jira61BeanFactory.TestBean testBean; + private static WrapDynaBean wrapDynaBean; - /** - * Sets up. - * - * @throws java.lang.Exception - */ - @Override - protected void setUp() throws Exception { - super.setUp(); + @BeforeAll + public static void beforeAll() throws Exception { testBean = Jira61BeanFactory.createBean(); PropertyUtils.getPropertyDescriptor(testBean, "mappedReadOnly"); PropertyUtils.getPropertyDescriptor(testBean, "mappedWriteOnly"); wrapDynaBean = new WrapDynaBean(testBean); } - /** - * Tear Down. - * - * @throws java.lang.Exception - */ - @Override - protected void tearDown() throws Exception { - super.tearDown(); - } - /** * Test {@link PropertyUtils#isReadable(Object, String)} * for simple properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_isReadable() { - boolean result = false; - - try { - result = PropertyUtils.isReadable(wrapDynaBean, "simpleReadOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("simpleReadOnly Threw exception: " + t); - } - assertTrue("PropertyUtils.isReadable(bean, \"simpleReadOnly\") returned " + result, result); - - try { - result = PropertyUtils.isReadable(wrapDynaBean, "simpleWriteOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("simpleWriteOnly Threw exception: " + t); - } - assertFalse("PropertyUtils.isReadable(bean, \"simpleWriteOnly\") returned " + result, result); + assertAll( + () -> assertTrue(PropertyUtils.isReadable(wrapDynaBean, "simpleReadOnly")), + () -> assertFalse(PropertyUtils.isReadable(wrapDynaBean, "simpleWriteOnly")) + ); } /** * Test {@link PropertyUtils#isWriteable(Object, String)} * for simple properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable() { - boolean result = false; - - try { - result = PropertyUtils.isWriteable(wrapDynaBean, "simpleReadOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("simpleReadOnly Threw exception: " + t); - } - assertFalse("PropertyUtils.isWriteable(bean, \"simpleReadOnly\") returned " + result, result); - - try { - result = PropertyUtils.isWriteable(wrapDynaBean, "simpleWriteOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("simpleWriteOnly Threw exception: " + t); - } - assertTrue("PropertyUtils.isWriteable(bean, \"simpleWriteOnly\") returned " + result, result); + assertAll( + () -> assertFalse(PropertyUtils.isWriteable(wrapDynaBean, "simpleReadOnly")), + () -> assertTrue(PropertyUtils.isWriteable(wrapDynaBean, "simpleWriteOnly")) + ); } /** * Test {@link PropertyUtils#isReadable(Object, String)} * for indexed properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Indexed() { - boolean result = false; - - try { - result = PropertyUtils.isReadable(wrapDynaBean, "indexedReadOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("indexedReadOnly Threw exception: " + t); - } - assertTrue("PropertyUtils.isReadable(bean, \"indexedReadOnly\") returned " + result, result); - - try { - result = PropertyUtils.isReadable(wrapDynaBean, "indexedWriteOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("indexedWriteOnly Threw exception: " + t); - } - assertFalse("PropertyUtils.isReadable(bean, \"indexedWriteOnly\") returned " + result, result); + assertAll( + () -> assertTrue(PropertyUtils.isReadable(wrapDynaBean, "indexedReadOnly")), + () -> assertFalse(PropertyUtils.isReadable(wrapDynaBean, "indexedWriteOnly")) + ); } /** * Test {@link PropertyUtils#isReadable(Object, String)} * for mapped properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Mapped() { - boolean result = false; - - try { - result = PropertyUtils.isReadable(wrapDynaBean, "mappedReadOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("mappedReadOnly Threw exception: " + t); - } - assertTrue("PropertyUtils.isReadable(bean, \"mappedReadOnly\") returned " + result, result); - - try { - result = PropertyUtils.isReadable(wrapDynaBean, "mappedWriteOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("mappedWriteOnly Threw exception: " + t); - } - assertFalse("PropertyUtils.isReadable(bean, \"mappedWriteOnly\") returned " + result, result); + assertAll( + () -> assertTrue(PropertyUtils.isReadable(wrapDynaBean, "mappedReadOnly")), + () -> assertFalse(PropertyUtils.isReadable(wrapDynaBean, "mappedWriteOnly")) + ); } /** * Test {@link PropertyUtils#isWriteable(Object, String)} * for indexed properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Indexed() { - boolean result = false; - - try { - result = PropertyUtils.isWriteable(wrapDynaBean, "indexedReadOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("indexedReadOnly Threw exception: " + t); - } - assertFalse("PropertyUtils.isWriteable(bean, \"indexedReadOnly\") returned " + result, result); - - try { - result = PropertyUtils.isWriteable(wrapDynaBean, "indexedWriteOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("indexedWriteOnly Threw exception: " + t); - } - assertTrue("PropertyUtils.isWriteable(bean, \"indexedWriteOnly\") returned " + result, result); + assertAll( + () -> assertFalse(PropertyUtils.isWriteable(wrapDynaBean, "indexedReadOnly")), + () -> assertTrue(PropertyUtils.isWriteable(wrapDynaBean, "indexedWriteOnly")) + ); } /** * Test {@link PropertyUtils#isWriteable(Object, String)} * for mapped properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Mapped() { - boolean result = false; - - try { - result = PropertyUtils.isWriteable(wrapDynaBean, "mappedReadOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("mappedReadOnly Threw exception: " + t); - } - assertFalse("PropertyUtils.isWriteable(bean, \"mappedReadOnly\") returned " + result, result); - - try { - result = PropertyUtils.isWriteable(wrapDynaBean, "mappedWriteOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("mappedWriteOnly Threw exception: " + t); - } - assertTrue("PropertyUtils.isWriteable(bean, \"mappedWriteOnly\") returned " + result, result); + assertAll( + () -> assertFalse(PropertyUtils.isWriteable(wrapDynaBean, "mappedReadOnly")), + () -> assertTrue(PropertyUtils.isWriteable(wrapDynaBean, "mappedWriteOnly")) + ); } /** * Test {@link PropertyUtils#getProperty(Object, String)} * for simple properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_getProperty() { - boolean threwIllegalArgumentException = false; - Object result = null; - try { - result = PropertyUtils.getProperty(wrapDynaBean, "simpleReadOnly"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("simpleWriteOnly Threw exception: " + t); - } - assertEquals("simpleReadOnly", testBean.getSimpleReadOnly(), result); - - try { - result = PropertyUtils.getProperty(wrapDynaBean, "simpleWriteOnly"); - } catch (final IllegalArgumentException ex) { - threwIllegalArgumentException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("simpleWriteOnly Threw exception: " + t); - } - assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException); + assertAll( + () -> assertEquals(testBean.getSimpleReadOnly(), PropertyUtils.getProperty(wrapDynaBean, "simpleReadOnly")), + () -> assertThrows(IllegalArgumentException.class, () -> { + PropertyUtils.getProperty(wrapDynaBean, "simpleWriteOnly"); + }) + ); } /** * Test {@link PropertyUtils#setProperty(Object, String, Object)} * for simple properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_setProperty() { - boolean threwIllegalArgumentException = false; - try { - PropertyUtils.setProperty(wrapDynaBean, "simpleReadOnly", "READONLY-SIMPLE-BAR"); - } catch (final IllegalArgumentException ex) { - threwIllegalArgumentException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("simpleReadOnly Threw exception: " + t); - } - assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException); - - try { - PropertyUtils.setProperty(wrapDynaBean, "simpleWriteOnly", "SIMPLE-BAR"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("simpleWriteOnly Threw exception: " + t); - } - assertEquals("simpleWriteOnly", testBean.getSimpleReadOnly(), "SIMPLE-BAR"); + assertAll( + () -> assertThrows(IllegalArgumentException.class, () -> { + PropertyUtils.setProperty(wrapDynaBean, "simpleReadOnly", "READONLY-SIMPLE-BAR"); + }), + () -> { + PropertyUtils.setProperty(wrapDynaBean, "simpleWriteOnly", "SIMPLE-BAR"); + assertEquals("SIMPLE-BAR", testBean.getSimpleReadOnly()); + } + ); } /** * Test {@link PropertyUtils#getProperty(Object, String)} * for indexed properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Indexed() { - boolean threwIllegalArgumentException = false; - Object result = null; - try { - result = PropertyUtils.getProperty(wrapDynaBean, "indexedReadOnly[0]"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("indexedReadOnly Threw exception: " + t); - } - assertEquals("indexedReadOnly", testBean.getIndexedReadOnly(0), result); - - try { - result = PropertyUtils.getProperty(wrapDynaBean, "indexedWriteOnly[0]"); - } catch (final IllegalArgumentException ex) { - threwIllegalArgumentException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("indexedWriteOnly Threw exception: " + t); - } - assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException); + assertAll( + () -> assertEquals(testBean.getIndexedReadOnly(0), PropertyUtils.getProperty(wrapDynaBean, "indexedReadOnly[0]")), + () -> assertThrows(IllegalArgumentException.class, () -> { + PropertyUtils.getProperty(wrapDynaBean, "indexedWriteOnly[0]"); + }) + ); } /** * Test {@link PropertyUtils#setProperty(Object, String, Object)} * for indexed properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Indexed() { - boolean threwIllegalArgumentException = false; - try { - PropertyUtils.setProperty(wrapDynaBean, "indexedReadOnly[0]", "READONLY-INDEXED-BAR"); - } catch (final IllegalArgumentException ex) { - threwIllegalArgumentException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("indexedReadOnly Threw exception: " + t); - } - assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException); - - try { - PropertyUtils.setProperty(wrapDynaBean, "indexedWriteOnly[0]", "INDEXED-BAR"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("indexedWriteOnly Threw exception: " + t); - } - assertEquals("indexedWriteOnly", testBean.getIndexedReadOnly(0), "INDEXED-BAR"); + assertAll( + () -> assertThrows(IllegalArgumentException.class, () -> { + PropertyUtils.setProperty(wrapDynaBean, "indexedReadOnly[0]", "READONLY-INDEXED-BAR"); + }), + () -> { + PropertyUtils.setProperty(wrapDynaBean, "indexedWriteOnly[0]", "INDEXED-BAR"); + assertEquals("INDEXED-BAR", testBean.getIndexedReadOnly(0)); + } + ); } /** * Test {@link PropertyUtils#getProperty(Object, String)} * for mapped properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Mapped() { - boolean threwIllegalArgumentException = false; - Object result = null; - try { - result = PropertyUtils.getProperty(wrapDynaBean, "mappedReadOnly(foo-key)"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("mappedReadOnly Threw exception: " + t); - } - assertEquals("mappedReadOnly", testBean.getMappedReadOnly("foo-key"), result); - - try { - result = PropertyUtils.getProperty(wrapDynaBean, "mappedWriteOnly(foo-key)"); - } catch (final IllegalArgumentException ex) { - threwIllegalArgumentException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("mappedWriteOnly Threw exception: " + t); - } - assertTrue("Expected IllegalArgumentException but returned '" + result + "'", threwIllegalArgumentException); + assertAll( + () -> assertEquals(testBean.getMappedReadOnly("foo-key"), PropertyUtils.getProperty(wrapDynaBean, "mappedReadOnly(foo-key)")), + () -> assertThrows(IllegalArgumentException.class, () -> { + PropertyUtils.getProperty(wrapDynaBean, "mappedWriteOnly(foo-key)"); + }) + ); } /** * Test {@link PropertyUtils#setProperty(Object, String, Object)} * for mapped properties. */ + @Test public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Mapped() { - boolean threwIllegalArgumentException = false; - try { - PropertyUtils.setProperty(wrapDynaBean, "mappedReadOnly(foo-key)", "READONLY-MAPPED-BAR"); - } catch (final IllegalArgumentException ex) { - threwIllegalArgumentException = true; // expected result - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("mappedReadOnly Threw exception: " + t); - } - assertTrue("Expected IllegalArgumentException", threwIllegalArgumentException); - - try { - PropertyUtils.setProperty(wrapDynaBean, "mappedWriteOnly(foo-key)", "MAPPED-BAR"); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("mappedWriteOnly Threw exception: " + t); - } - assertEquals("mappedWriteOnly", testBean.getMappedReadOnly("foo-key"), "MAPPED-BAR"); + assertAll( + () -> assertThrows(IllegalArgumentException.class, () -> { + PropertyUtils.setProperty(wrapDynaBean, "mappedReadOnly(foo-key)", "READONLY-MAPPED-BAR"); + }), + () -> { + PropertyUtils.setProperty(wrapDynaBean, "mappedWriteOnly(foo-key)", "MAPPED-BAR"); + assertEquals("MAPPED-BAR", testBean.getMappedReadOnly("foo-key")); + } + ); } /** * Test {@link PropertyUtils#copyProperties(Object, Object)} * to a read-only WrapDynaBean property. */ - public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_to_WrapDynaBean() { - final String value = "copied simpleReadOnly"; + @Test + public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_to_WrapDynaBean() throws Exception { + final String expected = "copied simpleReadOnly"; final Map source = new HashMap<>(); - source.put("simpleReadOnly", value); - try { - PropertyUtils.copyProperties(wrapDynaBean, source); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("copyProperties Threw exception: " + t); - } - assertFalse("Target value='" + value + "'", value.equals(testBean.getSimpleReadOnly())); + source.put("simpleReadOnly", expected); + PropertyUtils.copyProperties(wrapDynaBean, source); + + assertNotEquals(expected, testBean.getSimpleReadOnly()); } /** * Test {@link PropertyUtils#copyProperties(Object, Object)} * to a read-only WrapDynaBean property. */ - public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_from_WrapDynaBean() { + @Test + public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_from_WrapDynaBean() throws Exception { final String value = "ORIG TARGET VALUE"; final TestBean targetBean = Jira61BeanFactory.createBean(); targetBean.setSimpleWriteOnly(value); - try { - PropertyUtils.copyProperties(targetBean, wrapDynaBean); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("copyProperties Threw exception: " + t); - } - assertTrue("Target value='" + targetBean.getSimpleReadOnly() + "'", value.equals(targetBean.getSimpleReadOnly())); + PropertyUtils.copyProperties(targetBean, wrapDynaBean); + + assertEquals(value, targetBean.getSimpleReadOnly()); } /** * Test {@link BeanUtils#copyProperties(Object, Object)} * to a read-only WrapDynaBean property. */ - public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_to_WrapDynaBean() { + @Test + public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_to_WrapDynaBean() throws Exception { final String value = "copied simpleReadOnly"; final Map source = new HashMap<>(); source.put("simpleReadOnly", value); - try { - BeanUtils.copyProperties(wrapDynaBean, source); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("copyProperties Threw exception: " + t); - } - assertFalse("Target value='" + value + "'", value.equals(testBean.getSimpleReadOnly())); + BeanUtils.copyProperties(wrapDynaBean, source); + + assertNotEquals(value, testBean.getSimpleReadOnly()); } /** * Test {@link BeanUtils#copyProperties(Object, Object)} * to a read-only WrapDynaBean property. */ - public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_from_WrapDynaBean() { + @Test + public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_from_WrapDynaBean() throws Exception { final String value = "ORIG TARGET VALUE"; final TestBean targetBean = Jira61BeanFactory.createBean(); targetBean.setSimpleWriteOnly(value); - try { - BeanUtils.copyProperties(targetBean, wrapDynaBean); - } catch (final Throwable t) { - log.error("ERROR " + t, t); - fail("copyProperties Threw exception: " + t); - } - assertTrue("Target value='" + targetBean.getSimpleReadOnly() + "'", value.equals(targetBean.getSimpleReadOnly())); + BeanUtils.copyProperties(targetBean, wrapDynaBean); + + assertEquals(value, targetBean.getSimpleReadOnly()); } } From 4ebdc97ee489f8c17816ace8111391d6dc8508e1 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Fri, 23 Jul 2021 00:05:19 +0200 Subject: [PATCH 2/2] test: remove dependency on junit 4 --- pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pom.xml b/pom.xml index 240f8e18e..cc3cfb557 100644 --- a/pom.xml +++ b/pom.xml @@ -350,12 +350,6 @@ 5.7.2 test - - junit - junit - 4.13.2 - test - org.junit.vintage junit-vintage-engine