org.springframework.core.env.SystemEnvironmentPropertySource源码实例Demo

类org.springframework.core.env.SystemEnvironmentPropertySource源码实例Demo

下面列出了怎么用org.springframework.core.env.SystemEnvironmentPropertySource的API类实例代码及写法,或者点击链接到github查看源代码。

@Test
@EnabledOnOs(OS.WINDOWS)
public void testOnWindows() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("spring.cloud.deployer.local.working-directories-root", "file:/C:/tmp");
			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})

		.withUserConfiguration(Config1.class)
		.run((context) -> {
			LocalDeployerProperties properties = context.getBean(LocalDeployerProperties.class);
			assertThat(properties.getWorkingDirectoriesRoot()).isNotNull();
			assertThat(properties.getWorkingDirectoriesRoot().toString()).isEqualTo("C:\\tmp");
		});
}
 
@Test
@EnabledOnOs(OS.LINUX)
public void testOnLinux() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("spring.cloud.deployer.local.working-directories-root", "/tmp");

			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			LocalDeployerProperties properties = context.getBean(LocalDeployerProperties.class);
			assertThat(properties.getWorkingDirectoriesRoot()).isNotNull();
			assertThat(properties.getWorkingDirectoriesRoot().toString()).isEqualTo("/tmp");
		});
}
 
@Test
public void testBasicAuth() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("spring.cloud.dataflow.client.authentication.basic.username", "user1");
			map.put("spring.cloud.dataflow.client.authentication.basic.password", "pw1");
			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			DataFlowClientProperties properties = context.getBean(DataFlowClientProperties.class);
			assertThat(properties.getAuthentication().getBasic().getUsername()).isEqualTo("user1");
			assertThat(properties.getAuthentication().getBasic().getPassword()).isEqualTo("pw1");
		});
}
 
@Test
public void testLegacyOauth() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("spring.cloud.dataflow.client.authentication.access-token", "token1");
			map.put("spring.cloud.dataflow.client.authentication.client-id", "id1");
			map.put("spring.cloud.dataflow.client.authentication.client-secret", "secret1");
			map.put("spring.cloud.dataflow.client.authentication.token-uri", "uri1");
			map.put("spring.cloud.dataflow.client.authentication.scope", "s1,s2");
			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			DataFlowClientProperties properties = context.getBean(DataFlowClientProperties.class);
			assertThat(properties.getAuthentication().getAccessToken()).isEqualTo("token1");
			assertThat(properties.getAuthentication().getClientId()).isEqualTo("id1");
			assertThat(properties.getAuthentication().getClientSecret()).isEqualTo("secret1");
			assertThat(properties.getAuthentication().getTokenUri()).isEqualTo("uri1");
			assertThat(properties.getAuthentication().getScope()).containsExactly("s1", "s2");
		});
}
 
@Test
public void testCommonSpringSecurity() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("spring.cloud.dataflow.client.authentication.oauth2.client-registration-id", "regid1");
			map.put("spring.cloud.dataflow.client.authentication.oauth2.username", "user1");
			map.put("spring.cloud.dataflow.client.authentication.oauth2.password", "pw1");
			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			DataFlowClientProperties properties = context.getBean(DataFlowClientProperties.class);
			assertThat(properties.getAuthentication().getOauth2().getClientRegistrationId()).isEqualTo("regid1");
			assertThat(properties.getAuthentication().getOauth2().getUsername()).isEqualTo("user1");
			assertThat(properties.getAuthentication().getOauth2().getPassword()).isEqualTo("pw1");
		});
}
 
private static PropertyMapper getPropertyMapper(PropertySource<?> source) {
    if (source instanceof SystemEnvironmentPropertySource
            && hasSystemEnvironmentName(source)) {
        return new DelegatingPropertyMapper(SystemEnvironmentPropertyMapper.INSTANCE,
                DefaultPropertyMapper.INSTANCE);
    }
    return new DelegatingPropertyMapper(DefaultPropertyMapper.INSTANCE);
}
 
private StandardEnvironment newStandardEnvironment() {
  Map<String, Object> envProperties = new HashMap<>();
  envProperties.put("IFS-X", "${IFS-X}");
  PropertySource<Map<String, Object>> systemEnvironmentPropertySource = new SystemEnvironmentPropertySource("system-env", envProperties);

  StandardEnvironment environment = new StandardEnvironment();
  environment.getPropertySources()
          .addAfter(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, systemEnvironmentPropertySource);
  return environment;
}
 
