Skip to content

Commit 27e3f19

Browse files
authored
Merge pull request #98 from remoun/patch-1
Add `LiquibasePreparer.forFile`
2 parents 7d5b82e + 1f3dba2 commit 27e3f19

File tree

10 files changed

+206
-22
lines changed

10 files changed

+206
-22
lines changed

src/main/java/io/zonky/test/db/postgres/embedded/LiquibasePreparer.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
import liquibase.database.jvm.JdbcConnection;
2020
import liquibase.exception.LiquibaseException;
2121
import liquibase.resource.ClassLoaderResourceAccessor;
22+
import liquibase.resource.FileSystemResourceAccessor;
23+
import liquibase.resource.ResourceAccessor;
2224

2325
import javax.sql.DataSource;
26+
import java.io.File;
2427
import java.sql.Connection;
2528
import java.sql.SQLException;
2629
import java.util.Objects;
@@ -30,45 +33,60 @@
3033
public final class LiquibasePreparer implements DatabasePreparer {
3134

3235
private final String location;
36+
private final ResourceAccessor accessor;
3337
private final Contexts contexts;
3438

3539
public static LiquibasePreparer forClasspathLocation(String location) {
36-
return new LiquibasePreparer(location, new Contexts());
40+
return forClasspathLocation(location, null);
3741
}
42+
3843
public static LiquibasePreparer forClasspathLocation(String location, Contexts contexts) {
39-
return new LiquibasePreparer(location, contexts);
44+
return new LiquibasePreparer(location, new ClassLoaderResourceAccessor(), contexts);
45+
}
46+
47+
public static LiquibasePreparer forFile(File file) {
48+
return forFile(file, null);
49+
}
50+
51+
public static LiquibasePreparer forFile(File file, Contexts contexts) {
52+
if (file == null)
53+
throw new IllegalArgumentException("Missing file");
54+
File dir = file.getParentFile();
55+
if (dir == null)
56+
throw new IllegalArgumentException("Cannot get parent dir from file");
57+
58+
return new LiquibasePreparer(file.getName(), new FileSystemResourceAccessor(dir), contexts);
4059
}
4160

42-
private LiquibasePreparer(String location, Contexts contexts) {
61+
private LiquibasePreparer(String location, ResourceAccessor accessor, Contexts contexts) {
4362
this.location = location;
44-
this.contexts = contexts;
63+
this.accessor = accessor;
64+
this.contexts = contexts != null ? contexts : new Contexts();
4565
}
4666

4767
@Override
4868
public void prepare(DataSource ds) throws SQLException {
49-
Connection connection = null;
50-
try {
51-
connection = ds.getConnection();
69+
try (Connection connection = ds.getConnection()) {
5270
Database database = getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
53-
Liquibase liquibase = new Liquibase(location, new ClassLoaderResourceAccessor(), database);
71+
Liquibase liquibase = new Liquibase(location, accessor, database);
5472
liquibase.update(contexts);
5573
} catch (LiquibaseException e) {
5674
throw new SQLException(e);
57-
} finally {
58-
if (connection != null) {
59-
connection.rollback();
60-
connection.close();
61-
}
6275
}
6376
}
6477

6578
@Override
66-
public boolean equals(Object obj) {
67-
return obj instanceof LiquibasePreparer && Objects.equals(location, ((LiquibasePreparer) obj).location);
79+
public boolean equals(Object o) {
80+
if (this == o) return true;
81+
if (o == null || getClass() != o.getClass()) return false;
82+
LiquibasePreparer that = (LiquibasePreparer) o;
83+
return Objects.equals(location, that.location)
84+
&& Objects.equals(accessor, that.accessor)
85+
&& Objects.equals(contexts.getContexts(), that.contexts.getContexts());
6886
}
6987

7088
@Override
7189
public int hashCode() {
72-
return Objects.hashCode(location);
90+
return Objects.hash(location, accessor, contexts.getContexts());
7391
}
7492
}

src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerContextTest.java renamed to src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import static org.junit.Assert.assertEquals;
2626

27-
public class LiquibasePreparerContextTest {
27+
public class LiquibasePreparerClasspathContextTest {
2828

2929
@Rule
3030
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master-test.xml", new Contexts("test")));

src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerTest.java renamed to src/test/java/io/zonky/test/db/postgres/junit/LiquibasePreparerClasspathTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import static org.junit.Assert.assertEquals;
2525

26-
public class LiquibasePreparerTest {
26+
public class LiquibasePreparerClasspathTest {
2727

2828
@Rule
2929
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master.xml"));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.zonky.test.db.postgres.junit;
15+
16+
import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
17+
import liquibase.Contexts;
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
21+
import java.io.File;
22+
import java.sql.Connection;
23+
import java.sql.ResultSet;
24+
import java.sql.Statement;
25+
26+
import static org.junit.Assert.assertEquals;
27+
28+
public class LiquibasePreparerFileContextTest {
29+
30+
@Rule
31+
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master-test.xml"), new Contexts("test")));
32+
33+
@Test
34+
public void testEmptyTables() throws Exception {
35+
try (Connection c = db.getTestDatabase().getConnection();
36+
Statement s = c.createStatement()) {
37+
ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM foo");
38+
rs.next();
39+
assertEquals(0, rs.getInt(1));
40+
}
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.zonky.test.db.postgres.junit;
15+
16+
import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
17+
import org.junit.Rule;
18+
import org.junit.Test;
19+
20+
import java.io.File;
21+
import java.sql.Connection;
22+
import java.sql.ResultSet;
23+
import java.sql.Statement;
24+
25+
import static org.junit.Assert.assertEquals;
26+
27+
public class LiquibasePreparerFileTest {
28+
29+
@Rule
30+
public PreparedDbRule db = EmbeddedPostgresRules.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master.xml")));
31+
32+
@Test
33+
public void testTablesMade() throws Exception {
34+
try (Connection c = db.getTestDatabase().getConnection();
35+
Statement s = c.createStatement()) {
36+
ResultSet rs = s.executeQuery("SELECT * FROM foo");
37+
rs.next();
38+
assertEquals("bar", rs.getString(1));
39+
}
40+
}
41+
}

src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerContextTest.java renamed to src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import static org.junit.jupiter.api.Assertions.assertEquals;
2626

27-
public class LiquibasePreparerContextTest {
27+
public class LiquibasePreparerClasspathContextTest {
2828

2929
@RegisterExtension
3030
public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master-test.xml", new Contexts("test")));

src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerTest.java renamed to src/test/java/io/zonky/test/db/postgres/junit5/LiquibasePreparerClasspathTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import static org.junit.jupiter.api.Assertions.assertEquals;
2525

26-
public class LiquibasePreparerTest {
26+
public class LiquibasePreparerClasspathTest {
2727

2828
@RegisterExtension
2929
public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forClasspathLocation("liqui/master.xml"));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.zonky.test.db.postgres.junit5;
15+
16+
import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
17+
import liquibase.Contexts;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.RegisterExtension;
20+
21+
import java.io.File;
22+
import java.sql.Connection;
23+
import java.sql.ResultSet;
24+
import java.sql.Statement;
25+
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
28+
public class LiquibasePreparerFileContextTest {
29+
30+
@RegisterExtension
31+
public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master-test.xml"), new Contexts("test")));
32+
33+
@Test
34+
public void testEmptyTables() throws Exception {
35+
try (Connection c = db.getTestDatabase().getConnection();
36+
Statement s = c.createStatement()) {
37+
ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM foo");
38+
rs.next();
39+
assertEquals(0, rs.getInt(1));
40+
}
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package io.zonky.test.db.postgres.junit5;
15+
16+
import io.zonky.test.db.postgres.embedded.LiquibasePreparer;
17+
import org.junit.jupiter.api.Test;
18+
import org.junit.jupiter.api.extension.RegisterExtension;
19+
20+
import java.io.File;
21+
import java.sql.Connection;
22+
import java.sql.ResultSet;
23+
import java.sql.Statement;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
27+
public class LiquibasePreparerFileTest {
28+
29+
@RegisterExtension
30+
public PreparedDbExtension db = EmbeddedPostgresExtension.preparedDatabase(LiquibasePreparer.forFile(new File("src/test/resources/liqui/master.xml")));
31+
32+
@Test
33+
public void testTablesMade() throws Exception {
34+
try (Connection c = db.getTestDatabase().getConnection();
35+
Statement s = c.createStatement()) {
36+
ResultSet rs = s.executeQuery("SELECT * FROM foo");
37+
rs.next();
38+
assertEquals("bar", rs.getString(1));
39+
}
40+
}
41+
}

src/test/resources/liqui/master-test.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1818
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
1919

20-
<include file="liqui/master.xml"/>
20+
<include file="master.xml" relativeToChangelogFile="true" />
2121

2222
<changeSet id="deleteAll" author="foo.bar" context="test">
2323
<comment>Delete from `foo` table</comment>
@@ -29,4 +29,4 @@
2929
<sql>INSERT INTO foo VALUES('bar');</sql>
3030
</changeSet>
3131

32-
</databaseChangeLog>
32+
</databaseChangeLog>

0 commit comments

Comments
 (0)