diff --git a/README.md b/README.md index 6583652..fb5ef62 100644 --- a/README.md +++ b/README.md @@ -121,4 +121,4 @@ postgres.start(cachedRuntimeConfig("C:\\Users\\vasya\\pgembedded-installation")) * 11.1: on Mac OS X and Windows 64 bit * 10.6, 9.6.11, 9.5.15: on Linux, Windows, Mac OS X -* any custom version +* any custom version \ No newline at end of file diff --git a/pom.xml b/pom.xml index e739dc5..85cd55b 100644 --- a/pom.xml +++ b/pom.xml @@ -102,6 +102,11 @@ -Xdoclint:none + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + diff --git a/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java b/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java index f8f2a7d..fbae1ab 100644 --- a/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java +++ b/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresProcess.java @@ -283,22 +283,13 @@ protected final void onAfterProcessStart(ProcessControl process, } while (trial++ < MAX_CREATEDB_TRIALS); } - /** - * Import into database from file - * - * @param file The file to import into database - */ - public void importFromFile(File file) { - importFromFileWithArgs(file); - } - /** * Import into database from file with additional args * * @param file * @param cliArgs additional arguments for psql (be sure to separate args from their values) */ - public void importFromFileWithArgs(File file, String... cliArgs) { + public void importFromFile(File file, String... cliArgs) { if (file.exists()) { String[] args = { "-U", getConfig().credentials().username(), @@ -334,36 +325,51 @@ public void restoreFromFile(File file, String... cliArgs) { } } - public void exportToFile(File file) { - runCmd(getConfig(), runtimeConfig, PgDump, "", new HashSet<>(singletonList("export from " + getConfig().storage().dbName() + " failed")), - "-U", getConfig().credentials().username(), - "-d", getConfig().storage().dbName(), - "-h", getConfig().net().host(), - "-p", String.valueOf(getConfig().net().port()), - "-f", file.getAbsolutePath() - ); + /** + * Export (dump) database to file with additional args + * + * @param file + * @param cliArgs additional arguments for psql (be sure to separate args from their values) + */ + public void exportToFile(File file, String... cliArgs) { + String[] args = { + "-U", getConfig().credentials().username(), + "-d", getConfig().storage().dbName(), + "-h", getConfig().net().host(), + "-p", String.valueOf(getConfig().net().port()), + "-f", file.getAbsolutePath()}; + if (cliArgs != null && cliArgs.length != 0) { + args = ArrayUtils.addAll(args, cliArgs); + } + runCmd(getConfig(), runtimeConfig, PgDump, "", new HashSet<>(singletonList("export from " + getConfig().storage().dbName() + " failed")), args); } + /** + * Export (dump) database schema to file + * + * @param file + */ public void exportSchemeToFile(File file) { - runCmd(getConfig(), runtimeConfig, PgDump, "", new HashSet<>(singletonList("export from " + getConfig().storage().dbName() + " failed")), - "-U", getConfig().credentials().username(), - "-d", getConfig().storage().dbName(), - "-h", getConfig().net().host(), - "-p", String.valueOf(getConfig().net().port()), - "-f", file.getAbsolutePath(), - "-s" - ); + exportToFile(file, "-s"); } + /** + * Export (dump) database schema to file + * + * @param file + */ + /* Alias for English speakers */ + public void exportSchemaToFile(File file) { + exportSchemeToFile(file); + } + + /** + * Export (dump) database data to file + * + * @param file + */ public void exportDataToFile(File file) { - runCmd(getConfig(), runtimeConfig, PgDump, "", new HashSet<>(singletonList("export from " + getConfig().storage().dbName() + " failed")), - "-U", getConfig().credentials().username(), - "-d", getConfig().storage().dbName(), - "-h", getConfig().net().host(), - "-p", String.valueOf(getConfig().net().port()), - "-f", file.getAbsolutePath(), - "-a" - ); + exportToFile(file, "-a"); } public boolean isProcessReady() { diff --git a/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresStarter.java b/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresStarter.java index d0354fb..88df666 100644 --- a/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresStarter.java +++ b/src/main/java/ru/yandex/qatools/embed/postgresql/PostgresStarter.java @@ -52,7 +52,7 @@ public static IRuntimeConfig runtimeConfig(Command cmd) { public static , P extends AbstractPGProcess> PostgresStarter getCommand(Command command, IRuntimeConfig config) { - return new PostgresStarter<>(command.executableClass(), config); + return new PostgresStarter(command.executableClass(), config); } public static , P extends AbstractPGProcess> diff --git a/src/test/java/ru/yandex/qatools/embed/postgresql/TestPsqlDumpEndToEnd.java b/src/test/java/ru/yandex/qatools/embed/postgresql/TestPsqlDumpEndToEnd.java new file mode 100644 index 0000000..4f07cf7 --- /dev/null +++ b/src/test/java/ru/yandex/qatools/embed/postgresql/TestPsqlDumpEndToEnd.java @@ -0,0 +1,76 @@ +package ru.yandex.qatools.embed.postgresql; + +import org.junit.Test; + +import java.io.File; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class TestPsqlDumpEndToEnd extends AbstractPsqlTest { + + @Test + public void testPsqlDumpEndToEnd() throws Exception { + + // Load dump + process.importFromFile(new File("src/test/resources/test.backup")); + assertThat(conn, not(nullValue())); + + assertSchemaAndData(); + + // Create binary dump + File fullExportDump = File.createTempFile("full_", ".dmp"); + process.exportToFile(fullExportDump, "-Fc"); + assertTrue(fullExportDump.exists()); + assertTrue(fullExportDump.length() > 0); + + // Create new connection + tearDown(); + setUp(); + + // Load binary dump into a fresh database + assertTrue(fullExportDump.exists()); + process.restoreFromFile(fullExportDump); + assertThat(conn, not(nullValue())); + + assertSchemaAndData(); + + } + + private void assertSchemaAndData() throws SQLException { + + String expected; + try (Statement statement = conn.createStatement(); + ResultSet res = statement.executeQuery("SELECT * FROM table1;")) { + assertThat(res, not(nullValue())); + String tableString = readTable(res); + + assertThat("Missing content in relation 'table1' in dump file!", tableString, not(nullValue())); + + expected = "test\t1\ta\n" + "test\t2\tb\n" + "test\t3\tc\n" + "test\t4\td\n"; + assertEquals(expected, tableString); + } + } + + private String readTable(ResultSet res) throws SQLException { + StringBuilder sb = null; + while (res.next()) { + if (null == sb) + sb = new StringBuilder(); + sb.append(res.getString("col1")); + sb.append("\t"); + sb.append(res.getInt("col2")); + sb.append("\t"); + sb.append(res.getString("col3")); + sb.append("\n"); + } + return null != sb ? sb.toString() : null; + } +} \ No newline at end of file