Skip to content

Commit d3dae0d

Browse files
Add some helpers and refactor tests
1 parent b51a61c commit d3dae0d

File tree

16 files changed

+233
-241
lines changed

16 files changed

+233
-241
lines changed

src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesInput.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class DataTablesInput {
6262
private List<ColumnParameter> columns;
6363

6464
public DataTablesInput() {
65+
this.draw = 1;
66+
this.start = 0;
67+
this.length = 10;
6568
this.search = new SearchParameter();
6669
this.order = new ArrayList<OrderParameter>();
6770
this.columns = new ArrayList<ColumnParameter>();
@@ -127,6 +130,56 @@ public Map<String, ColumnParameter> getColumnsAsMap() {
127130
return map;
128131
}
129132

133+
/**
134+
* Find a column by its name
135+
*
136+
* @param columnName the name of the column
137+
* @return the given Column, or <code>null</code> if not found
138+
*/
139+
public ColumnParameter getColumn(String columnName) {
140+
if (columnName == null) {
141+
return null;
142+
}
143+
for (ColumnParameter column : columns) {
144+
if (columnName.equals(column.getData())) {
145+
return column;
146+
}
147+
}
148+
return null;
149+
}
150+
151+
/**
152+
* Add a new column
153+
*
154+
* @param columnName the name of the column
155+
* @param searchable whether the column is searchable or not
156+
* @param orderable whether the column is orderable or not
157+
* @param searchValue if any, the search value to apply
158+
*/
159+
public void addColumn(String columnName, boolean searchable, boolean orderable,
160+
String searchValue) {
161+
this.columns.add(new ColumnParameter(columnName, "", searchable, orderable,
162+
new SearchParameter(searchValue, false)));
163+
}
164+
165+
/**
166+
* Add an order on the given column
167+
*
168+
* @param columnName the name of the column
169+
* @param ascending whether the sorting is ascending or descending
170+
*/
171+
public void addOrder(String columnName, boolean ascending) {
172+
if (columnName == null) {
173+
return;
174+
}
175+
for (int i = 0; i < columns.size(); i++) {
176+
if (!columnName.equals(columns.get(i).getData())) {
177+
continue;
178+
}
179+
order.add(new OrderParameter(i, ascending ? "asc" : "desc"));
180+
}
181+
}
182+
130183
@Override
131184
public String toString() {
132185
return "DataTablesInput [draw=" + draw + ", start=" + start + ", length=" + length + ", search="

src/main/java/org/springframework/data/jpa/datatables/mapping/DataTablesOutput.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ public class DataTablesOutput<T> {
1414
* parameter, in order to prevent Cross Site Scripting (XSS) attacks.
1515
*/
1616
@JsonView(View.class)
17-
private Integer draw;
17+
private int draw;
1818

1919
/**
2020
* Total records, before filtering (i.e. the total number of records in the database)
2121
*/
2222
@JsonView(View.class)
23-
private Long recordsTotal = 0L;
23+
private long recordsTotal = 0L;
2424

2525
/**
2626
* Total records, after filtering (i.e. the total number of records after filtering has been
2727
* applied - not just the number of records being returned for this page of data).
2828
*/
2929
@JsonView(View.class)
30-
private Long recordsFiltered = 0L;
30+
private long recordsFiltered = 0L;
3131

3232
/**
3333
* The data to be displayed in the table. This is an array of data source objects, one for each
@@ -48,27 +48,27 @@ public class DataTablesOutput<T> {
4848
public interface View {
4949
}
5050

51-
public Integer getDraw() {
51+
public int getDraw() {
5252
return draw;
5353
}
5454

55-
public void setDraw(Integer draw) {
55+
public void setDraw(int draw) {
5656
this.draw = draw;
5757
}
5858

59-
public Long getRecordsTotal() {
59+
public long getRecordsTotal() {
6060
return recordsTotal;
6161
}
6262

63-
public void setRecordsTotal(Long recordsTotal) {
63+
public void setRecordsTotal(long recordsTotal) {
6464
this.recordsTotal = recordsTotal;
6565
}
6666

67-
public Long getRecordsFiltered() {
67+
public long getRecordsFiltered() {
6868
return recordsFiltered;
6969
}
7070

71-
public void setRecordsFiltered(Long recordsFiltered) {
71+
public void setRecordsFiltered(long recordsFiltered) {
7272
this.recordsFiltered = recordsFiltered;
7373
}
7474

src/main/java/org/springframework/data/jpa/datatables/parameter/ColumnParameter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public void setSearch(SearchParameter search) {
9595
this.search = search;
9696
}
9797

98+
/**
99+
* Set the search value to apply to this column
100+
*
101+
* @param searchValue if any, the search value to apply
102+
*/
103+
public void setSearchValue(String searchValue) {
104+
this.search.setValue(searchValue);
105+
}
106+
98107
@Override
99108
public String toString() {
100109
return "ColumnParameter [data=" + data + ", name=" + name + ", searchable=" + searchable
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.model;
2+
3+
import org.springframework.data.jpa.datatables.repository.DataTablesRepository;
4+
5+
public interface BillRepository extends DataTablesRepository<Bill, Integer> {
6+
7+
}
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.model;
2+
3+
import org.springframework.data.jpa.datatables.repository.DataTablesRepository;
4+
5+
public interface GameRepository extends DataTablesRepository<Game, Integer> {
6+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package org.springframework.data.jpa.datatables.repository;
1+
package org.springframework.data.jpa.datatables.model;
22

3-
import org.springframework.data.jpa.datatables.model.Home;
43
import org.springframework.data.repository.CrudRepository;
54

65
public interface HomeRepository extends CrudRepository<Home, Integer> {
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.model;
2+
3+
import org.springframework.data.jpa.datatables.repository.DataTablesRepository;
4+
5+
public interface UserRepository extends DataTablesRepository<User, Integer> {
6+
7+
}

src/test/java/org/springframework/data/jpa/datatables/repository/BillRepository.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/test/java/org/springframework/data/jpa/datatables/repository/BillRepositoryTest.java

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44
import static org.junit.Assert.assertNotNull;
55
import static org.junit.Assert.assertNull;
66

7-
import java.util.ArrayList;
8-
9-
import org.junit.Before;
107
import org.junit.Test;
118
import org.junit.runner.RunWith;
129
import org.springframework.beans.factory.annotation.Autowired;
1310
import org.springframework.data.jpa.datatables.Config;
1411
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
1512
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
1613
import org.springframework.data.jpa.datatables.model.Bill;
17-
import org.springframework.data.jpa.datatables.parameter.ColumnParameter;
18-
import org.springframework.data.jpa.datatables.parameter.OrderParameter;
19-
import org.springframework.data.jpa.datatables.parameter.SearchParameter;
14+
import org.springframework.data.jpa.datatables.model.BillRepository;
2015
import org.springframework.data.jpa.datatables.specification.PreFilteringSpecification;
2116
import org.springframework.test.context.ContextConfiguration;
2217
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -28,74 +23,52 @@ public class BillRepositoryTest {
2823
@Autowired
2924
private BillRepository billRepository;
3025

31-
/**
32-
* Insert sample data at the beginning of all tests
33-
*/
34-
@Before
35-
public void setUp() {
36-
if (billRepository.count() > 0)
37-
return;
38-
for (int i = 0; i < 12; i++) {
39-
Bill bill = new Bill();
40-
bill.setHasBeenPayed(i % 2 == 0);
41-
bill.setAmount((i + 1) * 100);
42-
if (i == 0) {
43-
bill.setDescription("foo%");
44-
} else if (i == 1) {
45-
bill.setDescription("foo_");
46-
} else {
47-
bill.setDescription("foo" + i);
48-
}
49-
billRepository.save(bill);
50-
}
51-
}
52-
5326
@Test
5427
public void testWithoutFilter() {
5528
DataTablesInput input = getBasicInput();
5629

5730
DataTablesOutput<Bill> output = billRepository.findAll(input);
5831
assertNotNull(output);
5932
assertNull(output.getError());
60-
assertEquals(12, (long) output.getRecordsFiltered());
61-
assertEquals(12, (long) output.getRecordsTotal());
33+
assertEquals(12, output.getRecordsFiltered());
34+
assertEquals(12, output.getRecordsTotal());
6235
}
6336

6437
@Test
6538
public void testBooleanFilter() {
6639
DataTablesInput input = getBasicInput();
6740

68-
input.getColumns().get(2).getSearch().setValue("TRUE");
41+
input.getColumn("hasBeenPayed").setSearchValue("TRUE");
6942
DataTablesOutput<Bill> output = billRepository.findAll(input);
7043
assertNotNull(output);
7144
assertNull(output.getError());
72-
assertEquals(6, (long) output.getRecordsFiltered());
45+
assertEquals(6, output.getRecordsFiltered());
7346
}
7447

7548
@Test
7649
public void testBooleanFilter2() {
7750
DataTablesInput input = getBasicInput();
7851

79-
input.getColumns().get(2).getSearch().setValue("TRUE+FALSE");
52+
input.getColumn("hasBeenPayed").setSearchValue("TRUE+FALSE");
8053
DataTablesOutput<Bill> output = billRepository.findAll(input);
8154
assertNotNull(output);
8255
assertNull(output.getError());
83-
assertEquals(12, (long) output.getRecordsFiltered());
56+
assertEquals(12, output.getRecordsFiltered());
8457
}
8558

8659
@Test
8760
public void testEscapeCharacter() {
8861
DataTablesInput input = getBasicInput();
8962

90-
input.getColumns().get(3).getSearch().setValue("foo%");
63+
input.getColumn("description").setSearchValue("foo%");
9164
DataTablesOutput<Bill> output = billRepository.findAll(input);
9265
assertNotNull(output);
93-
assertEquals(1, (long) output.getRecordsFiltered());
66+
assertEquals(1, output.getRecordsFiltered());
9467

95-
input.getColumns().get(3).getSearch().setValue("foo_");
68+
input.getColumn("description").setSearchValue("foo_");
9669
output = billRepository.findAll(input);
9770
assertNotNull(output);
98-
assertEquals(1, (long) output.getRecordsFiltered());
71+
assertEquals(1, output.getRecordsFiltered());
9972
}
10073

10174
@Test
@@ -105,8 +78,8 @@ public void testWithPreFiltering() {
10578
DataTablesOutput<Bill> output =
10679
billRepository.findAll(input, null, new PreFilteringSpecification<Bill>());
10780
assertNotNull(output);
108-
assertEquals(6, (long) output.getRecordsFiltered());
109-
assertEquals(6, (long) output.getRecordsTotal());
81+
assertEquals(6, output.getRecordsFiltered());
82+
assertEquals(6, output.getRecordsTotal());
11083
}
11184

11285
/**
@@ -115,23 +88,11 @@ public void testWithPreFiltering() {
11588
*/
11689
private static DataTablesInput getBasicInput() {
11790
DataTablesInput input = new DataTablesInput();
118-
input.setDraw(1);
119-
input.setStart(0);
120-
input.setLength(10);
121-
input.setSearch(new SearchParameter("", false));
122-
input.setOrder(new ArrayList<OrderParameter>());
123-
input.getOrder().add(new OrderParameter(0, "asc"));
124-
125-
input.setColumns(new ArrayList<ColumnParameter>());
126-
input.getColumns()
127-
.add(new ColumnParameter("id", "", true, true, new SearchParameter("", false)));
128-
input.getColumns()
129-
.add(new ColumnParameter("amount", "", true, true, new SearchParameter("", false)));
130-
input.getColumns()
131-
.add(new ColumnParameter("hasBeenPayed", "", true, true, new SearchParameter("", false)));
132-
input.getColumns()
133-
.add(new ColumnParameter("description", "", true, true, new SearchParameter("", false)));
134-
91+
input.addColumn("id", true, true, "");
92+
input.addColumn("amount", true, true, "");
93+
input.addColumn("hasBeenPayed", true, true, "");
94+
input.addColumn("description", true, true, "");
95+
input.addOrder("id", true);
13596
return input;
13697
}
13798
}

src/test/java/org/springframework/data/jpa/datatables/repository/GameRepository.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)