@Test
public void testPreemtiveEnabled() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("maven.use-wagon", "true");
			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			MavenProperties properties = context.getBean(MavenProperties.class);
			assertThat(properties.isUseWagon()).isTrue();
		});
}
 
@Test
public void testRemoteRepositories() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("maven.remote-repositories.repo1.url", "url1");
			map.put("maven.remote-repositories.repo1.wagon.http.all.use-preemptive", "true");
			map.put("maven.remote-repositories.repo1.wagon.http.all.use-default-headers", "true");
			map.put("maven.remote-repositories.repo1.wagon.http.all.connection-timeout", "2");
			map.put("maven.remote-repositories.repo1.wagon.http.all.read-timeout", "3");
			map.put("maven.remote-repositories.repo1.wagon.http.all.headers.header1", "value1");
			map.put("maven.remote-repositories.repo1.wagon.http.all.params.http.connection.timeout", "1");
			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			MavenProperties properties = context.getBean(MavenProperties.class);
			assertThat(properties.getRemoteRepositories().size()).isEqualTo(1);
			assertThat(properties.getRemoteRepositories()).containsOnlyKeys("repo1");
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp().size())
				.isEqualTo(1);
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp()
				.get(WagonHttpMethod.all).isUsePreemptive()).isTrue();
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp()
				.get(WagonHttpMethod.all).isUseDefaultHeaders()).isTrue();
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp()
				.get(WagonHttpMethod.all).getConnectionTimeout()).isEqualTo(2);
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp()
				.get(WagonHttpMethod.all).getReadTimeout()).isEqualTo(3);
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp()
				.get(WagonHttpMethod.all).getHeaders()).containsOnlyKeys("header1");
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp()
				.get(WagonHttpMethod.all).getHeaders().get("header1")).isEqualTo("value1");
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp()
				.get(WagonHttpMethod.all).getParams()).containsOnlyKeys("http.connection.timeout");
			assertThat(properties.getRemoteRepositories().get("repo1").getWagon().getHttp()
				.get(WagonHttpMethod.all).getParams().get("http.connection.timeout")).isEqualTo("1");
			});
}
 
@Test
public void setAllProperties() {
	this.contextRunner
	.withInitializer(context -> {
		Map<String, Object> map = new HashMap<>();
		map.put("spring.cloud.deployer.cloudfoundry.org", "org");
		map.put("spring.cloud.deployer.cloudfoundry.space", "space");
		map.put("spring.cloud.deployer.cloudfoundry.url", "http://example.com");
		map.put("spring.cloud.deployer.cloudfoundry.username", "username");
		map.put("spring.cloud.deployer.cloudfoundry.password", "password");
		map.put("spring.cloud.deployer.cloudfoundry.client-id", "id");
		map.put("spring.cloud.deployer.cloudfoundry.client-secret", "secret");
		map.put("spring.cloud.deployer.cloudfoundry.login-hint", "hint");
		map.put("spring.cloud.deployer.cloudfoundry.skip-ssl-validation", "true");
		context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
			StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			CloudFoundryConnectionProperties properties = context.getBean(CloudFoundryConnectionProperties.class);
			assertThat(properties.getOrg()).isEqualTo("org");
			assertThat(properties.getSpace()).isEqualTo("space");
			assertThat(properties.getUrl().toString()).isEqualTo("http://example.com");
			assertThat(properties.getUsername()).isEqualTo("username");
			assertThat(properties.getPassword()).isEqualTo("password");
			assertThat(properties.getClientId()).isEqualTo("id");
			assertThat(properties.getClientSecret()).isEqualTo("secret");
			assertThat(properties.getLoginHint()).isEqualTo("hint");
			assertThat(properties.isSkipSslValidation()).isTrue();
		});
}
 
@SuppressWarnings("unchecked")
private static Map<String, Object> getOrAdd(MutablePropertySources sources,
		String name) {
	if (sources.contains(name)) {
		return (Map<String, Object>) sources.get(name).getSource();
	}
	Map<String, Object> map = new HashMap<>();
	sources.addFirst(new SystemEnvironmentPropertySource(name, map));
	return map;
}
 
