Commit a869d25d authored by Phillip Webb's avatar Phillip Webb

Polish

parent eac4c7e8
......@@ -59,7 +59,7 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
......@@ -70,7 +70,7 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
throws MalformedObjectNameException, InstanceNotFoundException {
this.contextRunner.run((context) -> {
this.thrown.expect(InstanceNotFoundException.class);
this.mBeanServer.getObjectInstance(createDefaultObjectName());
this.server.getObjectInstance(createDefaultObjectName());
});
}
......@@ -78,7 +78,7 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
public void registeredWithProperty() throws Exception {
this.contextRunner.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
ObjectName objectName = createDefaultObjectName();
ObjectInstance objectInstance = this.mBeanServer.getObjectInstance(objectName);
ObjectInstance objectInstance = this.server.getObjectInstance(objectName);
assertThat(objectInstance).as("Lifecycle bean should have been registered")
.isNotNull();
});
......@@ -87,18 +87,20 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
@Test
public void registerWithCustomJmxName() throws InstanceNotFoundException {
String customJmxName = "org.acme:name=FooBar";
this.contextRunner.withSystemProperties(
"spring.application.admin.jmx-name=" + customJmxName)
this.contextRunner
.withSystemProperties(
"spring.application.admin.jmx-name=" + customJmxName)
.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
try {
this.mBeanServer.getObjectInstance(createObjectName(customJmxName));
}
catch (InstanceNotFoundException ex) {
fail("Admin MBean should have been exposed with custom name");
}
this.thrown.expect(InstanceNotFoundException.class); // Should not be exposed
this.mBeanServer.getObjectInstance(createDefaultObjectName());
});
try {
this.server.getObjectInstance(createObjectName(customJmxName));
}
catch (InstanceNotFoundException ex) {
fail("Admin MBean should have been exposed with custom name");
}
this.thrown.expect(InstanceNotFoundException.class); // Should not be
// exposed
this.server.getObjectInstance(createDefaultObjectName());
});
}
@Test
......@@ -110,7 +112,7 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
SpringApplicationAdminJmxAutoConfiguration.class)
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0")) {
assertThat(context).isInstanceOf(ServletWebServerApplicationContext.class);
assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(),
assertThat(this.server.getAttribute(createDefaultObjectName(),
"EmbeddedWebApplication")).isEqualTo(Boolean.TRUE);
int expected = ((ServletWebServerApplicationContext) context).getWebServer()
.getPort();
......@@ -128,11 +130,10 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
.child(JmxAutoConfiguration.class,
SpringApplicationAdminJmxAutoConfiguration.class)
.web(WebApplicationType.NONE);
try (ConfigurableApplicationContext parent = parentBuilder
.run("--" + ENABLE_ADMIN_PROP);
ConfigurableApplicationContext child = childBuilder
.run("--" + ENABLE_ADMIN_PROP)) {
ConfigurableApplicationContext child = childBuilder
.run("--" + ENABLE_ADMIN_PROP)) {
BeanFactoryUtils.beanOfType(parent.getBeanFactory(),
SpringApplicationAdminMXBeanRegistrar.class);
this.thrown.expect(NoSuchBeanDefinitionException.class);
......@@ -155,7 +156,7 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
}
private String getProperty(ObjectName objectName, String key) throws Exception {
return (String) this.mBeanServer.invoke(objectName, "getProperty",
return (String) this.server.invoke(objectName, "getProperty",
new Object[] { key }, new String[] { String.class.getName() });
}
......
......@@ -46,12 +46,12 @@ public class AopAutoConfigurationTests {
public void aopDisabled() {
this.contextRunner.withUserConfiguration(TestConfiguration.class)
.withPropertyValues("spring.aop.auto:false").run((context) -> {
TestAspect aspect = context.getBean(TestAspect.class);
assertThat(aspect.isCalled()).isFalse();
TestBean bean = context.getBean(TestBean.class);
bean.foo();
assertThat(aspect.isCalled()).isFalse();
});
TestAspect aspect = context.getBean(TestAspect.class);
assertThat(aspect.isCalled()).isFalse();
TestBean bean = context.getBean(TestBean.class);
bean.foo();
assertThat(aspect.isCalled()).isFalse();
});
}
@Test
......
......@@ -54,43 +54,46 @@ public class BatchAutoConfigurationWithoutJpaTests {
@Test
public void jdbcWithDefaultSettings() throws Exception {
this.contextRunner.withUserConfiguration(DefaultConfiguration.class,
EmbeddedDataSourceConfiguration.class).withPropertyValues(
"spring.datasource.generate-unique-name=true").run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context).hasSingleBean(JobExplorer.class);
assertThat(context).hasSingleBean(JobRepository.class);
assertThat(context).hasSingleBean(PlatformTransactionManager.class);
assertThat(context.getBean(PlatformTransactionManager.class).toString())
.contains("DataSourceTransactionManager");
assertThat(
context.getBean(BatchProperties.class).getInitializer().isEnabled())
.isTrue();
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
assertThat(
context.getBean(JobExplorer.class).findRunningJobExecutions("test"))
.isEmpty();
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test",
new JobParameters())).isNull();
});
this.contextRunner
.withUserConfiguration(DefaultConfiguration.class,
EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true")
.run((context) -> {
assertThat(context).hasSingleBean(JobLauncher.class);
assertThat(context).hasSingleBean(JobExplorer.class);
assertThat(context).hasSingleBean(JobRepository.class);
assertThat(context).hasSingleBean(PlatformTransactionManager.class);
assertThat(
context.getBean(PlatformTransactionManager.class).toString())
.contains("DataSourceTransactionManager");
assertThat(context.getBean(BatchProperties.class).getInitializer()
.isEnabled()).isTrue();
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
assertThat(context.getBean(JobExplorer.class)
.findRunningJobExecutions("test")).isEmpty();
assertThat(context.getBean(JobRepository.class)
.getLastJobExecution("test", new JobParameters())).isNull();
});
}
@Test
public void jdbcWithCustomPrefix() throws Exception {
this.contextRunner.withUserConfiguration(DefaultConfiguration.class,
EmbeddedDataSourceConfiguration.class).withPropertyValues(
"spring.datasource.generate-unique-name=true",
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.tablePrefix:PREFIX_").run((context) -> {
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
assertThat(
context.getBean(JobExplorer.class).findRunningJobExecutions("test"))
.isEmpty();
assertThat(context.getBean(JobRepository.class).getLastJobExecution("test",
new JobParameters())).isNull();
});
this.contextRunner
.withUserConfiguration(DefaultConfiguration.class,
EmbeddedDataSourceConfiguration.class)
.withPropertyValues("spring.datasource.generate-unique-name=true",
"spring.batch.schema:classpath:batch/custom-schema-hsql.sql",
"spring.batch.tablePrefix:PREFIX_")
.run((context) -> {
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
.queryForList("select * from PREFIX_JOB_EXECUTION"))
.isEmpty();
assertThat(context.getBean(JobExplorer.class)
.findRunningJobExecutions("test")).isEmpty();
assertThat(context.getBean(JobRepository.class)
.getLastJobExecution("test", new JobParameters())).isNull();
});
}
@EnableBatchProcessing
......
......@@ -80,8 +80,7 @@ public class JobLauncherCommandLineRunnerTests {
this.step = this.steps.get("step").tasklet(tasklet).build();
this.job = this.jobs.get("job").start(this.step).build();
this.jobExplorer = this.context.getBean(JobExplorer.class);
this.runner = new JobLauncherCommandLineRunner(jobLauncher,
this.jobExplorer);
this.runner = new JobLauncherCommandLineRunner(jobLauncher, this.jobExplorer);
this.context.getBean(BatchConfiguration.class).clear();
}
......
......@@ -50,31 +50,33 @@ public class CassandraAutoConfigurationTests {
@Test
public void createClusterWithOverrides() {
this.contextRunner.withPropertyValues(
"spring.data.cassandra.cluster-name=testcluster").run((context) -> {
assertThat(context).hasSingleBean(Cluster.class);
assertThat(context.getBean(Cluster.class).getClusterName())
.isEqualTo("testcluster");
});
this.contextRunner
.withPropertyValues("spring.data.cassandra.cluster-name=testcluster")
.run((context) -> {
assertThat(context).hasSingleBean(Cluster.class);
assertThat(context.getBean(Cluster.class).getClusterName())
.isEqualTo("testcluster");
});
}
@Test
public void createCustomizeCluster() {
this.contextRunner.withUserConfiguration(
MockCustomizerConfig.class).run((context) -> {
assertThat(context).hasSingleBean(Cluster.class);
assertThat(context).hasSingleBean(ClusterBuilderCustomizer.class);
});
this.contextRunner.withUserConfiguration(MockCustomizerConfig.class)
.run((context) -> {
assertThat(context).hasSingleBean(Cluster.class);
assertThat(context).hasSingleBean(ClusterBuilderCustomizer.class);
});
}
@Test
public void customizerOverridesAutoConfig() {
this.contextRunner.withUserConfiguration(SimpleCustomizerConfig.class)
.withPropertyValues("spring.data.cassandra.cluster-name=testcluster").run((context) -> {
assertThat(context).hasSingleBean(Cluster.class);
assertThat(context.getBean(Cluster.class).getClusterName())
.isEqualTo("overridden-name");
});
.withPropertyValues("spring.data.cassandra.cluster-name=testcluster")
.run((context) -> {
assertThat(context).hasSingleBean(Cluster.class);
assertThat(context.getBean(Cluster.class).getClusterName())
.isEqualTo("overridden-name");
});
}
@Test
......@@ -96,22 +98,23 @@ public class CassandraAutoConfigurationTests {
@Test
public void customizePoolOptions() {
this.contextRunner.withPropertyValues(
"spring.data.cassandra.pool.idle-timeout=42",
"spring.data.cassandra.pool.pool-timeout=52",
"spring.data.cassandra.pool.heartbeat-interval=62",
"spring.data.cassandra.pool.max-queue-size=72").run((context) -> {
assertThat(context).hasSingleBean(Cluster.class);
PoolingOptions poolingOptions = context.getBean(Cluster.class)
.getConfiguration().getPoolingOptions();
assertThat(poolingOptions.getIdleTimeoutSeconds()).isEqualTo(42);
assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(52);
assertThat(poolingOptions.getHeartbeatIntervalSeconds()).isEqualTo(62);
assertThat(poolingOptions.getMaxQueueSize()).isEqualTo(72);
});
this.contextRunner
.withPropertyValues("spring.data.cassandra.pool.idle-timeout=42",
"spring.data.cassandra.pool.pool-timeout=52",
"spring.data.cassandra.pool.heartbeat-interval=62",
"spring.data.cassandra.pool.max-queue-size=72")
.run((context) -> {
assertThat(context).hasSingleBean(Cluster.class);
PoolingOptions poolingOptions = context.getBean(Cluster.class)
.getConfiguration().getPoolingOptions();
assertThat(poolingOptions.getIdleTimeoutSeconds()).isEqualTo(42);
assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(52);
assertThat(poolingOptions.getHeartbeatIntervalSeconds())
.isEqualTo(62);
assertThat(poolingOptions.getMaxQueueSize()).isEqualTo(72);
});
}
@Configuration
static class MockCustomizerConfig {
......
......@@ -30,7 +30,6 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
......@@ -55,40 +54,42 @@ public class ConditionalOnBeanTests {
@Test
public void testNameOnBeanCondition() {
this.contextRunner.withUserConfiguration(FooConfiguration.class,
OnBeanNameConfiguration.class).run(hasBarBean());
OnBeanNameConfiguration.class).run(this::hasBarBean);
}
@Test
public void testNameAndTypeOnBeanCondition() {
this.contextRunner.withUserConfiguration(FooConfiguration.class,
OnBeanNameAndTypeConfiguration.class).run((context) ->
assertThat(context).doesNotHaveBean("bar"));
this.contextRunner
.withUserConfiguration(FooConfiguration.class,
OnBeanNameAndTypeConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("bar"));
}
@Test
public void testNameOnBeanConditionReverseOrder() {
// Ideally this should be true
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class,
FooConfiguration.class).run((context) ->
assertThat(context).doesNotHaveBean("bar"));
this.contextRunner
.withUserConfiguration(OnBeanNameConfiguration.class,
FooConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("bar"));
}
@Test
public void testClassOnBeanCondition() {
this.contextRunner.withUserConfiguration(FooConfiguration.class,
OnBeanClassConfiguration.class).run(hasBarBean());
OnBeanClassConfiguration.class).run(this::hasBarBean);
}
@Test
public void testClassOnBeanClassNameCondition() {
this.contextRunner.withUserConfiguration(FooConfiguration.class,
OnBeanClassNameConfiguration.class).run(hasBarBean());
OnBeanClassNameConfiguration.class).run(this::hasBarBean);
}
@Test
public void testOnBeanConditionWithXml() {
this.contextRunner.withUserConfiguration(XmlConfiguration.class,
OnBeanNameConfiguration.class).run(hasBarBean());
OnBeanNameConfiguration.class).run(this::hasBarBean);
}
@Test
......@@ -101,22 +102,23 @@ public class ConditionalOnBeanTests {
@Test
public void testAnnotationOnBeanCondition() {
this.contextRunner.withUserConfiguration(FooConfiguration.class,
OnAnnotationConfiguration.class).run(hasBarBean());
OnAnnotationConfiguration.class).run(this::hasBarBean);
}
@Test
public void testOnMissingBeanType() throws Exception {
this.contextRunner.withUserConfiguration(FooConfiguration.class,
OnBeanMissingClassConfiguration.class).run((context) ->
assertThat(context).doesNotHaveBean("bar"));
this.contextRunner
.withUserConfiguration(FooConfiguration.class,
OnBeanMissingClassConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean("bar"));
}
@Test
public void withPropertyPlaceholderClassName() throws Exception {
this.contextRunner.withUserConfiguration(
PropertySourcesPlaceholderConfigurer.class,
WithPropertyPlaceholderClassName.class,
OnBeanClassConfiguration.class)
this.contextRunner
.withUserConfiguration(PropertySourcesPlaceholderConfigurer.class,
WithPropertyPlaceholderClassName.class,
OnBeanClassConfiguration.class)
.withPropertyValues("mybeanclass=java.lang.String")
.run(context -> assertThat(context).hasNotFailed());
}
......@@ -125,19 +127,16 @@ public class ConditionalOnBeanTests {
public void beanProducedByFactoryBeanIsConsideredWhenMatchingOnAnnotation() {
this.contextRunner.withUserConfiguration(FactoryBeanConfiguration.class,
OnAnnotationWithFactoryBeanConfiguration.class).run((context) -> {
assertThat(context).hasBean("bar");
assertThat(context).hasSingleBean(ExampleBean.class);
});
assertThat(context).hasBean("bar");
assertThat(context).hasSingleBean(ExampleBean.class);
});
}
private ContextConsumer<AssertableApplicationContext> hasBarBean() {
return (context) -> {
assertThat(context).hasBean("bar");
assertThat(context.getBean("bar")).isEqualTo("bar");
};
private void hasBarBean(AssertableApplicationContext context) {
assertThat(context).hasBean("bar");
assertThat(context.getBean("bar")).isEqualTo("bar");
}
@Configuration
@ConditionalOnBean(name = "foo")
protected static class OnBeanNameConfiguration {
......
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
......@@ -20,7 +20,6 @@ import org.junit.Test;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
......@@ -40,38 +39,38 @@ public class ConditionalOnClassTests {
@Test
public void testVanillaOnClassCondition() {
this.contextRunner.withUserConfiguration(BasicConfiguration.class,
FooConfiguration.class).run(hasBarBean());
this.contextRunner
.withUserConfiguration(BasicConfiguration.class, FooConfiguration.class)
.run(this::hasBarBean);
}
@Test
public void testMissingOnClassCondition() {
this.contextRunner.withUserConfiguration(MissingConfiguration.class,
FooConfiguration.class).run((context) -> {
assertThat(context).doesNotHaveBean("bar");
assertThat(context).hasBean("foo");
assertThat(context.getBean("foo")).isEqualTo("foo");
});
this.contextRunner
.withUserConfiguration(MissingConfiguration.class, FooConfiguration.class)
.run((context) -> {
assertThat(context).doesNotHaveBean("bar");
assertThat(context).hasBean("foo");
assertThat(context.getBean("foo")).isEqualTo("foo");
});
}
@Test
public void testOnClassConditionWithXml() {
this.contextRunner.withUserConfiguration(BasicConfiguration.class,
XmlConfiguration.class).run(hasBarBean());
this.contextRunner
.withUserConfiguration(BasicConfiguration.class, XmlConfiguration.class)
.run(this::hasBarBean);
}
@Test
public void testOnClassConditionWithCombinedXml() {
this.contextRunner.withUserConfiguration(CombinedXmlConfiguration.class)
.run(hasBarBean());
.run(this::hasBarBean);
}
private ContextConsumer<AssertableApplicationContext> hasBarBean() {
return (context) -> {
assertThat(context).hasBean("bar");
assertThat(context.getBean("bar")).isEqualTo("bar");
};
private void hasBarBean(AssertableApplicationContext context) {
assertThat(context).hasBean("bar");
assertThat(context.getBean("bar")).isEqualTo("bar");
}
@Configuration
......
......@@ -19,9 +19,7 @@ package org.springframework.boot.autoconfigure.condition;
import org.junit.Test;
import org.springframework.boot.cloud.CloudPlatform;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -37,32 +35,21 @@ public class ConditionalOnCloudPlatformTests {
@Test
public void outcomeWhenCloudfoundryPlatformNotPresentShouldNotMatch() {
this.contextRunner.withUserConfiguration(CloudFoundryPlatformConfig.class)
.run(match(false));
.run((context) -> assertThat(context).doesNotHaveBean("foo"));
}
@Test
public void outcomeWhenCloudfoundryPlatformPresentShouldMatch() {
this.contextRunner.withUserConfiguration(CloudFoundryPlatformConfig.class)
.withPropertyValues("VCAP_APPLICATION:---")
.run(match(true));
.run((context) -> assertThat(context).hasBean("foo"));
}
@Test
public void outcomeWhenCloudfoundryPlatformPresentAndMethodTargetShouldMatch() {
this.contextRunner.withUserConfiguration(CloudFoundryPlatformOnMethodConfig.class)
.withPropertyValues("VCAP_APPLICATION:---")
.run(match(true));
}
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
return (context) -> {
if (expected) {
assertThat(context).hasBean("foo");
}
else {
assertThat(context).doesNotHaveBean("foo");
}
};
.run((context) -> assertThat(context).hasBean("foo"));
}
@Configuration
......
......@@ -18,9 +18,7 @@ package org.springframework.boot.autoconfigure.condition;
import org.junit.Test;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -39,31 +37,19 @@ public class ConditionalOnExpressionTests {
@Test
public void expressionIsTrue() {
this.contextRunner.withUserConfiguration(BasicConfiguration.class)
.run(match(true));
.run((context) -> assertThat(context.getBean("foo")).isEqualTo("foo"));
}
@Test
public void expressionIsFalse() {
this.contextRunner.withUserConfiguration(MissingConfiguration.class)
.run(match(false));
.run((context) -> assertThat(context).doesNotHaveBean("foo"));
}
@Test
public void expressionIsNull() {
this.contextRunner.withUserConfiguration(NullConfiguration.class)
.run(match(false));
}
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
return (context) -> {
if (expected) {
assertThat(context).hasBean("foo");
assertThat(context.getBean("foo")).isEqualTo("foo");
}
else {
assertThat(context).doesNotHaveBean("foo");
}
};
.run((context) -> assertThat(context).doesNotHaveBean("foo"));
}
@Configuration
......
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
......@@ -29,9 +29,7 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion;
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.Range;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
......@@ -52,17 +50,20 @@ public class ConditionalOnJavaTests {
@Test
public void doesNotMatchIfBetterVersionIsRequired() {
this.contextRunner.withUserConfiguration(Java9Required.class).run(match(false));
this.contextRunner.withUserConfiguration(Java9Required.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
public void doesNotMatchIfLowerIsRequired() {
this.contextRunner.withUserConfiguration(Java7Required.class).run(match(false));
this.contextRunner.withUserConfiguration(Java7Required.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
public void matchesIfVersionIsInRange() {
this.contextRunner.withUserConfiguration(Java8Required.class).run(match(true));
this.contextRunner.withUserConfiguration(Java8Required.class)
.run((context) -> assertThat(context).hasSingleBean(String.class));
}
@Test
......@@ -105,10 +106,8 @@ public class ConditionalOnJavaTests {
private String getJavaVersion(Class<?>... hiddenClasses) throws Exception {
URL[] urls = ((URLClassLoader) getClass().getClassLoader()).getURLs();
URLClassLoader classLoader = new ClassHidingClassLoader(urls, hiddenClasses);
Class<?> javaVersionClass = classLoader
.loadClass(ConditionalOnJava.JavaVersion.class.getName());
Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass,
"getJavaVersion");
Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null);
......@@ -123,17 +122,6 @@ public class ConditionalOnJavaTests {
assertThat(outcome.isMatch()).as(outcome.getMessage()).isEqualTo(expected);
}
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
return (context) -> {
if (expected) {
assertThat(context).hasSingleBean(String.class);
}
else {
assertThat(context).doesNotHaveBean(String.class);
}
};
}
private final class ClassHidingClassLoader extends URLClassLoader {
private final List<Class<?>> hiddenClasses;
......
......@@ -27,9 +27,7 @@ import org.junit.Test;
import org.springframework.boot.autoconfigure.jndi.JndiPropertiesHidingClassLoader;
import org.springframework.boot.autoconfigure.jndi.TestableInitialContextFactory;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.AnnotatedTypeMetadata;
......@@ -77,22 +75,26 @@ public class ConditionalOnJndiTests {
@Test
public void jndiNotAvailable() {
this.contextRunner.withUserConfiguration(JndiAvailableConfiguration.class,
JndiConditionConfiguration.class).run(match(false));
this.contextRunner
.withUserConfiguration(JndiAvailableConfiguration.class,
JndiConditionConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
public void jndiAvailable() {
setupJndi();
this.contextRunner.withUserConfiguration(JndiAvailableConfiguration.class,
JndiConditionConfiguration.class).run(match(true));
this.contextRunner
.withUserConfiguration(JndiAvailableConfiguration.class,
JndiConditionConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(String.class));
}
@Test
public void jndiLocationNotBound() {
setupJndi();
this.contextRunner.withUserConfiguration(JndiConditionConfiguration.class)
.run(match(false));
.run((context) -> assertThat(context).doesNotHaveBean(String.class));
}
@Test
......@@ -100,7 +102,7 @@ public class ConditionalOnJndiTests {
setupJndi();
TestableInitialContextFactory.bind("java:/FooManager", new Object());
this.contextRunner.withUserConfiguration(JndiConditionConfiguration.class)
.run(match(true));
.run((context) -> assertThat(context).hasSingleBean(String.class));
}
@Test
......@@ -124,17 +126,6 @@ public class ConditionalOnJndiTests {
TestableInitialContextFactory.class.getName());
}
private ContextConsumer<AssertableApplicationContext> match(boolean expected) {
return (context) -> {
if (expected) {
assertThat(context).hasSingleBean(String.class);
}
else {
assertThat(context).doesNotHaveBean(String.class);
}
};
}
private AnnotatedTypeMetadata mockMetaData(String... value) {
AnnotatedTypeMetadata metadata = mock(AnnotatedTypeMetadata.class);
Map<String, Object> attributes = new HashMap<>();
......
......@@ -292,20 +292,13 @@ abstract class AbstractApplicationContextRunner<SELF extends AbstractApplication
consumer.accept(context);
}
catch (Throwable ex) {
AnyThrow.throwUnchecked(ex);
rethrow(ex);
}
}
private static class AnyThrow {
static void throwUnchecked(Throwable e) {
AnyThrow.throwAny(e);
}
@SuppressWarnings("unchecked")
private static <E extends Throwable> void throwAny(Throwable e) throws E {
throw (E) e;
}
@SuppressWarnings("unchecked")
private <E extends Throwable> void rethrow(Throwable e) throws E {
throw (E) e;
}
}
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 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.
......
......@@ -130,11 +130,16 @@ public final class TestPropertyValues {
return call.call();
}
catch (Exception ex) {
AnyThrow.throwUnchecked(ex);
return null; // never reached
rethrow(ex);
throw new IllegalStateException("Original cause not rethrown", ex);
}
}
@SuppressWarnings("unchecked")
private <E extends Throwable> void rethrow(Throwable e) throws E {
throw (E) e;
}
@SuppressWarnings("unchecked")
private void addToSources(MutablePropertySources sources, Type type, String name) {
if (sources.contains(name)) {
......@@ -310,16 +315,4 @@ public final class TestPropertyValues {
}
private static class AnyThrow {
static void throwUnchecked(Throwable e) {
AnyThrow.throwAny(e);
}
@SuppressWarnings("unchecked")
private static <E extends Throwable> void throwAny(Throwable e) throws E {
throw (E) e;
}
}
}
......@@ -153,7 +153,8 @@ public abstract class AbstractApplicationContextRunnerTests<T extends AbstractAp
new HidePackagesClassLoader(Gson.class.getPackage().getName()))
.run((context) -> {
try {
ClassUtils.forName(Gson.class.getName(), context.getClassLoader());
ClassUtils.forName(Gson.class.getName(),
context.getClassLoader());
fail("Should have thrown a ClassNotFoundException");
}
catch (ClassNotFoundException e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment