Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Sep 3, 2024
2 parents 397d184 + 655200b commit e96a9b7
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 196 deletions.
2 changes: 1 addition & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
Refactor logger usage #72.
</action>
<action dev="ggregory" type="fix" due-to="SethFalco, Steve Bosman, Gary Gregory">
Migrate to JUnit 5 #93, #283, #284.
Migrate to JUnit 5 #93, #283, #284, #285.
</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">
Fix SpotBugs [ERROR] Medium: org.apache.commons.beanutils2.BasicDynaClass.constructorTypes should be both final and package protected [org.apache.commons.beanutils2.BasicDynaClass] At BasicDynaClass.java:[line 95] MS_FINAL_PKGPROTECT.
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

package org.apache.commons.beanutils2.converters;

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

import java.math.BigDecimal;

import org.apache.commons.beanutils2.Converter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test Case for the DoubleConverter class.
Expand Down Expand Up @@ -54,7 +59,7 @@ protected BigDecimalConverter makeConverter(final BigDecimal defaultValue) {
return new BigDecimalConverter(defaultValue);
}

@Override
@BeforeEach
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = new BigDecimal("-12");
Expand All @@ -63,11 +68,12 @@ public void setUp() throws Exception {
numbers[3] = new BigDecimal("23");
}

@Override
@AfterEach
public void tearDown() throws Exception {
converter = null;
}

