Skip to content

Commit 7ed7274

Browse files
committed
Updated documentation.
Refactored some converters to use generic methods. Updated unit tests that asserts exceptions to JUnit 4 syntax.
1 parent 7c765f5 commit 7ed7274

File tree

9 files changed

+37
-125
lines changed

9 files changed

+37
-125
lines changed

src/main/java/org/apache/commons/beanutils2/converters/CharacterConverter.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
package org.apache.commons.beanutils2.converters;
1818

19-
import java.util.Objects;
20-
2119
/**
2220
* {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion
2321
* to and from <b>java.lang.Character</b> objects.
@@ -37,7 +35,7 @@
3735
*/
3836
public final class CharacterConverter extends AbstractConverter {
3937

40-
/** This prefix allows us to determine an input is a hexadecimal {@link String}. */
38+
/** Determines if an input is a hexadecimal {@link String}. */
4139
private static final String HEX_PREFIX = "0x";
4240

4341
/**
@@ -96,8 +94,7 @@ protected String convertToString(final Object value) {
9694
@Override
9795
protected <T> T convertToType(final Class<T> type, final Object value) throws Exception {
9896
if (Character.class.equals(type) || Character.TYPE.equals(type)) {
99-
Objects.requireNonNull(value, "Value can't be null.");
100-
final String stringValue = value.toString();
97+
final String stringValue = toString(value);
10198

10299
if (stringValue.isEmpty()) {
103100
throw new IllegalArgumentException("Value can't be empty.");

src/main/java/org/apache/commons/beanutils2/converters/DurationConverter.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.time.Duration;
2020
import java.time.format.DateTimeParseException;
2121
import java.time.temporal.ChronoUnit;
22-
import java.util.Objects;
2322

2423
/**
2524
* {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion
@@ -85,8 +84,7 @@ protected Class<?> getDefaultType() {
8584
@Override
8685
protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
8786
if (Duration.class.equals(type)) {
88-
Objects.requireNonNull(value, "Value can't be null.");
89-
final String stringValue = value.toString();
87+
final String stringValue = toString(value);
9088

9189
try {
9290
return type.cast(Duration.parse(stringValue));

src/main/java/org/apache/commons/beanutils2/converters/EnumConverter.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.commons.beanutils2.converters;
1818

19-
import java.util.Objects;
2019
import java.util.regex.Matcher;
2120
import java.util.regex.Pattern;
2221

@@ -82,8 +81,7 @@ protected Class<?> getDefaultType() {
8281
@Override
8382
protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
8483
if (Enum.class.isAssignableFrom(type)) {
85-
Objects.requireNonNull(value, "Value can't be null.");
86-
final String stringValue = value.toString();
84+
final String stringValue = toString(value);
8785

8886
try {
8987
return type.cast((Enum)Enum.valueOf((Class)type, stringValue));

src/main/java/org/apache/commons/beanutils2/converters/PeriodConverter.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.time.Period;
2020
import java.time.format.DateTimeParseException;
21-
import java.util.Objects;
2221

2322
/**
2423
* {@link org.apache.commons.beanutils2.Converter} implementation that handles conversion
@@ -88,8 +87,7 @@ protected Class<?> getDefaultType() {
8887
@Override
8988
protected <T> T convertToType(final Class<T> type, final Object value) throws Throwable {
9089
if (Period.class.equals(type)) {
91-
Objects.requireNonNull(value, "Value can't be null.");
92-
final String stringValue = value.toString();
90+
final String stringValue = toString(value);
9391

9492
try {
9593
return type.cast(Period.parse(stringValue));

src/test/java/org/apache/commons/beanutils2/converters/CharacterConverterTestCase.java

+4-15
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import static org.junit.Assert.assertEquals;
2626
import static org.junit.Assert.assertNull;
27-
import static org.junit.Assert.fail;
2827

2928
/**
3029
* Test Case for the CharacterConverter class.
@@ -60,14 +59,9 @@ public void testConvertToCharacter() {
6059
* Tests a conversion to character for null input if no default value is
6160
* provided.
6261
*/
63-
@Test
62+
@Test(expected = ConversionException.class)
6463
public void testConvertToCharacterNullNoDefault() {
65-
try {
66-
converter.convert(Character.class, null);
67-
fail("Expected a ConversionException for null value");
68-
} catch (final Exception e) {
69-
// expected result
70-
}
64+
converter.convert(Character.class, null);
7165
}
7266

7367
/**
@@ -84,14 +78,9 @@ public void testConvertToString() {
8478
/**
8579
* Tries a conversion to an unsupported type.
8680
*/
87-
@Test
81+
@Test(expected = ConversionException.class)
8882
public void testConvertToUnsupportedType() {
89-
try {
90-
converter.convert(Integer.class, "Test");
91-
fail("Could convert to unsupported type!");
92-
} catch (final ConversionException cex) {
93-
// expected result
94-
}
83+
converter.convert(Integer.class, "Test");
9584
}
9685

9786
/**

src/test/java/org/apache/commons/beanutils2/converters/DurationConverterTestCase.java

+7-23
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.time.Duration;
2626

2727
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.fail;
2928

3029
/**
3130
* Test Case for the DurationConverter class.
@@ -78,14 +77,9 @@ public void testSimpleConversion() throws Exception {
7877
/**
7978
* Tests a conversion to an unsupported type.
8079
*/
81-
@Test
80+
@Test(expected = ConversionException.class)
8281
public void testUnsupportedType() {
83-
try {
84-
converter.convert(Integer.class, "http://www.apache.org");
85-
fail("Unsupported type could be converted!");
86-
} catch (final ConversionException cex) {
87-
// expected result
88-
}
82+
converter.convert(Integer.class, "http://www.apache.org");
8983
}
9084

9185
@Test
@@ -98,27 +92,17 @@ public void testConvertingDefault() {
9892

9993
/**
10094
* As this is meant to be for technical usage, such as developers, or administrators
101-
* of software, we don't allow localized {@link Number}s such as <code>1,000</code>. Only programatically
95+
* of software, we don't allow localized {@link Number}s such as <code>1,000</code>. Only programmatically
10296
* correct values like <code>1000</code>.
10397
*/
104-
@Test
98+
@Test(expected = ConversionException.class)
10599
public void testConvertingInvalidNumbers() {
106-
try {
107-
converter.convert(Duration.class, "1,000");
108-
Assert.fail();
109-
} catch (ConversionException ex) {
110-
// Do nothing
111-
}
100+
converter.convert(Duration.class, "1,000");
112101
}
113102

114-
@Test
103+
@Test(expected = ConversionException.class)
115104
public void testConvertingWords() {
116-
try {
117-
converter.convert(Duration.class, "Hello, world!");
118-
Assert.fail();
119-
} catch (ConversionException ex) {
120-
// Do nothing
121-
}
105+
converter.convert(Duration.class, "Hello, world!");
122106
}
123107
}
124108

src/test/java/org/apache/commons/beanutils2/converters/EnumConverterTestCase.java

+10-36
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.concurrent.TimeUnit;
2727

2828
import static org.junit.Assert.assertEquals;
29-
import static org.junit.Assert.fail;
3029

3130
/**
3231
* Test Case for the EnumConverter class.
@@ -77,14 +76,9 @@ public void testSimpleConversion() throws Exception {
7776
/**
7877
* Tests a conversion to an unsupported type.
7978
*/
80-
@Test
79+
@Test(expected = ConversionException.class)
8180
public void testUnsupportedType() {
82-
try {
83-
converter.convert(Integer.class, "http://www.apache.org");
84-
fail("Unsupported type could be converted!");
85-
} catch (final ConversionException cex) {
86-
// expected result
87-
}
81+
converter.convert(Integer.class, "http://www.apache.org");
8882
}
8983

9084
public enum PizzaStatus {
@@ -110,44 +104,24 @@ public void testConvertDayOfWeek() {
110104
Assert.assertEquals(expected, actual);
111105
}
112106

113-
@Test
107+
@Test(expected = ConversionException.class)
114108
public void testConvertWrongEnumType() {
115-
try {
116-
converter.convert(TimeUnit.class, "java.time.DayOfWeek#MONDAY");
117-
Assert.fail();
118-
} catch (ConversionException ex) {
119-
// Do nothing
120-
}
109+
converter.convert(TimeUnit.class, "java.time.DayOfWeek#MONDAY");
121110
}
122111

123-
@Test
112+
@Test(expected = ConversionException.class)
124113
public void testBrokenNamingConvention() {
125-
try {
126-
converter.convert(Enum.class, "JAVA-TIME-DAYOFWEEK#MONDAY");
127-
Assert.fail();
128-
} catch (ConversionException ex) {
129-
// Do nothing
130-
}
114+
converter.convert(Enum.class, "JAVA-TIME-DAYOFWEEK#MONDAY");
131115
}
132116

133-
@Test
117+
@Test(expected = ConversionException.class)
134118
public void testNonEnumClasses() {
135-
try {
136-
converter.convert(Enum.class, "java.lang.String#MONDAY");
137-
Assert.fail();
138-
} catch (ConversionException ex) {
139-
// Do nothing
140-
}
119+
converter.convert(Enum.class, "java.lang.String#MONDAY");
141120
}
142121

143-
@Test
122+
@Test(expected = ConversionException.class)
144123
public void testNonExistingClasses() {
145-
try {
146-
converter.convert(Enum.class, "java.lang.does.not.exist#MONDAY");
147-
Assert.fail();
148-
} catch (ConversionException ex) {
149-
// Do nothing
150-
}
124+
converter.convert(Enum.class, "java.lang.does.not.exist#MONDAY");
151125
}
152126
}
153127

src/test/java/org/apache/commons/beanutils2/converters/InstantConverterTestCase.java

+4-14
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,13 @@ public void testConvertingInstantString() {
4848
Assert.assertEquals(expected, actual);
4949
}
5050

51-
@Test
51+
@Test(expected = ConversionException.class)
5252
public void testText() {
53-
try {
54-
converter.convert(Instant.class, "Hello, world!");
55-
Assert.fail();
56-
} catch (ConversionException ex) {
57-
// Do nothing
58-
}
53+
converter.convert(Instant.class, "Hello, world!");
5954
}
6055

61-
@Test
56+
@Test(expected = ConversionException.class)
6257
public void testLocalizedNumber() {
63-
try {
64-
converter.convert(Instant.class, "200,000,000,000");
65-
Assert.fail();
66-
} catch (ConversionException ex) {
67-
// Do nothing
68-
}
58+
converter.convert(Instant.class, "200,000,000,000");
6959
}
7060
}

