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
35
35
import org .springframework .boot .liquibase .endpoint .LiquibaseEndpoint ;
36
36
import org .springframework .boot .liquibase .endpoint .LiquibaseEndpoint .LiquibaseBeanDescriptor ;
37
37
import org .springframework .boot .sql .init .DatabaseInitializationSettings ;
38
+ import org .springframework .boot .test .context .assertj .AssertableApplicationContext ;
38
39
import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
40
+ import org .springframework .boot .test .context .runner .ContextConsumer ;
39
41
import org .springframework .boot .testsupport .classpath .resources .WithResource ;
40
42
import org .springframework .context .ApplicationContext ;
41
43
import org .springframework .context .annotation .Bean ;
@@ -66,14 +68,7 @@ class LiquibaseEndpointTests {
66
68
67
69
@ Test
68
70
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
- });
71
+ this .contextRunner .withUserConfiguration (Config .class ).run (hasEndpointWithInitializedSchema ());
77
72
}
78
73
79
74
@ Test
@@ -91,33 +86,60 @@ void liquibaseReportIsReturnedForContextHierarchy() {
91
86
}
92
87
93
88
@ Test
94
- @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA CUSTOMSCHEMA ;" )
95
- void invokeWithCustomSchema () {
89
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA ANOTHER_SCHEMA ;" )
90
+ void invokeWithCustomDefaultSchemaFailsIfItDoesNotExist () {
96
91
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
- });
92
+ .withPropertyValues ("spring.liquibase.default-schema=CUSTOM_DEFAULT_SCHEMA" )
93
+ .run ((context ) -> assertThat (context ).hasFailed ()
94
+ .getFailure ()
95
+ .rootCause ()
96
+ .hasMessageContaining ("CUSTOM_DEFAULT_SCHEMA" ));
97
+ }
98
+
99
+ @ Test
100
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA CUSTOM_SCHEMA;" )
101
+ void invokeWithCustomDefaultSchema () {
102
+ this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
103
+ .withPropertyValues ("spring.liquibase.default-schema=CUSTOM_SCHEMA" )
104
+ .run (hasEndpointWithInitializedSchema ());
105
+ }
106
+
107
+ @ Test
108
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA ANOTHER_SCHEMA;" )
109
+ void invokeWithLiquibaseSchemaFailsIfItDoesNotExist () {
110
+ this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
111
+ .withPropertyValues ("spring.liquibase.liquibase-schema=CUSTOM_LIQUIBASE_SCHEMA" )
112
+ .run ((context ) -> assertThat (context ).hasFailed ()
113
+ .getFailure ()
114
+ .rootCause ()
115
+ .hasMessageContaining ("CUSTOM_LIQUIBASE_SCHEMA" ));
116
+ }
117
+
118
+ @ Test
119
+ @ WithResource (name = "db/create-custom-schema.sql" , content = "CREATE SCHEMA LIQUIBASE_SCHEMA;" )
120
+ void invokeWithLiquibaseSchema () {
121
+ this .contextRunner .withUserConfiguration (Config .class , DataSourceWithSchemaConfiguration .class )
122
+ .withPropertyValues ("spring.liquibase.liquibase-schema=LIQUIBASE_SCHEMA" )
123
+ .run (hasEndpointWithInitializedSchema ());
106
124
}
107
125
108
126
@ Test
109
127
void invokeWithCustomTables () {
110
128
this .contextRunner .withUserConfiguration (Config .class )
111
129
.withPropertyValues ("spring.liquibase.database-change-log-lock-table=liquibase_database_changelog_lock" ,
112
130
"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
- });
131
+ .run (hasEndpointWithInitializedSchema ());
132
+ }
133
+
134
+ private ContextConsumer <AssertableApplicationContext > hasEndpointWithInitializedSchema () {
135
+ return (context ) -> {
136
+ Map <String , LiquibaseBeanDescriptor > liquibaseBeans = context .getBean (LiquibaseEndpoint .class )
137
+ .liquibaseBeans ()
138
+ .getContexts ()
139
+ .get (context .getId ())
140
+ .getLiquibaseBeans ();
141
+ assertThat (liquibaseBeans .get ("liquibase" ).getChangeSets ()).hasSize (1 );
142
+ };
121
143
}
122
144
123
145
@ Test
@@ -180,7 +202,7 @@ DataSource dataSource() {
180
202
.setName (UUID .randomUUID ().toString ())
181
203
.build ();
182
204
DatabaseInitializationSettings settings = new DatabaseInitializationSettings ();
183
- settings .setSchemaLocations (Arrays . asList ("classpath:/db/create-custom-schema.sql" ));
205
+ settings .setSchemaLocations (List . of ("classpath:/db/create-custom-schema.sql" ));
184
206
DataSourceScriptDatabaseInitializer initializer = new DataSourceScriptDatabaseInitializer (dataSource ,
185
207
settings );
186
208
initializer .initializeDatabase ();
0 commit comments