@Test
public void testSimpleConversion() throws Exception {
final String[] message = { "from String", "from String", "from String", "from String", "from String", "from Byte", "from Short", "from Integer",
"from Long", "from Float", "from Double", "from BigDecimal", "from BigDecimal extension" };
Expand All @@ -80,8 +86,8 @@ public void testSimpleConversion() throws Exception {
new BigDecimal("3200.11"), new BigDecimal("3200.11") };

for (int i = 0; i < expected.length; i++) {
assertEquals(message[i] + " to BigDecimal", expected[i], converter.convert(BigDecimal.class, input[i]));
assertEquals(message[i] + " to null type", expected[i], converter.convert(null, input[i]));
assertEquals(expected[i], converter.convert(BigDecimal.class, input[i]), message[i] + " to BigDecimal");
assertEquals(expected[i], converter.convert(null, input[i]), message[i] + " to null type");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

package org.apache.commons.beanutils2.converters;

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

import java.math.BigInteger;

import org.apache.commons.beanutils2.Converter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test Case for the BigInteger class.
Expand All @@ -43,7 +48,7 @@ protected BigIntegerConverter makeConverter(final BigInteger defaultValue) {
return new BigIntegerConverter(defaultValue);
}

@Override
@BeforeEach
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = new BigInteger("-12");
Expand All @@ -52,11 +57,12 @@ public void setUp() throws Exception {
numbers[3] = new BigInteger("23");
}

@Override
@AfterEach
public void tearDown() throws Exception {
converter = null;
}

@Test
public void testSimpleConversion() throws Exception {
final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from Byte",
"from Short", "from Integer", "from Long", "from Float", "from Double" };
Expand All @@ -69,8 +75,8 @@ public void testSimpleConversion() throws Exception {
BigInteger.valueOf(9), BigInteger.valueOf(10), BigInteger.valueOf(11), BigInteger.valueOf(12) };

for (int i = 0; i < expected.length; i++) {
assertEquals(message[i] + " to BigInteger", expected[i], converter.convert(BigInteger.class, input[i]));
assertEquals(message[i] + " to null type", expected[i], converter.convert(null, input[i]));
assertEquals(expected[i], converter.convert(BigInteger.class, input[i]), message[i] + " to BigInteger");
assertEquals(expected[i], converter.convert(null, input[i]), message[i] + " to null type");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@

package org.apache.commons.beanutils2.converters;

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

import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.Converter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test Case for the ByteConverter class.
Expand All @@ -41,7 +48,7 @@ protected ByteConverter makeConverter(final Byte defaultValue) {
return new ByteConverter(defaultValue);
}

@Override
@BeforeEach
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = Byte.valueOf("-12");
Expand All @@ -50,14 +57,15 @@ public void setUp() throws Exception {
numbers[3] = Byte.valueOf("23");
}

@Override
@AfterEach
public void tearDown() throws Exception {
converter = null;
}

/**
* Test Invalid Amounts (too big/small)
*/
@Test
public void testInvalidAmount() {
final Converter<Byte> converter = makeConverter();
final Class<Byte> clazz = Byte.class;
Expand All @@ -68,28 +76,23 @@ public void testInvalidAmount() {
final Long maxPlusOne = Long.valueOf(max.longValue() + 1);

// Minimum
assertEquals("Minimum", Byte.valueOf(Byte.MIN_VALUE), converter.convert(clazz, min));
assertEquals(Byte.valueOf(Byte.MIN_VALUE), converter.convert(clazz, min), "Minimum");

// Maximum
assertEquals("Maximum", Byte.valueOf(Byte.MAX_VALUE), converter.convert(clazz, max));
assertEquals(Byte.valueOf(Byte.MAX_VALUE), converter.convert(clazz, max), "Maximum");

// Too Small
try {
assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
fail("Less than minimum, expected ConversionException");
} catch (final Exception e) {
// expected result
}
assertThrows(ConversionException.class,
() -> converter.convert(clazz, minMinusOne),
"Less than minimum, expected ConversionException");

// Too Large
try {
assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
fail("More than maximum, expected ConversionException");
} catch (final Exception e) {
// expected result
}
assertThrows(ConversionException.class,
() -> converter.convert(clazz, maxPlusOne),
"More than maximum, expected ConversionException");
}

@Test
public void testSimpleConversion() throws Exception {
final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from Byte",
"from Short", "from Integer", "from Long", "from Float", "from Double" };
Expand All @@ -102,9 +105,9 @@ public void testSimpleConversion() throws Exception {
Byte.valueOf((byte) 9), Byte.valueOf((byte) 10), Byte.valueOf((byte) 11), Byte.valueOf((byte) 12) };

for (int i = 0; i < expected.length; i++) {
assertEquals(message[i] + " to Byte", expected[i], converter.convert(Byte.class, input[i]));
assertEquals(message[i] + " to byte", expected[i], converter.convert(Byte.TYPE, input[i]));
assertEquals(message[i] + " to null type", expected[i], converter.convert(null, input[i]));
assertEquals(expected[i], converter.convert(Byte.class, input[i]), message[i] + " to Byte");
assertEquals(expected[i], converter.convert(Byte.TYPE, input[i]), message[i] + " to byte");
assertEquals(expected[i], converter.convert(null, input[i]), message[i] + " to null type");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

package org.apache.commons.beanutils2.converters;

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

import org.apache.commons.beanutils2.Converter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test Case for the DoubleConverter class.
Expand All @@ -41,7 +46,7 @@ protected DoubleConverter makeConverter(final Double defaultValue) {
return new DoubleConverter(defaultValue);
}

@Override
@BeforeEach
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = Double.valueOf("-12");
Expand All @@ -50,11 +55,12 @@ public void setUp() throws Exception {
numbers[3] = Double.valueOf("23");
}

@Override
@AfterEach
public void tearDown() throws Exception {
converter = null;
}

@Test
public void testSimpleConversion() {
final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from Byte",
"from Short", "from Integer", "from Long", "from Float", "from Double" };
Expand All @@ -67,9 +73,9 @@ public void testSimpleConversion() {
Double.valueOf(11.1), Double.valueOf(12.2) };

for (int i = 0; i < expected.length; i++) {
assertEquals(message[i] + " to Double", expected[i].doubleValue(), converter.convert(Double.class, input[i]).doubleValue(), 0.00001D);
assertEquals(message[i] + " to double", expected[i].doubleValue(), converter.convert(Double.TYPE, input[i]).doubleValue(), 0.00001D);
assertEquals(message[i] + " to null type", expected[i].doubleValue(), converter.convert((Class<Double>) null, input[i]).doubleValue(), 0.00001D);
assertEquals(expected[i].doubleValue(), converter.convert(Double.class, input[i]).doubleValue(), 0.00001D, message[i] + " to Double");
assertEquals(expected[i].doubleValue(), converter.convert(Double.TYPE, input[i]).doubleValue(), 0.00001D, message[i] + " to double");
assertEquals(expected[i].doubleValue(), converter.convert((Class<Double>) null, input[i]).doubleValue(), 0.00001D, message[i] + " to null type");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@

package org.apache.commons.beanutils2.converters;

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

import org.apache.commons.beanutils2.ConversionException;
import org.apache.commons.beanutils2.Converter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test Case for the FloatConverter class.
Expand All @@ -41,7 +48,7 @@ protected FloatConverter makeConverter(final Float defaultValue) {
return new FloatConverter(defaultValue);
}

@Override
@BeforeEach
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = Float.valueOf("-12");
Expand All @@ -50,14 +57,15 @@ public void setUp() throws Exception {
numbers[3] = Float.valueOf("23");
}

@Override
@AfterEach
public void tearDown() throws Exception {
converter = null;
}

/**
* Test Invalid Amounts (too big/small)
*/
@Test
public void testInvalidAmount() {
final Converter<Float> converter = makeConverter();
final Class<?> clazz = Float.class;
Expand All @@ -66,17 +74,15 @@ public void testInvalidAmount() {
final Double tooBig = Double.valueOf(Double.MAX_VALUE);

// Maximum
assertEquals("Maximum", Float.valueOf(Float.MAX_VALUE), converter.convert(clazz, max));
assertEquals(Float.valueOf(Float.MAX_VALUE), converter.convert(clazz, max), "Maximum");

// Too Large
try {
assertEquals("Too Big", null, converter.convert(clazz, tooBig));
fail("More than maximum, expected ConversionException");
} catch (final Exception e) {
// expected result
}
assertThrows(ConversionException.class,
() -> converter.convert(clazz, tooBig),
"More than maximum, expected ConversionException");
}

@Test
public void testSimpleConversion() {
final String[] message = { "from String", "from String", "from String", "from String", "from String", "from String", "from String", "from Byte",
"from Short", "from Integer", "from Long", "from Float", "from Double" };
Expand All @@ -89,9 +95,9 @@ public void testSimpleConversion() {
Float.valueOf(10), Float.valueOf((float) 11.1), Float.valueOf((float) 12.2) };

for (int i = 0; i < expected.length; i++) {
assertEquals(message[i] + " to Float", expected[i].floatValue(), converter.convert(Float.class, input[i]).floatValue(), 0.00001);
assertEquals(message[i] + " to float", expected[i].floatValue(), converter.convert(Float.TYPE, input[i]).floatValue(), 0.00001);
assertEquals(message[i] + " to null type", expected[i].floatValue(), converter.convert((Class<Float>) null, input[i]).floatValue(), 0.00001);
assertEquals(expected[i].floatValue(), converter.convert(Float.class, input[i]).floatValue(), 0.00001, message[i] + " to Float");
assertEquals(expected[i].floatValue(), converter.convert(Float.TYPE, input[i]).floatValue(), 0.00001, message[i] + " to float");
assertEquals(expected[i].floatValue(), converter.convert((Class<Float>) null, input[i]).floatValue(), 0.00001, message[i] + " to null type");
}
}
}
Loading

0 comments on commit e96a9b7

Please sign in to comment.