Skip to content

Commit 3803485

Browse files
committed
Fix tests that current fail on Java 20 onwards
1 parent 8da4e90 commit 3803485

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/test/java/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.apache.commons.beanutils.converters;
1919

2020
import java.sql.Time;
21+
import java.text.DateFormat;
22+
import java.text.SimpleDateFormat;
2123
import java.util.Calendar;
2224
import java.util.Locale;
2325

@@ -59,6 +61,12 @@ protected Class<?> getExpectedType() {
5961
return Time.class;
6062
}
6163

64+
private boolean isUSTimeFormatWithNarrowNoBreakSpace() {
65+
// Fix tests on Java 20 onwards. See https://bugs.openjdk.org/browse/JDK-8324308 for background.
66+
DateFormat usDateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
67+
return ((SimpleDateFormat) usDateFormat).toPattern().contains("\u202F");
68+
}
69+
6270
/**
6371
* Create the Converter with no default value.
6472
* @return A new Converter
@@ -108,23 +116,35 @@ public void testLocale() {
108116
final Locale defaultLocale = Locale.getDefault();
109117
Locale.setDefault(Locale.US);
110118

111-
final String pattern = "h:mm a"; // SHORT style time format for US Locale
119+
// SHORT style time format for US Locale
120+
final String pattern;
121+
// Valid String --> Type Conversion
122+
final String testString;
123+
if (isUSTimeFormatWithNarrowNoBreakSpace()) {
124+
pattern = "h:mm\u202Fa";
125+
testString = "3:06\u202Fpm";
126+
} else {
127+
pattern = "h:mm a";
128+
testString = "3:06 pm";
129+
}
112130

113131
// Create & Configure the Converter
114132
final DateTimeConverter converter = makeConverter();
115133
converter.setUseLocaleFormat(true);
116134

117-
// Valid String --> Type Conversion
118-
final String testString = "3:06 pm";
119135
final Object expected = toType(testString, pattern, null);
120136
validConversion(converter, expected, testString);
121137

122138
// Invalid Conversions
123139
invalidConversion(converter, null);
124140
invalidConversion(converter, "");
125141
invalidConversion(converter, "13:05");
142+
// Normal space
126143
invalidConversion(converter, "11:05 p");
127144
invalidConversion(converter, "11.05 pm");
145+
// Narrow no-break space (Java 20 onwards)
146+
invalidConversion(converter, "11:05\u202Fp");
147+
invalidConversion(converter, "11.05\\u202Fpm");
128148
invalidConversion(converter, Integer.valueOf(2));
129149

130150
// Test specified Locale

src/test/java/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.sql.Timestamp;
2121
import java.text.DateFormat;
22+
import java.text.SimpleDateFormat;
2223
import java.util.Calendar;
2324
import java.util.Date;
2425
import java.util.Locale;
@@ -27,9 +28,7 @@
2728

2829
/**
2930
* Test Case for the {@link SqlTimestampConverter} class.
30-
*
3131
*/
32-
3332
public class SqlTimestampConverterTestCase extends DateConverterTestBase {
3433

3534
/**
@@ -68,6 +67,12 @@ private boolean isUSFormatWithComma() {
6867
return loc.format(new Date()).contains(",");
6968
}
7069

70+
private boolean isUSTimeFormatWithNarrowNoBreakSpace() {
71+
// Fix tests on Java 20 onwards. See https://bugs.openjdk.org/browse/JDK-8324308 for background.
72+
DateFormat usDateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
73+
return ((SimpleDateFormat) usDateFormat).toPattern().contains("\u202F");
74+
}
75+
7176
/**
7277
* Create the Converter with no default value.
7378
* @return A new Converter
@@ -135,6 +140,10 @@ public void testLocale() {
135140
pattern = "M/d/yy h:mm a";
136141
testString = "3/21/06 3:06 PM";
137142
}
143+
if (isUSTimeFormatWithNarrowNoBreakSpace()) {
144+
pattern = pattern.replace(" a", "\u202Fa");
145+
testString = testString.replace(" PM", "\u202FPM");
146+
}
138147

139148

140149
// Valid String --> Type Conversion
@@ -144,9 +153,15 @@ public void testLocale() {
144153
// Invalid Conversions
145154
invalidConversion(converter, null);
146155
invalidConversion(converter, "");
156+
// Normal space
147157
invalidConversion(converter, "13:05 pm");
148158
invalidConversion(converter, "11:05 p");
149159
invalidConversion(converter, "11.05 pm");
160+
// Narrow no-break space (Java 20 onwards)
161+
invalidConversion(converter, "13:05\u202Fpm");
162+
invalidConversion(converter, "11:05\\u202Fp");
163+
invalidConversion(converter, "11.05\\u202Fpm");
164+
150165
invalidConversion(converter, Integer.valueOf(2));
151166

152167
// Restore the default Locale

0 commit comments

Comments
 (0)