Skip to content

Commit 8d67c84

Browse files
committed
DATACMNS-951 - Polishing.
Switched to parameterized tests for newly introduced Converters. Fixed imports and JavaDoc. Original pull request: #186.
1 parent 3125bbf commit 8d67c84

File tree

2 files changed

+153
-112
lines changed

2 files changed

+153
-112
lines changed

src/main/java/org/springframework/data/convert/Jsr310Converters.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,13 @@
1919
import static java.time.LocalDateTime.*;
2020
import static java.time.ZoneId.*;
2121

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;
2329
import java.util.ArrayList;
2430
import java.util.Arrays;
2531
import java.util.Collection;
@@ -34,6 +40,7 @@
3440
* Helper class to register JSR-310 specific {@link Converter} implementations in case the we're running on Java 8.
3541
*
3642
* @author Oliver Gierke
43+
* @author Barak Schoster
3744
*/
3845
public abstract class Jsr310Converters {
3946

@@ -182,11 +189,9 @@ public ZoneId convert(String source) {
182189
}
183190
}
184191

185-
/**
186-
* enable jsr-310 {@link java.time.Duration} write
187-
*/
188192
@WritingConverter
189193
public static enum DurationToStringConverter implements Converter<Duration, String> {
194+
190195
INSTANCE;
191196

192197
@Override
@@ -195,9 +200,6 @@ public String convert(Duration duration) {
195200
}
196201
}
197202

198-
/**
199-
* enable jsr-310 {@link java.time.Duration} read
200-
*/
201203
@ReadingConverter
202204
public static enum StringToDurationConverter implements Converter<String, Duration> {
203205

@@ -209,11 +211,9 @@ public Duration convert(String s) {
209211
}
210212
}
211213

212-
/**
213-
* enable jsr-310 {@link java.time.Period} write
214-
*/
215214
@WritingConverter
216215
public static enum PeriodToStringConverter implements Converter<Period, String> {
216+
217217
INSTANCE;
218218

219219
@Override
@@ -222,9 +222,6 @@ public String convert(Period period) {
222222
}
223223
}
224224

225-
/**
226-
* enable jsr-310 {@link java.time.Period} read
227-
*/
228225
@ReadingConverter
229226
public static enum StringToPeriodConverter implements Converter<String, Period> {
230227

Lines changed: 142 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,22 +19,43 @@
1919
import static org.junit.Assert.*;
2020

2121
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;
2331
import java.util.Date;
2432
import java.util.HashMap;
2533
import java.util.Map;
2634
import java.util.Map.Entry;
2735

2836
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;
2944
import org.springframework.core.convert.ConversionService;
3045
import org.springframework.core.convert.converter.Converter;
3146
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;
3250

3351
/**
3452
* Unit tests for {@link Jsr310Converters}.
3553
*
36-
* @author Oliver Gierke & Barak Schoster
54+
* @author Oliver Gierke
55+
* @author Barak Schoster
3756
*/
57+
@RunWith(Suite.class)
58+
@SuiteClasses({ CommonTests.class, DurationConversionTests.class, PeriodConversionTests.class })
3859
public class Jsr310ConvertersUnitTests {
3960

4061
static final Date NOW = new Date();
@@ -51,127 +72,150 @@ public class Jsr310ConvertersUnitTests {
5172
CONVERSION_SERVICE = conversionService;
5273
}
5374

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 {
6276

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+
}
6885

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() {
7291

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+
}
8095

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+
}
86103

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() {
90109

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+
}
98113

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+
}
104121

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() {
108127

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+
}
114131

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() {
118137

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+
}
124141

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() {
128147

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() {
131154

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"));
135158

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);
139167
}
140168
}
141169

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+
}
144189

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> {
152191

192+
/**
193+
* @see DATACMNS-951
194+
*/
195+
@Parameters
196+
public static Collection<Object[]> data() {
153197

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+
});
157204
}
158205
}
159206

160-
@Test
161-
public void convertsPeriodToStringAndBack() {
207+
@RunWith(Parameterized.class)
208+
public static class ConversionTest<T> {
162209

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;
168212

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));
172219
}
173220
}
174-
private static String format(Date date, String format) {
175-
return new SimpleDateFormat(format).format(date);
176-
}
177221
}

0 commit comments

Comments
 (0)