Merge branch '5.3.x'

This commit is contained in:
Sam Brannen 2022-01-09 16:17:42 +01:00
commit 5d508de2c0
9 changed files with 44 additions and 93 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -257,7 +257,7 @@ class MissingMergedAnnotationTests {
@Test
void synthesizeThrowsNoSuchElementException() {
assertThatNoSuchElementException().isThrownBy(() -> this.missing.synthesize());
assertThatNoSuchElementException().isThrownBy(this.missing::synthesize);
}
@Test

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -190,12 +190,7 @@ class GenericConversionServiceTests {
@Test
void convertSuperSourceType() {
conversionService.addConverter(new Converter<CharSequence, Integer>() {
@Override
public Integer convert(CharSequence source) {
return Integer.valueOf(source.toString());
}
});
conversionService.addConverter(CharSequence.class, Integer.class, source -> Integer.valueOf(source.toString()));
Integer result = conversionService.convert("3", Integer.class);
assertThat((int) result).isEqualTo((int) Integer.valueOf(3));
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -26,16 +26,18 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.InstanceOfAssertFactories.list;
import static org.assertj.core.api.InstanceOfAssertFactories.stream;
/**
* Tests for {@link StreamConverter}.
*
* @author Stephane Nicoll
* @author Sam Brannen
* @since 4.2
*/
class StreamConverterTests {
@ -57,60 +59,41 @@ class StreamConverterTests {
@Test
void convertFromStreamToList() throws NoSuchFieldException {
this.conversionService.addConverter(Number.class, String.class, new ObjectToStringConverter());
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
Stream<Integer> stream = Stream.of(1, 2, 3);
TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("listOfStrings"));
Object result = this.conversionService.convert(stream, listOfStrings);
assertThat(result).as("Converted object must not be null").isNotNull();
boolean condition = result instanceof List;
assertThat(condition).as("Converted object must be a list").isTrue();
@SuppressWarnings("unchecked")
List<String> content = (List<String>) result;
assertThat(content.get(0)).isEqualTo("1");
assertThat(content.get(1)).isEqualTo("2");
assertThat(content.get(2)).isEqualTo("3");
assertThat(content.size()).as("Wrong number of elements").isEqualTo(3);
assertThat(result).asInstanceOf(list(String.class)).containsExactly("1", "2", "3");
}
@Test
void convertFromStreamToArray() throws NoSuchFieldException {
this.conversionService.addConverterFactory(new NumberToNumberConverterFactory());
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
Stream<Integer> stream = Stream.of(1, 2, 3);
TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs"));
Object result = this.conversionService.convert(stream, arrayOfLongs);
assertThat(result).as("Converted object must not be null").isNotNull();
assertThat(result.getClass().isArray()).as("Converted object must be an array").isTrue();
Long[] content = (Long[]) result;
assertThat(content[0]).isEqualTo(Long.valueOf(1L));
assertThat(content[1]).isEqualTo(Long.valueOf(2L));
assertThat(content[2]).isEqualTo(Long.valueOf(3L));
assertThat(content.length).as("Wrong number of elements").isEqualTo(3);
assertThat(content).containsExactly(1L, 2L, 3L);
}
@Test
void convertFromStreamToRawList() throws NoSuchFieldException {
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
Stream<Integer> stream = Stream.of(1, 2, 3);
TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("rawList"));
Object result = this.conversionService.convert(stream, listOfStrings);
assertThat(result).as("Converted object must not be null").isNotNull();
boolean condition = result instanceof List;
assertThat(condition).as("Converted object must be a list").isTrue();
@SuppressWarnings("unchecked")
List<Object> content = (List<Object>) result;
assertThat(content.get(0)).isEqualTo(1);
assertThat(content.get(1)).isEqualTo(2);
assertThat(content.get(2)).isEqualTo(3);
assertThat(content.size()).as("Wrong number of elements").isEqualTo(3);
assertThat(result).asInstanceOf(list(Object.class)).containsExactly(1, 2, 3);
}
@Test
void convertFromStreamToArrayNoConverter() throws NoSuchFieldException {
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
Stream<Integer> stream = Stream.of(1, 2, 3);
TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs"));
assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() ->
this.conversionService.convert(stream, arrayOfLongs))
assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> this.conversionService.convert(stream, arrayOfLongs))
.withCauseInstanceOf(ConverterNotFoundException.class);
}
@ -118,13 +101,11 @@ class StreamConverterTests {
@SuppressWarnings("resource")
void convertFromListToStream() throws NoSuchFieldException {
this.conversionService.addConverterFactory(new StringToNumberConverterFactory());
List<String> stream = Arrays.asList("1", "2", "3");
List<String> list = Arrays.asList("1", "2", "3");
TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("streamOfIntegers"));
Object result = this.conversionService.convert(stream, streamOfInteger);
Object result = this.conversionService.convert(list, streamOfInteger);
assertThat(result).as("Converted object must not be null").isNotNull();
boolean condition = result instanceof Stream;
assertThat(condition).as("Converted object must be a stream").isTrue();
assertThat(result).as("Converted object must be a stream").isInstanceOf(Stream.class);
@SuppressWarnings("unchecked")
Stream<Integer> content = (Stream<Integer>) result;
assertThat(content.mapToInt(x -> x).sum()).isEqualTo(6);
@ -133,39 +114,25 @@ class StreamConverterTests {
@Test
@SuppressWarnings("resource")
void convertFromArrayToStream() throws NoSuchFieldException {
Integer[] stream = new Integer[] {1, 0, 1};
this.conversionService.addConverter(new Converter<Integer, Boolean>() {
@Override
public Boolean convert(Integer source) {
return source == 1;
}
});
Integer[] array = new Integer[] {1, 0, 1};
this.conversionService.addConverter(Integer.class, Boolean.class, source -> source == 1);
TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans"));
Object result = this.conversionService.convert(stream, streamOfBoolean);
Object result = this.conversionService.convert(array, streamOfBoolean);
assertThat(result).as("Converted object must not be null").isNotNull();
boolean condition = result instanceof Stream;
assertThat(condition).as("Converted object must be a stream").isTrue();
@SuppressWarnings("unchecked")
Stream<Boolean> content = (Stream<Boolean>) result;
assertThat(content.filter(x -> x).count()).isEqualTo(2);
assertThat(result).asInstanceOf(stream(Boolean.class)).filteredOn(x -> x).hasSize(2);
}
@Test
@SuppressWarnings("resource")
void convertFromListToRawStream() throws NoSuchFieldException {
List<String> stream = Arrays.asList("1", "2", "3");
List<String> list = Arrays.asList("1", "2", "3");
TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("rawStream"));
Object result = this.conversionService.convert(stream, streamOfInteger);
Object result = this.conversionService.convert(list, streamOfInteger);
assertThat(result).as("Converted object must not be null").isNotNull();
boolean condition = result instanceof Stream;
assertThat(condition).as("Converted object must be a stream").isTrue();
assertThat(result).as("Converted object must be a stream").isInstanceOf(Stream.class);
@SuppressWarnings("unchecked")
Stream<Object> content = (Stream<Object>) result;
StringBuilder sb = new StringBuilder();
content.forEach(sb::append);
assertThat(sb.toString()).isEqualTo("123");
assertThat(content).containsExactly("1", "2", "3");
}
@Test

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -47,8 +47,8 @@ class ProfilesTests {
@Test
void ofWhenEmptyThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() ->
Profiles.of())
assertThatIllegalArgumentException()
.isThrownBy(Profiles::of)
.withMessageContaining("Must specify at least one profile");
}
@ -354,8 +354,8 @@ class ProfilesTests {
private static void assertMalformed(Supplier<Profiles> supplier) {
assertThatIllegalArgumentException().isThrownBy(
supplier::get)
assertThatIllegalArgumentException()
.isThrownBy(supplier::get)
.withMessageContaining("Malformed");
}

View File

@ -209,7 +209,7 @@ public class StandardEnvironmentTests {
void defaultProfileWithCircularPlaceholder() {
try {
System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "${spring.profiles.default}");
assertThatIllegalArgumentException().isThrownBy(() -> environment.getDefaultProfiles());
assertThatIllegalArgumentException().isThrownBy(environment::getDefaultProfiles);
}
finally {
System.clearProperty(DEFAULT_PROFILES_PROPERTY_NAME);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -80,7 +80,7 @@ class DataBufferUtilsTests extends AbstractDataBufferAllocatingTests {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> flux = DataBufferUtils.readInputStream(
() -> this.resource.getInputStream(), super.bufferFactory, 3);
this.resource::getInputStream, super.bufferFactory, 3);
verifyReadData(flux);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -16,8 +16,6 @@
package org.springframework.core.task;
import java.util.concurrent.ThreadFactory;
import org.junit.jupiter.api.Test;
import org.springframework.util.ConcurrencyThrottleSupport;
@ -61,12 +59,7 @@ class SimpleAsyncTaskExecutorTests {
@Test
void threadFactoryOverridesDefaults() throws Exception {
final Object monitor = new Object();
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "test");
}
});
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(runnable -> new Thread(runnable, "test"));
ThreadNameHarvester task = new ThreadNameHarvester(monitor);
executeAndWait(executor, task, monitor);
assertThat(task.getThreadName()).isEqualTo("test");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -332,8 +332,7 @@ class SettableListenableFutureTests {
void cancelStateThrowsExceptionWhenCallingGet() throws ExecutionException, InterruptedException {
settableListenableFuture.cancel(true);
assertThatExceptionOfType(CancellationException.class).isThrownBy(() ->
settableListenableFuture.get());
assertThatExceptionOfType(CancellationException.class).isThrownBy(settableListenableFuture::get);
assertThat(settableListenableFuture.isCancelled()).isTrue();
assertThat(settableListenableFuture.isDone()).isTrue();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -183,12 +183,9 @@ abstract class AbstractStaxXMLReaderTests {
ContentHandler contentHandler = mock(ContentHandler.class);
willAnswer(new CopyCharsAnswer()).given(contentHandler).characters(any(char[].class), anyInt(), anyInt());
willAnswer(new CopyCharsAnswer()).given(contentHandler).ignorableWhitespace(any(char[].class), anyInt(), anyInt());
willAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
return null;
}
willAnswer(invocation -> {
invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
return null;
}).given(contentHandler).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
return contentHandler;
}