From e189686b4a55267a1a0b369ef0f170576b2a0eb7 Mon Sep 17 00:00:00 2001 From: naXa! Date: Sun, 17 Mar 2019 22:24:10 +0300 Subject: [PATCH] [BAEL-2749] create new module `dbunit`; add DbUnit tests: `OldSchoolDbUnitTest` + `PrepAndExpectedDbUnitTest` + relevant data --- dbunit/README.md | 6 + dbunit/docs/db_schema.png | Bin 0 -> 17099 bytes dbunit/pom.xml | 35 ++++++ .../main/java/com/baeldung/dbunit/.gitkeep | 0 dbunit/src/main/resources/logback.xml | 13 ++ .../baeldung/dbunit/OldSchoolDbUnitTest.java | 111 ++++++++++++++++++ .../dbunit/PrepAndExpectedDbUnitTest.java | 103 ++++++++++++++++ dbunit/src/test/resources/data.xml | 11 ++ dbunit/src/test/resources/items.xml | 9 ++ .../src/test/resources/items_exp_delete.xml | 8 ++ .../src/test/resources/items_exp_rename.xml | 9 ++ dbunit/src/test/resources/schema.sql | 28 +++++ dbunit/src/test/resources/users.xml | 7 ++ .../src/test/resources/users_exp_delete.xml | 6 + .../src/test/resources/users_exp_rename.xml | 7 ++ 15 files changed, 353 insertions(+) create mode 100644 dbunit/README.md create mode 100644 dbunit/docs/db_schema.png create mode 100644 dbunit/pom.xml create mode 100644 dbunit/src/main/java/com/baeldung/dbunit/.gitkeep create mode 100644 dbunit/src/main/resources/logback.xml create mode 100644 dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java create mode 100644 dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java create mode 100644 dbunit/src/test/resources/data.xml create mode 100644 dbunit/src/test/resources/items.xml create mode 100644 dbunit/src/test/resources/items_exp_delete.xml create mode 100644 dbunit/src/test/resources/items_exp_rename.xml create mode 100644 dbunit/src/test/resources/schema.sql create mode 100644 dbunit/src/test/resources/users.xml create mode 100644 dbunit/src/test/resources/users_exp_delete.xml create mode 100644 dbunit/src/test/resources/users_exp_rename.xml diff --git a/dbunit/README.md b/dbunit/README.md new file mode 100644 index 0000000000..383bd6fb7e --- /dev/null +++ b/dbunit/README.md @@ -0,0 +1,6 @@ +### Database schema + +![db schema](docs/db_schema.png) + +### Relevant Articles: +- [Introduction To DBUnit](https://www.baeldung.com/dbunit) diff --git a/dbunit/docs/db_schema.png b/dbunit/docs/db_schema.png new file mode 100644 index 0000000000000000000000000000000000000000..d9185bacd2c624c8e05aea87bf542cfdfd7f1604 GIT binary patch literal 17099 zcmch91VJ!}GI}RUv>+r(2*N1QqxWuf5_R<6d+(z6 z-!1Yy&%5`2fBT$$UFV!XTv)U2wZ3buZ~d&#JpnIY$Y5iTVIUzPVav%%svsfVi2;60 z(eDB+822AMMnVz~my;BG<)XV4zoAOL7CX9s?c%i38RO6$T#=SwZAPC=hhk8vhx^8Q z*#LRjxRBks5RbaoFYa^Wn-AE&AA=r-3LTBMOb=R@GsRgBsavVYrG>wGOr>)A<@u7s z^fs|#&Wtt9po;d?v-)=(LkBv$2R%Cz>lKM!Z?Tb(M9(jn_pok+y9+`_62*OwKtk$8 zo&_Tz9UxT#?LtWZr<(&I!)U8__hyKYxIU@CNRd7xgOCsIRBmSfPkxJaK+5s27#r~* zG6v{|b|Wo_IW3hMBw6xVuqc&L6v7t0DSG9~u4~{8J^CJ)nh02y2NI}y{pRx+^Gg$FS!w(9&E1EFBz$I6)Zflx43pyBK@pRkNHk|*mL@sdv$r;pt=FadpwQ?=zHNdLYUp` zF=E$=R4ZeDuOJz%!k(2ue$8WNeoEsp^^NH#@Xo(m(OzoI=t5# zzXlZrt-6`k>}PDay%Fq$Rk^rGDj)f3K@vy3E-Dw&-~b*&gh!uFTJcZhM}TWc%fgs1HHhc(^S$SoaX%_2&&^E=uy9?xVmmd3L1=gH@|d3U@_L+}J9#zeZdmZmu-5Pbw4##7NQ+(l99IQMFD~j3~ zbQ^50WpOG3nZE_(Y=$Hm&sz7~mjU`Kjq51&gG^89YIt!*& zd#WI1JErze&qk;QrQdye`bWJ35d(&*au%Wk z>GREqQy__f+kVP0D=Mr)x7kKs63{&`nqOAt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?ImydC{~Sn6|T6DkY0Mr-`ura?*70vCDTOa z4!b%lxE?p!>O3-|n`ZAJVq{v?S zDcy_Sy@3wO2WAhR~VEP8^(Kghuf#}(c^Wb z6kYOl?c~F-sY(QS*BsB>VDIE)r{dShNS&bCp2u(;6)ba;y;kupYJS+B6(||nD-7=n zQ&E2MRWDJ-{@w+h-VX?aYq=@ocwtQq1@~BQ!94&gNFr;mRLIw9DQoIItG~Y7QMSOm zA5wqSR}U#r{m=^y-*{>XOOB8Xk`0$^5i@yP*GL-9JWF*-whSECl9)$lN(ybe>OCZC zO8SLji%lUG$7K6sg!ObyyUt>!>e%4mD{bwd^!kfm@{588bQQuXsOH&=%ga|G&6Ygc zdBYY(pa^ZWt=>6Xh)X@zp*j2|if55bF?^O;*3&2qLEi&_Y24di2>QlS zrkTy90&fqbA1bT8I=|8>HNYi8#qU@Bz+cpZcQAOK#N%*CBa>jf!6(*!lF}KVjSh=3 zQw!;w2r;Y=Bu9wlBxC!<3p!@Ey>H|888U3p_;S&9QtNeL*=N_cmV88Z) + 4.0.0 + dbunit + 1.0 + dbunit + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.dbunit + dbunit + ${dbunit.version} + test + + + + com.h2database + h2 + 1.4.197 + test + + + + + + 2.6.0 + + + diff --git a/dbunit/src/main/java/com/baeldung/dbunit/.gitkeep b/dbunit/src/main/java/com/baeldung/dbunit/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/dbunit/src/main/resources/logback.xml b/dbunit/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/dbunit/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java b/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java new file mode 100644 index 0000000000..a703863614 --- /dev/null +++ b/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java @@ -0,0 +1,111 @@ +package com.baeldung.dbunit; + +import org.dbunit.Assertion; +import org.dbunit.IDatabaseTester; +import org.dbunit.JdbcDatabaseTester; +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.ITable; +import org.dbunit.dataset.xml.FlatXmlDataSet; +import org.dbunit.operation.DatabaseOperation; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.InputStream; +import java.sql.Connection; +import java.sql.ResultSet; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class OldSchoolDbUnitTest { + private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); + private static final String JDBC_URL = "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'"; + private static final String USER = "sa"; + private static final String PASSWORD = ""; + + private static IDatabaseTester tester = null; + + @BeforeClass + public static void setUp() throws Exception { + tester = initDatabaseTester(); + } + + private static IDatabaseTester initDatabaseTester() throws Exception { + final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD); + tester.setDataSet(initDataSet()); + tester.setSetUpOperation(DatabaseOperation.REFRESH); + tester.setTearDownOperation(DatabaseOperation.NONE); + return tester; + } + + private static IDataSet initDataSet() throws Exception { + final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("data.xml"); + return new FlatXmlDataSet(is); + } + + @Before + public void setup() throws Exception { + tester.onSetup(); + } + + @After + public void tearDown() throws Exception { + tester.onTearDown(); + } + + @Test + public void testSelect() throws Exception { + // Arrange + final Connection connection = tester.getConnection().getConnection(); + + // Act + final ResultSet rs = connection.createStatement().executeQuery("select * from iTEMS where id = 1"); + + // Assert + assertTrue(rs.next()); + assertEquals("Grey T-Shirt", rs.getString("title")); + } + + @Test + public void testDelete() throws Exception { + // Arrange + final Connection connection = tester.getConnection().getConnection(); + + final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_delete.xml"); + ITable expectedTable = (new FlatXmlDataSet(is)).getTable("items"); + //expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"produced"}); + + // Act + connection.createStatement().executeUpdate("delete from ITEMS where id = 2"); + + // Assert + final IDataSet databaseDataSet = tester.getConnection().createDataSet(); + ITable actualTable = databaseDataSet.getTable("items"); + //actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[]{"produced"}); + + Assertion.assertEquals(expectedTable, actualTable); + } + + @Test + public void testUpdate() throws Exception { + // Arrange + final Connection connection = tester.getConnection().getConnection(); + + final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml"); + ITable expectedTable = (new FlatXmlDataSet(is)).getTable("items"); + //expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"produced"}); + + // Act + connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); + + // Assert + final IDataSet databaseDataSet = tester.getConnection().createDataSet(); + ITable actualTable = databaseDataSet.getTable("items"); + //actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[]{"produced"}); + + Assertion.assertEquals(expectedTable, actualTable); + } + +} diff --git a/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java b/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java new file mode 100644 index 0000000000..c3882d5eab --- /dev/null +++ b/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java @@ -0,0 +1,103 @@ +package com.baeldung.dbunit; + +import org.dbunit.*; +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; +import org.dbunit.operation.DatabaseOperation; +import org.dbunit.util.fileloader.DataFileLoader; +import org.dbunit.util.fileloader.FlatXmlDataFileLoader; +import org.junit.Test; + +import java.io.InputStream; +import java.sql.Connection; +import java.sql.ResultSet; + +public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { + private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); + private static final String JDBC_URL = "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'"; + private static final String USER = "sa"; + private static final String PASSWORD = ""; + + @Override + public void setUp() throws Exception { + setDatabaseTester(initDatabaseTester()); + setDataFileLoader(initDataFileLoader()); + super.setUp(); + } + + private IDatabaseTester initDatabaseTester() throws Exception { + final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD); + tester.setDataSet(initDataSet()); + tester.setSetUpOperation(DatabaseOperation.REFRESH); + return tester; + } + + private IDataSet initDataSet() throws Exception { + final InputStream is = getClass().getClassLoader().getResourceAsStream("data.xml"); + return new FlatXmlDataSetBuilder().build(is); + } + + private DataFileLoader initDataFileLoader() { + return new FlatXmlDataFileLoader(); + } + + @Test + public void testSelect() throws Exception { + // Arrange + final Connection connection = getConnection().getConnection(); + final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; + final String[] prepDataFiles = {"/users.xml"}; + final String[] expectedDataFiles = {"/users.xml"}; + final PrepAndExpectedTestCaseSteps testSteps = () -> { + // invoke the method being tested here; for the sake of simplicity we use JDBC API directly in this example + final ResultSet rs = connection.createStatement().executeQuery("select * from USERS where id = 1"); + + // either place assertions here + //assertTrue(rs.next()); + //assertEquals("Xavier", rs.getString("last_name")); + + return rs; + }; + + // Act + final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); + + // or place assertions at the end + assertTrue(rs.next()); + assertEquals("Xavier", rs.getString("last_name")); + } + + @Test + public void testUpdate() throws Exception { + // Arrange + final Connection connection = getConnection().getConnection(); + final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; // define tables to verify + final String[] prepDataFiles = {"/users.xml"}; // define prep files + final String[] expectedDataFiles = {"/users_exp_rename.xml"}; // define expected files + final PrepAndExpectedTestCaseSteps testSteps = () -> { + // invoke the method being tested here; for the sake of simplicity we use JDBC API directly in this example + return connection.createStatement().executeUpdate("update USERS set first_name = 'new name' where id = 1"); + // after this method exits, dbUnit will: + // * verify configured tables + // * cleanup tables as configured + }; + + // Act + super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); + } + + @Test + public void testDelete() throws Exception { + // Arrange + final Connection connection = getConnection().getConnection(); + final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; + final String[] prepDataFiles = {"/users.xml"}; + final String[] expectedDataFiles = {"/users_exp_delete.xml"}; + final PrepAndExpectedTestCaseSteps testSteps = + () -> connection.createStatement().executeUpdate("delete from USERS where id = 2"); + + // Act + super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); + } + +} diff --git a/dbunit/src/test/resources/data.xml b/dbunit/src/test/resources/data.xml new file mode 100644 index 0000000000..e4498513e0 --- /dev/null +++ b/dbunit/src/test/resources/data.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/dbunit/src/test/resources/items.xml b/dbunit/src/test/resources/items.xml new file mode 100644 index 0000000000..04a975d7ee --- /dev/null +++ b/dbunit/src/test/resources/items.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/dbunit/src/test/resources/items_exp_delete.xml b/dbunit/src/test/resources/items_exp_delete.xml new file mode 100644 index 0000000000..1aaeff59ca --- /dev/null +++ b/dbunit/src/test/resources/items_exp_delete.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/dbunit/src/test/resources/items_exp_rename.xml b/dbunit/src/test/resources/items_exp_rename.xml new file mode 100644 index 0000000000..237280e758 --- /dev/null +++ b/dbunit/src/test/resources/items_exp_rename.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/dbunit/src/test/resources/schema.sql b/dbunit/src/test/resources/schema.sql new file mode 100644 index 0000000000..9b5018985a --- /dev/null +++ b/dbunit/src/test/resources/schema.sql @@ -0,0 +1,28 @@ +CREATE TABLE IF NOT EXISTS USERS +( + `id` int AUTO_INCREMENT NOT NULL, + `first_name` varchar(100) NOT NULL, + `last_name` varchar(100) NOT NULL, + PRIMARY KEY (`id`) +); + +CREATE TABLE IF NOT EXISTS ITEMS +( + `id` int AUTO_INCREMENT NOT NULL, + `title` varchar(100) NOT NULL, + `produced` date, + `price` float, + PRIMARY KEY (`id`) +); + +CREATE TABLE IF NOT EXISTS PURCHASES +( + `id` int NOT NULL AUTO_INCREMENT, + `id_user` int NOT NULL, + `id_item` int NOT NULL, + `total_price` float NOT NULL, + `quantity` int(11) NOT NULL, + PRIMARY KEY (`id`), + FOREIGN KEY (`id_user`) REFERENCES USERS (`id`) ON DELETE CASCADE, + FOREIGN KEY (`id_item`) REFERENCES ITEMS (`id`) ON DELETE CASCADE ON UPDATE CASCADE +); diff --git a/dbunit/src/test/resources/users.xml b/dbunit/src/test/resources/users.xml new file mode 100644 index 0000000000..cebadf2e67 --- /dev/null +++ b/dbunit/src/test/resources/users.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/dbunit/src/test/resources/users_exp_delete.xml b/dbunit/src/test/resources/users_exp_delete.xml new file mode 100644 index 0000000000..8b72ceef89 --- /dev/null +++ b/dbunit/src/test/resources/users_exp_delete.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/dbunit/src/test/resources/users_exp_rename.xml b/dbunit/src/test/resources/users_exp_rename.xml new file mode 100644 index 0000000000..9bc1254967 --- /dev/null +++ b/dbunit/src/test/resources/users_exp_rename.xml @@ -0,0 +1,7 @@ + + + + + + +