1
1
/*
2
- * Copyright 2014 the original author or authors.
2
+ * Copyright 2014-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import static org .junit .Assert .*;
20
20
21
21
import java .text .SimpleDateFormat ;
22
- import java .time .*;
22
+ import java .time .Duration ;
23
+ import java .time .Instant ;
24
+ import java .time .LocalDate ;
25
+ import java .time .LocalDateTime ;
26
+ import java .time .LocalTime ;
27
+ import java .time .Period ;
28
+ import java .time .ZoneId ;
29
+ import java .util .Arrays ;
30
+ import java .util .Collection ;
23
31
import java .util .Date ;
24
32
import java .util .HashMap ;
25
33
import java .util .Map ;
26
34
import java .util .Map .Entry ;
27
35
28
36
import org .junit .Test ;
37
+ import org .junit .runner .RunWith ;
38
+ import org .junit .runners .Parameterized ;
39
+ import org .junit .runners .Parameterized .Parameter ;
40
+ import org .junit .runners .Parameterized .Parameters ;
41
+ import org .junit .runners .Suite ;
42
+ import org .junit .runners .Suite .SuiteClasses ;
43
+ import org .springframework .core .ResolvableType ;
29
44
import org .springframework .core .convert .ConversionService ;
30
45
import org .springframework .core .convert .converter .Converter ;
31
46
import org .springframework .core .convert .support .GenericConversionService ;
47
+ import org .springframework .data .convert .Jsr310ConvertersUnitTests .CommonTests ;
48
+ import org .springframework .data .convert .Jsr310ConvertersUnitTests .DurationConversionTests ;
49
+ import org .springframework .data .convert .Jsr310ConvertersUnitTests .PeriodConversionTests ;
32
50
33
51
/**
34
52
* Unit tests for {@link Jsr310Converters}.
35
53
*
36
- * @author Oliver Gierke & Barak Schoster
54
+ * @author Oliver Gierke
55
+ * @author Barak Schoster
37
56
*/
57
+ @ RunWith (Suite .class )
58
+ @ SuiteClasses ({ CommonTests .class , DurationConversionTests .class , PeriodConversionTests .class })
38
59
public class Jsr310ConvertersUnitTests {
39
60
40
61
static final Date NOW = new Date ();
@@ -51,127 +72,150 @@ public class Jsr310ConvertersUnitTests {
51
72
CONVERSION_SERVICE = conversionService ;
52
73
}
53
74
54
- /**
55
- * @see DATACMNS-606
56
- */
57
- @ Test
58
- public void convertsDateToLocalDateTime () {
59
- assertThat (CONVERSION_SERVICE .convert (NOW , LocalDateTime .class ).toString (),
60
- is (format (NOW , "yyyy-MM-dd'T'HH:mm:ss.SSS" )));
61
- }
75
+ public static class CommonTests {
62
76
63
- /**
64
- * @see DATACMNS-606
65
- */
66
- @ Test
67
- public void convertsLocalDateTimeToDate () {
77
+ /**
78
+ * @see DATACMNS-606
79
+ */
80
+ @ Test
81
+ public void convertsDateToLocalDateTime () {
82
+ assertThat (CONVERSION_SERVICE .convert (NOW , LocalDateTime .class ).toString (),
83
+ is (format (NOW , "yyyy-MM-dd'T'HH:mm:ss.SSS" )));
84
+ }
68
85
69
- LocalDateTime now = LocalDateTime .now ();
70
- assertThat (format (CONVERSION_SERVICE .convert (now , Date .class ), "yyyy-MM-dd'T'HH:mm:ss.SSS" ), is (now .toString ()));
71
- }
86
+ /**
87
+ * @see DATACMNS-606
88
+ */
89
+ @ Test
90
+ public void convertsLocalDateTimeToDate () {
72
91
73
- /**
74
- * @see DATACMNS-606
75
- */
76
- @ Test
77
- public void convertsDateToLocalDate () {
78
- assertThat (CONVERSION_SERVICE .convert (NOW , LocalDate .class ).toString (), is (format (NOW , "yyyy-MM-dd" )));
79
- }
92
+ LocalDateTime now = LocalDateTime .now ();
93
+ assertThat (format (CONVERSION_SERVICE .convert (now , Date .class ), "yyyy-MM-dd'T'HH:mm:ss.SSS" ), is (now .toString ()));
94
+ }
80
95
81
- /**
82
- * @see DATACMNS-606
83
- */
84
- @ Test
85
- public void convertsLocalDateToDate () {
96
+ /**
97
+ * @see DATACMNS-606
98
+ */
99
+ @ Test
100
+ public void convertsDateToLocalDate () {
101
+ assertThat (CONVERSION_SERVICE .convert (NOW , LocalDate .class ).toString (), is (format (NOW , "yyyy-MM-dd" )));
102
+ }
86
103
87
- LocalDate now = LocalDate .now ();
88
- assertThat (format (CONVERSION_SERVICE .convert (now , Date .class ), "yyyy-MM-dd" ), is (now .toString ()));
89
- }
104
+ /**
105
+ * @see DATACMNS-606
106
+ */
107
+ @ Test
108
+ public void convertsLocalDateToDate () {
90
109
91
- /**
92
- * @see DATACMNS-606
93
- */
94
- @ Test
95
- public void convertsDateToLocalTime () {
96
- assertThat (CONVERSION_SERVICE .convert (NOW , LocalTime .class ).toString (), is (format (NOW , "HH:mm:ss.SSS" )));
97
- }
110
+ LocalDate now = LocalDate .now ();
111
+ assertThat (format (CONVERSION_SERVICE .convert (now , Date .class ), "yyyy-MM-dd" ), is (now .toString ()));
112
+ }
98
113
99
- /**
100
- * @see DATACMNS-606
101
- */
102
- @ Test
103
- public void convertsLocalTimeToDate () {
114
+ /**
115
+ * @see DATACMNS-606
116
+ */
117
+ @ Test
118
+ public void convertsDateToLocalTime () {
119
+ assertThat (CONVERSION_SERVICE .convert (NOW , LocalTime .class ).toString (), is (format (NOW , "HH:mm:ss.SSS" )));
120
+ }
104
121
105
- LocalTime now = LocalTime .now ();
106
- assertThat (format (CONVERSION_SERVICE .convert (now , Date .class ), "HH:mm:ss.SSS" ), is (now .toString ()));
107
- }
122
+ /**
123
+ * @see DATACMNS-606
124
+ */
125
+ @ Test
126
+ public void convertsLocalTimeToDate () {
108
127
109
- /**
110
- * @see DATACMNS-623
111
- */
112
- @ Test
113
- public void convertsDateToInstant () {
128
+ LocalTime now = LocalTime .now ();
129
+ assertThat (format (CONVERSION_SERVICE .convert (now , Date .class ), "HH:mm:ss.SSS" ), is (now .toString ()));
130
+ }
114
131
115
- Date now = new Date ();
116
- assertThat (CONVERSION_SERVICE .convert (now , Instant .class ), is (now .toInstant ()));
117
- }
132
+ /**
133
+ * @see DATACMNS-623
134
+ */
135
+ @ Test
136
+ public void convertsDateToInstant () {
118
137
119
- /**
120
- * @see DATACMNS-623
121
- */
122
- @ Test
123
- public void convertsInstantToDate () {
138
+ Date now = new Date ();
139
+ assertThat (CONVERSION_SERVICE .convert (now , Instant .class ), is (now .toInstant ()));
140
+ }
124
141
125
- Date now = new Date ();
126
- assertThat (CONVERSION_SERVICE .convert (now .toInstant (), Date .class ), is (now ));
127
- }
142
+ /**
143
+ * @see DATACMNS-623
144
+ */
145
+ @ Test
146
+ public void convertsInstantToDate () {
128
147
129
- @ Test
130
- public void convertsZoneIdToStringAndBack () {
148
+ Date now = new Date ();
149
+ assertThat (CONVERSION_SERVICE .convert (now .toInstant (), Date .class ), is (now ));
150
+ }
151
+
152
+ @ Test
153
+ public void convertsZoneIdToStringAndBack () {
131
154
132
- Map <String , ZoneId > ids = new HashMap <String , ZoneId >();
133
- ids .put ("Europe/Berlin" , ZoneId .of ("Europe/Berlin" ));
134
- ids .put ("+06:00" , ZoneId .of ("+06:00" ));
155
+ Map <String , ZoneId > ids = new HashMap <String , ZoneId >();
156
+ ids .put ("Europe/Berlin" , ZoneId .of ("Europe/Berlin" ));
157
+ ids .put ("+06:00" , ZoneId .of ("+06:00" ));
135
158
136
- for (Entry <String , ZoneId > entry : ids .entrySet ()) {
137
- assertThat (CONVERSION_SERVICE .convert (entry .getValue (), String .class ), is (entry .getKey ()));
138
- assertThat (CONVERSION_SERVICE .convert (entry .getKey (), ZoneId .class ), is (entry .getValue ()));
159
+ for (Entry <String , ZoneId > entry : ids .entrySet ()) {
160
+ assertThat (CONVERSION_SERVICE .convert (entry .getValue (), String .class ), is (entry .getKey ()));
161
+ assertThat (CONVERSION_SERVICE .convert (entry .getKey (), ZoneId .class ), is (entry .getValue ()));
162
+ }
163
+ }
164
+
165
+ private static String format (Date date , String format ) {
166
+ return new SimpleDateFormat (format ).format (date );
139
167
}
140
168
}
141
169
142
- @ Test
143
- public void convertsDurationToStringAndBack () {
170
+ @ RunWith (Parameterized .class )
171
+ public static class DurationConversionTests extends ConversionTest <Duration > {
172
+
173
+ /**
174
+ * @see DATACMNS-951
175
+ */
176
+ @ Parameters
177
+ public static Collection <Object []> data () {
178
+
179
+ return Arrays .asList (new Object [][] { //
180
+ { "PT240H" , Duration .ofDays (10 ) }, //
181
+ { "PT2H" , Duration .ofHours (2 ) }, //
182
+ { "PT3M" , Duration .ofMinutes (3 ) }, //
183
+ { "PT4S" , Duration .ofSeconds (4 ) }, //
184
+ { "PT0.005S" , Duration .ofMillis (5 ) }, //
185
+ { "PT0.000000006S" , Duration .ofNanos (6 ) } //
186
+ });
187
+ }
188
+ }
144
189
145
- Map <String , Duration > ids = new HashMap <String , Duration >();
146
- ids .put ("PT240H" , Duration .ofDays (10 ));
147
- ids .put ("PT2H" , Duration .ofHours (2 ));
148
- ids .put ("PT3M" , Duration .ofMinutes (3 ));
149
- ids .put ("PT4S" , Duration .ofSeconds (4 ));
150
- ids .put ("PT0.005S" , Duration .ofMillis (5 ));
151
- ids .put ("PT0.000000006S" , Duration .ofNanos (6 ));
190
+ public static class PeriodConversionTests extends ConversionTest <Period > {
152
191
192
+ /**
193
+ * @see DATACMNS-951
194
+ */
195
+ @ Parameters
196
+ public static Collection <Object []> data () {
153
197
154
- for (Entry <String , Duration > entry : ids .entrySet ()) {
155
- assertThat (CONVERSION_SERVICE .convert (entry .getValue (), String .class ), is (entry .getKey ()));
156
- assertThat (CONVERSION_SERVICE .convert (entry .getKey (), Duration .class ), is (entry .getValue ()));
198
+ return Arrays .asList (new Object [][] { //
199
+ { "P2D" , Period .ofDays (2 ) }, //
200
+ { "P21D" , Period .ofWeeks (3 ) }, //
201
+ { "P4M" , Period .ofMonths (4 ) }, //
202
+ { "P5Y" , Period .ofYears (5 ) }, //
203
+ });
157
204
}
158
205
}
159
206
160
- @ Test
161
- public void convertsPeriodToStringAndBack () {
207
+ @ RunWith ( Parameterized . class )
208
+ public static class ConversionTest < T > {
162
209
163
- Map <String , Period > ids = new HashMap <String , Period >();
164
- ids .put ("P2D" , Period .ofDays (2 ));
165
- ids .put ("P21D" , Period .ofWeeks (3 ));
166
- ids .put ("P4M" , Period .ofMonths (4 ));
167
- ids .put ("P5Y" , Period .ofYears (5 ));
210
+ public @ Parameter (0 ) String string ;
211
+ public @ Parameter (1 ) T target ;
168
212
169
- for (Entry <String , Period > entry : ids .entrySet ()) {
170
- assertThat (CONVERSION_SERVICE .convert (entry .getValue (), String .class ), is (entry .getKey ()));
171
- assertThat (CONVERSION_SERVICE .convert (entry .getKey (), Period .class ), is (entry .getValue ()));
213
+ @ Test
214
+ public void convertsPeriodToStringAndBack () {
215
+
216
+ ResolvableType type = ResolvableType .forClass (ConversionTest .class , this .getClass ());
217
+ assertThat (CONVERSION_SERVICE .convert (target , String .class ), is (string ));
218
+ assertThat (CONVERSION_SERVICE .convert (string , type .getGeneric (0 ).getRawClass ()), is ((Object ) target ));
172
219
}
173
220
}
174
- private static String format (Date date , String format ) {
175
- return new SimpleDateFormat (format ).format (date );
176
- }
177
221
}
0 commit comments