Polishing

This commit is contained in:
Juergen Hoeller
2023-07-15 14:20:00 +02:00
parent 6183f06846
commit 3a278cc66d
4 changed files with 39 additions and 36 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@@ -33,57 +33,56 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class ByteBufferConverterTests {
private GenericConversionService conversionService;
private final GenericConversionService conversionService = new DefaultConversionService();
@BeforeEach
void setup() {
this.conversionService = new DefaultConversionService();
this.conversionService.addConverter(new ByteArrayToOtherTypeConverter());
this.conversionService.addConverter(new OtherTypeToByteArrayConverter());
conversionService.addConverter(new ByteArrayToOtherTypeConverter());
conversionService.addConverter(new OtherTypeToByteArrayConverter());
}
@Test
void byteArrayToByteBuffer() throws Exception {
void byteArrayToByteBuffer() {
byte[] bytes = new byte[] { 1, 2, 3 };
ByteBuffer convert = this.conversionService.convert(bytes, ByteBuffer.class);
ByteBuffer convert = conversionService.convert(bytes, ByteBuffer.class);
assertThat(convert.array()).isNotSameAs(bytes);
assertThat(convert.array()).isEqualTo(bytes);
}
@Test
void byteBufferToByteArray() throws Exception {
void byteBufferToByteArray() {
byte[] bytes = new byte[] { 1, 2, 3 };
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
byte[] convert = this.conversionService.convert(byteBuffer, byte[].class);
byte[] convert = conversionService.convert(byteBuffer, byte[].class);
assertThat(convert).isNotSameAs(bytes);
assertThat(convert).isEqualTo(bytes);
}
@Test
void byteBufferToOtherType() throws Exception {
void byteBufferToOtherType() {
byte[] bytes = new byte[] { 1, 2, 3 };
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
OtherType convert = this.conversionService.convert(byteBuffer, OtherType.class);
OtherType convert = conversionService.convert(byteBuffer, OtherType.class);
assertThat(convert.bytes).isNotSameAs(bytes);
assertThat(convert.bytes).isEqualTo(bytes);
}
@Test
void otherTypeToByteBuffer() throws Exception {
void otherTypeToByteBuffer() {
byte[] bytes = new byte[] { 1, 2, 3 };
OtherType otherType = new OtherType(bytes);
ByteBuffer convert = this.conversionService.convert(otherType, ByteBuffer.class);
ByteBuffer convert = conversionService.convert(otherType, ByteBuffer.class);
assertThat(convert.array()).isNotSameAs(bytes);
assertThat(convert.array()).isEqualTo(bytes);
}
@Test
void byteBufferToByteBuffer() throws Exception {
void byteBufferToByteBuffer() {
byte[] bytes = new byte[] { 1, 2, 3 };
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
ByteBuffer convert = this.conversionService.convert(byteBuffer, ByteBuffer.class);
ByteBuffer convert = conversionService.convert(byteBuffer, ByteBuffer.class);
assertThat(convert).isNotSameAs(byteBuffer.rewind());
assertThat(convert).isEqualTo(byteBuffer.rewind());
assertThat(convert).isEqualTo(ByteBuffer.wrap(bytes));

View File

@@ -49,11 +49,11 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
class CollectionToCollectionConverterTests {
private GenericConversionService conversionService = new GenericConversionService();
private final GenericConversionService conversionService = new GenericConversionService();
@BeforeEach
void setUp() {
void setup() {
conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
}

View File

@@ -38,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Keith Donald
* @author Phil Webb
* @author Phillip Webb
* @author Juergen Hoeller
*/
class MapToMapConverterTests {
@@ -47,7 +47,7 @@ class MapToMapConverterTests {
@BeforeEach
void setUp() {
void setup() {
conversionService.addConverter(new MapToMapConverter(conversionService));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -18,6 +18,7 @@ package org.springframework.core.convert.support;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.convert.ConverterNotFoundException;
@@ -29,15 +30,19 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* Unit tests for {@link ObjectToObjectConverter}.
*
* @author Sam Brannen
* @author Phil Webb
* @author Phillip Webb
* @since 5.3.21
* @see org.springframework.core.convert.converter.DefaultConversionServiceTests#convertObjectToObjectUsingValueOfMethod()
*/
class ObjectToObjectConverterTests {
private final GenericConversionService conversionService = new GenericConversionService() {{
addConverter(new ObjectToObjectConverter());
}};
private final GenericConversionService conversionService = new GenericConversionService();
@BeforeEach
void setup() {
conversionService.addConverter(new ObjectToObjectConverter());
}
/**
@@ -47,7 +52,7 @@ class ObjectToObjectConverterTests {
@Test
void nonStaticToTargetTypeSimpleNameMethodWithMatchingReturnType() {
assertThat(conversionService.canConvert(Source.class, Data.class))
.as("can convert Source to Data").isTrue();
.as("can convert Source to Data").isTrue();
Data data = conversionService.convert(new Source("test"), Data.class);
assertThat(data).asString().isEqualTo("test");
}
@@ -55,21 +60,21 @@ class ObjectToObjectConverterTests {
@Test
void nonStaticToTargetTypeSimpleNameMethodWithDifferentReturnType() {
assertThat(conversionService.canConvert(Text.class, Data.class))
.as("can convert Text to Data").isFalse();
.as("can convert Text to Data").isFalse();
assertThat(conversionService.canConvert(Text.class, Optional.class))
.as("can convert Text to Optional").isFalse();
.as("can convert Text to Optional").isFalse();
assertThatExceptionOfType(ConverterNotFoundException.class)
.as("convert Text to Data")
.isThrownBy(() -> conversionService.convert(new Text("test"), Data.class));
.as("convert Text to Data")
.isThrownBy(() -> conversionService.convert(new Text("test"), Data.class));
}
@Test
void staticValueOfFactoryMethodWithDifferentReturnType() {
assertThat(conversionService.canConvert(String.class, Data.class))
.as("can convert String to Data").isFalse();
.as("can convert String to Data").isFalse();
assertThatExceptionOfType(ConverterNotFoundException.class)
.as("convert String to Data")
.isThrownBy(() -> conversionService.convert("test", Data.class));
.as("convert String to Data")
.isThrownBy(() -> conversionService.convert("test", Data.class));
}
@@ -84,9 +89,9 @@ class ObjectToObjectConverterTests {
public Data toData() {
return new Data(this.value);
}
}
static class Text {
private final String value;
@@ -98,9 +103,9 @@ class ObjectToObjectConverterTests {
public Optional<Data> toData() {
return Optional.of(new Data(this.value));
}
}
static class Data {
private final String value;
@@ -115,9 +120,8 @@ class ObjectToObjectConverterTests {
}
public static Optional<Data> valueOf(String string) {
return (string != null) ? Optional.of(new Data(string)) : Optional.empty();
return (string != null ? Optional.of(new Data(string)) : Optional.empty());
}
}
}