@Test
public void setAllProperties() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("spring.cloud.deployer.local.debug-port", "8888");
			map.put("spring.cloud.deployer.local.debug-suspend", "n");
			map.put("spring.cloud.deployer.local.delete-files-on-exit", false);
			map.put("spring.cloud.deployer.local.env-vars-to-inherit", "FOO,BAR");
			map.put("spring.cloud.deployer.local.inherit-logging", true);
			map.put("spring.cloud.deployer.local.java-cmd", "foobar1");
			map.put("spring.cloud.deployer.local.java-opts", "foobar2");
			map.put("spring.cloud.deployer.local.maximum-concurrent-tasks", 1234);
			map.put("spring.cloud.deployer.local.port-range.low", 2345);
			map.put("spring.cloud.deployer.local.port-range.high", 2346);
			map.put("spring.cloud.deployer.local.shutdown-timeout", 3456);
			map.put("spring.cloud.deployer.local.use-spring-application-json", false);
			map.put("spring.cloud.deployer.local.docker.network", "spring-cloud-dataflow-server_default");

			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			LocalDeployerProperties properties = context.getBean(LocalDeployerProperties.class);
			assertThat(properties.getDebugPort()).isEqualTo(8888);
			assertThat(properties.getDebugSuspend()).isEqualTo("n");
			assertThat(properties.isDeleteFilesOnExit()).isFalse();
			assertThat(properties.getEnvVarsToInherit()).containsExactly("FOO", "BAR");
			assertThat(properties.isInheritLogging()).isTrue();
			assertThat(properties.getJavaCmd()).contains("foobar1");
			assertThat(properties.getJavaOpts()).contains("foobar2");
			assertThat(properties.getMaximumConcurrentTasks()).isEqualTo(1234);
			assertThat(properties.getPortRange()).isNotNull();
			assertThat(properties.getPortRange().getLow()).isEqualTo(2345);
			assertThat(properties.getPortRange().getHigh()).isEqualTo(2346);
			assertThat(properties.getShutdownTimeout()).isEqualTo(3456);
			assertThat(properties.isUseSpringApplicationJson()).isFalse();
			assertThat(properties.getDocker().getNetwork()).isEqualTo("spring-cloud-dataflow-server_default");
		});
}
 
@Test
public void setAllPropertiesCamelCase() {
	this.contextRunner
		.withInitializer(context -> {
			Map<String, Object> map = new HashMap<>();
			map.put("spring.cloud.deployer.local.debugPort", "8888");
			map.put("spring.cloud.deployer.local.debugSuspend", "n");
			map.put("spring.cloud.deployer.local.deleteFilesOnExit", false);
			map.put("spring.cloud.deployer.local.envVarsToInherit", "FOO,BAR");
			map.put("spring.cloud.deployer.local.inheritLogging", true);
			map.put("spring.cloud.deployer.local.javaCmd", "foobar1");
			map.put("spring.cloud.deployer.local.javaOpts", "foobar2");
			map.put("spring.cloud.deployer.local.maximumConcurrentTasks", 1234);
			map.put("spring.cloud.deployer.local.portRange.low", 2345);
			map.put("spring.cloud.deployer.local.portRange.high", 2346);
			map.put("spring.cloud.deployer.local.shutdownTimeout", 3456);
			map.put("spring.cloud.deployer.local.useSpringApplicationJson", false);
			map.put("spring.cloud.deployer.local.docker.network", "spring-cloud-dataflow-server_default");

			context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
				StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
		})
		.withUserConfiguration(Config1.class)
		.run((context) -> {
			LocalDeployerProperties properties = context.getBean(LocalDeployerProperties.class);
			assertThat(properties.getDebugPort()).isEqualTo(8888);
			assertThat(properties.getDebugSuspend()).isEqualTo("n");
			assertThat(properties.isDeleteFilesOnExit()).isFalse();
			assertThat(properties.getEnvVarsToInherit()).containsExactly("FOO", "BAR");
			assertThat(properties.isInheritLogging()).isTrue();
			assertThat(properties.getJavaCmd()).contains("foobar1");
			assertThat(properties.getJavaOpts()).contains("foobar2");
			assertThat(properties.getMaximumConcurrentTasks()).isEqualTo(1234);
			assertThat(properties.getPortRange()).isNotNull();
			assertThat(properties.getPortRange().getLow()).isEqualTo(2345);
			assertThat(properties.getPortRange().getHigh()).isEqualTo(2346);
			assertThat(properties.getShutdownTimeout()).isEqualTo(3456);
			assertThat(properties.isUseSpringApplicationJson()).isFalse();
			assertThat(properties.getDocker().getNetwork()).isEqualTo("spring-cloud-dataflow-server_default");
		});
}
 
