This commit is contained in:
Stéphane Nicoll 2024-01-08 11:16:11 +01:00
parent 8552e149b5
commit 2d3b02a89d
36 changed files with 618 additions and 633 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,13 +25,13 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.mockito.BDDMockito.mock; import static org.mockito.BDDMockito.mock;
/** /**
* Unit tests for {@link ResourceDatabasePopulator}. * Tests for {@link ResourceDatabasePopulator}.
* *
* @author Sam Brannen * @author Sam Brannen
* @since 4.1 * @since 4.1
* @see AbstractDatabasePopulatorTests * @see AbstractDatabasePopulatorTests
*/ */
class ResourceDatabasePopulatorUnitTests { class ResourceDatabasePopulatorTests {
private static final Resource script1 = mock(); private static final Resource script1 = mock();
private static final Resource script2 = mock(); private static final Resource script2 = mock();

View File

@ -30,7 +30,7 @@ import static org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScr
* *
* @author Sam Brannen * @author Sam Brannen
* @since 4.0.3 * @since 4.0.3
* @see ScriptUtilsUnitTests * @see ScriptUtilsTests
*/ */
class ScriptUtilsIntegrationTests extends AbstractDatabaseInitializationTests { class ScriptUtilsIntegrationTests extends AbstractDatabaseInitializationTests {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,7 +36,7 @@ import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_COMME
import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_STATEMENT_SEPARATOR; import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_STATEMENT_SEPARATOR;
/** /**
* Unit tests for {@link ScriptUtils}. * Tests for {@link ScriptUtils}.
* *
* @author Thomas Risberg * @author Thomas Risberg
* @author Sam Brannen * @author Sam Brannen
@ -46,7 +46,7 @@ import static org.springframework.jdbc.datasource.init.ScriptUtils.DEFAULT_STATE
* @since 4.0.3 * @since 4.0.3
* @see ScriptUtilsIntegrationTests * @see ScriptUtilsIntegrationTests
*/ */
class ScriptUtilsUnitTests { class ScriptUtilsTests {
@Test @Test
void splitSqlScriptDelimitedWithSemicolon() { void splitSqlScriptDelimitedWithSemicolon() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -45,7 +45,7 @@ public abstract class AbstractEntityManagerFactoryBeanTests {
} }
@AfterEach @AfterEach
public void tearDown() { void tearDown() {
assertThat(TransactionSynchronizationManager.getResourceMap()).isEmpty(); assertThat(TransactionSynchronizationManager.getResourceMap()).isEmpty();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse(); assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse(); assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -104,7 +104,7 @@ public abstract class AbstractEntityManagerFactoryIntegrationTests {
} }
@AfterEach @AfterEach
public void cleanup() { void cleanup() {
if (this.transactionStatus != null && !this.transactionStatus.isCompleted()) { if (this.transactionStatus != null && !this.transactionStatus.isCompleted()) {
endTransaction(); endTransaction();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -71,7 +71,7 @@ class JpaTransactionManagerTests {
} }
@AfterEach @AfterEach
public void verifyTransactionSynchronizationManagerState() { void verifyTransactionSynchronizationManagerState() {
assertThat(TransactionSynchronizationManager.getResourceMap()).isEmpty(); assertThat(TransactionSynchronizationManager.getResourceMap()).isEmpty();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse(); assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse(); assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -43,7 +43,7 @@ class LocalEntityManagerFactoryBeanTests extends AbstractEntityManagerFactoryBea
private static Map actualProps; private static Map actualProps;
@AfterEach @AfterEach
public void verifyClosed() { void verifyClosed() {
verify(mockEmf).close(); verify(mockEmf).close();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -82,7 +82,7 @@ class OpenEntityManagerInViewTests {
} }
@AfterEach @AfterEach
public void tearDown() { void tearDown() {
assertThat(TransactionSynchronizationManager.getResourceMap()).isEmpty(); assertThat(TransactionSynchronizationManager.getResourceMap()).isEmpty();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse(); assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse(); assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,12 +40,12 @@ import org.springframework.r2dbc.UncategorizedR2dbcException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Unit tests for {@link ConnectionFactoryUtils}. * Tests for {@link ConnectionFactoryUtils}.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
class ConnectionFactoryUtilsUnitTests { class ConnectionFactoryUtilsTests {
@Test @Test
void shouldTranslateTransientResourceException() { void shouldTranslateTransientResourceException() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,11 +26,11 @@ import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.when; import static org.mockito.BDDMockito.when;
/** /**
* Unit tests for {@link DelegatingConnectionFactory}. * Tests for {@link DelegatingConnectionFactory}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
class DelegatingConnectionFactoryUnitTests { class DelegatingConnectionFactoryTests {
ConnectionFactory delegate = mock(); ConnectionFactory delegate = mock();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -55,12 +55,12 @@ import static org.mockito.BDDMockito.verifyNoMoreInteractions;
import static org.mockito.BDDMockito.when; import static org.mockito.BDDMockito.when;
/** /**
* Unit tests for {@link R2dbcTransactionManager}. * Tests for {@link R2dbcTransactionManager}.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
class R2dbcTransactionManagerUnitTests { class R2dbcTransactionManagerTests {
ConnectionFactory connectionFactoryMock = mock(); ConnectionFactory connectionFactoryMock = mock();
@ -624,7 +624,7 @@ class R2dbcTransactionManagerUnitTests {
private static class TestTransactionSynchronization implements TransactionSynchronization { private static class TestTransactionSynchronization implements TransactionSynchronization {
private int status; private final int status;
public boolean beforeCommitCalled; public boolean beforeCommitCalled;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,11 +33,11 @@ import static org.mockito.BDDMockito.verify;
import static org.mockito.BDDMockito.when; import static org.mockito.BDDMockito.when;
/** /**
* Unit tests for {@link SingleConnectionFactory}. * Tests for {@link SingleConnectionFactory}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
class SingleConnectionFactoryUnitTests { class SingleConnectionFactoryTests {
@Test @Test
void shouldAllocateSameConnection() { void shouldAllocateSameConnection() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,12 +38,12 @@ import static org.mockito.BDDMockito.verifyNoInteractions;
import static org.mockito.BDDMockito.when; import static org.mockito.BDDMockito.when;
/** /**
* Unit tests for {@link TransactionAwareConnectionFactoryProxy}. * Tests for {@link TransactionAwareConnectionFactoryProxy}.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl * @author Christoph Strobl
*/ */
class TransactionAwareConnectionFactoryProxyUnitTests { class TransactionAwareConnectionFactoryProxyTests {
ConnectionFactory connectionFactoryMock = mock(); ConnectionFactory connectionFactoryMock = mock();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,11 +28,11 @@ import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.when; import static org.mockito.BDDMockito.when;
/** /**
* Unit tests for {@link ConnectionFactoryInitializer}. * Tests for {@link ConnectionFactoryInitializer}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
class ConnectionFactoryInitializerUnitTests { class ConnectionFactoryInitializerTests {
AtomicBoolean called = new AtomicBoolean(); AtomicBoolean called = new AtomicBoolean();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,12 +25,12 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.mockito.BDDMockito.mock; import static org.mockito.BDDMockito.mock;
/** /**
* Unit tests for {@link ResourceDatabasePopulator}. * Tests for {@link ResourceDatabasePopulator}.
* *
* @author Sam Brannen * @author Sam Brannen
* @author Mark Paluch * @author Mark Paluch
*/ */
class ResourceDatabasePopulatorUnitTests { class ResourceDatabasePopulatorTests {
private static final Resource script1 = mock(); private static final Resource script1 = mock();
private static final Resource script2 = mock(); private static final Resource script2 = mock();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,7 +34,7 @@ import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_COMM
import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_STATEMENT_SEPARATOR; import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_STATEMENT_SEPARATOR;
/** /**
* Unit tests for {@link ScriptUtils}. * Tests for {@link ScriptUtils}.
* *
* @author Thomas Risberg * @author Thomas Risberg
* @author Sam Brannen * @author Sam Brannen
@ -44,10 +44,10 @@ import static org.springframework.r2dbc.connection.init.ScriptUtils.DEFAULT_STAT
* @author Mark Paluch * @author Mark Paluch
* @since 5.3 * @since 5.3
*/ */
public class ScriptUtilsUnitTests { class ScriptUtilsTests {
@Test @Test
public void splitSqlScriptDelimitedWithSemicolon() { void splitSqlScriptDelimitedWithSemicolon() {
String rawStatement1 = "insert into customer (id, name)\nvalues (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')"; String rawStatement1 = "insert into customer (id, name)\nvalues (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
String cleanedStatement1 = "insert into customer (id, name) values (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')"; String cleanedStatement1 = "insert into customer (id, name) values (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
String rawStatement2 = "insert into orders(id, order_date, customer_id)\nvalues (1, '2008-01-02', 2)"; String rawStatement2 = "insert into orders(id, order_date, customer_id)\nvalues (1, '2008-01-02', 2)";
@ -64,7 +64,7 @@ public class ScriptUtilsUnitTests {
} }
@Test @Test
public void splitSqlScriptDelimitedWithNewLine() { void splitSqlScriptDelimitedWithNewLine() {
String statement1 = "insert into customer (id, name) values (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')"; String statement1 = "insert into customer (id, name) values (1, 'Rod ; Johnson'), (2, 'Adrian \n Collier')";
String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)"; String statement2 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
String statement3 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)"; String statement3 = "insert into orders(id, order_date, customer_id) values (1, '2008-01-02', 2)";
@ -78,7 +78,7 @@ public class ScriptUtilsUnitTests {
} }
@Test @Test
public void splitSqlScriptDelimitedWithNewLineButDefaultDelimiterSpecified() { void splitSqlScriptDelimitedWithNewLineButDefaultDelimiterSpecified() {
String statement1 = "do something"; String statement1 = "do something";
String statement2 = "do something else"; String statement2 = "do something else";
@ -103,7 +103,7 @@ public class ScriptUtilsUnitTests {
} }
@Test // SPR-11560 @Test // SPR-11560
public void readAndSplitScriptWithMultipleNewlinesAsSeparator() throws Exception { public void readAndSplitScriptWithMultipleNewlinesAsSeparator() {
String script = readScript("db-test-data-multi-newline.sql"); String script = readScript("db-test-data-multi-newline.sql");
List<String> statements = splitSqlScript(script, "\n\n"); List<String> statements = splitSqlScript(script, "\n\n");
@ -114,19 +114,19 @@ public class ScriptUtilsUnitTests {
} }
@Test @Test
public void readAndSplitScriptContainingComments() throws Exception { void readAndSplitScriptContainingComments() {
String script = readScript("test-data-with-comments.sql"); String script = readScript("test-data-with-comments.sql");
splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES); splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES);
} }
@Test @Test
public void readAndSplitScriptContainingCommentsWithWindowsLineEnding() throws Exception { void readAndSplitScriptContainingCommentsWithWindowsLineEnding() {
String script = readScript("test-data-with-comments.sql").replaceAll("\n", "\r\n"); String script = readScript("test-data-with-comments.sql").replaceAll("\n", "\r\n");
splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES); splitScriptContainingComments(script, DEFAULT_COMMENT_PREFIXES);
} }
@Test @Test
public void readAndSplitScriptContainingCommentsWithMultiplePrefixes() throws Exception { void readAndSplitScriptContainingCommentsWithMultiplePrefixes() {
String script = readScript("test-data-with-multi-prefix-comments.sql"); String script = readScript("test-data-with-multi-prefix-comments.sql");
splitScriptContainingComments(script, "--", "#", "^"); splitScriptContainingComments(script, "--", "#", "^");
} }
@ -145,7 +145,7 @@ public class ScriptUtilsUnitTests {
} }
@Test // SPR-10330 @Test // SPR-10330
public void readAndSplitScriptContainingCommentsWithLeadingTabs() throws Exception { public void readAndSplitScriptContainingCommentsWithLeadingTabs() {
String script = readScript("test-data-with-comments-and-leading-tabs.sql"); String script = readScript("test-data-with-comments-and-leading-tabs.sql");
List<String> statements = splitSqlScript(script, ";"); List<String> statements = splitSqlScript(script, ";");
@ -157,7 +157,7 @@ public class ScriptUtilsUnitTests {
} }
@Test // SPR-9531 @Test // SPR-9531
public void readAndSplitScriptContainingMultiLineComments() throws Exception { public void readAndSplitScriptContainingMultiLineComments() {
String script = readScript("test-data-with-multi-line-comments.sql"); String script = readScript("test-data-with-multi-line-comments.sql");
List<String> statements = splitSqlScript(script, ";"); List<String> statements = splitSqlScript(script, ";");
@ -168,7 +168,7 @@ public class ScriptUtilsUnitTests {
} }
@Test @Test
public void readAndSplitScriptContainingMultiLineNestedComments() throws Exception { void readAndSplitScriptContainingMultiLineNestedComments() {
String script = readScript("test-data-with-multi-line-nested-comments.sql"); String script = readScript("test-data-with-multi-line-nested-comments.sql");
List<String> statements = splitSqlScript(script, ";"); List<String> statements = splitSqlScript(script, ";");

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,13 +32,13 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Unit tests for {@link AbstractRoutingConnectionFactory}. * Tests for {@link AbstractRoutingConnectionFactory}.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Jens Schauder * @author Jens Schauder
*/ */
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class AbstractRoutingConnectionFactoryUnitTests { class AbstractRoutingConnectionFactoryTests {
private static final String ROUTING_KEY = "routingKey"; private static final String ROUTING_KEY = "routingKey";

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,12 +32,12 @@ import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.when; import static org.mockito.BDDMockito.when;
/** /**
* Unit tests for {@link BeanFactoryConnectionFactoryLookup}. * Tests for {@link BeanFactoryConnectionFactoryLookup}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
public class BeanFactoryConnectionFactoryLookupUnitTests { public class BeanFactoryConnectionFactoryLookupTests {
private static final String CONNECTION_FACTORY_BEAN_NAME = "connectionFactory"; private static final String CONNECTION_FACTORY_BEAN_NAME = "connectionFactory";
@ -46,7 +46,7 @@ public class BeanFactoryConnectionFactoryLookupUnitTests {
@Test @Test
public void shouldLookupConnectionFactory() { void shouldLookupConnectionFactory() {
DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory(); DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory();
when(beanFactory.getBean(CONNECTION_FACTORY_BEAN_NAME, ConnectionFactory.class)) when(beanFactory.getBean(CONNECTION_FACTORY_BEAN_NAME, ConnectionFactory.class))
.thenReturn(expectedConnectionFactory); .thenReturn(expectedConnectionFactory);
@ -60,7 +60,7 @@ public class BeanFactoryConnectionFactoryLookupUnitTests {
} }
@Test @Test
public void shouldLookupWhereBeanFactoryYieldsNonConnectionFactoryType() { void shouldLookupWhereBeanFactoryYieldsNonConnectionFactoryType() {
BeanFactory beanFactory = mock(); BeanFactory beanFactory = mock();
when(beanFactory.getBean(CONNECTION_FACTORY_BEAN_NAME, ConnectionFactory.class)) when(beanFactory.getBean(CONNECTION_FACTORY_BEAN_NAME, ConnectionFactory.class))
.thenThrow(new BeanNotOfRequiredTypeException( .thenThrow(new BeanNotOfRequiredTypeException(
@ -72,7 +72,7 @@ public class BeanFactoryConnectionFactoryLookupUnitTests {
} }
@Test @Test
public void shouldLookupWhereBeanFactoryHasNotBeenSupplied() { void shouldLookupWhereBeanFactoryHasNotBeenSupplied() {
BeanFactoryConnectionFactoryLookup lookup = new BeanFactoryConnectionFactoryLookup(); BeanFactoryConnectionFactoryLookup lookup = new BeanFactoryConnectionFactoryLookup();
assertThatThrownBy(() -> lookup.getConnectionFactory(CONNECTION_FACTORY_BEAN_NAME)) assertThatThrownBy(() -> lookup.getConnectionFactory(CONNECTION_FACTORY_BEAN_NAME))

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -26,17 +26,17 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** /**
* Unit tests for {@link MapConnectionFactoryLookup}. * Tests for {@link MapConnectionFactoryLookup}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
public class MapConnectionFactoryLookupUnitTests { class MapConnectionFactoryLookupTests {
private static final String CONNECTION_FACTORY_NAME = "connectionFactory"; private static final String CONNECTION_FACTORY_NAME = "connectionFactory";
@Test @Test
public void getConnectionFactoriesReturnsUnmodifiableMap() { void getConnectionFactoriesReturnsUnmodifiableMap() {
MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup(); MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup();
Map<String, ConnectionFactory> connectionFactories = lookup.getConnectionFactories(); Map<String, ConnectionFactory> connectionFactories = lookup.getConnectionFactories();
@ -45,7 +45,7 @@ public class MapConnectionFactoryLookupUnitTests {
} }
@Test @Test
public void shouldLookupConnectionFactory() { void shouldLookupConnectionFactory() {
Map<String, ConnectionFactory> connectionFactories = new HashMap<>(); Map<String, ConnectionFactory> connectionFactories = new HashMap<>();
DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory(); DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory();
connectionFactories.put(CONNECTION_FACTORY_NAME, expectedConnectionFactory); connectionFactories.put(CONNECTION_FACTORY_NAME, expectedConnectionFactory);
@ -58,7 +58,7 @@ public class MapConnectionFactoryLookupUnitTests {
} }
@Test @Test
public void addingConnectionFactoryPermitsOverride() { void addingConnectionFactoryPermitsOverride() {
Map<String, ConnectionFactory> connectionFactories = new HashMap<>(); Map<String, ConnectionFactory> connectionFactories = new HashMap<>();
DummyConnectionFactory overriddenConnectionFactory = new DummyConnectionFactory(); DummyConnectionFactory overriddenConnectionFactory = new DummyConnectionFactory();
DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory(); DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory();
@ -84,7 +84,7 @@ public class MapConnectionFactoryLookupUnitTests {
} }
@Test @Test
public void getConnectionFactoryWhereSuppliedMapHasNoEntryForSpecifiedKey() { void getConnectionFactoryWhereSuppliedMapHasNoEntryForSpecifiedKey() {
MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup(); MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup();
assertThatThrownBy( assertThatThrownBy(

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -76,7 +76,7 @@ abstract class AbstractDatabaseClientIntegrationTests {
@Test @Test
public void executeInsert() { void executeInsert() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory); DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.sql("INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)") databaseClient.sql("INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)")
@ -97,7 +97,7 @@ abstract class AbstractDatabaseClientIntegrationTests {
} }
@Test @Test
public void executeInsertWithMap() { void executeInsertWithMap() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory); DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.sql("INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)") databaseClient.sql("INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)")
@ -118,7 +118,7 @@ abstract class AbstractDatabaseClientIntegrationTests {
} }
@Test @Test
public void executeInsertWithRecords() { void executeInsertWithRecords() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory); DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.sql("INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)") databaseClient.sql("INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)")
@ -137,7 +137,7 @@ abstract class AbstractDatabaseClientIntegrationTests {
} }
@Test @Test
public void shouldTranslateDuplicateKeyException() { void shouldTranslateDuplicateKeyException() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory); DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
executeInsert(); executeInsert();
@ -156,7 +156,7 @@ abstract class AbstractDatabaseClientIntegrationTests {
} }
@Test @Test
public void executeDeferred() { void executeDeferred() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory); DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.sql(() -> "INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)") databaseClient.sql(() -> "INSERT INTO legoset (id, name, manual) VALUES(:id, :name, :manual)")
@ -176,7 +176,7 @@ abstract class AbstractDatabaseClientIntegrationTests {
} }
@Test @Test
public void shouldEmitGeneratedKey() { void shouldEmitGeneratedKey() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory); DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
databaseClient.sql( databaseClient.sql(

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -111,7 +111,7 @@ abstract class AbstractTransactionalDatabaseClientIntegrationTests {
@Test @Test
public void executeInsertInTransaction() { void executeInsertInTransaction() {
Flux<Long> longFlux = databaseClient Flux<Long> longFlux = databaseClient
.sql(getInsertIntoLegosetStatement()) .sql(getInsertIntoLegosetStatement())
.bind(0, 42055) .bind(0, 42055)
@ -133,7 +133,7 @@ abstract class AbstractTransactionalDatabaseClientIntegrationTests {
} }
@Test @Test
public void shouldRollbackTransaction() { void shouldRollbackTransaction() {
Mono<Object> integerFlux = databaseClient.sql(getInsertIntoLegosetStatement()) Mono<Object> integerFlux = databaseClient.sql(getInsertIntoLegosetStatement())
.bind(0, 42055) .bind(0, 42055)
.bind(1, "SCHAUFELRADBAGGER") .bind(1, "SCHAUFELRADBAGGER")
@ -155,7 +155,7 @@ abstract class AbstractTransactionalDatabaseClientIntegrationTests {
} }
@Test @Test
public void shouldRollbackTransactionUsingTransactionalOperator() { void shouldRollbackTransactionUsingTransactionalOperator() {
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory); DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
TransactionalOperator transactionalOperator = TransactionalOperator TransactionalOperator transactionalOperator = TransactionalOperator

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -60,7 +60,7 @@ import static org.mockito.BDDMockito.verifyNoMoreInteractions;
import static org.mockito.BDDMockito.when; import static org.mockito.BDDMockito.when;
/** /**
* Unit tests for {@link DefaultDatabaseClient}. * Tests for {@link DefaultDatabaseClient}.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Ferdinand Jacobs * @author Ferdinand Jacobs
@ -68,7 +68,7 @@ import static org.mockito.BDDMockito.when;
* @author Simon Baslé * @author Simon Baslé
*/ */
@MockitoSettings(strictness = Strictness.LENIENT) @MockitoSettings(strictness = Strictness.LENIENT)
class DefaultDatabaseClientUnitTests { class DefaultDatabaseClientTests {
@Mock @Mock
private Connection connection; private Connection connection;
@ -499,21 +499,9 @@ class DefaultDatabaseClientUnitTests {
} }
static class ParameterBean { record ParameterBean(String key) {}
private final String key;
public ParameterBean(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
record ParameterRecord(String key) { record ParameterRecord(String key) {}
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 6.1 * @since 6.1
*/ */
public class H2DatabaseClientContextIntegrationTests extends H2DatabaseClientIntegrationTests { class H2DatabaseClientContextIntegrationTests extends H2DatabaseClientIntegrationTests {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,7 +24,7 @@ import io.r2dbc.spi.ConnectionFactory;
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
public class H2DatabaseClientIntegrationTests extends AbstractDatabaseClientIntegrationTests { class H2DatabaseClientIntegrationTests extends AbstractDatabaseClientIntegrationTests {
private static final String CREATE_TABLE_LEGOSET = """ private static final String CREATE_TABLE_LEGOSET = """
CREATE TABLE legoset ( CREATE TABLE legoset (

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,7 +24,7 @@ import io.r2dbc.spi.ConnectionFactory;
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
public class H2TransactionalDatabaseClientIntegrationTests extends AbstractTransactionalDatabaseClientIntegrationTests { class H2TransactionalDatabaseClientIntegrationTests extends AbstractTransactionalDatabaseClientIntegrationTests {
private static final String CREATE_TABLE_LEGOSET = """ private static final String CREATE_TABLE_LEGOSET = """
CREATE TABLE legoset ( CREATE TABLE legoset (

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,19 +35,19 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Unit tests for {@link NamedParameterUtils}. * Tests for {@link NamedParameterUtils}.
* *
* @author Mark Paluch * @author Mark Paluch
* @author Jens Schauder * @author Jens Schauder
* @author Anton Naydenov * @author Anton Naydenov
*/ */
public class NamedParameterUtilsUnitTests { class NamedParameterUtilsTests {
private final BindMarkersFactory BIND_MARKERS = BindMarkersFactory.indexed("$", 1); private final BindMarkersFactory BIND_MARKERS = BindMarkersFactory.indexed("$", 1);
@Test @Test
public void shouldParseSql() { void shouldParseSql() {
String sql = "xxx :a yyyy :b :c :a zzzzz"; String sql = "xxx :a yyyy :b :c :a zzzzz";
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql); ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
assertThat(psql.getParameterNames()).containsExactly("a", "b", "c", "a"); assertThat(psql.getParameterNames()).containsExactly("a", "b", "c", "a");
@ -66,7 +66,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void substituteNamedParameters() { void substituteNamedParameters() {
MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>()); MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>());
namedParams.addValue("a", "a").addValue("b", "b").addValue("c", "c"); namedParams.addValue("a", "a").addValue("b", "b").addValue("c", "c");
@ -82,7 +82,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void substituteObjectArray() { void substituteObjectArray() {
MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>()); MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>());
namedParams.addValue("a", namedParams.addValue("a",
Arrays.asList(new Object[] {"Walter", "Heisenberg"}, Arrays.asList(new Object[] {"Walter", "Heisenberg"},
@ -95,7 +95,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void shouldBindObjectArray() { void shouldBindObjectArray() {
MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>()); MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>());
namedParams.addValue("a", namedParams.addValue("a",
Arrays.asList(new Object[] {"Walter", "Heisenberg"}, Arrays.asList(new Object[] {"Walter", "Heisenberg"},
@ -114,7 +114,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlContainingComments() { void parseSqlContainingComments() {
String sql1 = "/*+ HINT */ xxx /* comment ? */ :a yyyy :b :c :a zzzzz -- :xx XX\n"; String sql1 = "/*+ HINT */ xxx /* comment ? */ :a yyyy :b :c :a zzzzz -- :xx XX\n";
ParsedSql psql1 = NamedParameterUtils.parseSqlStatement(sql1); ParsedSql psql1 = NamedParameterUtils.parseSqlStatement(sql1);
@ -133,7 +133,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithPostgresCasting() { void parseSqlStatementWithPostgresCasting() {
String expectedSql = "select 'first name' from artists where id = $1 and birth_date=$2::timestamp"; String expectedSql = "select 'first name' from artists where id = $1 and birth_date=$2::timestamp";
String sql = "select 'first name' from artists where id = :id and birth_date=:birthDate::timestamp"; String sql = "select 'first name' from artists where id = :id and birth_date=:birthDate::timestamp";
@ -145,7 +145,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithPostgresContainedOperator() { void parseSqlStatementWithPostgresContainedOperator() {
String expectedSql = "select 'first name' from artists where info->'stat'->'albums' = ?? $1 and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'"; String expectedSql = "select 'first name' from artists where info->'stat'->'albums' = ?? $1 and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'";
String sql = "select 'first name' from artists where info->'stat'->'albums' = ?? :album and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'"; String sql = "select 'first name' from artists where info->'stat'->'albums' = ?? :album and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'";
@ -155,7 +155,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() { void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() {
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]"; String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
String sql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]"; String sql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
@ -165,7 +165,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() { void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() {
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND $1 = 'Back in Black'"; String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND $1 = 'Back in Black'";
String sql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND :album = 'Back in Black'"; String sql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND :album = 'Back in Black'";
@ -175,7 +175,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithEscapedColon() { void parseSqlStatementWithEscapedColon() {
String expectedSql = "select '0\\:0' as a, foo from bar where baz < DATE($1 23:59:59) and baz = $2"; String expectedSql = "select '0\\:0' as a, foo from bar where baz < DATE($1 23:59:59) and baz = $2";
String sql = "select '0\\:0' as a, foo from bar where baz < DATE(:p1 23\\:59\\:59) and baz = :p2"; String sql = "select '0\\:0' as a, foo from bar where baz < DATE(:p1 23\\:59\\:59) and baz = :p2";
@ -185,7 +185,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithBracketDelimitedParameterNames() { void parseSqlStatementWithBracketDelimitedParameterNames() {
String expectedSql = "select foo from bar where baz = b$1$2z"; String expectedSql = "select foo from bar where baz = b$1$2z";
String sql = "select foo from bar where baz = b:{p1}:{p2}z"; String sql = "select foo from bar where baz = b:{p1}:{p2}z";
@ -195,7 +195,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() { void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() {
String expectedSql = "select foo from bar where baz = b:{}z"; String expectedSql = "select foo from bar where baz = b:{}z";
String sql = "select foo from bar where baz = b:{}z"; String sql = "select foo from bar where baz = b:{}z";
@ -212,7 +212,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithSingleLetterInBrackets() { void parseSqlStatementWithSingleLetterInBrackets() {
String expectedSql = "select foo from bar where baz = b$1z"; String expectedSql = "select foo from bar where baz = b$1z";
String sql = "select foo from bar where baz = b:{p}z"; String sql = "select foo from bar where baz = b:{p}z";
@ -222,7 +222,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithLogicalAnd() { void parseSqlStatementWithLogicalAnd() {
String expectedSql = "xxx & yyyy"; String expectedSql = "xxx & yyyy";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(expectedSql); ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(expectedSql);
@ -230,21 +230,21 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void substituteNamedParametersWithLogicalAnd() { void substituteNamedParametersWithLogicalAnd() {
String expectedSql = "xxx & yyyy"; String expectedSql = "xxx & yyyy";
assertThat(expand(expectedSql)).isEqualTo(expectedSql); assertThat(expand(expectedSql)).isEqualTo(expectedSql);
} }
@Test @Test
public void variableAssignmentOperator() { void variableAssignmentOperator() {
String expectedSql = "x := 1"; String expectedSql = "x := 1";
assertThat(expand(expectedSql)).isEqualTo(expectedSql); assertThat(expand(expectedSql)).isEqualTo(expectedSql);
} }
@Test @Test
public void parseSqlStatementWithQuotedSingleQuote() { void parseSqlStatementWithQuotedSingleQuote() {
String sql = "SELECT ':foo'':doo', :xxx FROM DUAL"; String sql = "SELECT ':foo'':doo', :xxx FROM DUAL";
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql); ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
@ -253,7 +253,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithQuotesAndCommentBefore() { void parseSqlStatementWithQuotesAndCommentBefore() {
String sql = "SELECT /*:doo*/':foo', :xxx FROM DUAL"; String sql = "SELECT /*:doo*/':foo', :xxx FROM DUAL";
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql); ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);
@ -262,7 +262,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void parseSqlStatementWithQuotesAndCommentAfter() { void parseSqlStatementWithQuotesAndCommentAfter() {
String sql2 = "SELECT ':foo'/*:doo*/, :xxx FROM DUAL"; String sql2 = "SELECT ':foo'/*:doo*/, :xxx FROM DUAL";
ParsedSql psql2 = NamedParameterUtils.parseSqlStatement(sql2); ParsedSql psql2 = NamedParameterUtils.parseSqlStatement(sql2);
@ -288,7 +288,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void shouldAllowParsingMultipleUseOfParameter() { void shouldAllowParsingMultipleUseOfParameter() {
String sql = "SELECT * FROM person where name = :id or lastname = :id"; String sql = "SELECT * FROM person where name = :id or lastname = :id";
ParsedSql parsed = NamedParameterUtils.parseSqlStatement(sql); ParsedSql parsed = NamedParameterUtils.parseSqlStatement(sql);
@ -298,7 +298,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void multipleEqualParameterReferencesBindsValueOnce() { void multipleEqualParameterReferencesBindsValueOnce() {
String sql = "SELECT * FROM person where name = :id or lastname = :id"; String sql = "SELECT * FROM person where name = :id or lastname = :id";
BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0); BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);
@ -332,7 +332,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void multipleEqualCollectionParameterReferencesBindsValueOnce() { void multipleEqualCollectionParameterReferencesBindsValueOnce() {
String sql = "SELECT * FROM person where name IN (:ids) or lastname IN (:ids)"; String sql = "SELECT * FROM person where name IN (:ids) or lastname IN (:ids)";
BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0); BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);
@ -373,7 +373,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void multipleEqualParameterReferencesForAnonymousMarkersBindsValueMultipleTimes() { void multipleEqualParameterReferencesForAnonymousMarkersBindsValueMultipleTimes() {
String sql = "SELECT * FROM person where name = :id or lastname = :id"; String sql = "SELECT * FROM person where name = :id or lastname = :id";
BindMarkersFactory factory = BindMarkersFactory.anonymous("?"); BindMarkersFactory factory = BindMarkersFactory.anonymous("?");
@ -410,7 +410,7 @@ public class NamedParameterUtilsUnitTests {
} }
@Test @Test
public void multipleEqualParameterReferencesBindsNullOnce() { void multipleEqualParameterReferencesBindsNullOnce() {
String sql = "SELECT * FROM person where name = :id or lastname = :id"; String sql = "SELECT * FROM person where name = :id or lastname = :id";
BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0); BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -57,10 +57,9 @@ class R2dbcBeanPropertyRowMapperTests {
@Test @Test
void mappingRowSimpleObject() { void mappingRowSimpleObject() {
MockRow mockRow = SIMPLE_PERSON_ROW;
BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class); BeanPropertyRowMapper<Person> mapper = new BeanPropertyRowMapper<>(Person.class);
Person result = mapper.apply(mockRow); Person result = mapper.apply(SIMPLE_PERSON_ROW);
assertThat(result.firstName).as("firstName").isEqualTo("John"); assertThat(result.firstName).as("firstName").isEqualTo("John");
assertThat(result.lastName).as("lastName").isEqualTo("Doe"); assertThat(result.lastName).as("lastName").isEqualTo("Doe");
@ -69,10 +68,9 @@ class R2dbcBeanPropertyRowMapperTests {
@Test @Test
void mappingRowMissingAttributeAccepted() { void mappingRowMissingAttributeAccepted() {
MockRow mockRow = SIMPLE_PERSON_ROW;
BeanPropertyRowMapper<ExtendedPerson> mapper = new BeanPropertyRowMapper<>(ExtendedPerson.class); BeanPropertyRowMapper<ExtendedPerson> mapper = new BeanPropertyRowMapper<>(ExtendedPerson.class);
ExtendedPerson result = mapper.apply(mockRow); ExtendedPerson result = mapper.apply(SIMPLE_PERSON_ROW);
assertThat(result.firstName).as("firstName").isEqualTo("John"); assertThat(result.firstName).as("firstName").isEqualTo("John");
assertThat(result.lastName).as("lastName").isEqualTo("Doe"); assertThat(result.lastName).as("lastName").isEqualTo("Doe");
@ -82,10 +80,9 @@ class R2dbcBeanPropertyRowMapperTests {
@Test @Test
void mappingRowWithDifferentName() { void mappingRowWithDifferentName() {
MockRow mockRow = EMAIL_PERSON_ROW;
BeanPropertyRowMapper<EmailPerson> mapper = new BeanPropertyRowMapper<>(EmailPerson.class); BeanPropertyRowMapper<EmailPerson> mapper = new BeanPropertyRowMapper<>(EmailPerson.class);
EmailPerson result = mapper.apply(mockRow); EmailPerson result = mapper.apply(EMAIL_PERSON_ROW);
assertThat(result.firstName).as("firstName").isEqualTo("John"); assertThat(result.firstName).as("firstName").isEqualTo("John");
assertThat(result.lastName).as("lastName").isEqualTo("Doe"); assertThat(result.lastName).as("lastName").isEqualTo("Doe");
@ -95,11 +92,10 @@ class R2dbcBeanPropertyRowMapperTests {
@Test @Test
void rowTypeAndMappingTypeMisaligned() { void rowTypeAndMappingTypeMisaligned() {
MockRow mockRow = EXTENDED_PERSON_ROW;
BeanPropertyRowMapper<TypeMismatchExtendedPerson> mapper = new BeanPropertyRowMapper<>(TypeMismatchExtendedPerson.class); BeanPropertyRowMapper<TypeMismatchExtendedPerson> mapper = new BeanPropertyRowMapper<>(TypeMismatchExtendedPerson.class);
assertThatExceptionOfType(TypeMismatchException.class) assertThatExceptionOfType(TypeMismatchException.class)
.isThrownBy(() -> mapper.apply(mockRow)) .isThrownBy(() -> mapper.apply(EXTENDED_PERSON_ROW))
.withMessage("Failed to convert property value of type 'java.lang.String' to required type " .withMessage("Failed to convert property value of type 'java.lang.String' to required type "
+ "'java.lang.String' for property 'address'; simulating type mismatch for address"); + "'java.lang.String' for property 'address'; simulating type mismatch for address");
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,10 +38,10 @@ class R2dbcDataClassRowMapperTests {
@Test @Test
void staticQueryWithDataClass() { void staticQueryWithDataClass() {
MockRow mockRow = MOCK_ROW; // uses name, age, birth_date
DataClassRowMapper<ConstructorPerson> mapper = new DataClassRowMapper<>(ConstructorPerson.class); DataClassRowMapper<ConstructorPerson> mapper = new DataClassRowMapper<>(ConstructorPerson.class);
ConstructorPerson person = mapper.apply(mockRow); // uses name, age, birth_date
ConstructorPerson person = mapper.apply(MOCK_ROW);
assertThat(person.name).as("name").isEqualTo("Bubba"); assertThat(person.name).as("name").isEqualTo("Bubba");
assertThat(person.age).as("age").isEqualTo(22L); assertThat(person.age).as("age").isEqualTo(22L);
@ -63,9 +63,10 @@ class R2dbcDataClassRowMapperTests {
@Test @Test
void staticQueryWithDataRecord() { void staticQueryWithDataRecord() {
MockRow mockRow = MOCK_ROW; // uses name, age, birth_date, balance
DataClassRowMapper<RecordPerson> mapper = new DataClassRowMapper<>(RecordPerson.class); DataClassRowMapper<RecordPerson> mapper = new DataClassRowMapper<>(RecordPerson.class);
RecordPerson person = mapper.apply(mockRow);
// uses name, age, birth_date, balance
RecordPerson person = mapper.apply(MOCK_ROW);
assertThat(person.name()).isEqualTo("Bubba"); assertThat(person.name()).isEqualTo("Bubba");
assertThat(person.age()).isEqualTo(22L); assertThat(person.age()).isEqualTo(22L);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,14 +23,14 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Unit tests for {@link AnonymousBindMarkers}. * Tests for {@link AnonymousBindMarkers}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
class AnonymousBindMarkersUnitTests { class AnonymousBindMarkersTests {
@Test @Test
public void shouldCreateNewBindMarkers() { void shouldCreateNewBindMarkers() {
BindMarkersFactory factory = BindMarkersFactory.anonymous("?"); BindMarkersFactory factory = BindMarkersFactory.anonymous("?");
BindMarkers bindMarkers1 = factory.create(); BindMarkers bindMarkers1 = factory.create();
@ -41,7 +41,7 @@ class AnonymousBindMarkersUnitTests {
} }
@Test @Test
public void shouldBindByIndex() { void shouldBindByIndex() {
BindTarget bindTarget = mock(); BindTarget bindTarget = mock();
BindMarkers bindMarkers = BindMarkersFactory.anonymous("?").create(); BindMarkers bindMarkers = BindMarkersFactory.anonymous("?").create();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,11 +25,11 @@ import org.reactivestreams.Publisher;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Unit tests for {@link BindMarkersFactoryResolver}. * Tests for {@link BindMarkersFactoryResolver}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
class BindMarkersFactoryResolverUnitTests { class BindMarkersFactoryResolverTests {
@Test @Test
void shouldReturnBindMarkersFactoryForH2() { void shouldReturnBindMarkersFactoryForH2() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -27,11 +27,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Unit tests for {@link Bindings}. * Tests for {@link Bindings}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
class BindingsUnitTests { class BindingsTests {
BindMarkersFactory markersFactory = BindMarkersFactory.indexed("$", 1); BindMarkersFactory markersFactory = BindMarkersFactory.indexed("$", 1);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,11 +23,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Unit tests for {@link IndexedBindMarkers}. * Tests for {@link IndexedBindMarkers}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
class IndexedBindMarkersUnitTests { class IndexedBindMarkersTests {
@Test @Test
void shouldCreateNewBindMarkers() { void shouldCreateNewBindMarkers() {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,11 +25,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
/** /**
* Unit tests for {@link NamedBindMarkers}. * Tests for {@link NamedBindMarkers}.
* *
* @author Mark Paluch * @author Mark Paluch
*/ */
class NamedBindMarkersUnitTests { class NamedBindMarkersTests {
@Test @Test
void shouldCreateNewBindMarkers() { void shouldCreateNewBindMarkers() {

View File

@ -0,0 +1,444 @@
/*
* Copyright 2002-2024 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.context.aot;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.aot.AotDetector;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GeneratedFiles.Kind;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;
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.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.ClassName;
import org.springframework.test.context.BootstrapUtils;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextBootstrapper;
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.aot.samples.jdbc.SqlScriptsSpringJupiterTests;
import org.springframework.test.context.aot.samples.web.WebSpringJupiterTests;
import org.springframework.test.context.aot.samples.web.WebSpringTestNGTests;
import org.springframework.test.context.aot.samples.web.WebSpringVintageTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringJupiterTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringTestNGTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringVintageTests;
import org.springframework.test.context.env.YamlPropertySourceFactory;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.function.ThrowingConsumer;
import org.springframework.web.context.WebApplicationContext;
import static java.util.Comparator.comparing;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_CONSTRUCTORS;
import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_METHODS;
import static org.springframework.aot.hint.MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS;
import static org.springframework.aot.hint.MemberCategory.INVOKE_PUBLIC_METHODS;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.resource;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
/**
* Tests for {@link TestContextAotGenerator}, {@link AotTestContextInitializers},
* {@link AotTestAttributes}, {@link AotContextLoader}, and run-time hints.
*
* @author Sam Brannen
* @since 6.0
*/
@CompileWithForkedClassLoader
class TestContextAotGeneratorIntegrationTests extends AbstractAotTests {
/**
* End-to-end tests within the scope of the {@link TestContextAotGenerator}.
*
* @see AotIntegrationTests
*/
@Test
void endToEndTests() {
Set<Class<?>> testClasses = Set.of(
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class,
SqlScriptsSpringJupiterTests.class,
XmlSpringJupiterTests.class,
WebSpringJupiterTests.class);
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
generator.processAheadOfTime(testClasses.stream().sorted(comparing(Class::getName)));
assertRuntimeHints(generator.getRuntimeHints());
List<String> sourceFiles = generatedFiles.getGeneratedFiles(Kind.SOURCE).keySet().stream().toList();
assertThat(sourceFiles).containsExactlyInAnyOrder(expectedSourceFiles);
TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles)).compile(ThrowingConsumer.of(compiled -> {
try {
System.setProperty(AotDetector.AOT_ENABLED, "true");
AotTestAttributesFactory.reset();
AotTestContextInitializersFactory.reset();
AotTestAttributes aotAttributes = AotTestAttributes.getInstance();
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> aotAttributes.setAttribute("foo", "bar"))
.withMessage("AOT attributes cannot be modified during AOT run-time execution");
String key = "@SpringBootConfiguration-" + BasicSpringVintageTests.class.getName();
assertThat(aotAttributes.getString(key)).isEqualTo("org.example.Main");
assertThat(aotAttributes.getBoolean(key + "-active1")).isTrue();
assertThat(aotAttributes.getBoolean(key + "-active2")).isTrue();
assertThat(aotAttributes.getString("bogus")).isNull();
assertThat(aotAttributes.getBoolean("bogus")).isFalse();
AotTestContextInitializers aotContextInitializers = new AotTestContextInitializers();
for (Class<?> testClass : testClasses) {
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
ApplicationContextInitializer<ConfigurableApplicationContext> contextInitializer =
aotContextInitializers.getContextInitializer(testClass);
assertThat(contextInitializer).isNotNull();
ApplicationContext context = ((AotContextLoader) mergedConfig.getContextLoader())
.loadContextForAotRuntime(mergedConfig, contextInitializer);
if (context instanceof WebApplicationContext wac) {
assertContextForWebTests(wac);
}
else if (testClass.getPackageName().contains("jdbc")) {
assertContextForJdbcTests(context);
}
else {
assertContextForBasicTests(context);
}
}
}
finally {
System.clearProperty(AotDetector.AOT_ENABLED);
AotTestAttributesFactory.reset();
}
}));
}
private static void assertRuntimeHints(RuntimeHints runtimeHints) {
assertReflectionRegistered(runtimeHints, AotTestContextInitializersCodeGenerator.GENERATED_MAPPINGS_CLASS_NAME, INVOKE_PUBLIC_METHODS);
assertReflectionRegistered(runtimeHints, AotTestAttributesCodeGenerator.GENERATED_ATTRIBUTES_CLASS_NAME, INVOKE_PUBLIC_METHODS);
Stream.of(
"org.opentest4j.TestAbortedException",
"org.junit.AssumptionViolatedException",
"org.testng.SkipException"
).forEach(type -> assertReflectionRegistered(runtimeHints, type));
Stream.of(
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.class,
org.springframework.test.context.support.DefaultBootstrapContext.class
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_PUBLIC_CONSTRUCTORS));
Stream.of(
org.springframework.test.context.support.DefaultTestContextBootstrapper.class,
org.springframework.test.context.support.DelegatingSmartContextLoader.class,
org.springframework.test.context.support.GenericGroovyXmlContextLoader.class,
org.springframework.test.context.web.GenericGroovyXmlWebContextLoader.class,
org.springframework.test.context.web.WebDelegatingSmartContextLoader.class,
org.springframework.test.context.web.WebTestContextBootstrapper.class
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
Stream.of(
org.springframework.test.context.web.WebAppConfiguration.class
).forEach(type -> assertAnnotationRegistered(runtimeHints, type));
// TestExecutionListener
Stream.of(
// @TestExecutionListeners
org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests.DummyTestExecutionListener.class,
org.springframework.test.context.event.ApplicationEventsTestExecutionListener.class,
org.springframework.test.context.event.EventPublishingTestExecutionListener.class,
org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.class,
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.class,
org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener.class,
org.springframework.test.context.support.DirtiesContextTestExecutionListener.class,
org.springframework.test.context.transaction.TransactionalTestExecutionListener.class,
org.springframework.test.context.web.ServletTestExecutionListener.class
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
// ContextCustomizerFactory
Stream.of(
"org.springframework.test.context.support.DynamicPropertiesContextCustomizerFactory",
"org.springframework.test.context.web.socket.MockServerContainerContextCustomizerFactory",
"org.springframework.test.context.aot.samples.basic.ImportsContextCustomizerFactory"
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
Stream.of(
// @BootstrapWith
org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests.CustomXmlBootstrapper.class,
// @ContextConfiguration(initializers = ...)
org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests.CustomInitializer.class,
// @ContextConfiguration(loader = ...)
org.springframework.test.context.support.AnnotationConfigContextLoader.class,
// @ActiveProfiles(resolver = ...)
org.springframework.test.context.aot.samples.basic.SpanishActiveProfilesResolver.class
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
// @ContextConfiguration(locations = ...)
assertThat(resource().forResource("org/springframework/test/context/aot/samples/xml/test-config.xml"))
.accepts(runtimeHints);
// @TestPropertySource(locations = ...)
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties"))
.as("@TestPropertySource(locations)")
.accepts(runtimeHints);
// @YamlTestProperties(...)
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/test1.yaml"))
.as("@YamlTestProperties: test1.yaml")
.accepts(runtimeHints);
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/test2.yaml"))
.as("@YamlTestProperties: test2.yaml")
.accepts(runtimeHints);
// @TestPropertySource(factory = ...)
assertReflectionRegistered(runtimeHints, YamlPropertySourceFactory.class.getName(), INVOKE_DECLARED_CONSTRUCTORS);
// @WebAppConfiguration(value = ...)
assertThat(resource().forResource("META-INF/web-resources/resources/Spring.js")).accepts(runtimeHints);
assertThat(resource().forResource("META-INF/web-resources/WEB-INF/views/home.jsp")).accepts(runtimeHints);
// @Sql(scripts = ...)
assertThat(resource().forResource("org/springframework/test/context/jdbc/schema.sql"))
.accepts(runtimeHints);
assertThat(resource().forResource("org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql"))
.accepts(runtimeHints);
}
private static void assertReflectionRegistered(RuntimeHints runtimeHints, String type) {
assertThat(reflection().onType(TypeReference.of(type)))
.as("Reflection hint for %s", type)
.accepts(runtimeHints);
}
private static void assertReflectionRegistered(RuntimeHints runtimeHints, String type, MemberCategory memberCategory) {
assertThat(reflection().onType(TypeReference.of(type)).withMemberCategory(memberCategory))
.as("Reflection hint for %s with category %s", type, memberCategory)
.accepts(runtimeHints);
}
private static void assertReflectionRegistered(RuntimeHints runtimeHints, Class<?> type, MemberCategory memberCategory) {
assertThat(reflection().onType(type).withMemberCategory(memberCategory))
.as("Reflection hint for %s with category %s", type.getSimpleName(), memberCategory)
.accepts(runtimeHints);
}
private static void assertAnnotationRegistered(RuntimeHints runtimeHints, Class<? extends Annotation> annotationType) {
assertReflectionRegistered(runtimeHints, annotationType, INVOKE_DECLARED_METHODS);
}
@Test
void processAheadOfTimeWithBasicTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class);
processAheadOfTime(testClasses, this::assertContextForBasicTests);
}
private void assertContextForBasicTests(ApplicationContext context) {
assertThat(context.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
MessageService messageService = context.getBean(MessageService.class);
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
String expectedMessage = cac.getEnvironment().matchesProfiles("spanish") ?
"¡Hola, AOT!" : "Hello, AOT!";
assertThat(messageService.generateMessage()).isEqualTo(expectedMessage);
}
private void assertContextForJdbcTests(ApplicationContext context) throws Exception {
assertThat(context.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
assertThat(context.getBean(DataSource.class)).as("DataSource").isNotNull();
}
private void assertContextForWebTests(WebApplicationContext wac) throws Exception {
assertThat(wac.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
MockMvc mockMvc = webAppContextSetup(wac).build();
mockMvc.perform(get("/hello")).andExpectAll(status().isOk(), content().string("Hello, AOT!"));
}
@Test
void processAheadOfTimeWithXmlTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
XmlSpringJupiterTests.class,
XmlSpringTestNGTests.class,
XmlSpringVintageTests.class);
processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();
MessageService messageService = context.getBean(MessageService.class);
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
});
}
@Test
void processAheadOfTimeWithWebTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
WebSpringJupiterTests.class,
WebSpringTestNGTests.class,
WebSpringVintageTests.class);
processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();
MockMvc mockMvc = webAppContextSetup((WebApplicationContext) context).build();
mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
});
}
@SuppressWarnings("unchecked")
private void processAheadOfTime(Set<Class<?>> testClasses, ThrowingConsumer<ApplicationContext> result) {
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
List<Mapping> mappings = processAheadOfTime(generator, testClasses);
TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles)).compile(ThrowingConsumer.of(compiled -> {
for (Mapping mapping : mappings) {
MergedContextConfiguration mergedConfig = mapping.mergedConfig();
ApplicationContextInitializer<ConfigurableApplicationContext> contextInitializer =
compiled.getInstance(ApplicationContextInitializer.class, mapping.className().reflectionName());
ApplicationContext context = ((AotContextLoader) mergedConfig.getContextLoader())
.loadContextForAotRuntime(mergedConfig, contextInitializer);
result.accept(context);
}
}));
}
private List<Mapping> processAheadOfTime(TestContextAotGenerator generator, Set<Class<?>> testClasses) {
List<Mapping> mappings = new ArrayList<>();
testClasses.forEach(testClass -> {
DefaultGenerationContext generationContext = generator.createGenerationContext(testClass);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
ClassName className = generator.processAheadOfTime(mergedConfig, generationContext);
assertThat(className).isNotNull();
mappings.add(new Mapping(mergedConfig, className));
generationContext.writeGeneratedContent();
});
return mappings;
}
private static MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) {
TestContextBootstrapper testContextBootstrapper = BootstrapUtils.resolveTestContextBootstrapper(testClass);
return testContextBootstrapper.buildMergedContextConfiguration();
}
record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {
}
private static final String[] expectedSourceFiles = {
// Global
"org/springframework/test/context/aot/AotTestContextInitializers__Generated.java",
"org/springframework/test/context/aot/AotTestAttributes__Generated.java",
// BasicSpringJupiterSharedConfigTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext001_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext001_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ManagementApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ManagementBeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext001_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext001_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext001_ManagementBeanDefinitions.java",
// BasicSpringJupiterTests -- not generated b/c already generated for BasicSpringJupiterSharedConfigTests.
// BasicSpringJupiterTests.NestedTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext002_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext002_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ManagementApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ManagementBeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext002_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext002_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext002_ManagementBeanDefinitions.java",
// BasicSpringTestNGTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext003_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext003_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext003_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext003_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext003_BeanDefinitions.java",
// BasicSpringVintageTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext004_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext004_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext004_BeanDefinitions.java",
// SqlScriptsSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext005_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext005_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext005_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext005_BeanFactoryRegistrations.java",
"org/springframework/test/context/jdbc/EmptyDatabaseConfig__TestContext005_BeanDefinitions.java",
// WebSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext006_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext006_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext006_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext006_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext006_BeanDefinitions.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext006_Autowiring.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext006_BeanDefinitions.java",
"org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport__TestContext006_BeanDefinitions.java",
// XmlSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext007_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext007_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext007_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext007_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext007_BeanFactoryRegistrations.java"
};
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,429 +16,53 @@
package org.springframework.test.context.aot; package org.springframework.test.context.aot;
import java.lang.annotation.Annotation; import org.junit.jupiter.api.AfterEach;
import java.util.ArrayList; import org.junit.jupiter.api.BeforeEach;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.aot.AotDetector; import org.springframework.core.SpringProperties;
import org.springframework.aot.generate.DefaultGenerationContext;
import org.springframework.aot.generate.GeneratedFiles.Kind;
import org.springframework.aot.generate.InMemoryGeneratedFiles;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;
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.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.ClassName;
import org.springframework.test.context.BootstrapUtils;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextBootstrapper;
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterSharedConfigTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.aot.samples.jdbc.SqlScriptsSpringJupiterTests;
import org.springframework.test.context.aot.samples.web.WebSpringJupiterTests;
import org.springframework.test.context.aot.samples.web.WebSpringTestNGTests;
import org.springframework.test.context.aot.samples.web.WebSpringVintageTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringJupiterTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringTestNGTests;
import org.springframework.test.context.aot.samples.xml.XmlSpringVintageTests;
import org.springframework.test.context.env.YamlPropertySourceFactory;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.util.function.ThrowingConsumer;
import org.springframework.web.context.WebApplicationContext;
import static java.util.Comparator.comparing;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.springframework.test.context.aot.TestContextAotGenerator.FAIL_ON_ERROR_PROPERTY_NAME;
import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_CONSTRUCTORS;
import static org.springframework.aot.hint.MemberCategory.INVOKE_DECLARED_METHODS;
import static org.springframework.aot.hint.MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS;
import static org.springframework.aot.hint.MemberCategory.INVOKE_PUBLIC_METHODS;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.resource;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
/** /**
* Tests for {@link TestContextAotGenerator}, {@link AotTestContextInitializers}, * Tests for {@link TestContextAotGenerator}.
* {@link AotTestAttributes}, {@link AotContextLoader}, and run-time hints.
* *
* @author Sam Brannen * @author Sam Brannen
* @since 6.0 * @since 6.1
*/ */
@CompileWithForkedClassLoader class TestContextAotGeneratorTests {
class TestContextAotGeneratorTests extends AbstractAotTests {
/** @BeforeEach
* End-to-end tests within the scope of the {@link TestContextAotGenerator}. @AfterEach
* void resetFlag() {
* @see AotIntegrationTests SpringProperties.setProperty(FAIL_ON_ERROR_PROPERTY_NAME, null);
*/
@Test
void endToEndTests() {
Set<Class<?>> testClasses = Set.of(
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class,
SqlScriptsSpringJupiterTests.class,
XmlSpringJupiterTests.class,
WebSpringJupiterTests.class);
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
generator.processAheadOfTime(testClasses.stream().sorted(comparing(Class::getName)));
assertRuntimeHints(generator.getRuntimeHints());
List<String> sourceFiles = generatedFiles.getGeneratedFiles(Kind.SOURCE).keySet().stream().toList();
assertThat(sourceFiles).containsExactlyInAnyOrder(expectedSourceFiles);
TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles)).compile(ThrowingConsumer.of(compiled -> {
try {
System.setProperty(AotDetector.AOT_ENABLED, "true");
AotTestAttributesFactory.reset();
AotTestContextInitializersFactory.reset();
AotTestAttributes aotAttributes = AotTestAttributes.getInstance();
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(() -> aotAttributes.setAttribute("foo", "bar"))
.withMessage("AOT attributes cannot be modified during AOT run-time execution");
String key = "@SpringBootConfiguration-" + BasicSpringVintageTests.class.getName();
assertThat(aotAttributes.getString(key)).isEqualTo("org.example.Main");
assertThat(aotAttributes.getBoolean(key + "-active1")).isTrue();
assertThat(aotAttributes.getBoolean(key + "-active2")).isTrue();
assertThat(aotAttributes.getString("bogus")).isNull();
assertThat(aotAttributes.getBoolean("bogus")).isFalse();
AotTestContextInitializers aotContextInitializers = new AotTestContextInitializers();
for (Class<?> testClass : testClasses) {
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
ApplicationContextInitializer<ConfigurableApplicationContext> contextInitializer =
aotContextInitializers.getContextInitializer(testClass);
assertThat(contextInitializer).isNotNull();
ApplicationContext context = ((AotContextLoader) mergedConfig.getContextLoader())
.loadContextForAotRuntime(mergedConfig, contextInitializer);
if (context instanceof WebApplicationContext wac) {
assertContextForWebTests(wac);
}
else if (testClass.getPackageName().contains("jdbc")) {
assertContextForJdbcTests(context);
}
else {
assertContextForBasicTests(context);
}
}
}
finally {
System.clearProperty(AotDetector.AOT_ENABLED);
AotTestAttributesFactory.reset();
}
}));
}
private static void assertRuntimeHints(RuntimeHints runtimeHints) {
assertReflectionRegistered(runtimeHints, AotTestContextInitializersCodeGenerator.GENERATED_MAPPINGS_CLASS_NAME, INVOKE_PUBLIC_METHODS);
assertReflectionRegistered(runtimeHints, AotTestAttributesCodeGenerator.GENERATED_ATTRIBUTES_CLASS_NAME, INVOKE_PUBLIC_METHODS);
Stream.of(
"org.opentest4j.TestAbortedException",
"org.junit.AssumptionViolatedException",
"org.testng.SkipException"
).forEach(type -> assertReflectionRegistered(runtimeHints, type));
Stream.of(
org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.class,
org.springframework.test.context.support.DefaultBootstrapContext.class
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_PUBLIC_CONSTRUCTORS));
Stream.of(
org.springframework.test.context.support.DefaultTestContextBootstrapper.class,
org.springframework.test.context.support.DelegatingSmartContextLoader.class,
org.springframework.test.context.support.GenericGroovyXmlContextLoader.class,
org.springframework.test.context.web.GenericGroovyXmlWebContextLoader.class,
org.springframework.test.context.web.WebDelegatingSmartContextLoader.class,
org.springframework.test.context.web.WebTestContextBootstrapper.class
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
Stream.of(
org.springframework.test.context.web.WebAppConfiguration.class
).forEach(type -> assertAnnotationRegistered(runtimeHints, type));
// TestExecutionListener
Stream.of(
// @TestExecutionListeners
org.springframework.test.context.aot.samples.basic.BasicSpringJupiterTests.DummyTestExecutionListener.class,
org.springframework.test.context.event.ApplicationEventsTestExecutionListener.class,
org.springframework.test.context.event.EventPublishingTestExecutionListener.class,
org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.class,
org.springframework.test.context.support.DependencyInjectionTestExecutionListener.class,
org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener.class,
org.springframework.test.context.support.DirtiesContextTestExecutionListener.class,
org.springframework.test.context.transaction.TransactionalTestExecutionListener.class,
org.springframework.test.context.web.ServletTestExecutionListener.class
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
// ContextCustomizerFactory
Stream.of(
"org.springframework.test.context.support.DynamicPropertiesContextCustomizerFactory",
"org.springframework.test.context.web.socket.MockServerContainerContextCustomizerFactory",
"org.springframework.test.context.aot.samples.basic.ImportsContextCustomizerFactory"
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
Stream.of(
// @BootstrapWith
org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests.CustomXmlBootstrapper.class,
// @ContextConfiguration(initializers = ...)
org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests.CustomInitializer.class,
// @ContextConfiguration(loader = ...)
org.springframework.test.context.support.AnnotationConfigContextLoader.class,
// @ActiveProfiles(resolver = ...)
org.springframework.test.context.aot.samples.basic.SpanishActiveProfilesResolver.class
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
// @ContextConfiguration(locations = ...)
assertThat(resource().forResource("org/springframework/test/context/aot/samples/xml/test-config.xml"))
.accepts(runtimeHints);
// @TestPropertySource(locations = ...)
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests.properties"))
.as("@TestPropertySource(locations)")
.accepts(runtimeHints);
// @YamlTestProperties(...)
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/test1.yaml"))
.as("@YamlTestProperties: test1.yaml")
.accepts(runtimeHints);
assertThat(resource().forResource("org/springframework/test/context/aot/samples/basic/test2.yaml"))
.as("@YamlTestProperties: test2.yaml")
.accepts(runtimeHints);
// @TestPropertySource(factory = ...)
assertReflectionRegistered(runtimeHints, YamlPropertySourceFactory.class.getName(), INVOKE_DECLARED_CONSTRUCTORS);
// @WebAppConfiguration(value = ...)
assertThat(resource().forResource("META-INF/web-resources/resources/Spring.js")).accepts(runtimeHints);
assertThat(resource().forResource("META-INF/web-resources/WEB-INF/views/home.jsp")).accepts(runtimeHints);
// @Sql(scripts = ...)
assertThat(resource().forResource("org/springframework/test/context/jdbc/schema.sql"))
.accepts(runtimeHints);
assertThat(resource().forResource("org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests.test.sql"))
.accepts(runtimeHints);
}
private static void assertReflectionRegistered(RuntimeHints runtimeHints, String type) {
assertThat(reflection().onType(TypeReference.of(type)))
.as("Reflection hint for %s", type)
.accepts(runtimeHints);
}
private static void assertReflectionRegistered(RuntimeHints runtimeHints, String type, MemberCategory memberCategory) {
assertThat(reflection().onType(TypeReference.of(type)).withMemberCategory(memberCategory))
.as("Reflection hint for %s with category %s", type, memberCategory)
.accepts(runtimeHints);
}
private static void assertReflectionRegistered(RuntimeHints runtimeHints, Class<?> type, MemberCategory memberCategory) {
assertThat(reflection().onType(type).withMemberCategory(memberCategory))
.as("Reflection hint for %s with category %s", type.getSimpleName(), memberCategory)
.accepts(runtimeHints);
}
private static void assertAnnotationRegistered(RuntimeHints runtimeHints, Class<? extends Annotation> annotationType) {
assertReflectionRegistered(runtimeHints, annotationType, INVOKE_DECLARED_METHODS);
}
@Test
void processAheadOfTimeWithBasicTests() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
BasicSpringJupiterSharedConfigTests.class,
BasicSpringJupiterTests.class,
BasicSpringJupiterTests.NestedTests.class,
BasicSpringTestNGTests.class,
BasicSpringVintageTests.class);
processAheadOfTime(testClasses, this::assertContextForBasicTests);
}
private void assertContextForBasicTests(ApplicationContext context) {
assertThat(context.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
MessageService messageService = context.getBean(MessageService.class);
ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context;
String expectedMessage = cac.getEnvironment().matchesProfiles("spanish") ?
"¡Hola, AOT!" : "Hello, AOT!";
assertThat(messageService.generateMessage()).isEqualTo(expectedMessage);
}
private void assertContextForJdbcTests(ApplicationContext context) throws Exception {
assertThat(context.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
assertThat(context.getBean(DataSource.class)).as("DataSource").isNotNull();
}
private void assertContextForWebTests(WebApplicationContext wac) throws Exception {
assertThat(wac.getEnvironment().getProperty("test.engine")).as("Environment").isNotNull();
MockMvc mockMvc = webAppContextSetup(wac).build();
mockMvc.perform(get("/hello")).andExpectAll(status().isOk(), content().string("Hello, AOT!"));
} }
@Test @Test
void processAheadOfTimeWithXmlTests() { void failOnErrorEnabledByDefault() {
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess assertThat(createGenerator().failOnError).isTrue();
// cannot support @ParameterizedTest methods.
Set<Class<?>> testClasses = Set.of(
XmlSpringJupiterTests.class,
XmlSpringTestNGTests.class,
XmlSpringVintageTests.class);
processAheadOfTime(testClasses, context -> {
assertThat(context.getEnvironment().getProperty("test.engine"))
.as("Environment").isNotNull();
MessageService messageService = context.getBean(MessageService.class);
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
});
} }
@Test @ParameterizedTest
void processAheadOfTimeWithWebTests() { @ValueSource(strings = { "true", " True\t" })
// We cannot parameterize with the test classes, since @CompileWithTargetClassAccess void failOnErrorEnabledViaSpringProperty(String value) {
// cannot support @ParameterizedTest methods. SpringProperties.setProperty(FAIL_ON_ERROR_PROPERTY_NAME, value);
Set<Class<?>> testClasses = Set.of( assertThat(createGenerator().failOnError).isTrue();
WebSpringJupiterTests.class, }
WebSpringTestNGTests.class,
WebSpringVintageTests.class);
processAheadOfTime(testClasses, context -> { @ParameterizedTest
assertThat(context.getEnvironment().getProperty("test.engine")) @ValueSource(strings = { "false", " False\t", "x" })
.as("Environment").isNotNull(); void failOnErrorDisabledViaSpringProperty(String value) {
SpringProperties.setProperty(FAIL_ON_ERROR_PROPERTY_NAME, value);
MockMvc mockMvc = webAppContextSetup((WebApplicationContext) context).build(); assertThat(createGenerator().failOnError).isFalse();
mockMvc.perform(get("/hello"))
.andExpectAll(status().isOk(), content().string("Hello, AOT!"));
});
} }
@SuppressWarnings("unchecked") private static TestContextAotGenerator createGenerator() {
private void processAheadOfTime(Set<Class<?>> testClasses, ThrowingConsumer<ApplicationContext> result) { return new TestContextAotGenerator(null);
InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
TestContextAotGenerator generator = new TestContextAotGenerator(generatedFiles);
List<Mapping> mappings = processAheadOfTime(generator, testClasses);
TestCompiler.forSystem().with(CompilerFiles.from(generatedFiles)).compile(ThrowingConsumer.of(compiled -> {
for (Mapping mapping : mappings) {
MergedContextConfiguration mergedConfig = mapping.mergedConfig();
ApplicationContextInitializer<ConfigurableApplicationContext> contextInitializer =
compiled.getInstance(ApplicationContextInitializer.class, mapping.className().reflectionName());
ApplicationContext context = ((AotContextLoader) mergedConfig.getContextLoader())
.loadContextForAotRuntime(mergedConfig, contextInitializer);
result.accept(context);
}
}));
} }
private List<Mapping> processAheadOfTime(TestContextAotGenerator generator, Set<Class<?>> testClasses) {
List<Mapping> mappings = new ArrayList<>();
testClasses.forEach(testClass -> {
DefaultGenerationContext generationContext = generator.createGenerationContext(testClass);
MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);
ClassName className = generator.processAheadOfTime(mergedConfig, generationContext);
assertThat(className).isNotNull();
mappings.add(new Mapping(mergedConfig, className));
generationContext.writeGeneratedContent();
});
return mappings;
}
private static MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) {
TestContextBootstrapper testContextBootstrapper = BootstrapUtils.resolveTestContextBootstrapper(testClass);
return testContextBootstrapper.buildMergedContextConfiguration();
}
record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {
}
private static final String[] expectedSourceFiles = {
// Global
"org/springframework/test/context/aot/AotTestContextInitializers__Generated.java",
"org/springframework/test/context/aot/AotTestAttributes__Generated.java",
// BasicSpringJupiterSharedConfigTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext001_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext001_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ManagementApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterSharedConfigTests__TestContext001_ManagementBeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext001_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext001_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext001_ManagementBeanDefinitions.java",
// BasicSpringJupiterTests -- not generated b/c already generated for BasicSpringJupiterSharedConfigTests.
// BasicSpringJupiterTests.NestedTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext002_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext002_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ManagementApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests_NestedTests__TestContext002_ManagementBeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext002_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/management/ManagementConfiguration__TestContext002_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/management/ManagementMessageService__TestContext002_ManagementBeanDefinitions.java",
// BasicSpringTestNGTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext003_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext003_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext003_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringTestNGTests__TestContext003_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext003_BeanDefinitions.java",
// BasicSpringVintageTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext004_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext004_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/basic/BasicSpringVintageTests__TestContext004_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/basic/BasicTestConfiguration__TestContext004_BeanDefinitions.java",
// SqlScriptsSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext005_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext005_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext005_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext005_BeanFactoryRegistrations.java",
"org/springframework/test/context/jdbc/EmptyDatabaseConfig__TestContext005_BeanDefinitions.java",
// WebSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext006_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext006_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext006_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext006_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext006_BeanDefinitions.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext006_Autowiring.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext006_BeanDefinitions.java",
"org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport__TestContext006_BeanDefinitions.java",
// XmlSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext007_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext007_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext007_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext007_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext007_BeanFactoryRegistrations.java"
};
} }

View File

@ -1,68 +0,0 @@
/*
* 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.context.aot;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.core.SpringProperties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.context.aot.TestContextAotGenerator.FAIL_ON_ERROR_PROPERTY_NAME;
/**
* Unit tests for {@link TestContextAotGenerator}.
*
* @author Sam Brannen
* @since 6.1
*/
class TestContextAotGeneratorUnitTests {
@BeforeEach
@AfterEach
void resetFlag() {
SpringProperties.setProperty(FAIL_ON_ERROR_PROPERTY_NAME, null);
}
@Test
void failOnErrorEnabledByDefault() {
assertThat(createGenerator().failOnError).isTrue();
}
@ParameterizedTest
@ValueSource(strings = {"true", " True\t"})
void failOnErrorEnabledViaSpringProperty(String value) {
SpringProperties.setProperty(FAIL_ON_ERROR_PROPERTY_NAME, value);
assertThat(createGenerator().failOnError).isTrue();
}
@ParameterizedTest
@ValueSource(strings = {"false", " False\t", "x"})
void failOnErrorDisabledViaSpringProperty(String value) {
SpringProperties.setProperty(FAIL_ON_ERROR_PROPERTY_NAME, value);
assertThat(createGenerator().failOnError).isFalse();
}
private static TestContextAotGenerator createGenerator() {
return new TestContextAotGenerator(null);
}
}