diff --git a/persistence-modules/jooq/pom.xml b/persistence-modules/jooq/pom.xml index c6de8e3d40..d0f1b733a4 100644 --- a/persistence-modules/jooq/pom.xml +++ b/persistence-modules/jooq/pom.xml @@ -31,20 +31,34 @@ jooq-codegen ${jooq.version} + + + + commons-io + commons-io + 2.15.1 + test + + org.postgresql postgresql ${postgresql.version} + runtime com.h2database h2 ${h2.version} + runtime - 3.18.7 + 17 + 17 + 17 + 3.19.0 \ No newline at end of file diff --git a/persistence-modules/jooq/src/test/java/com/baeldung/jooq/CodeGenerationIntegrationTest.java b/persistence-modules/jooq/src/test/java/com/baeldung/jooq/CodeGenerationIntegrationTest.java new file mode 100644 index 0000000000..3ca0919eaa --- /dev/null +++ b/persistence-modules/jooq/src/test/java/com/baeldung/jooq/CodeGenerationIntegrationTest.java @@ -0,0 +1,75 @@ +package com.baeldung.jooq; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +import org.jooq.DSLContext; +import org.jooq.SQLDialect; +import org.jooq.codegen.GenerationTool; +import org.jooq.impl.DSL; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.jooq.model.tables.Article; +import com.baeldung.jooq.model.tables.Author; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.apache.commons.io.FileUtils; + +public class CodeGenerationIntegrationTest { + + static DSLContext context; + + @BeforeClass + public static void setup() throws SQLException { + Connection conn = DriverManager.getConnection("jdbc:h2:mem:tes;INIT=CREATE SCHEMA IF NOT EXISTS \"public\""); + context = DSL.using(conn, SQLDialect.H2); + + context.createTable(Author.AUTHOR) + .columns( + Author.AUTHOR.ID, + Author.AUTHOR.FIRST_NAME, + Author.AUTHOR.LAST_NAME, + Author.AUTHOR.AGE + ) + .execute(); + context.createTable(Article.ARTICLE) + .columns( + Article.ARTICLE.ID, + Article.ARTICLE.TITLE, + Article.ARTICLE.DESCRIPTION, + Article.ARTICLE.AUTHOR_ID + ) + .execute(); + } + + @AfterClass + public static void cleanup() throws IOException { + File generatedDirectory = new File("src/main/java/com/baeldung/jooq/generated"); + FileUtils.deleteDirectory(generatedDirectory); + } + + @Test + public void testClassGenerationFromExistingDatabase() throws Exception { + + File generatedDirectory = new File("src/main/java/com/baeldung/jooq/generated"); + + assertFalse(generatedDirectory.exists()); + + URL jooqConfigURL = getClass().getClassLoader().getResource("jooq-config.xml"); + assertNotNull(jooqConfigURL); + File file = new File(jooqConfigURL.getFile()); + GenerationTool.generate(Files.readString(file.toPath())); + + assertTrue(generatedDirectory.exists()); + } +} diff --git a/persistence-modules/jooq/src/test/java/com/baeldung/jooq/CrudLiveTest.java b/persistence-modules/jooq/src/test/java/com/baeldung/jooq/CrudIntegrationTest.java similarity index 99% rename from persistence-modules/jooq/src/test/java/com/baeldung/jooq/CrudLiveTest.java rename to persistence-modules/jooq/src/test/java/com/baeldung/jooq/CrudIntegrationTest.java index d41344c08e..0d8df27ccc 100644 --- a/persistence-modules/jooq/src/test/java/com/baeldung/jooq/CrudLiveTest.java +++ b/persistence-modules/jooq/src/test/java/com/baeldung/jooq/CrudIntegrationTest.java @@ -28,7 +28,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -public class CrudLiveTest { +public class CrudIntegrationTest { static DSLContext context; diff --git a/persistence-modules/jooq/src/test/resources/jooq-config.xml b/persistence-modules/jooq/src/test/resources/jooq-config.xml new file mode 100644 index 0000000000..2eed2fbe80 --- /dev/null +++ b/persistence-modules/jooq/src/test/resources/jooq-config.xml @@ -0,0 +1,26 @@ + + + + + org.h2.Driver + jdbc:h2:mem:tes + + + + + + org.jooq.codegen.JavaGenerator + + + org.jooq.meta.h2.H2Database + public + .* + + + + + com.baeldung.jooq.generated + src/main/java + + + \ No newline at end of file