diff --git a/spring-core/src/test/java/org/springframework/core/annotation/MissingMergedAnnotationTests.java b/spring-core/src/test/java/org/springframework/core/annotation/MissingMergedAnnotationTests.java index 383536912f..151b31f3ae 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/MissingMergedAnnotationTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/MissingMergedAnnotationTests.java @@ -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 diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 01c40274fd..304dcb1537 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -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() { - @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)); } diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java index 42f4041ca9..56ec69a206 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java @@ -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 stream = Arrays.asList(1, 2, 3).stream(); + Stream 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 content = (List) 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 stream = Arrays.asList(1, 2, 3).stream(); + Stream 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 stream = Arrays.asList(1, 2, 3).stream(); + Stream 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 content = (List) 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 stream = Arrays.asList(1, 2, 3).stream(); + Stream 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 stream = Arrays.asList("1", "2", "3"); + List 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 content = (Stream) 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() { - @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 content = (Stream) 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 stream = Arrays.asList("1", "2", "3"); + List 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 content = (Stream) result; - StringBuilder sb = new StringBuilder(); - content.forEach(sb::append); - assertThat(sb.toString()).isEqualTo("123"); + assertThat(content).containsExactly("1", "2", "3"); } @Test diff --git a/spring-core/src/test/java/org/springframework/core/env/ProfilesTests.java b/spring-core/src/test/java/org/springframework/core/env/ProfilesTests.java index 3e39969799..01454ad10e 100644 --- a/spring-core/src/test/java/org/springframework/core/env/ProfilesTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/ProfilesTests.java @@ -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 supplier) { - assertThatIllegalArgumentException().isThrownBy( - supplier::get) + assertThatIllegalArgumentException() + .isThrownBy(supplier::get) .withMessageContaining("Malformed"); } diff --git a/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java b/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java index 5716a80650..9600680b36 100644 --- a/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java @@ -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); diff --git a/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferUtilsTests.java b/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferUtilsTests.java index ba85a1575b..d202478623 100644 --- a/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferUtilsTests.java @@ -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 flux = DataBufferUtils.readInputStream( - () -> this.resource.getInputStream(), super.bufferFactory, 3); + this.resource::getInputStream, super.bufferFactory, 3); verifyReadData(flux); } diff --git a/spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java b/spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java index c78f9a95f5..175002cbfb 100644 --- a/spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java +++ b/spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java @@ -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"); diff --git a/spring-core/src/test/java/org/springframework/util/concurrent/SettableListenableFutureTests.java b/spring-core/src/test/java/org/springframework/util/concurrent/SettableListenableFutureTests.java index a14ecf5f4e..5f1c5e0410 100644 --- a/spring-core/src/test/java/org/springframework/util/concurrent/SettableListenableFutureTests.java +++ b/spring-core/src/test/java/org/springframework/util/concurrent/SettableListenableFutureTests.java @@ -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(); diff --git a/spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTests.java b/spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTests.java index 504e69d6c7..3eb66addcd 100644 --- a/spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTests.java +++ b/spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTests.java @@ -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() { - @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; }