[#1425] Add integration tests to jooq-codegen-maven-example using Spring
and DataSources
This commit is contained in:
parent
7261e582df
commit
0cbb4d9cc5
@ -5,6 +5,7 @@
|
||||
<classpathentry kind="src" path="target/generated-sources/jooq-ase"/>
|
||||
<classpathentry kind="src" path="target/generated-sources/jooq-mysql"/>
|
||||
<classpathentry kind="src" path="target/generated-sources/jooq-postgres"/>
|
||||
<classpathentry kind="src" path="target/generated-sources/jooq-hsqldb"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
|
||||
|
||||
@ -45,9 +45,50 @@
|
||||
<name>Sander Plas</name>
|
||||
<email>sander.plas@gmail.com</email>
|
||||
</developer>
|
||||
<developer>
|
||||
<name>Sergey Epik</name>
|
||||
<email>sergey.epik@gmail.com</email>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>3.0.6.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>3.0.6.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>3.0.6.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>3.0.6.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.16</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib</artifactId>
|
||||
<version>2.2.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq</artifactId>
|
||||
@ -62,6 +103,14 @@
|
||||
<type>jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>2.2.6</version>
|
||||
<type>jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
@ -108,6 +157,38 @@
|
||||
<artifactId>jooq-codegen-maven</artifactId>
|
||||
<version>2.4.0-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>exec0</id>
|
||||
<goals>
|
||||
<goal>generate</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<jdbc>
|
||||
<driver>org.hsqldb.jdbcDriver</driver>
|
||||
<url>jdbc:hsqldb:hsql://localhost</url>
|
||||
<user>sa</user>
|
||||
<password/>
|
||||
</jdbc>
|
||||
<generator>
|
||||
<name>org.jooq.util.DefaultGenerator</name>
|
||||
<database>
|
||||
<name>org.jooq.util.hsqldb.HSQLDBDatabase</name>
|
||||
<includes>.*</includes>
|
||||
<excludes/>
|
||||
<inputSchema>PUBLIC</inputSchema>
|
||||
</database>
|
||||
<generate>
|
||||
<relations>true</relations>
|
||||
<deprecated>false</deprecated>
|
||||
</generate>
|
||||
<target>
|
||||
<packageName>org.jooq.util.maven.example.hsqldb</packageName>
|
||||
<directory>target/generated-sources/jooq-hsqldb</directory>
|
||||
</target>
|
||||
</generator>
|
||||
</configuration>
|
||||
</execution>
|
||||
|
||||
<execution>
|
||||
<id>exec1</id>
|
||||
<goals>
|
||||
@ -284,6 +365,11 @@
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>2.2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.jtds</groupId>
|
||||
<artifactId>jtds</artifactId>
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
package org.jooq.test.util.spring;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.jooq.test.util.spring.domain.Author;
|
||||
|
||||
public interface AuthorDao {
|
||||
|
||||
Integer add(Author author);
|
||||
|
||||
void addBatch(List<Author> authors);
|
||||
|
||||
void save(Author author);
|
||||
|
||||
void delete(Author author);
|
||||
|
||||
Author findById(Integer id);
|
||||
|
||||
List<Author> findAll();
|
||||
|
||||
long countAuthors();
|
||||
|
||||
long countDistinctForLastName(String name);
|
||||
|
||||
boolean authorExists(Integer id);
|
||||
|
||||
boolean authorExists(Author c);
|
||||
|
||||
}
|
||||
@ -0,0 +1,104 @@
|
||||
package org.jooq.test.util.spring;
|
||||
|
||||
import static org.jooq.impl.Factory.param;
|
||||
import static org.jooq.util.maven.example.hsqldb.Sequences.S_AUTHOR_ID;
|
||||
import static org.jooq.util.maven.example.hsqldb.Tables.T_AUTHOR;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.jooq.BatchBindStep;
|
||||
import org.jooq.FactoryOperations;
|
||||
import org.jooq.impl.Factory;
|
||||
import org.jooq.test.util.spring.domain.Author;
|
||||
import org.jooq.util.maven.example.hsqldb.tables.records.TAuthorRecord;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
@Component
|
||||
public class AuthorDaoImpl implements AuthorDao {
|
||||
|
||||
private FactoryOperations factory;
|
||||
|
||||
@Autowired
|
||||
public void setFactoryOperations(FactoryOperations factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Author findById(Integer id) {
|
||||
TAuthorRecord authorRecord = factory.selectFrom(T_AUTHOR)
|
||||
.where(T_AUTHOR.ID.equal(id)).fetchOne();
|
||||
return authorRecord == null ? null : authorRecord.into(Author.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Author> findAll() {
|
||||
return factory.selectFrom(T_AUTHOR).fetchInto(Author.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer add(Author author) {
|
||||
Integer id = factory.nextval(S_AUTHOR_ID);
|
||||
factory.insertInto(T_AUTHOR)
|
||||
.set(T_AUTHOR.ID, id)
|
||||
.set(T_AUTHOR.FIRST_NAME, author.getFirstName())
|
||||
.set(T_AUTHOR.LAST_NAME, author.getLastName())
|
||||
.execute();
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addBatch(List<Author> authors) {
|
||||
BatchBindStep step = factory.batch(
|
||||
factory.insertInto(T_AUTHOR, T_AUTHOR.ID, T_AUTHOR.FIRST_NAME, T_AUTHOR.LAST_NAME)
|
||||
.values(param("id"), param("first"), param("last")));
|
||||
for (Author author : authors) {
|
||||
Integer id = factory.nextval(S_AUTHOR_ID).intValue();
|
||||
step = step.bind(id, author.getFirstName(), author.getLastName());
|
||||
}
|
||||
step.execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Author author) {
|
||||
factory.update(T_AUTHOR)
|
||||
.set(T_AUTHOR.FIRST_NAME, author.getFirstName())
|
||||
.set(T_AUTHOR.LAST_NAME, author.getLastName())
|
||||
.where(T_AUTHOR.ID.equal(author.getId()))
|
||||
.execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Author author) {
|
||||
factory.delete(T_AUTHOR)
|
||||
.where(T_AUTHOR.ID.equal(author.getId()))
|
||||
.execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long countAuthors() {
|
||||
return factory.selectCount().from(T_AUTHOR)
|
||||
.fetchOne().getValue(0, Long.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long countDistinctForLastName(String name) {
|
||||
return factory.select(Factory.countDistinct(T_AUTHOR.LAST_NAME)).from(T_AUTHOR)
|
||||
.where(T_AUTHOR.LAST_NAME.like(name + "%"))
|
||||
.fetchOne().getValue(0, Long.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean authorExists(Integer id) {
|
||||
return factory.selectFrom(T_AUTHOR).where(T_AUTHOR.ID.equal(id)).fetchOne() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean authorExists(Author author) {
|
||||
return factory.selectFrom(T_AUTHOR).where(T_AUTHOR.ID.equal(author.getId()))
|
||||
.fetchOne() != null;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,134 @@
|
||||
package org.jooq.test.util.spring;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.jooq.test.util.spring.domain.Author;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@ContextConfiguration(locations = "classpath:spring-context.xml")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class AuthorDaoTest {
|
||||
|
||||
@Autowired
|
||||
private AuthorDao authorDao;
|
||||
|
||||
@Test
|
||||
public void testFindAll() {
|
||||
List<Author> authors = authorDao.findAll();
|
||||
Assert.assertEquals(2, authors.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindById() {
|
||||
Author a = authorDao.findById(1);
|
||||
Assert.assertEquals("George", a.getFirstName());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testAdd() {
|
||||
Author a = new Author();
|
||||
a.setFirstName("Lewis");
|
||||
a.setLastName("Carroll");
|
||||
authorDao.add(a);
|
||||
List<Author> authors = authorDao.findAll();
|
||||
Assert.assertEquals(3, authors.size());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testAddBatch() {
|
||||
List<Author> authors = new ArrayList<Author>();
|
||||
Author a1 = new Author();
|
||||
a1.setFirstName("Lewis");
|
||||
a1.setLastName("Carroll");
|
||||
authors.add(a1);
|
||||
Author a2 = new Author();
|
||||
a2.setFirstName("Agatha");
|
||||
a2.setLastName("Christie");
|
||||
authors.add(a2);
|
||||
Author a3 = new Author();
|
||||
a3.setFirstName("Charles");
|
||||
a3.setLastName("Dickens");
|
||||
authors.add(a3);
|
||||
Author a4 = new Author();
|
||||
a4.setFirstName("Mark");
|
||||
a4.setLastName("Twain");
|
||||
authors.add(a4);
|
||||
authorDao.addBatch(authors);
|
||||
List<Author> results = authorDao.findAll();
|
||||
Assert.assertEquals(6, results.size());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testSaveAuthor() {
|
||||
Author author = authorDao.findById(1);
|
||||
author.setFirstName("Arthur Conan");
|
||||
author.setLastName("Doyle");
|
||||
authorDao.save(author);
|
||||
Author found = authorDao.findById(1);
|
||||
Assert.assertEquals("Arthur Conan", found.getFirstName());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testDeleteAuthor() {
|
||||
Author c = authorDao.findById(1);
|
||||
authorDao.delete(c);
|
||||
List<Author> authors = authorDao.findAll();
|
||||
Assert.assertEquals(1, authors.size());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testCountAuthor() {
|
||||
Author a = new Author();
|
||||
a.setFirstName("Lewis");
|
||||
a.setLastName("Carroll");
|
||||
authorDao.add(a);
|
||||
long count = authorDao.countAuthors();
|
||||
List<Author> authors = authorDao.findAll();
|
||||
for (Author author : authors) {
|
||||
System.out.println(author.getFirstName());
|
||||
}
|
||||
Assert.assertEquals(3, count);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testCountDistinctAuthor() {
|
||||
Author a1 = new Author();
|
||||
a1.setFirstName("Calvin");
|
||||
a1.setLastName("Coolidge");
|
||||
authorDao.add(a1);
|
||||
Author a2 = new Author();
|
||||
a2.setFirstName("James Fenimore");
|
||||
a2.setLastName("Cooper");
|
||||
authorDao.add(a2);
|
||||
long count = authorDao.countDistinctForLastName("Coo");
|
||||
Assert.assertEquals(2, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthorExists() {
|
||||
Author c = authorDao.findById(1);
|
||||
boolean exists = authorDao.authorExists(c);
|
||||
Assert.assertTrue(exists);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthorNotExists() {
|
||||
boolean exists = authorDao.authorExists(1000);
|
||||
Assert.assertFalse(exists);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package org.jooq.test.util.spring.domain;
|
||||
|
||||
public class Author {
|
||||
|
||||
private Integer id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
15
jOOQ-codegen-maven-example/src/test/resources/db-init.sql
Normal file
15
jOOQ-codegen-maven-example/src/test/resources/db-init.sql
Normal file
@ -0,0 +1,15 @@
|
||||
CREATE TABLE t_author (
|
||||
ID INT,
|
||||
FIRST_NAME VARCHAR(50),
|
||||
LAST_NAME VARCHAR(50) NOT NULL,
|
||||
DATE_OF_BIRTH DATE,
|
||||
YEAR_OF_BIRTH INT,
|
||||
ADDRESS VARCHAR(50),
|
||||
|
||||
CONSTRAINT pk_t_author PRIMARY KEY (ID)
|
||||
);
|
||||
|
||||
CREATE SEQUENCE s_author_id START WITH 1;
|
||||
|
||||
INSERT INTO t_author (ID, FIRST_NAME, LAST_NAME) VALUES (next value for s_author_id, 'George', 'Orwell');
|
||||
INSERT INTO t_author (ID, FIRST_NAME, LAST_NAME) VALUES (next value for s_author_id, 'Paulo', 'Coelho');
|
||||
41
jOOQ-codegen-maven-example/src/test/resources/log4j.xml
Normal file
41
jOOQ-codegen-maven-example/src/test/resources/log4j.xml
Normal file
@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<appender name="console" class="org.apache.log4j.ConsoleAppender">
|
||||
<param name="Target" value="System.out"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%-5p: %c - %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework.core">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.beans">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.context">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.web">
|
||||
<level value="info"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.springframework.jdbc.datasource.DataSourceUtils">
|
||||
<level value="debug"/>
|
||||
</logger>
|
||||
|
||||
<logger name="org.jooq.impl">
|
||||
<level value="debug"/>
|
||||
</logger>
|
||||
|
||||
<root>
|
||||
<priority value="info"/>
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
||||
@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<tx:annotation-driven transaction-manager="transactionManager"/>
|
||||
|
||||
<jdbc:embedded-database id="dataSource" type="HSQL">
|
||||
<jdbc:script location="classpath:db-init.sql"/>
|
||||
</jdbc:embedded-database>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionAwareDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
|
||||
<constructor-arg ref="dataSource"/>
|
||||
</bean>
|
||||
|
||||
<bean id="factoryProxy" class="org.jooq.impl.Factory">
|
||||
<constructor-arg name="datasource" ref="transactionAwareDataSource"/>
|
||||
<constructor-arg name="dialect">
|
||||
<value type="org.jooq.SQLDialect">HSQLDB</value>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<context:component-scan base-package="org.jooq.test.util.spring"/>
|
||||
|
||||
</beans>
|
||||
Loading…
Reference in New Issue
Block a user