Skip to content

Commit 4a3896e

Browse files
[test] Add tests for MySQL and PostgreSQL (darrachequesne#32)
1 parent cc1e0f2 commit 4a3896e

File tree

9 files changed

+90
-20
lines changed

9 files changed

+90
-20
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@ jdk:
55
- oraclejdk7
66
- openjdk7
77
- openjdk6
8+
services:
9+
- mysql
10+
- pgsql
11+
matrix:
12+
include:
13+
- jdk: oraclejdk8
14+
env: DB=mysql
15+
- jdk: oraclejdk8
16+
env: DB=pgsql
17+
18+
script: mvn test -Dspring.profiles.active=$DB
19+
20+
before_script:
21+
- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE test;' -U postgres; fi"
22+
- sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test;' -uroot; fi"

pom.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
<javax.el.api.version>2.2.5</javax.el.api.version>
7171
<slf4j.version>1.7.10</slf4j.version>
7272
<querydsl.version>3.7.1</querydsl.version>
73+
<h2.version>1.3.166</h2.version>
74+
<mysql.version>5.1.39</mysql.version>
75+
<postgresql.version>9.4.1209.jre7</postgresql.version>
7376
</properties>
7477

7578
<dependencies>
@@ -180,7 +183,21 @@
180183
<dependency>
181184
<groupId>com.h2database</groupId>
182185
<artifactId>h2</artifactId>
183-
<version>1.3.166</version>
186+
<version>${h2.version}</version>
187+
<scope>test</scope>
188+
</dependency>
189+
190+
<dependency>
191+
<groupId>mysql</groupId>
192+
<artifactId>mysql-connector-java</artifactId>
193+
<version>${mysql.version}</version>
194+
<scope>test</scope>
195+
</dependency>
196+
197+
<dependency>
198+
<groupId>org.postgresql</groupId>
199+
<artifactId>postgresql</artifactId>
200+
<version>${postgresql.version}</version>
184201
<scope>test</scope>
185202
</dependency>
186203

src/main/java/org/springframework/data/jpa/datatables/repository/SpecificationFactory.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public DataTablesSpecification(DataTablesInput input) {
4141
@Override
4242
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
4343
Predicate predicate = cb.conjunction();
44+
Expression<Boolean> booleanExpression;
45+
Expression<String> stringExpression;
4446

4547
// check for each searchable column whether a filter value exists
4648
for (ColumnParameter column : input.getColumns()) {
@@ -49,7 +51,6 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
4951
if (!isColumnSearchable) {
5052
continue;
5153
}
52-
Expression<String> expression = getExpression(root, column.getData());
5354

5455
if (filterValue.contains(OR_SEPARATOR)) {
5556
// the filter contains multiple values, add a 'WHERE .. IN' clause
@@ -59,18 +60,22 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
5960
for (int i = 0; i < values.length; i++) {
6061
booleanValues[i] = Boolean.valueOf(values[i]);
6162
}
62-
predicate = cb.and(predicate, expression.as(Boolean.class).in(booleanValues));
63+
booleanExpression = getExpression(root, column.getData(), Boolean.class);
64+
predicate = cb.and(predicate, booleanExpression.in(booleanValues));
6365
} else {
64-
predicate = cb.and(predicate, expression.in(Arrays.asList(values)));
66+
stringExpression = getExpression(root, column.getData(), String.class);
67+
predicate = cb.and(predicate, stringExpression.in(Arrays.asList(values)));
6568
}
6669
} else {
6770
// the filter contains only one value, add a 'WHERE .. LIKE' clause
6871
if (isBoolean(filterValue)) {
69-
predicate = cb.and(predicate,
70-
cb.equal(expression.as(Boolean.class), Boolean.valueOf(filterValue)));
72+
booleanExpression = getExpression(root, column.getData(), Boolean.class);
73+
predicate =
74+
cb.and(predicate, cb.equal(booleanExpression, Boolean.valueOf(filterValue)));
7175
} else {
76+
stringExpression = getExpression(root, column.getData(), String.class);
7277
predicate = cb.and(predicate,
73-
cb.like(cb.lower(expression), getLikeFilterValue(filterValue), ESCAPE_CHAR));
78+
cb.like(cb.lower(stringExpression), getLikeFilterValue(filterValue), ESCAPE_CHAR));
7479
}
7580
}
7681
}
@@ -82,7 +87,7 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
8287
// add a 'WHERE .. LIKE' clause on each searchable column
8388
for (ColumnParameter column : input.getColumns()) {
8489
if (column.getSearchable()) {
85-
Expression<String> expression = getExpression(root, column.getData());
90+
Expression<String> expression = getExpression(root, column.getData(), String.class);
8691

8792
matchOneColumnPredicate = cb.or(matchOneColumnPredicate,
8893
cb.like(cb.lower(expression), getLikeFilterValue(globalFilterValue), ESCAPE_CHAR));
@@ -119,23 +124,23 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
119124
}
120125
}
121126

122-
private static Expression<String> getExpression(Root<?> root, String columnData) {
127+
private static <S> Expression<S> getExpression(Root<?> root, String columnData, Class<S> clazz) {
123128
if (!columnData.contains(ATTRIBUTE_SEPARATOR)) {
124129
// columnData is like "attribute" so nothing particular to do
125-
return root.get(columnData).as(String.class);
130+
return root.get(columnData).as(clazz);
126131
}
127132
// columnData is like "joinedEntity.attribute" so add a join clause
128133
String[] values = columnData.split(ESCAPED_ATTRIBUTE_SEPARATOR);
129134
if (root.getModel().getAttribute(values[0])
130135
.getPersistentAttributeType() == PersistentAttributeType.EMBEDDED) {
131136
// with @Embedded attribute
132-
return root.get(values[0]).get(values[1]).as(String.class);
137+
return root.get(values[0]).get(values[1]).as(clazz);
133138
}
134139
From<?, ?> from = root;
135140
for (int i = 0; i < values.length - 1; i++) {
136141
from = from.join(values[i], JoinType.LEFT);
137142
}
138-
return from.get(values[values.length - 1]).as(String.class);
143+
return from.get(values[values.length - 1]).as(clazz);
139144
}
140145

141146
}

