Skip to content

Commit

Permalink
Test support for multiple composed @propertysource annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Aug 12, 2023
1 parent 6acf78e commit 3bab2d8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
@Configuration
@ConnectedToModuleA
@ConnectedToModuleB
@PropertySourceA
@PropertySourceB
class AppConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,31 @@
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.gh30941.a.ModuleAClient;
import org.springframework.test.gh30941.b.ModuleBClient;

import static org.assertj.core.api.Assertions.assertThat;

@SpringJUnitConfig(AppConfig.class)
class ComponentScanTests {
class ComponentScanAndPropertySourceTests {

@Autowired
ModuleAClient clientA;

@Autowired
ModuleBClient clientB;

@Autowired
Environment env;

@Test
void test() {
assertThat(clientA).isNotNull();
assertThat(clientB).isNotNull();
assertThat(env.getProperty("A")).isEqualTo("apple");
assertThat(env.getProperty("B")).isEqualTo("banana");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.gh30941;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@PropertySource(value = "", factory = PropertySourceFactoryA.class)
@interface PropertySourceA {
}

class PropertySourceFactoryA implements PropertySourceFactory {

@Override
public org.springframework.core.env.PropertySource<?> createPropertySource(String name, EncodedResource resource) {
return new MapPropertySource("A", Map.of("A", "apple"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.gh30941;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@PropertySource(value = "", factory = PropertySourceFactoryB.class)
@interface PropertySourceB {
}

class PropertySourceFactoryB implements PropertySourceFactory {

@Override
public org.springframework.core.env.PropertySource<?> createPropertySource(String name, EncodedResource resource) {
return new MapPropertySource("B", Map.of("B", "banana"));
}

}

0 comments on commit 3bab2d8

Please sign in to comment.