Skip to content

Commit

Permalink
Better JUnit API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Aug 5, 2023
1 parent fd5ba00 commit 5e9052a
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ public void testSetSimpleBoolean() {
final boolean oldValue = ((Boolean) bean.get("booleanProperty")).booleanValue();
final boolean newValue = !oldValue;
bean.set("booleanProperty", Boolean.valueOf(newValue));
assertTrue("Matched new value", newValue == ((Boolean) bean.get("booleanProperty")).booleanValue());
assertEquals("Matched new value", newValue, ((Boolean) bean.get("booleanProperty")).booleanValue());
} catch (final Throwable e) {
fail("Exception: " + e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void tearDown() {
public void testCompareBeanAgainstSelf() {
final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
final int result = beanComparator.compare(alphaBean1, alphaBean1);
assertTrue("Comparator did not sort properly. Result:" + result, result == 0);
assertEquals("Comparator did not sort properly. Result:" + result, 0, result);
}

/**
Expand All @@ -86,7 +86,7 @@ public void testCompareIdentical() {
alphaBean2 = new AlphaBean("alphabean");
final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
final int result = beanComparator.compare(alphaBean1, alphaBean2);
assertTrue("Comparator did not sort properly. Result:" + result, result == 0);
assertEquals("Comparator did not sort properly. Result:" + result, 0, result);
}

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public void testSetProperty() {
final BeanComparator<TestBean, String> beanComparator = new BeanComparator<>("doubleProperty");
int result = beanComparator.compare(testBeanA, testBeanB);

assertTrue("Comparator did not sort properly. Result:" + result, result == 1);
assertEquals("Comparator did not sort properly. Result:" + result, 1, result);

testBeanA.setStringProperty("string 1");
testBeanB.setStringProperty("string 2");
Expand All @@ -162,7 +162,7 @@ public void testSetProperty() {

result = beanComparator.compare(testBeanA, testBeanB);

assertTrue("Comparator did not sort properly. Result:" + result, result == -1);
assertEquals("Comparator did not sort properly. Result:" + result, -1, result);
}

/**
Expand All @@ -171,7 +171,7 @@ public void testSetProperty() {
public void testSimpleCompare() {
final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
final int result = beanComparator.compare(alphaBean1, alphaBean2);
assertTrue("Comparator did not sort properly. Result:" + result, result == -1);
assertEquals("Comparator did not sort properly. Result:" + result, -1, result);
}

/**
Expand All @@ -180,6 +180,6 @@ public void testSimpleCompare() {
public void testSimpleCompareInverse() {
final BeanComparator<AlphaBean, String> beanComparator = new BeanComparator<>("name");
final int result = beanComparator.compare(alphaBean2, alphaBean1);
assertTrue("Comparator did not sort properly. Result:" + result, result == 1);
assertEquals("Comparator did not sort properly. Result:" + result, 1, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.commons.beanutils2;

import static org.junit.jupiter.api.Assertions.assertThrows;

import junit.framework.TestCase;

/**
Expand Down Expand Up @@ -46,7 +48,7 @@ public BeanPropertyValueChangeConsumerTestCase(final String name) {
public void testExecuteWithIndexedProperty() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("intIndexed[0]", expectedIntegerValue).accept(testBean);
assertTrue(expectedIntegerValue.intValue() == testBean.getIntIndexed(0));
assertSame(expectedIntegerValue.intValue(), testBean.getIntIndexed(0));
}

/**
Expand Down Expand Up @@ -126,7 +128,7 @@ public void testExecuteWithReadOnlyProperty() {
public void testExecuteWithSimpleBooleanPropertyAndBooleanValue() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("booleanProperty", expectedBooleanValue).accept(testBean);
assertTrue(expectedBooleanValue.booleanValue() == testBean.getBooleanProperty());
assertEquals(expectedBooleanValue.booleanValue(), testBean.getBooleanProperty());
}

/**
Expand All @@ -147,19 +149,14 @@ public void testExecuteWithSimpleBooleanPropertyAndStringValue() {
public void testExecuteWithSimpleBytePropertyAndByteValue() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("byteProperty", expectedByteValue).accept(testBean);
assertTrue(expectedByteValue.byteValue() == testBean.getByteProperty());
assertEquals(expectedByteValue.byteValue(), testBean.getByteProperty());
}

/**
* Test execute with simple boolean property and String value.
*/
public void testExecuteWithSimpleBytePropertyAndStringValue() {
try {
new BeanPropertyValueChangeConsumer<>("byteProperty", "foo").accept(new TestBean());
fail("Should have thrown an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
/* this is what we expect */
}
assertThrows(IllegalArgumentException.class, () -> new BeanPropertyValueChangeConsumer<>("byteProperty", "foo").accept(new TestBean()));
}

/**
Expand All @@ -168,7 +165,7 @@ public void testExecuteWithSimpleBytePropertyAndStringValue() {
public void testExecuteWithSimpleDoublePropertyAndDoubleValue() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedDoubleValue).accept(testBean);
assertTrue(expectedDoubleValue.doubleValue() == testBean.getDoubleProperty());
assertEquals(expectedDoubleValue.doubleValue(), testBean.getDoubleProperty());
}

/**
Expand All @@ -177,7 +174,7 @@ public void testExecuteWithSimpleDoublePropertyAndDoubleValue() {
public void testExecuteWithSimpleDoublePropertyAndFloatValue() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedFloatValue).accept(testBean);
assertTrue(expectedFloatValue.doubleValue() == testBean.getDoubleProperty());
assertEquals(expectedFloatValue.doubleValue(), testBean.getDoubleProperty());
}

/**
Expand All @@ -186,7 +183,7 @@ public void testExecuteWithSimpleDoublePropertyAndFloatValue() {
public void testExecuteWithSimpleDoublePropertyAndIntegerValue() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("doubleProperty", expectedIntegerValue).accept(testBean);
assertTrue(expectedIntegerValue.doubleValue() == testBean.getDoubleProperty());
assertEquals(expectedIntegerValue.doubleValue(), testBean.getDoubleProperty());
}

/**
Expand Down Expand Up @@ -219,7 +216,7 @@ public void testExecuteWithSimpleFloatPropertyAndDoubleValue() {
public void testExecuteWithSimpleFloatPropertyAndFloatValue() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("floatProperty", expectedFloatValue).accept(testBean);
assertTrue(expectedFloatValue.floatValue() == testBean.getFloatProperty());
assertEquals(expectedFloatValue.floatValue(), testBean.getFloatProperty());
}

/**
Expand All @@ -228,7 +225,7 @@ public void testExecuteWithSimpleFloatPropertyAndFloatValue() {
public void testExecuteWithSimpleFloatPropertyAndIntegerValue() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("floatProperty", expectedIntegerValue).accept(testBean);
assertTrue(expectedIntegerValue.floatValue() == testBean.getFloatProperty());
assertEquals(expectedIntegerValue.floatValue(), testBean.getFloatProperty());
}

/**
Expand Down Expand Up @@ -273,7 +270,7 @@ public void testExecuteWithSimpleIntPropertyAndFloatValue() {
public void testExecuteWithSimpleIntPropertyAndIntegerValue() {
final TestBean testBean = new TestBean();
new BeanPropertyValueChangeConsumer<>("intProperty", expectedIntegerValue).accept(testBean);
assertTrue(expectedIntegerValue.intValue() == testBean.getIntProperty());
assertEquals(expectedIntegerValue.intValue(), testBean.getIntProperty());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,12 +716,12 @@ public void testGetArrayProperty() {
String[] arr = BeanUtils.getArrayProperty(bean, "stringArray");
final String[] comp = bean.getStringArray();

assertTrue("String array length = " + comp.length, comp.length == arr.length);
assertEquals("String array length = " + comp.length, comp.length, arr.length);

arr = BeanUtils.getArrayProperty(bean, "intArray");
final int[] iarr = bean.getIntArray();

assertTrue("String array length = " + iarr.length, iarr.length == arr.length);
assertEquals("String array length = " + iarr.length, iarr.length, arr.length);

// Test property which isn't array or collection
arr = BeanUtils.getArrayProperty(bean, "shortProperty");
Expand Down Expand Up @@ -768,7 +768,7 @@ public void testGetGeneralProperty() {
final String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]");
final String comp = String.valueOf(bean.getIntIndexed(2));

assertTrue("nested.intIndexed[2] == " + comp, val.equals(comp));
assertEquals("nested.intIndexed[2] == " + comp, val, comp);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand All @@ -785,11 +785,11 @@ public void testGetIndexedProperty1() {
try {
String val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]");
String comp = String.valueOf(bean.getIntIndexed(3));
assertTrue("intIndexed[3] == " + comp, val.equals(comp));
assertEquals("intIndexed[3] == " + comp, val, comp);

val = BeanUtils.getIndexedProperty(bean, "stringIndexed[3]");
comp = bean.getStringIndexed(3);
assertTrue("stringIndexed[3] == " + comp, val.equals(comp));
assertEquals("stringIndexed[3] == " + comp, val, comp);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand All @@ -807,12 +807,12 @@ public void testGetIndexedProperty2() {
String val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3);
String comp = String.valueOf(bean.getIntIndexed(3));

assertTrue("intIndexed,3 == " + comp, val.equals(comp));
assertEquals("intIndexed,3 == " + comp, val, comp);

val = BeanUtils.getIndexedProperty(bean, "stringIndexed", 3);
comp = bean.getStringIndexed(3);

assertTrue("stringIndexed,3 == " + comp, val.equals(comp));
assertEquals("stringIndexed,3 == " + comp, val, comp);

} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
Expand Down Expand Up @@ -844,7 +844,7 @@ public void testGetNestedProperty() {
try {
final String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty");
final String comp = bean.getNested().getStringProperty();
assertTrue("nested.StringProperty == " + comp, val.equals(comp));
assertEquals("nested.StringProperty == " + comp, val, comp);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand All @@ -862,7 +862,7 @@ public void testGetSimpleProperty() {
final String val = BeanUtils.getSimpleProperty(bean, "shortProperty");
final String comp = String.valueOf(bean.getShortProperty());

assertTrue("shortProperty == " + comp, val.equals(comp));
assertEquals("shortProperty == " + comp, val, comp);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand Down Expand Up @@ -1361,12 +1361,12 @@ public void testSetPropertyNullValues() throws Exception {
assertNotNull("stringArray is not null", newValue);
assertTrue("stringArray of correct type", newValue instanceof String[]);
assertEquals("stringArray length", 5, ((String[]) newValue).length);
assertTrue("stringArray[2] is null", ((String[]) newValue)[2] == null);
assertNull("stringArray[2] is null", ((String[]) newValue)[2]);
PropertyUtils.setProperty(bean, "stringArray", oldValue);

// Value into scalar
BeanUtils.setProperty(bean, "stringProperty", null);
assertTrue("stringProperty is now null", BeanUtils.getProperty(bean, "stringProperty") == null);
assertNull("stringProperty is now null", BeanUtils.getProperty(bean, "stringProperty"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,12 @@ public void testGetArrayProperty() {
String[] arr = BeanUtils.getArrayProperty(bean, "stringArray");
final String[] comp = (String[]) bean.get("stringArray");

assertTrue("String array length = " + comp.length, comp.length == arr.length);
assertEquals("String array length = " + comp.length, comp.length, arr.length);

arr = BeanUtils.getArrayProperty(bean, "intArray");
final int[] iarr = (int[]) bean.get("intArray");

assertTrue("String array length = " + iarr.length, iarr.length == arr.length);
assertEquals("String array length = " + iarr.length, iarr.length, arr.length);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand All @@ -685,7 +685,7 @@ public void testGetGeneralProperty() {
final String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]");
final String comp = String.valueOf(bean.get("intIndexed", 2));

assertTrue("nested.intIndexed[2] == " + comp, val.equals(comp));
assertEquals("nested.intIndexed[2] == " + comp, val, comp);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand All @@ -702,11 +702,11 @@ public void testGetIndexedProperty1() {
try {
String val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]");
String comp = String.valueOf(bean.get("intIndexed", 3));
assertTrue("intIndexed[3] == " + comp, val.equals(comp));
assertEquals("intIndexed[3] == " + comp, val, comp);

val = BeanUtils.getIndexedProperty(bean, "stringIndexed[3]");
comp = (String) bean.get("stringIndexed", 3);
assertTrue("stringIndexed[3] == " + comp, val.equals(comp));
assertEquals("stringIndexed[3] == " + comp, val, comp);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand All @@ -724,12 +724,12 @@ public void testGetIndexedProperty2() {
String val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3);
String comp = String.valueOf(bean.get("intIndexed", 3));

assertTrue("intIndexed,3 == " + comp, val.equals(comp));
assertEquals("intIndexed,3 == " + comp, val, comp);

val = BeanUtils.getIndexedProperty(bean, "stringIndexed", 3);
comp = (String) bean.get("stringIndexed", 3);

assertTrue("stringIndexed,3 == " + comp, val.equals(comp));
assertEquals("stringIndexed,3 == " + comp, val, comp);

} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
Expand All @@ -747,7 +747,7 @@ public void testGetNestedProperty() {
try {
final String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty");
final String comp = nested.getStringProperty();
assertTrue("nested.StringProperty == " + comp, val.equals(comp));
assertEquals("nested.StringProperty == " + comp, val, comp);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand All @@ -765,7 +765,7 @@ public void testGetSimpleProperty() {
final String val = BeanUtils.getSimpleProperty(bean, "shortProperty");
final String comp = String.valueOf(bean.get("shortProperty"));

assertTrue("shortProperty == " + comp, val.equals(comp));
assertEquals("shortProperty == " + comp, val, comp);
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final InvocationTargetException e) {
Expand Down Expand Up @@ -1000,12 +1000,12 @@ public void testSetPropertyNullValues() throws Exception {
assertNotNull("stringArray is not null", newValue);
assertTrue("stringArray of correct type", newValue instanceof String[]);
assertEquals("stringArray length", 5, ((String[]) newValue).length);
assertTrue("stringArray[2] is null", ((String[]) newValue)[2] == null);
assertNull("stringArray[2] is null", ((String[]) newValue)[2]);
PropertyUtils.setProperty(bean, "stringArray", oldValue);

// Value into scalar
BeanUtils.setProperty(bean, "stringProperty", null);
assertTrue("stringProperty is now null", BeanUtils.getProperty(bean, "stringProperty") == null);
assertNull("stringProperty is now null", BeanUtils.getProperty(bean, "stringProperty"));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public void testGetNestedBoolean() {
assertNotNull("Got a value", value);
assertTrue("Got correct type", value instanceof Boolean);
final TestBean nested = (TestBean) bean.get("nested");
assertTrue("Got correct value", ((Boolean) value).booleanValue() == nested.getBooleanProperty());
assertEquals("Got correct value", ((Boolean) value).booleanValue(), nested.getBooleanProperty());
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final IllegalArgumentException e) {
Expand Down Expand Up @@ -1696,7 +1696,7 @@ public void testSetNestedBoolean() {
final boolean oldValue = nested.getBooleanProperty();
final boolean newValue = !oldValue;
PropertyUtils.setNestedProperty(bean, "nested.booleanProperty", Boolean.valueOf(newValue));
assertTrue("Matched new value", newValue == nested.getBooleanProperty());
assertEquals("Matched new value", newValue, nested.getBooleanProperty());
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final IllegalArgumentException e) {
Expand Down Expand Up @@ -1941,7 +1941,7 @@ public void testSetSimpleBoolean() {
final boolean oldValue = ((Boolean) bean.get("booleanProperty")).booleanValue();
final boolean newValue = !oldValue;
PropertyUtils.setSimpleProperty(bean, "booleanProperty", Boolean.valueOf(newValue));
assertTrue("Matched new value", newValue == ((Boolean) bean.get("booleanProperty")).booleanValue());
assertEquals("Matched new value", newValue, ((Boolean) bean.get("booleanProperty")).booleanValue());
} catch (final IllegalAccessException e) {
fail("IllegalAccessException");
} catch (final IllegalArgumentException e) {
Expand Down
Loading

0 comments on commit 5e9052a

Please sign in to comment.