@Test
public void registryConfigurationProperties() {
	this.contextRunner
			.withInitializer(context -> {
				Map<String, Object> map = new HashMap<>();

				// harbor - dockeroauth2
				map.put("spring.cloud.dataflow.container.registry-configurations[goharbor].registry-host", "demo.goharbor.io");
				map.put("spring.cloud.dataflow.container.registry-configurations[goharbor].authorization-type", "dockeroauth2");
				map.put("spring.cloud.dataflow.container.registry-configurations[goharbor].user", "admin");
				map.put("spring.cloud.dataflow.container.registry-configurations[goharbor].secret", "Harbor12345");
				map.put("spring.cloud.dataflow.container.registry-configurations[goharbor].disable-ssl-verification", "true");

				// amazon ecr - awsecr
				map.put("spring.cloud.dataflow.container.registry-configurations[myamazonaws].registry-host", "283191309520.dkr.ecr.us-west-1.amazonaws.com");
				map.put("spring.cloud.dataflow.container.registry-configurations[myamazonaws].authorization-type", "awsecr");
				map.put("spring.cloud.dataflow.container.registry-configurations[myamazonaws].user", "myawsuser");
				map.put("spring.cloud.dataflow.container.registry-configurations[myamazonaws].secret", "myawspassword");
				map.put("spring.cloud.dataflow.container.registry-configurations[myamazonaws].extra[region]", "us-west-1");
				map.put("spring.cloud.dataflow.container.registry-configurations[myamazonaws].extra[registryIds]", "283191309520");

				context.getEnvironment().getPropertySources().addLast(new SystemEnvironmentPropertySource(
						StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
			})
			.withUserConfiguration(Config1.class)
			.run((context) -> {
				ContainerImageMetadataProperties properties = context.getBean(ContainerImageMetadataProperties.class);
				assertThat(properties.getRegistryConfigurations()).hasSize(2);

				RegistryConfiguration goharborConf = properties.getRegistryConfigurations().get("goharbor");
				assertThat(goharborConf).isNotNull();
				assertThat(goharborConf.getRegistryHost()).isEqualTo("demo.goharbor.io");
				assertThat(goharborConf.getAuthorizationType()).isEqualTo(RegistryConfiguration.AuthorizationType.dockeroauth2);
				assertThat(goharborConf.getUser()).isEqualTo("admin");
				assertThat(goharborConf.getSecret()).isEqualTo("Harbor12345");
				assertThat(goharborConf.isDisableSslVerification()).isTrue();
				assertThat(goharborConf.getExtra()).isEmpty();

				RegistryConfiguration myamazonawsConf = properties.getRegistryConfigurations().get("myamazonaws");
				assertThat(myamazonawsConf).isNotNull();
				assertThat(myamazonawsConf.getRegistryHost()).isEqualTo("283191309520.dkr.ecr.us-west-1.amazonaws.com");
				assertThat(myamazonawsConf.getAuthorizationType()).isEqualTo(RegistryConfiguration.AuthorizationType.awsecr);
				assertThat(myamazonawsConf.getUser()).isEqualTo("myawsuser");
				assertThat(myamazonawsConf.getSecret()).isEqualTo("myawspassword");
				assertThat(myamazonawsConf.isDisableSslVerification()).isFalse();
				assertThat(myamazonawsConf.getExtra()).hasSize(2);
				assertThat(myamazonawsConf.getExtra().get("region")).isEqualTo("us-west-1");
				assertThat(myamazonawsConf.getExtra().get("registryIds")).isEqualTo("283191309520");
			});
}
 
public EncryptableSystemEnvironmentPropertySourceWrapper(SystemEnvironmentPropertySource delegate, EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter) {
    super(delegate.getName(), delegate.getSource());
    encryptableDelegate = new CachingDelegateEncryptablePropertySource<>(delegate, resolver, filter);
}