src/test/java/org/apache/commons/beanutils2/converters/PeriodConverterTestCase.java

+7-23
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.time.Period;
2626

2727
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.fail;
2928

3029
/**
3130
* Test Case for the PeriodConverter class.
@@ -78,14 +77,9 @@ public void testSimpleConversion() throws Exception {
7877
/**
7978
* Tests a conversion to an unsupported type.
8079
*/
81-
@Test
80+
@Test(expected = ConversionException.class)
8281
public void testUnsupportedType() {
83-
try {
84-
converter.convert(Integer.class, "http://www.apache.org");
85-
fail("Unsupported type could be converted!");
86-
} catch (final ConversionException cex) {
87-
// expected result
88-
}
82+
converter.convert(Integer.class, "http://www.apache.org");
8983
}
9084

9185
@Test
@@ -99,26 +93,16 @@ public void testConvertingDefault() {
9993
/**
10094
* As this is meant to be for technical usage, such as developers, or administrators
10195
* of software, we don't allow localized {@link Number}s such as <code>1,000</code>.
102-
* Only programatically correct values like <code>1000</code>.
96+
* Only programmatically correct values like <code>1000</code>.
10397
*/
104-
@Test
98+
@Test(expected = ConversionException.class)
10599
public void testLocalizedNumber() {
106-
try {
107-
converter.convert(Period.class, "1,000");
108-
Assert.fail();
109-
} catch (ConversionException ex) {
110-
// Do nothing
111-
}
100+
converter.convert(Period.class, "1,000");
112101
}
113102

114-
@Test
103+
@Test(expected = ConversionException.class)
115104
public void testLetter() {
116-
try {
117-
converter.convert(Period.class, "Z");
118-
Assert.fail();
119-
} catch (ConversionException ex) {
120-
// Do nothing
121-
}
105+
converter.convert(Period.class, "Z");
122106
}
123107
}
124108

0 commit comments

Comments
 (0)