Add support for DataSize
This commit adds support for Spring Framework's `DataSize` allowing to express a size in bytes and other convenient units. Similar to the `Duration` support introduced previously, this commit adds transparent binding support as well as detection of default values in `@ConfigurationProperties`-annotated object. Closes gh-13974pull/14021/head
parent
78dd7bd934
commit
94013aaba6
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.docs.context.properties.bind;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.convert.DataSizeUnit;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
/**
|
||||
* A {@link ConfigurationProperties} example that uses {@link DataSize}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
// tag::example[]
|
||||
@ConfigurationProperties("app.io")
|
||||
public class AppIoProperties {
|
||||
|
||||
@DataSizeUnit(DataUnit.MEGABYTES)
|
||||
private DataSize bufferSize = DataSize.ofMegaBytes(2);
|
||||
|
||||
private DataSize sizeThreshold = DataSize.ofBytes(512);
|
||||
|
||||
public DataSize getBufferSize() {
|
||||
return this.bufferSize;
|
||||
}
|
||||
|
||||
public void setBufferSize(DataSize bufferSize) {
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
public DataSize getSizeThreshold() {
|
||||
return this.sizeThreshold;
|
||||
}
|
||||
|
||||
public void setSizeThreshold(DataSize sizeThreshold) {
|
||||
this.sizeThreshold = sizeThreshold;
|
||||
}
|
||||
|
||||
}
|
||||
// end::example[]
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.convert;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
/**
|
||||
* Annotation that can be used to change the default unit used when converting a
|
||||
* {@link DataSize}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DataSizeUnit {
|
||||
|
||||
/**
|
||||
* The {@link DataUnit} to use if one is not specified.
|
||||
* @return the data unit
|
||||
*/
|
||||
DataUnit value();
|
||||
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.convert;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.GenericConverter;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
/**
|
||||
* {@link Converter} to convert from a {@link String} to a {@link DataSize}. Supports
|
||||
* {@link DataSize#parse(CharSequence)}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @see DataSizeUnit
|
||||
*/
|
||||
final class StringToDataSizeConverter implements GenericConverter {
|
||||
|
||||
@Override
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return Collections.singleton(new ConvertiblePair(String.class, DataSize.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Object source, TypeDescriptor sourceType,
|
||||
TypeDescriptor targetType) {
|
||||
if (ObjectUtils.isEmpty(source)) {
|
||||
return null;
|
||||
}
|
||||
return convert(source.toString(), getDataUnit(targetType));
|
||||
}
|
||||
|
||||
private DataUnit getDataUnit(TypeDescriptor targetType) {
|
||||
DataSizeUnit annotation = targetType.getAnnotation(DataSizeUnit.class);
|
||||
return (annotation != null) ? annotation.value() : null;
|
||||
}
|
||||
|
||||
private DataSize convert(String source, DataUnit unit) {
|
||||
return DataSize.parse(source, unit);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.convert;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Create a mock {@link TypeDescriptor} with optional {@link DataUnit} annotation.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public final class MockDataSizeTypeDescriptor {
|
||||
|
||||
private MockDataSizeTypeDescriptor() {
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public static TypeDescriptor get(DataUnit unit) {
|
||||
TypeDescriptor descriptor = mock(TypeDescriptor.class);
|
||||
if (unit != null) {
|
||||
DataSizeUnit unitAnnotation = AnnotationUtils.synthesizeAnnotation(
|
||||
Collections.singletonMap("value", unit), DataSizeUnit.class, null);
|
||||
given(descriptor.getAnnotation(DataSizeUnit.class))
|
||||
.willReturn(unitAnnotation);
|
||||
}
|
||||
given(descriptor.getType()).willReturn((Class) DataSize.class);
|
||||
given(descriptor.getObjectType()).willReturn((Class) DataSize.class);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.convert;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.util.unit.DataSize;
|
||||
import org.springframework.util.unit.DataUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link StringToDataSizeConverter}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class StringToDataSizeConverterTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private final ConversionService conversionService;
|
||||
|
||||
public StringToDataSizeConverterTests(String name,
|
||||
ConversionService conversionService) {
|
||||
this.conversionService = conversionService;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenSimpleBytesShouldReturnDataSize() {
|
||||
assertThat(convert("10B")).isEqualTo(DataSize.ofBytes(10));
|
||||
assertThat(convert("+10B")).isEqualTo(DataSize.ofBytes(10));
|
||||
assertThat(convert("-10B")).isEqualTo(DataSize.ofBytes(-10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenSimpleKiloBytesShouldReturnDataSize() {
|
||||
assertThat(convert("10KB")).isEqualTo(DataSize.ofKiloBytes(10));
|
||||
assertThat(convert("+10KB")).isEqualTo(DataSize.ofKiloBytes(10));
|
||||
assertThat(convert("-10KB")).isEqualTo(DataSize.ofKiloBytes(-10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenSimpleMegaBytesShouldReturnDataSize() {
|
||||
assertThat(convert("10MB")).isEqualTo(DataSize.ofMegaBytes(10));
|
||||
assertThat(convert("+10MB")).isEqualTo(DataSize.ofMegaBytes(10));
|
||||
assertThat(convert("-10MB")).isEqualTo(DataSize.ofMegaBytes(-10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenSimpleGigaBytesShouldReturnDataSize() {
|
||||
assertThat(convert("10GB")).isEqualTo(DataSize.ofGigaBytes(10));
|
||||
assertThat(convert("+10GB")).isEqualTo(DataSize.ofGigaBytes(10));
|
||||
assertThat(convert("-10GB")).isEqualTo(DataSize.ofGigaBytes(-10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenSimpleTeraBytesShouldReturnDataSize() {
|
||||
assertThat(convert("10TB")).isEqualTo(DataSize.ofTeraBytes(10));
|
||||
assertThat(convert("+10TB")).isEqualTo(DataSize.ofTeraBytes(10));
|
||||
assertThat(convert("-10TB")).isEqualTo(DataSize.ofTeraBytes(-10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenSimpleWithoutSuffixShouldReturnDataSize() {
|
||||
assertThat(convert("10")).isEqualTo(DataSize.ofBytes(10));
|
||||
assertThat(convert("+10")).isEqualTo(DataSize.ofBytes(10));
|
||||
assertThat(convert("-10")).isEqualTo(DataSize.ofBytes(-10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenSimpleWithoutSuffixButWithAnnotationShouldReturnDataSize() {
|
||||
assertThat(convert("10", DataUnit.KILOBYTES)).isEqualTo(DataSize.ofKiloBytes(10));
|
||||
assertThat(convert("+10", DataUnit.KILOBYTES))
|
||||
.isEqualTo(DataSize.ofKiloBytes(10));
|
||||
assertThat(convert("-10", DataUnit.KILOBYTES))
|
||||
.isEqualTo(DataSize.ofKiloBytes(-10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenBadFormatShouldThrowException() {
|
||||
this.thrown.expect(ConversionFailedException.class);
|
||||
this.thrown.expectMessage("'10WB' is not a valid data size");
|
||||
convert("10WB");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertWhenEmptyShouldReturnNull() {
|
||||
assertThat(convert("")).isNull();
|
||||
}
|
||||
|
||||
private DataSize convert(String source) {
|
||||
return this.conversionService.convert(source, DataSize.class);
|
||||
}
|
||||
|
||||
private DataSize convert(String source, DataUnit unit) {
|
||||
return (DataSize) this.conversionService.convert(source,
|
||||
TypeDescriptor.forObject(source), MockDataSizeTypeDescriptor.get(unit));
|
||||
}
|
||||
|
||||
@Parameters(name = "{0}")
|
||||
public static Iterable<Object[]> conversionServices() {
|
||||
return new ConversionServiceParameters(new StringToDataSizeConverter());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue