Skip to content

Commit 48ae3d1

Browse files
[test] Add tests for querydsl implementation (darrachequesne#33)
Fixes darrachequesne#26
1 parent 4a3896e commit 48ae3d1

File tree

13 files changed

+639
-9
lines changed

13 files changed

+639
-9
lines changed

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@
212212

213213
<build>
214214
<plugins>
215+
<plugin>
216+
<groupId>com.mysema.maven</groupId>
217+
<artifactId>maven-apt-plugin</artifactId>
218+
<version>1.0</version>
219+
<executions>
220+
<execution>
221+
<goals>
222+
<goal>test-process</goal>
223+
</goals>
224+
<configuration>
225+
<outputDirectory>target/generated-sources/java</outputDirectory>
226+
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
227+
</configuration>
228+
</execution>
229+
</executions>
230+
</plugin>
215231
<plugin>
216232
<groupId>org.apache.maven.plugins</groupId>
217233
<artifactId>maven-compiler-plugin</artifactId>

src/main/java/org/springframework/data/jpa/datatables/qrepository/PredicateFactory.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.springframework.data.jpa.datatables.qrepository;
22

33
import static org.springframework.data.jpa.datatables.repository.DataTablesUtils.ESCAPED_OR_SEPARATOR;
4-
import static org.springframework.data.jpa.datatables.repository.DataTablesUtils.ESCAPE_CHAR;
54
import static org.springframework.data.jpa.datatables.repository.DataTablesUtils.OR_SEPARATOR;
6-
import static org.springframework.data.jpa.datatables.repository.DataTablesUtils.getLikeFilterValue;
75
import static org.springframework.data.jpa.datatables.repository.DataTablesUtils.isBoolean;
86

97
import java.util.ArrayList;
@@ -22,6 +20,8 @@
2220

2321
class PredicateFactory {
2422

23+
private final static char ESCAPE_CHAR = '~';
24+
2525
public static Predicate createPredicate(PathBuilder<?> entity, DataTablesInput input) {
2626
BooleanBuilder predicate = new BooleanBuilder();
2727

@@ -77,4 +77,9 @@ private static StringExpression getStringExpression(PathBuilder<?> entity, Strin
7777
return Expressions.stringOperation(Ops.STRING_CAST, entity.get(columnData));
7878
}
7979

80+
private static String getLikeFilterValue(String filterValue) {
81+
return "%" + filterValue.toLowerCase().replaceAll("~", "~~").replaceAll("%", "~%")
82+
.replaceAll("_", "~_") + "%";
83+
}
84+
8085
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
* @author Damien Arrachequesne
3030
*/
3131
@Configuration
32-
@EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class)
32+
@EnableJpaRepositories(repositoryFactoryBeanClass = DataTablesRepositoryFactoryBean.class,
33+
basePackages = "org.springframework.data.jpa.datatables.model")
3334
public class Config {
3435

3536
@Bean
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.springframework.data.jpa.datatables;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.data.jpa.datatables.qrepository.QDataTablesRepositoryFactoryBean;
5+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
6+
7+
@Configuration
8+
@EnableJpaRepositories(repositoryFactoryBeanClass = QDataTablesRepositoryFactoryBean.class,
9+
basePackages = "org.springframework.data.jpa.datatables.qrepository")
10+
public class QConfig {
11+
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.springframework.data.jpa.datatables.qrepository;
2+
3+
import org.springframework.data.jpa.datatables.model.Bill;
4+
5+
public interface QBillRepository extends QDataTablesRepository<Bill, Integer> {
6+
7+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.springframework.data.jpa.datatables.qrepository;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.jpa.datatables.Config;
11+
import org.springframework.data.jpa.datatables.QConfig;
12+
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
13+
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
14+
import org.springframework.data.jpa.datatables.model.Bill;
15+
import org.springframework.test.context.ContextConfiguration;
16+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17+
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@ContextConfiguration(classes = {Config.class, QConfig.class})
20+
public class QBillRepositoryTest {
21+
22+
@Autowired
23+
private QBillRepository billRepository;
24+
25+
@Test
26+
public void testWithoutFilter() {
27+
DataTablesInput input = getBasicInput();
28+
29+
DataTablesOutput<Bill> output = billRepository.findAll(input);
30+
assertNotNull(output);
31+
assertNull(output.getError());
32+
assertEquals(12, output.getRecordsFiltered());
33+
assertEquals(12, output.getRecordsTotal());
34+
}
35+
36+
@Test
37+
public void testBooleanFilter() {
38+
DataTablesInput input = getBasicInput();
39+
40+
input.getColumn("hasBeenPayed").setSearchValue("TRUE");
41+
DataTablesOutput<Bill> output = billRepository.findAll(input);
42+
assertNotNull(output);
43+
assertNull(output.getError());
44+
assertEquals(6, output.getRecordsFiltered());
45+
}
46+
47+
@Test
48+
public void testBooleanFilter2() {
49+
DataTablesInput input = getBasicInput();
50+
51+
input.getColumn("hasBeenPayed").setSearchValue("TRUE+FALSE");
52+
DataTablesOutput<Bill> output = billRepository.findAll(input);
53+
assertNotNull(output);
54+
assertNull(output.getError());
55+
assertEquals(12, output.getRecordsFiltered());
56+
}
57+
58+
@Test
59+
public void testEscapeCharacter() {
60+
DataTablesInput input = getBasicInput();
61+
62+
input.getColumn("description").setSearchValue("foo%");
63+
DataTablesOutput<Bill> output = billRepository.findAll(input);
64+
assertNotNull(output);
65+
assertEquals(1, output.getRecordsFiltered());
66+
67+
input.getColumn("description").setSearchValue("foo_");
68+
output = billRepository.findAll(input);
69+
assertNotNull(output);
70+
assertEquals(1, output.getRecordsFiltered());
71+
}
72+
73+
// @Test
74+
// public void testWithPreFiltering() {
75+
// DataTablesInput input = getBasicInput();
76+
//
77+
// DataTablesOutput<Bill> output =
78+
// billRepository.findAll(input, null, new PreFilteringSpecification<Bill>());
79+
// assertNotNull(output);
80+
// assertEquals(6, output.getRecordsFiltered());
81+
// assertEquals(6, output.getRecordsTotal());
82+
// }
83+
84+
/**
85+
*
86+
* @return basic input parameters
87+
*/
88+
private static DataTablesInput getBasicInput() {
89+
DataTablesInput input = new DataTablesInput();
90+
input.addColumn("id", true, true, "");
91+
input.addColumn("amount", true, true, "");
92+
input.addColumn("hasBeenPayed", true, true, "");
93+
input.addColumn("description", true, true, "");
94+
input.addOrder("id", true);
95+
return input;
96+
}
97+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.springframework.data.jpa.datatables.qrepository;
2+
3+
import org.springframework.data.jpa.datatables.model.Game;
4+
5+
public interface QGameRepository extends QDataTablesRepository<Game, Integer> {
6+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.springframework.data.jpa.datatables.qrepository;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.jpa.datatables.Config;
11+
import org.springframework.data.jpa.datatables.QConfig;
12+
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
13+
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
14+
import org.springframework.data.jpa.datatables.model.Game;
15+
import org.springframework.test.context.ContextConfiguration;
16+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17+
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@ContextConfiguration(classes = {Config.class, QConfig.class})
20+
public class QGameRepositoryTest {
21+
22+
@Autowired
23+
private QGameRepository gameRepository;
24+
25+
@Test
26+
public void testWithoutFilter() {
27+
DataTablesInput input = getBasicInput();
28+
29+
DataTablesOutput<Game> output = gameRepository.findAll(input);
30+
assertNotNull(output);
31+
assertNull(output.getError());
32+
assertEquals(12, output.getRecordsFiltered());
33+
assertEquals(12, output.getRecordsTotal());
34+
}
35+
36+
@Test
37+
public void testSearchOnEmbeddedProperty() {
38+
DataTablesInput input = getBasicInput();
39+
40+
input.getColumn("prize.name").setSearchValue("prize1");
41+
DataTablesOutput<Game> output = gameRepository.findAll(input);
42+
assertNotNull(output);
43+
assertNull(output.getError());
44+
assertEquals(3, output.getRecordsFiltered());
45+
}
46+
47+
@Test
48+
public void testOrderOnEmbeddedProperty() {
49+
DataTablesInput input = getBasicInput();
50+
input.getOrder().clear();
51+
input.addOrder("prize.name", false);
52+
53+
DataTablesOutput<Game> output = gameRepository.findAll(input);
54+
assertNotNull(output);
55+
assertNull(output.getError());
56+
assertEquals(12, output.getRecordsFiltered());
57+
assertEquals("prize9", output.getData().get(0).getPrize().getName());
58+
}
59+
60+
/**
61+
*
62+
* @return basic input parameters
63+
*/
64+
private static DataTablesInput getBasicInput() {
65+
DataTablesInput input = new DataTablesInput();
66+
input.addColumn("id", true, true, "");
67+
input.addColumn("prize.name", true, true, "");
68+
input.addOrder("id", true);
69+
return input;
70+
}
71+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.springframework.data.jpa.datatables.qrepository;
2+
3+
import org.springframework.data.jpa.datatables.model.Lesson;
4+
5+
public interface QLessonRepository extends QDataTablesRepository<Lesson, Long> {
6+
7+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.springframework.data.jpa.datatables.qrepository;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.hibernate.SessionFactory;
8+
import org.hibernate.stat.Statistics;
9+
import org.junit.Ignore;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.data.jpa.datatables.Config;
14+
import org.springframework.data.jpa.datatables.QConfig;
15+
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
16+
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
17+
import org.springframework.data.jpa.datatables.model.Lesson;
18+
import org.springframework.test.context.ContextConfiguration;
19+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@ContextConfiguration(classes = {Config.class, QConfig.class})
23+
public class QLessonRepositoryTest {
24+
25+
@Autowired
26+
private QLessonRepository lessonRepository;
27+
28+
@Autowired
29+
private SessionFactory sessionFactory;
30+
31+
@Test
32+
public void testThroughTwoManyToOneRelationships() {
33+
DataTablesInput input = getBasicInput();
34+
35+
input.getColumn("course.type.name").setSearchValue("CourseTypeA");
36+
DataTablesOutput<Lesson> output = lessonRepository.findAll(input);
37+
assertNotNull(output);
38+
assertNull(output.getError());
39+
assertEquals(5, output.getRecordsFiltered());
40+
assertEquals(7, output.getRecordsTotal());
41+
42+
input.getColumn("course.name").setSearchValue("CourseA-2");
43+
output = lessonRepository.findAll(input);
44+
assertNotNull(output);
45+
assertNull(output.getError());
46+
assertEquals(2, output.getRecordsFiltered());
47+
assertEquals(7, output.getRecordsTotal());
48+
}
49+
50+
@Test
51+
@Ignore
52+
public void testEagerLoading() {
53+
DataTablesInput input = getBasicInput();
54+
55+
Statistics statistics = sessionFactory.getStatistics();
56+
statistics.setStatisticsEnabled(true);
57+
DataTablesOutput<Lesson> output = lessonRepository.findAll(input);
58+
assertEquals("CourseTypeA", output.getData().get(0).getCourse().getType().getName());
59+
statistics.setStatisticsEnabled(false);
60+
61+
// there should be only three executed queries : count unfiltered, count filtered and actual
62+
// data (with FETCH JOIN)
63+
assertEquals(3, statistics.getPrepareStatementCount());
64+
assertEquals(7 + 3 + 2, statistics.getEntityLoadCount());
65+
}
66+
67+
/**
68+
*
69+
* @return basic input parameters
70+
*/
71+
private static DataTablesInput getBasicInput() {
72+
DataTablesInput input = new DataTablesInput();
73+
input.addColumn("id", true, true, "");
74+
input.addColumn("name", true, true, "");
75+
input.addColumn("course.name", true, true, "");
76+
input.addColumn("course.type.name", true, true, "");
77+
input.addOrder("id", true);
78+
return input;
79+
}
80+
}

0 commit comments

Comments
 (0)