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 ;
@@ -65,14 +67,7 @@ class LiquibaseEndpointTests {
65
67
66
68
@ Test
67
69
void liquibaseReportIsReturned () {
68
- this .contextRunner .withUserConfiguration (Config .class ).run ((context ) -> {
69
- Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
70
- .liquibaseBeans ()
71
- .getContexts ()
72
- .get (context .getId ())
73
- .getLiquibaseBeans ();
74
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
75
- });
70
+ this .contextRunner .withUserConfiguration (Config .class ).run (hasEndpointWithInitializedSchema ());
76
71
}
77
72
78
73
@ Test
@@ -90,33 +85,60 @@ void liquibaseReportIsReturnedForContextHierarchy() {
90
85
}
91
86
92
87
@ Test
93
- @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA CUSTOMSCHEMA ;" )
94
- void invokeWithCustomSchema () {
88
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA ANOTHER_SCHEMA ;" )
89
+ void invokeWithCustomDefaultSchemaFailsIfItDoesNotExist () {
95
90
this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
96
- .withPropertyValues ("spring.liquibase.default-schema=CUSTOMSCHEMA" )
97
- .run ((context ) -> {
98
- Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
99
- .liquibaseBeans ()
100
- .getContexts ()
101
- .get (context .getId ())
102
- .getLiquibaseBeans ();
103
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
104
- });
91
+ .withPropertyValues ("spring.liquibase.default-schema=CUSTOM_DEFAULT_SCHEMA" )
92
+ .run ((context ) -> assertThat (context ).hasFailed ()
93
+ .getFailure ()
94
+ .rootCause ()
95
+ .hasMessageContaining ("CUSTOM_DEFAULT_SCHEMA" ));
96
+ }
97
+
98
+ @ Test
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" ));
115
+ }
116
+
117
+ @ Test
118
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA LIQUIBASE_SCHEMA;" )
119
+ void invokeWithLiquibaseSchema () {
120
+ this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
121
+ .withPropertyValues ("spring.liquibase.liquibase-schema=LIQUIBASE_SCHEMA" )
122
+ .run (hasEndpointWithInitializedSchema ());
105
123
}
106
124
107
125
@ Test
108
126
void invokeWithCustomTables () {
109
127
this .contextRunner .withUserConfiguration (Config .class )
110
128
.withPropertyValues ("spring.liquibase.database-change-log-lock-table=liquibase_database_changelog_lock" ,
111
129
"spring.liquibase.database-change-log-table=liquibase_database_changelog" )
112
- .run ((context ) -> {
113
- Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
114
- .liquibaseBeans ()
115
- .getContexts ()
116
- .get (context .getId ())
117
- .getLiquibaseBeans ();
118
- assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
119
- });
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
+ };
120
142
}
121
143
122
144
@ Test
@@ -179,7 +201,7 @@ DataSource dataSource() {
179
201
.setName (UUID .randomUUID ().toString ())
180
202
.build ();
181
203
DatabaseInitializationSettings settings = new DatabaseInitializationSettings ();
182
- settings .setSchemaLocations (Arrays . asList ("classpath:/db/create-custom-schema.sql" ));
204
+ settings .setSchemaLocations (List . of ("classpath:/db/create-custom-schema.sql" ));
183
205
DataSourceScriptDatabaseInitializer initializer = new DataSourceScriptDatabaseInitializer (dataSource ,
184
206
settings );
185
207
initializer .initializeDatabase ();
0 commit comments