From d39c94f43772c8a9c3c3d808d6d31c11fab9dde8 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 2 Mar 2023 21:42:27 -0800 Subject: [PATCH] Refine ConfigurationProcessorIntegrationTests resource detection logic --- ...onfigurationProcessorIntegrationTests.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/spring-boot-tests/spring-boot-integration-tests/spring-boot-configuration-processor-tests/src/test/java/org/springframework/boot/configurationprocessor/tests/ConfigurationProcessorIntegrationTests.java b/spring-boot-tests/spring-boot-integration-tests/spring-boot-configuration-processor-tests/src/test/java/org/springframework/boot/configurationprocessor/tests/ConfigurationProcessorIntegrationTests.java index 8a357b6d3f..c891786a37 100644 --- a/spring-boot-tests/spring-boot-integration-tests/spring-boot-configuration-processor-tests/src/test/java/org/springframework/boot/configurationprocessor/tests/ConfigurationProcessorIntegrationTests.java +++ b/spring-boot-tests/spring-boot-integration-tests/spring-boot-configuration-processor-tests/src/test/java/org/springframework/boot/configurationprocessor/tests/ConfigurationProcessorIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-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. @@ -17,6 +17,9 @@ package org.springframework.boot.configurationprocessor.tests; import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -24,8 +27,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty; import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepository; import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepositoryJsonBuilder; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; +import org.springframework.util.CollectionUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -40,12 +42,20 @@ class ConfigurationProcessorIntegrationTests { @BeforeAll static void readMetadata() throws IOException { - Resource resource = new ClassPathResource("META-INF/spring-configuration-metadata.json"); - assertThat(resource.exists()).isTrue(); - // Make sure the right file is detected - assertThat(resource.getURL().toString()).contains("spring-boot-configuration-processor-tests"); - repository = ConfigurationMetadataRepositoryJsonBuilder.create(resource.getInputStream()).build(); + repository = ConfigurationMetadataRepositoryJsonBuilder.create(getResource().openStream()).build(); + } + private static URL getResource() throws IOException { + ClassLoader classLoader = ConfigurationProcessorIntegrationTests.class.getClassLoader(); + List urls = new ArrayList<>(); + CollectionUtils.toIterator(classLoader.getResources("META-INF/spring-configuration-metadata.json")) + .forEachRemaining(urls::add); + for (URL url : urls) { + if (url.toString().contains("spring-boot-configuration-processor-tests")) { + return url; + } + } + throw new IllegalStateException("Unable to find correct configuration-metadata resource from " + urls); } @Test