src/test/java/org/springframework/data/jpa/datatables/Config.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
import org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor;
1212
import org.springframework.context.annotation.Bean;
1313
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.context.annotation.Profile;
1415
import org.springframework.data.jpa.datatables.repository.DataTablesRepositoryFactoryBean;
1516
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
17+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
1618
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
1719
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
1820
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
1921
import org.springframework.orm.jpa.JpaTransactionManager;
2022
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
21-
import org.springframework.orm.jpa.vendor.Database;
2223
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
2324
import org.springframework.transaction.PlatformTransactionManager;
2425

@@ -32,26 +33,49 @@
3233
public class Config {
3334

3435
@Bean
35-
public DataSource dataSource() throws SQLException {
36+
@Profile({"default", "h2"})
37+
public DataSource dataSource_H2() throws SQLException {
3638
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
3739
}
3840

41+
@Bean
42+
@Profile("mysql")
43+
public DataSource dataSource_MySQL() throws SQLException {
44+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
45+
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
46+
dataSource.setUrl("jdbc:mysql://127.0.0.1/test");
47+
dataSource.setUsername("root");
48+
dataSource.setPassword("");
49+
return dataSource;
50+
}
51+
52+
@Bean
53+
@Profile("pgsql")
54+
public DataSource dataSource_PostgreSQL() throws SQLException {
55+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
56+
dataSource.setDriverClassName("org.postgresql.Driver");
57+
dataSource.setUrl("jdbc:postgresql://127.0.0.1/test");
58+
dataSource.setUsername("postgres");
59+
dataSource.setPassword("");
60+
return dataSource;
61+
}
62+
3963
@Bean
4064
public PlatformTransactionManager transactionManager() throws SQLException {
4165
return new JpaTransactionManager();
4266
}
4367

4468
@Bean
45-
public AbstractEntityManagerFactoryBean entityManagerFactory() throws SQLException {
69+
public AbstractEntityManagerFactoryBean entityManagerFactory(DataSource dataSource)
70+
throws SQLException {
4671

4772
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
48-
jpaVendorAdapter.setDatabase(Database.H2);
4973
jpaVendorAdapter.setGenerateDdl(true);
5074

5175
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
5276
bean.setJpaVendorAdapter(jpaVendorAdapter);
5377
bean.setPackagesToScan(Config.class.getPackage().getName());
54-
bean.setDataSource(dataSource());
78+
bean.setDataSource(dataSource);
5579

5680
Properties jpaProperties = new Properties();
5781
jpaProperties.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
@@ -64,8 +88,9 @@ public AbstractEntityManagerFactoryBean entityManagerFactory() throws SQLExcepti
6488
}
6589

6690
@Bean
67-
public SessionFactory sessionFactory() throws SQLException {
68-
return ((HibernateEntityManagerFactory) entityManagerFactory().getObject()).getSessionFactory();
91+
public SessionFactory sessionFactory(AbstractEntityManagerFactoryBean entityManagerFactory)
92+
throws SQLException {
93+
return ((HibernateEntityManagerFactory) entityManagerFactory.getObject()).getSessionFactory();
6994
}
7095

7196
}

src/test/java/org/springframework/data/jpa/datatables/model/Bill.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import javax.persistence.Entity;
44
import javax.persistence.GeneratedValue;
55
import javax.persistence.Id;
6+
import javax.persistence.Table;
67

78
@Entity
9+
@Table(name = "bill")
810
public class Bill {
911

1012
@Id

src/test/java/org/springframework/data/jpa/datatables/model/Game.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import javax.persistence.Entity;
55
import javax.persistence.GeneratedValue;
66
import javax.persistence.Id;
7+
import javax.persistence.Table;
78

89
@Entity
10+
@Table(name = "game")
911
public class Game {
1012

1113
@Id

src/test/java/org/springframework/data/jpa/datatables/model/Home.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import javax.persistence.GeneratedValue;
77
import javax.persistence.Id;
88
import javax.persistence.OneToMany;
9+
import javax.persistence.Table;
910

1011
@Entity
12+
@Table(name = "home")
1113
public class Home {
1214

1315
@Id

src/test/java/org/springframework/data/jpa/datatables/model/User.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import javax.persistence.Id;
88
import javax.persistence.JoinColumn;
99
import javax.persistence.ManyToOne;
10+
import javax.persistence.Table;
1011

1112
@Entity
13+
@Table(name = "users")
1214
public class User {
1315

1416
@Id

src/test/resources/init.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ INSERT INTO home (id, town) VALUES
5252
(3, 'town2'),
5353
(4, 'town3');
5454

55-
INSERT INTO user (id, username, role, status, id_home, visible) VALUES
55+
INSERT INTO users (id, username, role, status, id_home, visible) VALUES
5656
(1, 'john0', 'ADMIN', 'ACTIVE', null, true),
5757
(2, 'john1', 'AUTHOR', 'BLOCKED', null, false),
5858
(3, 'john2', 'USER', 'ACTIVE', null, true),

0 commit comments

Comments
 (0)