40
40
import javafx .util .converter .LocalDateTimeStringConverter ;
41
41
import javafx .util .converter .LocalDateTimeStringConverterShim ;
42
42
43
+ import org .junit .AfterClass ;
44
+ import org .junit .Before ;
45
+ import org .junit .BeforeClass ;
43
46
import org .junit .Test ;
44
47
import org .junit .runner .RunWith ;
45
48
import org .junit .runners .Parameterized ;
@@ -53,44 +56,98 @@ public class LocalDateTimeStringConverterTest {
53
56
private static final LocalDateTime VALID_LDT_WITH_SECONDS = LocalDateTime .of (1985 , 1 , 12 , 12 , 34 , 56 );
54
57
private static final LocalDateTime VALID_LDT_WITHOUT_SECONDS = LocalDateTime .of (1985 , 1 , 12 , 12 , 34 , 0 );
55
58
56
- private static final DateTimeFormatter aFormatter = DateTimeFormatter .ofPattern ("dd MM yyyy HH mm ss" );
57
- private static final DateTimeFormatter aParser = DateTimeFormatter .ofPattern ("yyyy MM dd hh mm ss a" );
59
+ private static DateTimeFormatter aFormatter ;
60
+ private static DateTimeFormatter aParser ;
61
+ private static Locale oldLocale ;
62
+
63
+ // We can only create LocalDateTimeStringConverter object after Locale is set.
64
+ // Unfortunately, due to unpredictability of @Parameterized.Parameters methods
65
+ // in JUnit, we have to allocate it after @BeforeClass sets up Locale and
66
+ // necessary static fields. Otherwise, the test may collide with other
67
+ // Local*StringConverter tests and cause unpredictable results.
68
+ private enum LocalDateTimeStringConverterVariant {
69
+ NO_PARAM ,
70
+ WITH_FORMATTER_PARSER ,
71
+ WITH_FORMAT_STYLES ,
72
+ };
58
73
59
74
@ Parameterized .Parameters public static Collection implementations () {
60
- // Tests require that default locale is en_US
61
- Locale .setDefault (Locale .US );
62
-
63
75
return Arrays .asList (new Object [][] {
64
- { new LocalDateTimeStringConverter (),
65
- Locale .getDefault (Locale .Category .FORMAT ), FormatStyle .SHORT , FormatStyle .SHORT ,
66
- VALID_LDT_WITHOUT_SECONDS , null , null },
76
+ { LocalDateTimeStringConverterVariant .NO_PARAM ,
77
+ FormatStyle .SHORT , FormatStyle .SHORT , VALID_LDT_WITHOUT_SECONDS },
67
78
68
- { new LocalDateTimeStringConverter (aFormatter , aParser ),
69
- Locale .getDefault (Locale .Category .FORMAT ), null , null ,
70
- VALID_LDT_WITH_SECONDS , aFormatter , aParser },
79
+ { LocalDateTimeStringConverterVariant .WITH_FORMATTER_PARSER ,
80
+ null , null , VALID_LDT_WITH_SECONDS },
71
81
72
- { new LocalDateTimeStringConverter (FormatStyle .SHORT , FormatStyle .SHORT , Locale .UK , IsoChronology .INSTANCE ),
73
- Locale .UK , FormatStyle .SHORT , FormatStyle .SHORT ,
74
- VALID_LDT_WITHOUT_SECONDS , null , null },
82
+ { LocalDateTimeStringConverterVariant .WITH_FORMAT_STYLES ,
83
+ FormatStyle .SHORT , FormatStyle .SHORT , VALID_LDT_WITHOUT_SECONDS },
75
84
});
76
85
}
77
86
78
- private LocalDateTimeStringConverter converter ;
79
- private Locale locale ;
87
+ private LocalDateTimeStringConverterVariant converterVariant ;
80
88
private FormatStyle dateStyle ;
81
89
private FormatStyle timeStyle ;
90
+ private LocalDateTime validDateTime ;
91
+
92
+ private LocalDateTimeStringConverter converter ;
93
+ private Locale locale ;
82
94
private DateTimeFormatter formatter , parser ;
83
95
84
- private LocalDateTime validDateTime ;
85
96
86
- public LocalDateTimeStringConverterTest (LocalDateTimeStringConverter converter , Locale locale , FormatStyle dateStyle , FormatStyle timeStyle , LocalDateTime validDateTime , DateTimeFormatter formatter , DateTimeFormatter parser ) {
87
- this .converter = converter ;
88
- this .locale = locale ;
97
+ public LocalDateTimeStringConverterTest (LocalDateTimeStringConverterVariant converterVariant , FormatStyle dateStyle , FormatStyle timeStyle , LocalDateTime validDateTime ) {
98
+ this .converterVariant = converterVariant ;
89
99
this .dateStyle = dateStyle ;
90
100
this .timeStyle = timeStyle ;
91
101
this .validDateTime = validDateTime ;
92
- this .formatter = formatter ;
93
- this .parser = parser ;
102
+
103
+ this .converter = null ;
104
+ this .locale = null ;
105
+ this .formatter = null ;
106
+ this .parser = null ;
107
+ }
108
+
109
+ @ BeforeClass
110
+ public static void setupBeforeAll () {
111
+ // Tests require that default locale is en_US
112
+ oldLocale = Locale .getDefault ();
113
+ Locale .setDefault (Locale .US );
114
+
115
+ // DateTimeFormatter uses default locale, so we can init this after updating locale
116
+ aFormatter = DateTimeFormatter .ofPattern ("dd MM yyyy HH mm ss" );
117
+ aParser = DateTimeFormatter .ofPattern ("yyyy MM dd hh mm ss a" );
118
+ }
119
+
120
+ @ AfterClass
121
+ public static void teardownAfterAll () {
122
+ // Restore VM's old locale
123
+ Locale .setDefault (oldLocale );
124
+ }
125
+
126
+ @ Before
127
+ public void setup () {
128
+ // Locale is established now, so we can allocate objects depending on it
129
+ switch (this .converterVariant ) {
130
+ case NO_PARAM :
131
+ this .converter = new LocalDateTimeStringConverter ();
132
+ this .locale = Locale .getDefault (Locale .Category .FORMAT );
133
+ this .formatter = null ;
134
+ this .parser = null ;
135
+ break ;
136
+ case WITH_FORMATTER_PARSER :
137
+ this .converter = new LocalDateTimeStringConverter (aFormatter , aParser );
138
+ this .locale = Locale .getDefault (Locale .Category .FORMAT );
139
+ this .formatter = aFormatter ;
140
+ this .parser = aParser ;
141
+ break ;
142
+ case WITH_FORMAT_STYLES :
143
+ this .converter = new LocalDateTimeStringConverter (FormatStyle .SHORT , FormatStyle .SHORT , Locale .UK , IsoChronology .INSTANCE );
144
+ this .locale = Locale .UK ;
145
+ this .formatter = null ;
146
+ this .parser = null ;
147
+ break ;
148
+ default :
149
+ fail ("Invalid converter variant: " + this .converterVariant .toString ());
150
+ }
94
151
}
95
152
96
153
/*********************************************************************
1 commit comments
openjdk-notifier[bot] commentedon Nov 29, 2022
Review
Issues