Use new Environment.matchesProfiles() method
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
@@ -36,7 +35,7 @@ class ProfileCondition implements Condition {
|
||||
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
|
||||
if (attrs != null) {
|
||||
for (Object value : attrs.get("value")) {
|
||||
if (context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value))) {
|
||||
if (context.getEnvironment().matchesProfiles((String[]) value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -55,7 +55,6 @@ import org.springframework.context.testfixture.SimpleMapScope;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.testfixture.io.SerializationTestUtils;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
@@ -198,7 +197,7 @@ public class ComponentScanAnnotationIntegrationTests {
|
||||
@Test
|
||||
public void withAwareTypeFilter() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithAwareTypeFilter.class);
|
||||
assertThat(ctx.getEnvironment().acceptsProfiles(Profiles.of("the-filter-ran"))).isTrue();
|
||||
assertThat(ctx.getEnvironment().matchesProfiles("the-filter-ran")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -32,14 +32,18 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Unit tests covering the extensibility of {@link AbstractEnvironment}.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
*/
|
||||
class CustomEnvironmentTests {
|
||||
|
||||
private static final String DEFAULT_PROFILE = AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME;
|
||||
|
||||
|
||||
@Test
|
||||
void control() {
|
||||
Environment env = new AbstractEnvironment() { };
|
||||
assertThat(env.acceptsProfiles(defaultProfile())).isTrue();
|
||||
assertThat(env.matchesProfiles(DEFAULT_PROFILE)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -52,7 +56,7 @@ class CustomEnvironmentTests {
|
||||
}
|
||||
|
||||
Environment env = new CustomEnvironment();
|
||||
assertThat(env.acceptsProfiles(defaultProfile())).isFalse();
|
||||
assertThat(env.matchesProfiles(DEFAULT_PROFILE)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -65,8 +69,8 @@ class CustomEnvironmentTests {
|
||||
}
|
||||
|
||||
Environment env = new CustomEnvironment();
|
||||
assertThat(env.acceptsProfiles(defaultProfile())).isFalse();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("rd1"))).isTrue();
|
||||
assertThat(env.matchesProfiles(DEFAULT_PROFILE)).isFalse();
|
||||
assertThat(env.matchesProfiles("rd1")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -83,28 +87,28 @@ class CustomEnvironmentTests {
|
||||
}
|
||||
|
||||
ConfigurableEnvironment env = new CustomEnvironment();
|
||||
assertThat(env.acceptsProfiles(defaultProfile())).isFalse();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2"))).isTrue();
|
||||
assertThat(env.matchesProfiles(DEFAULT_PROFILE)).isFalse();
|
||||
assertThat(env.matchesProfiles("rd1 | rd2")).isTrue();
|
||||
|
||||
// finally, issue additional assertions to cover all combinations of calling these
|
||||
// methods, however unlikely.
|
||||
env.setDefaultProfiles("d1");
|
||||
assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2"))).isFalse();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("d1"))).isTrue();
|
||||
assertThat(env.matchesProfiles("rd1 | rd2")).isFalse();
|
||||
assertThat(env.matchesProfiles("d1")).isTrue();
|
||||
|
||||
env.setActiveProfiles("a1", "a2");
|
||||
assertThat(env.acceptsProfiles(Profiles.of("d1"))).isFalse();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("a1 | a2"))).isTrue();
|
||||
assertThat(env.matchesProfiles("d1")).isFalse();
|
||||
assertThat(env.matchesProfiles("a1 | a2")).isTrue();
|
||||
|
||||
env.setActiveProfiles();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("d1"))).isTrue();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("a1 | a2"))).isFalse();
|
||||
assertThat(env.matchesProfiles("d1")).isTrue();
|
||||
assertThat(env.matchesProfiles("a1 | a2")).isFalse();
|
||||
|
||||
env.setDefaultProfiles();
|
||||
assertThat(env.acceptsProfiles(defaultProfile())).isFalse();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("rd1 | rd2"))).isFalse();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("d1"))).isFalse();
|
||||
assertThat(env.acceptsProfiles(Profiles.of("a1 | a2"))).isFalse();
|
||||
assertThat(env.matchesProfiles(DEFAULT_PROFILE)).isFalse();
|
||||
assertThat(env.matchesProfiles("rd1 | rd2")).isFalse();
|
||||
assertThat(env.matchesProfiles("d1")).isFalse();
|
||||
assertThat(env.matchesProfiles("a1 | a2")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -127,7 +131,7 @@ class CustomEnvironmentTests {
|
||||
PropertySource<?> propertySource = new MapPropertySource("test", values);
|
||||
env.getPropertySources().addFirst(propertySource);
|
||||
assertThat(env.getActiveProfiles()).isEmpty();
|
||||
assertThat(env.getDefaultProfiles()).containsExactly(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
|
||||
assertThat(env.getDefaultProfiles()).containsExactly(DEFAULT_PROFILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -165,8 +169,4 @@ class CustomEnvironmentTests {
|
||||
assertThat(env.getProperty("spring")).isEqualTo("framework-test");
|
||||
}
|
||||
|
||||
private Profiles defaultProfile() {
|
||||
return Profiles.of(AbstractEnvironment.RESERVED_DEFAULT_PROFILE_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -37,7 +37,6 @@ import org.springframework.aot.test.generate.CompilerFiles;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.Profiles;
|
||||
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
|
||||
import org.springframework.core.test.tools.TestCompiler;
|
||||
import org.springframework.javapoet.ClassName;
|
||||
@@ -263,7 +262,7 @@ class TestContextAotGeneratorTests extends AbstractAotTests {
|
||||
|
||||
MessageService messageService = context.getBean(MessageService.class);
|
||||
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
|
||||
String expectedMessage = cac.getEnvironment().acceptsProfiles(Profiles.of("spanish")) ?
|
||||
String expectedMessage = cac.getEnvironment().matchesProfiles("spanish") ?
|
||||
"¡Hola, AOT!" : "Hello, AOT!";
|
||||
assertThat(messageService.generateMessage()).isEqualTo(expectedMessage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user