Skip to content

Commit

Permalink
Use idiomatic AssertJ assertions
Browse files Browse the repository at this point in the history
1. Replace assertThat(x.isEmpty()).isTrue() with assertThat(x).isEmpty()
Search for : assertThat\((.+).isEmpty\(\)\).isTrue\(\)
Replace with : assertThat($1).isEmpty()
Search for : assertThat\((.+).isEmpty\(\)\).isFalse\(\)
Replace with : assertThat($1).isNotEmpty()

2. Replace assertThat(x.iterator().next()) with assertThat(x).element(0)
Search for : assertThat\((.+).iterator\(\).next\(\)\)
Replace with : assertThat($1).element(0)

3. Replace assertThat(x.get(i)). with assertThat(x).element(i).
Search for : assertThat\((.+)\.get\((\d+)\)\)\.
Replace with : assertThat($1).element($2).
  • Loading branch information
quaff committed Dec 7, 2023
1 parent 359a6cb commit e0a3fe0
Show file tree
Hide file tree
Showing 29 changed files with 110 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ void autoConfiguresObservationHandlers() {
assertThat(handlers).hasSize(2);
// Multiple MeterObservationHandler are wrapped in
// FirstMatchingCompositeObservationHandler, which calls only the first one
assertThat(handlers.get(0)).isInstanceOf(CustomMeterObservationHandler.class);
assertThat(handlers).element(0).isInstanceOf(CustomMeterObservationHandler.class);
assertThat(((CustomMeterObservationHandler) handlers.get(0)).getName())
.isEqualTo("customMeterObservationHandler1");
// Regular handlers are registered last
assertThat(handlers.get(1)).isInstanceOf(CustomObservationHandler.class);
assertThat(handlers).element(1).isInstanceOf(CustomObservationHandler.class);
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
});
Expand All @@ -268,7 +268,7 @@ void autoConfiguresObservationHandlerWithCustomContext() {
CustomContext customContext = new CustomContext();
Observation.start("test-observation", () -> customContext, observationRegistry).stop();
assertThat(handlers).hasSize(1);
assertThat(handlers.get(0)).isInstanceOf(ObservationHandlerWithCustomContext.class);
assertThat(handlers).element(0).isInstanceOf(ObservationHandlerWithCustomContext.class);
assertThat(context).hasSingleBean(DefaultMeterObservationHandler.class);
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
});
Expand All @@ -283,7 +283,7 @@ void autoConfiguresTracingAwareMeterObservationHandler() {
// TracingAwareMeterObservationHandler that we don't test here
Observation.start("test-observation", observationRegistry);
assertThat(handlers).hasSize(1);
assertThat(handlers.get(0)).isInstanceOf(CustomTracingObservationHandler.class);
assertThat(handlers).element(0).isInstanceOf(CustomTracingObservationHandler.class);
assertThat(context).hasSingleBean(TracingAwareMeterObservationHandler.class);
assertThat(context.getBeansOfType(ObservationHandler.class)).hasSize(2);
});
Expand All @@ -298,16 +298,16 @@ void autoConfiguresObservationHandlerWhenTracingIsActive() {
assertThat(handlers).hasSize(3);
// Multiple TracingObservationHandler are wrapped in
// FirstMatchingCompositeObservationHandler, which calls only the first one
assertThat(handlers.get(0)).isInstanceOf(CustomTracingObservationHandler.class);
assertThat(handlers).element(0).isInstanceOf(CustomTracingObservationHandler.class);
assertThat(((CustomTracingObservationHandler) handlers.get(0)).getName())
.isEqualTo("customTracingHandler1");
// Multiple MeterObservationHandler are wrapped in
// FirstMatchingCompositeObservationHandler, which calls only the first one
assertThat(handlers.get(1)).isInstanceOf(CustomMeterObservationHandler.class);
assertThat(handlers).element(1).isInstanceOf(CustomMeterObservationHandler.class);
assertThat(((CustomMeterObservationHandler) handlers.get(1)).getName())
.isEqualTo("customMeterObservationHandler1");
// Regular handlers are registered last
assertThat(handlers.get(2)).isInstanceOf(CustomObservationHandler.class);
assertThat(handlers).element(2).isInstanceOf(CustomObservationHandler.class);
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ void shouldGroupCategoriesIntoFirstMatchingHandlerAndRespectCategoryOrder() {
List<ObservationHandler<?>> handlers = getObservationHandlers(config);
assertThat(handlers).hasSize(2);
// Category A is first
assertThat(handlers.get(0)).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
assertThat(handlers).element(0).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
FirstMatchingCompositeObservationHandler firstMatching0 = (FirstMatchingCompositeObservationHandler) handlers
.get(0);
assertThat(firstMatching0.getHandlers()).containsExactly(handlerA1, handlerA2);
// Category B is second
assertThat(handlers.get(1)).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
assertThat(handlers).element(1).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
FirstMatchingCompositeObservationHandler firstMatching1 = (FirstMatchingCompositeObservationHandler) handlers
.get(1);
assertThat(firstMatching1.getHandlers()).containsExactly(handlerB1, handlerB2);
Expand All @@ -72,12 +72,12 @@ void uncategorizedHandlersShouldBeOrderedAfterCategories() {
List<ObservationHandler<?>> handlers = getObservationHandlers(config);
assertThat(handlers).hasSize(2);
// Category A is first
assertThat(handlers.get(0)).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
assertThat(handlers).element(0).isInstanceOf(FirstMatchingCompositeObservationHandler.class);
FirstMatchingCompositeObservationHandler firstMatching0 = (FirstMatchingCompositeObservationHandler) handlers
.get(0);
// Uncategorized handlers follow
assertThat(firstMatching0.getHandlers()).containsExactly(handlerA1, handlerA2);
assertThat(handlers.get(1)).isEqualTo(handlerB1);
assertThat(handlers).element(1).isEqualTo(handlerB1);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void permitAllForHealth() {
@Test
void securesEverythingElse() {
this.contextRunner.run((context) -> {
assertThat(getAuthenticateHeader(context, "/actuator").get(0)).contains("Basic realm=");
assertThat(getAuthenticateHeader(context, "/actuator")).element(0).asString().contains("Basic realm=");
assertThat(getAuthenticateHeader(context, "/foo").toString()).contains("Basic realm=");
});
}
Expand All @@ -91,7 +91,7 @@ void securesEverythingElse() {
void usesMatchersBasedOffConfiguredActuatorBasePath() {
this.contextRunner.withPropertyValues("management.endpoints.web.base-path=/").run((context) -> {
assertThat(getAuthenticateHeader(context, "/health")).isNull();
assertThat(getAuthenticateHeader(context, "/foo").get(0)).contains("Basic realm=");
assertThat(getAuthenticateHeader(context, "/foo")).element(0).asString().contains("Basic realm=");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void shouldSupplyBeansInCorrectOrder() {
.isInstanceOf(PropagatingReceiverTracingObservationHandler.class);
assertThat(tracingObservationHandlers.get(1))
.isInstanceOf(PropagatingSenderTracingObservationHandler.class);
assertThat(tracingObservationHandlers.get(2)).isInstanceOf(DefaultTracingObservationHandler.class);
assertThat(tracingObservationHandlers).element(2).isInstanceOf(DefaultTracingObservationHandler.class);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void testList() {
Map<String, Object> map = foo.getProperties();
assertThat(map).isNotNull();
assertThat(map).hasSize(3);
assertThat(((List<String>) map.get("list")).get(0)).isEqualTo("foo");
assertThat(((List<String>) map.get("list"))).element(0).isEqualTo("foo");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ void descriptorWithSimpleList() {
assertThat(properties.get("simpleList")).isInstanceOf(List.class);
List<String> list = (List<String>) properties.get("simpleList");
assertThat(list).hasSize(2);
assertThat(list.get(0)).isEqualTo("a");
assertThat(list.get(1)).isEqualTo("b");
assertThat(list).element(0).isEqualTo("a");
assertThat(list).element(1).isEqualTo("b");
}, (inputs) -> {
List<Object> list = (List<Object>) inputs.get("simpleList");
assertThat(list).hasSize(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ void autoConfiguredCacheManagerCanBeSwapped() {
getCacheManager(context, SimpleCacheManager.class);
CacheManagerPostProcessor postProcessor = context.getBean(CacheManagerPostProcessor.class);
assertThat(postProcessor.cacheManagers).hasSize(1);
assertThat(postProcessor.cacheManagers.get(0)).isInstanceOf(CaffeineCacheManager.class);
assertThat(postProcessor.cacheManagers).element(0).isInstanceOf(CaffeineCacheManager.class);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ void addNewConverters() {
HttpMessageConverter<?> converter1 = mock(HttpMessageConverter.class);
HttpMessageConverter<?> converter2 = mock(HttpMessageConverter.class);
HttpMessageConverters converters = new HttpMessageConverters(converter1, converter2);
assertThat(converters.getConverters().get(0)).isEqualTo(converter1);
assertThat(converters.getConverters().get(1)).isEqualTo(converter2);
assertThat(converters.getConverters()).element(0).isEqualTo(converter1);
assertThat(converters.getConverters()).element(1).isEqualTo(converter2);
}

@Test
Expand All @@ -103,8 +103,8 @@ void convertersAreAddedToFormPartConverter() {
HttpMessageConverter<?> converter2 = mock(HttpMessageConverter.class);
List<HttpMessageConverter<?>> converters = new HttpMessageConverters(converter1, converter2).getConverters();
List<HttpMessageConverter<?>> partConverters = extractFormPartConverters(converters);
assertThat(partConverters.get(0)).isEqualTo(converter1);
assertThat(partConverters.get(1)).isEqualTo(converter2);
assertThat(partConverters).element(0).isEqualTo(converter1);
assertThat(partConverters).element(1).isEqualTo(converter2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void userProvidedCustomizerCanOverrideJacksonCodecCustomizer() {
.run((context) -> {
List<CodecCustomizer> codecCustomizers = context.getBean(CodecCustomizers.class).codecCustomizers;
assertThat(codecCustomizers).hasSize(3);
assertThat(codecCustomizers.get(2)).isInstanceOf(TestCodecCustomizer.class);
assertThat(codecCustomizers).element(2).isInstanceOf(TestCodecCustomizer.class);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void setTrustedPackages() {
.createConnectionFactory(ActiveMQConnectionFactory.class);
assertThat(factory.isTrustAllPackages()).isFalse();
assertThat(factory.getTrustedPackages()).hasSize(1);
assertThat(factory.getTrustedPackages().get(0)).isEqualTo("trusted.package");
assertThat(factory.getTrustedPackages()).element(0).isEqualTo("trusted.package");
}

private ActiveMQConnectionFactoryFactory createFactory(ActiveMQProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ void restTemplateWhenHasCustomBuilderShouldUseCustomBuilder() {
assertThat(context).hasSingleBean(RestTemplate.class);
RestTemplate restTemplate = context.getBean(RestTemplate.class);
assertThat(restTemplate.getMessageConverters()).hasSize(1);
assertThat(restTemplate.getMessageConverters().get(0)).isInstanceOf(CustomHttpMessageConverter.class);
assertThat(restTemplate.getMessageConverters()).element(0)
.isInstanceOf(CustomHttpMessageConverter.class);
then(context.getBean(RestTemplateCustomizer.class)).shouldHaveNoInteractions();
});
}
Expand All @@ -150,7 +151,8 @@ void restTemplateWhenHasCustomBuilderCouldReuseBuilderConfigurer() {
assertThat(context).hasSingleBean(RestTemplate.class);
RestTemplate restTemplate = context.getBean(RestTemplate.class);
assertThat(restTemplate.getMessageConverters()).hasSize(1);
assertThat(restTemplate.getMessageConverters().get(0)).isInstanceOf(CustomHttpMessageConverter.class);
assertThat(restTemplate.getMessageConverters()).element(0)
.isInstanceOf(CustomHttpMessageConverter.class);
RestTemplateCustomizer customizer = context.getBean(RestTemplateCustomizer.class);
then(customizer).should().customize(restTemplate);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ void filterIsOrderedHighest() {
load(OrderedConfiguration.class);
List<Filter> beans = new ArrayList<>(this.context.getBeansOfType(Filter.class).values());
AnnotationAwareOrderComparator.sort(beans);
assertThat(beans.get(0)).isInstanceOf(CharacterEncodingFilter.class);
assertThat(beans.get(1)).isInstanceOf(HiddenHttpMethodFilter.class);
assertThat(beans).element(0).isInstanceOf(CharacterEncodingFilter.class);
assertThat(beans).element(1).isInstanceOf(HiddenHttpMethodFilter.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void resourceHandlerMappingOverrideWebjars() {
this.contextRunner.withUserConfiguration(WebJars.class).run((context) -> {
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
assertThat(locations.get("/webjars/**")).hasSize(1);
assertThat(locations.get("/webjars/**").get(0)).isEqualTo(new ClassPathResource("/foo/"));
assertThat(locations.get("/webjars/**")).element(0).isEqualTo(new ClassPathResource("/foo/"));
});
}

Expand All @@ -228,7 +228,7 @@ void resourceHandlerMappingOverrideAll() {
this.contextRunner.withUserConfiguration(AllResources.class).run((context) -> {
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
assertThat(locations.get("/**")).hasSize(1);
assertThat(locations.get("/**").get(0)).isEqualTo(new ClassPathResource("/foo/"));
assertThat(locations.get("/**")).element(0).isEqualTo(new ClassPathResource("/foo/"));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ void triggerReload() throws Exception {
this.server.triggerReload();
List<String> messages = await().atMost(Duration.ofSeconds(10))
.until(handler::getMessages, (msgs) -> msgs.size() == 2);
assertThat(messages.get(0)).contains("http://livereload.com/protocols/official-7");
assertThat(messages.get(1)).contains("command\":\"reload\"");
assertThat(messages).element(0).asString().contains("http://livereload.com/protocols/official-7");
assertThat(messages).element(1).asString().contains("command\":\"reload\"");
}

@Test // gh-26813
Expand All @@ -125,8 +125,8 @@ void triggerReloadWithUppercaseHeaders() throws Exception {
this.server.triggerReload();
List<String> messages = await().atMost(Duration.ofSeconds(10))
.until(handler::getMessages, (msgs) -> msgs.size() == 2);
assertThat(messages.get(0)).contains("http://livereload.com/protocols/official-7");
assertThat(messages.get(1)).contains("command\":\"reload\"");
assertThat(messages).element(0).asString().contains("http://livereload.com/protocols/official-7");
assertThat(messages).element(1).asString().contains("command\":\"reload\"");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void resolveAllWithBuilderBuildpackReferenceReturnsExpectedBuildpack() {
BuildpackReference reference = BuildpackReference.of("urn:cnb:builder:paketo-buildpacks/[email protected]");
Buildpacks buildpacks = BuildpackResolvers.resolveAll(this.resolverContext, Collections.singleton(reference));
assertThat(buildpacks.getBuildpacks()).hasSize(1);
assertThat(buildpacks.getBuildpacks().get(0)).isInstanceOf(BuilderBuildpack.class);
assertThat(buildpacks.getBuildpacks()).element(0).isInstanceOf(BuilderBuildpack.class);
}

@Test
Expand All @@ -67,7 +67,7 @@ void resolveAllWithDirectoryBuildpackReferenceReturnsExpectedBuildpack(@TempDir
BuildpackReference reference = BuildpackReference.of(temp.toAbsolutePath().toString());
Buildpacks buildpacks = BuildpackResolvers.resolveAll(this.resolverContext, Collections.singleton(reference));
assertThat(buildpacks.getBuildpacks()).hasSize(1);
assertThat(buildpacks.getBuildpacks().get(0)).isInstanceOf(DirectoryBuildpack.class);
assertThat(buildpacks.getBuildpacks()).element(0).isInstanceOf(DirectoryBuildpack.class);
}

@Test
Expand All @@ -77,7 +77,7 @@ void resolveAllWithTarGzipBuildpackReferenceReturnsExpectedBuildpack(@TempDir Fi
BuildpackReference reference = BuildpackReference.of(archive.toString());
Buildpacks buildpacks = BuildpackResolvers.resolveAll(this.resolverContext, Collections.singleton(reference));
assertThat(buildpacks.getBuildpacks()).hasSize(1);
assertThat(buildpacks.getBuildpacks().get(0)).isInstanceOf(TarGzipBuildpack.class);
assertThat(buildpacks.getBuildpacks()).element(0).isInstanceOf(TarGzipBuildpack.class);
}

@Test
Expand All @@ -89,7 +89,7 @@ void resolveAllWithImageBuildpackReferenceReturnsExpectedBuildpack() throws IOEx
BuildpackReference reference = BuildpackReference.of("docker://example/buildpack1:latest");
Buildpacks buildpacks = BuildpackResolvers.resolveAll(resolverContext, Collections.singleton(reference));
assertThat(buildpacks.getBuildpacks()).hasSize(1);
assertThat(buildpacks.getBuildpacks().get(0)).isInstanceOf(ImageBuildpack.class);
assertThat(buildpacks.getBuildpacks()).element(0).isInstanceOf(ImageBuildpack.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ void readAllWhenAnsiStreamReturnsEvents() throws Exception {
List<LogUpdateEvent> events = readAll("log-update-event-ansi.stream");
assertThat(events).hasSize(20);
assertThat(events.get(0).toString()).isEmpty();
assertThat(events.get(1)).hasToString("Cloud Foundry OpenJDK Buildpack v1.0.64");
assertThat(events.get(2)).hasToString(" OpenJDK JRE 11.0.5: Reusing cached layer");
assertThat(events).element(1).hasToString("Cloud Foundry OpenJDK Buildpack v1.0.64");
assertThat(events).element(2).hasToString(" OpenJDK JRE 11.0.5: Reusing cached layer");
}

@Test
void readSucceedsWhenStreamTypeIsInvalid() throws IOException {
List<LogUpdateEvent> events = readAll("log-update-event-invalid-stream-type.stream");
assertThat(events).hasSize(1);
assertThat(events.get(0)).hasToString("Stream type is out of bounds. Must be >= 0 and < 3, but was 3");
assertThat(events).element(0).hasToString("Stream type is out of bounds. Must be >= 0 and < 3, but was 3");
}

private List<LogUpdateEvent> readAll(String name) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ void subSequenceWhenStartPastExistingEndShouldThrowException() {

@Test
void isEmptyWhenEmptyShouldReturnTrue() {
assertThat(new StringSequence("").isEmpty()).isTrue();
assertThat(new StringSequence("")).isEmpty();
}

@Test
void isEmptyWhenNotEmptyShouldReturnFalse() {
assertThat(new StringSequence("x").isEmpty()).isFalse();
assertThat(new StringSequence("x")).isNotEmpty();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void encodedFileUrlLoaderPathIsHandledCorrectly() throws Exception {
this.launcher = new PropertiesLauncher();
Set<URL> urls = this.launcher.getClassPathUrls();
assertThat(urls).hasSize(1);
assertThat(urls.iterator().next()).isEqualTo(loaderPath.toURI().toURL());
assertThat(urls).element(0).isEqualTo(loaderPath.toURI().toURL());
}

@Test // gh-21575
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void filterDependencies() throws MojoExecutionException {
Set<Artifact> artifacts = mojo.filterDependencies(createArtifact("com.foo", "one"),
createArtifact("com.foo", "two"), artifact);
assertThat(artifacts).hasSize(1);
assertThat(artifacts.iterator().next()).isSameAs(artifact);
assertThat(artifacts).element(0).isSameAs(artifact);
}

@Test
Expand All @@ -69,7 +69,7 @@ void filterGroupIdExactMatch() throws MojoExecutionException {
Set<Artifact> artifacts = mojo.filterDependencies(createArtifact("com.foo", "one"),
createArtifact("com.foo", "two"), artifact);
assertThat(artifacts).hasSize(1);
assertThat(artifacts.iterator().next()).isSameAs(artifact);
assertThat(artifacts).element(0).isSameAs(artifact);
}

@Test
Expand Down
Loading

0 comments on commit e0a3fe0

Please sign in to comment.