Skip to content

Commit cb7de2a

Browse files
committed
Allow binding number to DataSize
Closes gh-14294
1 parent 9496e87 commit cb7de2a

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/ApplicationConversionService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public static void addApplicationConverters(ConverterRegistry registry) {
100100
registry.addConverter(new NumberToDurationConverter());
101101
registry.addConverter(new DurationToNumberConverter());
102102
registry.addConverter(new StringToDataSizeConverter());
103+
registry.addConverter(new NumberToDataSizeConverter());
103104
registry.addConverterFactory(new StringToEnumIgnoringCaseConverterFactory());
104105
}
105106

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.convert;
18+
19+
import java.util.Collections;
20+
import java.util.Set;
21+
22+
import org.springframework.core.convert.TypeDescriptor;
23+
import org.springframework.core.convert.converter.Converter;
24+
import org.springframework.core.convert.converter.GenericConverter;
25+
import org.springframework.util.unit.DataSize;
26+
27+
/**
28+
* {@link Converter} to convert from a {@link Number} to a {@link DataSize}.
29+
*
30+
* @author Stephane Nicoll
31+
* @see DataSizeUnit
32+
*/
33+
final class NumberToDataSizeConverter implements GenericConverter {
34+
35+
private final StringToDataSizeConverter delegate = new StringToDataSizeConverter();
36+
37+
@Override
38+
public Set<ConvertiblePair> getConvertibleTypes() {
39+
return Collections.singleton(new ConvertiblePair(Number.class, DataSize.class));
40+
}
41+
42+
@Override
43+
public Object convert(Object source, TypeDescriptor sourceType,
44+
TypeDescriptor targetType) {
45+
return this.delegate.convert((source != null) ? source.toString() : null,
46+
TypeDescriptor.valueOf(String.class), targetType);
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.convert;
18+
19+
import java.util.Collections;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.Parameterized;
24+
25+
import org.springframework.core.annotation.AnnotationUtils;
26+
import org.springframework.core.convert.ConversionService;
27+
import org.springframework.core.convert.TypeDescriptor;
28+
import org.springframework.util.unit.DataSize;
29+
import org.springframework.util.unit.DataUnit;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.mockito.BDDMockito.given;
33+
import static org.mockito.Mockito.mock;
34+
35+
/**
36+
* Tests for {@link NumberToDataSizeConverter}.
37+
*
38+
* @author Stephane Nicoll
39+
*/
40+
@RunWith(Parameterized.class)
41+
public class NumberToDataSizeConverterTests {
42+
43+
private final ConversionService conversionService;
44+
45+
public NumberToDataSizeConverterTests(String name,
46+
ConversionService conversionService) {
47+
this.conversionService = conversionService;
48+
}
49+
50+
@Test
51+
public void convertWhenSimpleWithoutSuffixShouldReturnDataSize() {
52+
assertThat(convert(10)).isEqualTo(DataSize.ofBytes(10));
53+
assertThat(convert(+10)).isEqualTo(DataSize.ofBytes(10));
54+
assertThat(convert(-10)).isEqualTo(DataSize.ofBytes(-10));
55+
}
56+
57+
@Test
58+
public void convertWhenSimpleWithoutSuffixButWithAnnotationShouldReturnDataSize() {
59+
assertThat(convert(10, DataUnit.KILOBYTES)).isEqualTo(DataSize.ofKiloBytes(10));
60+
assertThat(convert(+10, DataUnit.KILOBYTES)).isEqualTo(DataSize.ofKiloBytes(10));
61+
assertThat(convert(-10, DataUnit.KILOBYTES)).isEqualTo(DataSize.ofKiloBytes(-10));
62+
}
63+
64+
private DataSize convert(Integer source) {
65+
return this.conversionService.convert(source, DataSize.class);
66+
}
67+
68+
@SuppressWarnings({ "rawtypes", "unchecked" })
69+
private DataSize convert(Integer source, DataUnit defaultUnit) {
70+
TypeDescriptor targetType = mock(TypeDescriptor.class);
71+
if (defaultUnit != null) {
72+
DataSizeUnit unitAnnotation = AnnotationUtils.synthesizeAnnotation(
73+
Collections.singletonMap("value", defaultUnit), DataSizeUnit.class,
74+
null);
75+
given(targetType.getAnnotation(DataSizeUnit.class))
76+
.willReturn(unitAnnotation);
77+
}
78+
given(targetType.getType()).willReturn((Class) DataSize.class);
79+
return (DataSize) this.conversionService.convert(source,
80+
TypeDescriptor.forObject(source), targetType);
81+
}
82+
83+
@Parameterized.Parameters(name = "{0}")
84+
public static Iterable<Object[]> conversionServices() {
85+
return new ConversionServiceParameters(new NumberToDataSizeConverter());
86+
}
87+
88+
}

0 commit comments

Comments
 (0)