From 444474926c23cb7251b2601bc3744386eb4b1f21 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 14 May 2021 11:30:28 +0100 Subject: [PATCH] Configure JTA tests to write logs beneath build/ Closes gh-26440 --- .gitignore | 1 - .../AbstractJpaAutoConfigurationTests.java | 8 +++- .../jta/JtaAutoConfigurationTests.java | 44 ++++++++++++++----- .../build.gradle | 1 + .../SampleAtomikosApplicationTests.java | 7 ++- .../build.gradle | 1 + .../SampleBitronixApplicationTests.java | 11 +++-- 7 files changed, 54 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 675c38d512..6edbdcda31 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,6 @@ manifest.yml out overridedb.* target -transaction-logs .flattened-pom.xml secrets.yml .gradletasknamecache diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java index c061ec3392..fa50db82c4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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,6 +16,7 @@ package org.springframework.boot.autoconfigure.orm.jpa; +import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -37,6 +38,7 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContex import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.boot.testsupport.BuildOutput; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -68,7 +70,9 @@ abstract class AbstractJpaAutoConfigurationTests { protected AbstractJpaAutoConfigurationTests(Class autoConfiguredClass) { this.autoConfiguredClass = autoConfiguredClass; this.contextRunner = new ApplicationContextRunner() - .withPropertyValues("spring.datasource.generate-unique-name=true") + .withPropertyValues("spring.datasource.generate-unique-name=true", + "spring.jta.log-dir=" + + new File(new BuildOutput(getClass()).getRootLocation(), "transaction-logs")) .withUserConfiguration(TestConfiguration.class).withConfiguration(AutoConfigurations.of( DataSourceAutoConfiguration.class, TransactionAutoConfiguration.class, autoConfiguredClass)); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/jta/JtaAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/jta/JtaAutoConfigurationTests.java index 1170e0b71f..e03fcfcdd3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/jta/JtaAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/transaction/jta/JtaAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -51,6 +51,7 @@ import org.springframework.boot.jta.bitronix.BitronixDependentBeanFactoryPostPro import org.springframework.boot.jta.bitronix.PoolingConnectionFactoryBean; import org.springframework.boot.jta.bitronix.PoolingDataSourceBean; import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.boot.testsupport.BuildOutput; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -74,6 +75,8 @@ import static org.mockito.Mockito.mock; @SuppressWarnings("deprecation") class JtaAutoConfigurationTests { + private final File buildOutput = new BuildOutput(JtaAutoConfigurationTests.class).getRootLocation(); + private AnnotationConfigApplicationContext context; @AfterEach @@ -104,7 +107,11 @@ class JtaAutoConfigurationTests { @Test void atomikosSanityCheck() { - this.context = new AnnotationConfigApplicationContext(JtaProperties.class, AtomikosJtaConfiguration.class); + this.context = new AnnotationConfigApplicationContext(); + TestPropertyValues.of("spring.jta.log-dir:" + new File(this.buildOutput, "atomikos-logs")) + .applyTo(this.context); + this.context.register(JtaProperties.class, AtomikosJtaConfiguration.class); + this.context.refresh(); this.context.getBean(AtomikosProperties.class); this.context.getBean(UserTransactionService.class); this.context.getBean(UserTransactionManager.class); @@ -118,7 +125,11 @@ class JtaAutoConfigurationTests { @Test @Deprecated void bitronixSanityCheck() { - this.context = new AnnotationConfigApplicationContext(JtaProperties.class, BitronixJtaConfiguration.class); + this.context = new AnnotationConfigApplicationContext(); + TestPropertyValues.of("spring.jta.log-dir:" + new File(this.buildOutput, "bitronix-logs")) + .applyTo(this.context); + this.context.register(JtaProperties.class, BitronixJtaConfiguration.class); + this.context.refresh(); this.context.getBean(bitronix.tm.Configuration.class); this.context.getBean(TransactionManager.class); this.context.getBean(XADataSourceWrapper.class); @@ -130,7 +141,11 @@ class JtaAutoConfigurationTests { @Test @Deprecated void defaultBitronixServerId() throws UnknownHostException { - this.context = new AnnotationConfigApplicationContext(BitronixJtaConfiguration.class); + this.context = new AnnotationConfigApplicationContext(); + TestPropertyValues.of("spring.jta.log-dir:" + new File(this.buildOutput, "bitronix-logs")) + .applyTo(this.context); + this.context.register(BitronixJtaConfiguration.class); + this.context.refresh(); String serverId = this.context.getBean(bitronix.tm.Configuration.class).getServerId(); assertThat(serverId).isEqualTo(InetAddress.getLocalHost().getHostAddress()); } @@ -139,7 +154,8 @@ class JtaAutoConfigurationTests { @Deprecated void customBitronixServerId() { this.context = new AnnotationConfigApplicationContext(); - TestPropertyValues.of("spring.jta.transactionManagerId:custom").applyTo(this.context); + TestPropertyValues.of("spring.jta.transactionManagerId:custom", + "spring.jta.log-dir:" + new File(this.buildOutput, "bitronix-logs")).applyTo(this.context); this.context.register(BitronixJtaConfiguration.class); this.context.refresh(); String serverId = this.context.getBean(bitronix.tm.Configuration.class).getServerId(); @@ -162,7 +178,8 @@ class JtaAutoConfigurationTests { void atomikosConnectionFactoryPoolConfiguration() { this.context = new AnnotationConfigApplicationContext(); TestPropertyValues.of("spring.jta.atomikos.connectionfactory.minPoolSize:5", - "spring.jta.atomikos.connectionfactory.maxPoolSize:10").applyTo(this.context); + "spring.jta.atomikos.connectionfactory.maxPoolSize:10", + "spring.jta.log-dir:" + new File(this.buildOutput, "atomikos-logs")).applyTo(this.context); this.context.register(AtomikosJtaConfiguration.class, PoolConfiguration.class); this.context.refresh(); AtomikosConnectionFactoryBean connectionFactory = this.context.getBean(AtomikosConnectionFactoryBean.class); @@ -175,7 +192,8 @@ class JtaAutoConfigurationTests { void bitronixConnectionFactoryPoolConfiguration() { this.context = new AnnotationConfigApplicationContext(); TestPropertyValues.of("spring.jta.bitronix.connectionfactory.minPoolSize:5", - "spring.jta.bitronix.connectionfactory.maxPoolSize:10").applyTo(this.context); + "spring.jta.bitronix.connectionfactory.maxPoolSize:10", + "spring.jta.log-dir:" + new File(this.buildOutput, "bitronix-logs")).applyTo(this.context); this.context.register(BitronixJtaConfiguration.class, PoolConfiguration.class); this.context.refresh(); PoolingConnectionFactoryBean connectionFactory = this.context.getBean(PoolingConnectionFactoryBean.class); @@ -187,7 +205,8 @@ class JtaAutoConfigurationTests { void atomikosDataSourcePoolConfiguration() { this.context = new AnnotationConfigApplicationContext(); TestPropertyValues - .of("spring.jta.atomikos.datasource.minPoolSize:5", "spring.jta.atomikos.datasource.maxPoolSize:10") + .of("spring.jta.atomikos.datasource.minPoolSize:5", "spring.jta.atomikos.datasource.maxPoolSize:10", + "spring.jta.log-dir:" + new File(this.buildOutput, "atomikos-logs")) .applyTo(this.context); this.context.register(AtomikosJtaConfiguration.class, PoolConfiguration.class); this.context.refresh(); @@ -201,7 +220,8 @@ class JtaAutoConfigurationTests { void bitronixDataSourcePoolConfiguration() { this.context = new AnnotationConfigApplicationContext(); TestPropertyValues - .of("spring.jta.bitronix.datasource.minPoolSize:5", "spring.jta.bitronix.datasource.maxPoolSize:10") + .of("spring.jta.bitronix.datasource.minPoolSize:5", "spring.jta.bitronix.datasource.maxPoolSize:10", + "spring.jta.log-dir:" + new File(this.buildOutput, "bitronix-logs")) .applyTo(this.context); this.context.register(BitronixJtaConfiguration.class, PoolConfiguration.class); this.context.refresh(); @@ -214,7 +234,8 @@ class JtaAutoConfigurationTests { void atomikosCustomizeJtaTransactionManagerUsingProperties() { this.context = new AnnotationConfigApplicationContext(); TestPropertyValues - .of("spring.transaction.default-timeout:30", "spring.transaction.rollback-on-commit-failure:true") + .of("spring.transaction.default-timeout:30", "spring.transaction.rollback-on-commit-failure:true", + "spring.jta.log-dir:" + new File(this.buildOutput, "atomikos-logs")) .applyTo(this.context); this.context.register(AtomikosJtaConfiguration.class, TransactionAutoConfiguration.class); this.context.refresh(); @@ -228,7 +249,8 @@ class JtaAutoConfigurationTests { void bitronixCustomizeJtaTransactionManagerUsingProperties() { this.context = new AnnotationConfigApplicationContext(); TestPropertyValues - .of("spring.transaction.default-timeout:30", "spring.transaction.rollback-on-commit-failure:true") + .of("spring.transaction.default-timeout:30", "spring.transaction.rollback-on-commit-failure:true", + "spring.jta.log-dir:" + new File(this.buildOutput, "bitronix-logs")) .applyTo(this.context); this.context.register(BitronixJtaConfiguration.class, TransactionAutoConfiguration.class); this.context.refresh(); diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/build.gradle index 3af4cc0d3b..6a67d0e624 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/build.gradle +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/build.gradle @@ -9,6 +9,7 @@ dependencies { implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-artemis")) implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-jpa")) implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-jta-atomikos")) + implementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) if (JavaVersion.current().java9Compatible) { implementation("jakarta.xml.bind:jakarta.xml.bind-api") } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/src/test/java/smoketest/atomikos/SampleAtomikosApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/src/test/java/smoketest/atomikos/SampleAtomikosApplicationTests.java index 33eb82c079..c65ae06d24 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/src/test/java/smoketest/atomikos/SampleAtomikosApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-atomikos/src/test/java/smoketest/atomikos/SampleAtomikosApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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,6 +16,7 @@ package smoketest.atomikos; +import java.io.File; import java.util.function.Consumer; import org.junit.jupiter.api.Test; @@ -23,6 +24,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; +import org.springframework.boot.testsupport.BuildOutput; import org.springframework.util.StringUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -37,7 +39,8 @@ class SampleAtomikosApplicationTests { @Test void testTransactionRollback(CapturedOutput output) throws Exception { - SampleAtomikosApplication.main(new String[] {}); + File logDir = new File(new BuildOutput(getClass()).getRootLocation(), "atomikos-logs"); + SampleAtomikosApplication.main(new String[] { "--spring.jta.log-dir=" + logDir }); assertThat(output).satisfies(numberOfOccurrences("---->", 1)); assertThat(output).satisfies(numberOfOccurrences("----> josh", 1)); assertThat(output).satisfies(numberOfOccurrences("Count is 1", 2)); diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/build.gradle index 55e14c4ede..d14f51108e 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/build.gradle +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/build.gradle @@ -9,6 +9,7 @@ dependencies { implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-artemis")) implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-jpa")) implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-jta-bitronix")) + implementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support")) if (JavaVersion.current().java9Compatible) { implementation("jakarta.xml.bind:jakarta.xml.bind-api") } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/src/test/java/smoketest/bitronix/SampleBitronixApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/src/test/java/smoketest/bitronix/SampleBitronixApplicationTests.java index 422980c4a0..2c9aa5350d 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/src/test/java/smoketest/bitronix/SampleBitronixApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-jta-bitronix/src/test/java/smoketest/bitronix/SampleBitronixApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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,6 +16,7 @@ package smoketest.bitronix; +import java.io.File; import java.util.function.Consumer; import bitronix.tm.resource.jms.PoolingConnectionFactory; @@ -25,6 +26,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.SpringApplication; import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; +import org.springframework.boot.testsupport.BuildOutput; import org.springframework.context.ApplicationContext; import org.springframework.util.StringUtils; @@ -38,9 +40,11 @@ import static org.assertj.core.api.Assertions.assertThat; @ExtendWith(OutputCaptureExtension.class) class SampleBitronixApplicationTests { + private final File jtaLogDir = new File(new BuildOutput(getClass()).getRootLocation(), "bitronix-logs"); + @Test void testTransactionRollback(CapturedOutput output) throws Exception { - SampleBitronixApplication.main(new String[] {}); + SampleBitronixApplication.main(new String[] { "--spring.jta.log-dir=" + this.jtaLogDir }); assertThat(output).satisfies(numberOfOccurrences("---->", 1)); assertThat(output).satisfies(numberOfOccurrences("----> josh", 1)); assertThat(output).satisfies(numberOfOccurrences("Count is 1", 2)); @@ -49,7 +53,8 @@ class SampleBitronixApplicationTests { @Test void testExposesXaAndNonXa() { - ApplicationContext context = SpringApplication.run(SampleBitronixApplication.class); + ApplicationContext context = SpringApplication.run(SampleBitronixApplication.class, + "--spring.jta.log-dir=" + this.jtaLogDir); Object jmsConnectionFactory = context.getBean("jmsConnectionFactory"); Object xaJmsConnectionFactory = context.getBean("xaJmsConnectionFactory"); Object nonXaJmsConnectionFactory = context.getBean("nonXaJmsConnectionFactory");