18
18
19
19
import java .sql .Connection ;
20
20
import java .sql .SQLException ;
21
- import java .util .Arrays ;
21
+ import java .util .List ;
22
22
import java .util .Map ;
23
23
import java .util .UUID ;
24
24
34
34
import org .springframework .boot .jdbc .EmbeddedDatabaseConnection ;
35
35
import org .springframework .boot .jdbc .init .DataSourceScriptDatabaseInitializer ;
36
36
import org .springframework .boot .sql .init .DatabaseInitializationSettings ;
37
+ import org .springframework .boot .test .context .assertj .AssertableApplicationContext ;
37
38
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
39
+ import org .springframework .boot .test .context .runner .ContextConsumer ;
38
40
import org .springframework .boot .testsupport .classpath .resources .WithResource ;
39
41
import org .springframework .context .ApplicationContext ;
40
42
import org .springframework .context .annotation .Bean ;
50
52
* @author Andy Wilkinson
51
53
* @author Stephane Nicoll
52
54
* @author Leo Li
53
- * @author Nabil Fawwaz Elqayyim
54
55
*/
55
56
@ WithResource (name = "db/changelog/db.changelog-master.yaml" , content = """
56
57
databaseChangeLog:
@@ -66,14 +67,7 @@ class LiquibaseEndpointTests {
66
67
67
68
@ Test
68
69
void liquibaseReportIsReturned () {
69
- this .contextRunner .withUserConfiguration (Config .class ).run ((context ) -> {
70
- Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
71
- .liquibaseBeans ()
72
- .getContexts ()
73
- .get (context .getId ())
74
- .getLiquibaseBeans ();
75
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
76
- });
70
+ this .contextRunner .withUserConfiguration (Config .class ).run (hasEndpointWithInitializedSchema ());
77
71
}
78
72
79
73
@ Test
@@ -91,48 +85,60 @@ void liquibaseReportIsReturnedForContextHierarchy() {
91
85
}
92
86
93
87
@ Test
94
- @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA CUSTOMSCHEMA ;" )
95
- void invokeWithCustomSchema () {
88
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA ANOTHER_SCHEMA ;" )
89
+ void invokeWithCustomDefaultSchemaFailsIfItDoesNotExist () {
96
90
this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
97
- .withPropertyValues ("spring.liquibase.default-schema=CUSTOMSCHEMA" )
98
- .run ((context ) -> {
99
- Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
100
- .liquibaseBeans ()
101
- .getContexts ()
102
- .get (context .getId ())
103
- .getLiquibaseBeans ();
104
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
105
- });
91
+ .withPropertyValues ("spring.liquibase.default-schema=CUSTOM_DEFAULT_SCHEMA" )
92
+ .run ((context ) -> assertThat (context ).hasFailed ()
93
+ .getFailure ()
94
+ .rootCause ()
95
+ .hasMessageContaining ("CUSTOM_DEFAULT_SCHEMA" ));
106
96
}
107
97
108
98
@ Test
109
- void invokeWithCustomTables () {
110
- this .contextRunner .withUserConfiguration (Config .class )
111
- .withPropertyValues ("spring.liquibase.database-change-log-lock-table=liquibase_database_changelog_lock" ,
112
- "spring.liquibase.database-change-log-table=liquibase_database_changelog" )
113
- .run ((context ) -> {
114
- Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
115
- .liquibaseBeans ()
116
- .getContexts ()
117
- .get (context .getId ())
118
- .getLiquibaseBeans ();
119
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
120
- });
99
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA CUSTOM_SCHEMA;" )
100
+ void invokeWithCustomDefaultSchema () {
101
+ this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
102
+ .withPropertyValues ("spring.liquibase.default-schema=CUSTOM_SCHEMA" )
103
+ .run (hasEndpointWithInitializedSchema ());
104
+ }
105
+
106
+ @ Test
107
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA ANOTHER_SCHEMA;" )
108
+ void invokeWithLiquibaseSchemaFailsIfItDoesNotExist () {
109
+ this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
110
+ .withPropertyValues ("spring.liquibase.liquibase-schema=CUSTOM_LIQUIBASE_SCHEMA" )
111
+ .run ((context ) -> assertThat (context ).hasFailed ()
112
+ .getFailure ()
113
+ .rootCause ()
114
+ .hasMessageContaining ("CUSTOM_LIQUIBASE_SCHEMA" ));
121
115
}
122
116
123
117
@ Test
124
118
@ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA LIQUIBASE_SCHEMA;" )
125
119
void invokeWithLiquibaseSchema () {
126
120
this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
127
- .withPropertyValues ("spring.liquibase.liquibase-schema=LIQUIBASE_SCHEMA" )
128
- .run ((context ) -> {
129
- Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
130
- .liquibaseBeans ()
131
- .getContexts ()
132
- .get (context .getId ())
133
- .getLiquibaseBeans ();
134
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
135
- });
121
+ .withPropertyValues ("spring.liquibase.liquibase-schema=LIQUIBASE_SCHEMA" )
122
+ .run (hasEndpointWithInitializedSchema ());
123
+ }
124
+
125
+ @ Test
126
+ void invokeWithCustomTables () {
127
+ this .contextRunner .withUserConfiguration (Config .class )
128
+ .withPropertyValues ("spring.liquibase.database-change-log-lock-table=liquibase_database_changelog_lock" ,
129
+ "spring.liquibase.database-change-log-table=liquibase_database_changelog" )
130
+ .run (hasEndpointWithInitializedSchema ());
131
+ }
132
+
133
+ private ContextConsumer <AssertableApplicationContext > hasEndpointWithInitializedSchema () {
134
+ return (context ) -> {
135
+ Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
136
+ .liquibaseBeans ()
137
+ .getContexts ()
138
+ .get (context .getId ())
139
+ .getLiquibaseBeans ();
140
+ assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
141
+ };
136
142
}
137
143
138
144
@ Test
@@ -195,7 +201,7 @@ DataSource dataSource() {
195
201
.setName (UUID .randomUUID ().toString ())
196
202
.build ();
197
203
DatabaseInitializationSettings settings = new DatabaseInitializationSettings ();
198
- settings .setSchemaLocations (Arrays . asList ("classpath:/db/create-custom-schema.sql" ));
204
+ settings .setSchemaLocations (List . of ("classpath:/db/create-custom-schema.sql" ));
199
205
DataSourceScriptDatabaseInitializer initializer = new DataSourceScriptDatabaseInitializer (dataSource ,
200
206
settings );
201
207
initializer .initializeDatabase ();
0 commit comments