JAVA-29283 | added missing code for article (#15459)

This commit is contained in:
Gaetano Piazzolla
2023-12-24 15:02:39 +01:00
committed by GitHub
parent 37bddcdfce
commit 750cc9b748
4 changed files with 117 additions and 2 deletions
@@ -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());
}
}
@@ -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;
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.13.0.xsd">
<jdbc>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:mem:tes</url>
<user></user>
<password></password>
</jdbc>
<generator>
<name>org.jooq.codegen.JavaGenerator</name>
<database>
<name>org.jooq.meta.h2.H2Database</name>
<inputSchema>public</inputSchema>
<includes>.*</includes>
<excludes></excludes>
</database>
<target>
<packageName>com.baeldung.jooq.generated</packageName>
<directory>src/main/java</directory>
</target>
</generator>
</configuration>