diff --git a/src/test/java/com/github/wenhao/jpa/integration/AndTest.java b/src/test/java/com/github/wenhao/jpa/integration/AndTest.java index 3af9bd3..8a057c2 100644 --- a/src/test/java/com/github/wenhao/jpa/integration/AndTest.java +++ b/src/test/java/com/github/wenhao/jpa/integration/AndTest.java @@ -41,13 +41,13 @@ public class AndTest { public void should_be_able_to_find_by_using_many_to_one_query() { // given final Person jack = new PersonBuilder() - .name("Jack") - .age(18) - .phone("iPhone", "139000000000") - .phone("HuaWei", "13600000000") - .phone("HuaWei", "18000000000") - .phone("Samsung", "13600000000") - .build(); + .name("Jack") + .age(18) + .phone("iPhone", "139000000000") + .phone("HuaWei", "13600000000") + .phone("HuaWei", "18000000000") + .phone("Samsung", "13600000000") + .build(); Set jackPhones = jack.getPhones(); for (Phone phone : jackPhones) { @@ -58,15 +58,15 @@ public void should_be_able_to_find_by_using_many_to_one_query() { // when Specification specification = Specifications.builder() - .eq("brand", "HuaWei") - .and(StringUtils.isNotBlank(jack.getName()), new Specification() { - @Override - public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { - Path person = root.get("person"); - return cb.equal(person.get("name"), jack.getName()); - } - }) - .build(); + .eq("brand", "HuaWei") + .and(StringUtils.isNotBlank(jack.getName()), new Specification() { + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + Path person = root.get("person"); + return cb.equal(person.get("name"), jack.getName()); + } + }) + .build(); List phones = phoneRepository.findAll(specification); @@ -75,31 +75,31 @@ public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaB } @Test - public void should_be_able_to_find_by_using_many_to_many_query() throws ParseException { + public void should_be_able_to_find_by_using_many_to_many_query() { // given Person jack = new PersonBuilder() - .name("Jack") - .age(18) - .address("Sichuan", 3) - .address("Sichuan", 5) - .address("Chengdu", 4) - .address("Zhonghe", 7) - .build(); + .name("Jack") + .age(18) + .address("Sichuan", 3) + .address("Sichuan", 5) + .address("Chengdu", 4) + .address("Zhonghe", 7) + .build(); Person eric = new PersonBuilder() - .name("Eric") - .age(20) - .address("GaoXin", 8) - .address("Tianfu", 9) - .address("Chengdu", 4) - .build(); + .name("Eric") + .age(20) + .address("GaoXin", 8) + .address("Tianfu", 9) + .address("Chengdu", 4) + .build(); Person alex = new PersonBuilder() - .name("Alex") - .age(30) - .address("HuaYang", 1) - .address("NeiJiang", 2) - .build(); + .name("Alex") + .age(30) + .address("HuaYang", 1) + .address("NeiJiang", 2) + .build(); personRepository.save(jack); personRepository.save(eric); @@ -107,15 +107,15 @@ public void should_be_able_to_find_by_using_many_to_many_query() throws ParseExc // when Specification specification = Specifications.builder() - .between("age", new Range(10, 35)) - .and(StringUtils.isNotBlank(jack.getName()), new Specification() { - @Override - public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { - Join address = root.join("addresses", JoinType.LEFT); - return cb.equal(address.get("street"), "Chengdu"); - } - }) - .build(); + .between("age", new Range(10, 35)) + .and(StringUtils.isNotBlank(jack.getName()), new Specification() { + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + Join address = root.join("addresses", JoinType.LEFT); + return cb.equal(address.get("street"), "Chengdu"); + } + }) + .build(); List phones = personRepository.findAll(specification); diff --git a/src/test/java/com/github/wenhao/jpa/integration/BetweenTest.java b/src/test/java/com/github/wenhao/jpa/integration/BetweenTest.java index fb5fee6..7ad2730 100644 --- a/src/test/java/com/github/wenhao/jpa/integration/BetweenTest.java +++ b/src/test/java/com/github/wenhao/jpa/integration/BetweenTest.java @@ -30,20 +30,20 @@ public class BetweenTest { public void should_be_able_to_find_by_using_between() throws ParseException { // given Person jack = new PersonBuilder() - .name("Jack") - .birthday(getDate("1987-11-14")) - .build(); + .name("Jack") + .birthday(getDate("1987-11-14")) + .build(); Person eric = new PersonBuilder() - .name("Eric") - .birthday(getDate("1990-10-12")) - .build(); + .name("Eric") + .birthday(getDate("1990-10-12")) + .build(); personRepository.save(jack); personRepository.save(eric); // when Specification specification = Specifications.builder() - .between(jack.getBirthday() != null, "birthday", new Range(getDate("1980-01-01"), getDate("1989-12-31"))) - .build(); + .between(jack.getBirthday() != null, "birthday", new Range(getDate("1980-01-01"), getDate("1989-12-31"))) + .build(); List persons = personRepository.findAll(specification); diff --git a/src/test/java/com/github/wenhao/jpa/integration/JoinTest.java b/src/test/java/com/github/wenhao/jpa/integration/JoinTest.java index 675987f..4e06046 100644 --- a/src/test/java/com/github/wenhao/jpa/integration/JoinTest.java +++ b/src/test/java/com/github/wenhao/jpa/integration/JoinTest.java @@ -1,7 +1,5 @@ package com.github.wenhao.jpa.integration; -import static org.assertj.core.api.Assertions.assertThat; - import com.github.wenhao.jpa.Specifications; import com.github.wenhao.jpa.builder.PersonBuilder; import com.github.wenhao.jpa.model.Person; @@ -17,10 +15,11 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.test.context.junit4.SpringRunner; -import java.text.ParseException; import java.util.List; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + @RunWith(SpringRunner.class) @DataJpaTest public class JoinTest { @@ -61,7 +60,7 @@ public void should_be_able_to_find_by_using_many_to_one_query() { } @Test - public void should_be_able_to_find_by_using_many_to_many_query() throws ParseException { + public void should_be_able_to_find_by_using_many_to_many_query() { // given Person jack = new PersonBuilder() .name("Jack") diff --git a/src/test/java/com/github/wenhao/jpa/integration/OrTest.java b/src/test/java/com/github/wenhao/jpa/integration/OrTest.java index 1ba4570..5da39ba 100644 --- a/src/test/java/com/github/wenhao/jpa/integration/OrTest.java +++ b/src/test/java/com/github/wenhao/jpa/integration/OrTest.java @@ -29,31 +29,31 @@ public class OrTest { public void should_be_able_to_find_by_using_or_with_multiple_values() { // given Person jack = new PersonBuilder() - .name("Jack") - .age(18) - .build(); + .name("Jack") + .age(18) + .build(); Person eric = new PersonBuilder() - .name("Eric") - .age(20) - .build(); + .name("Eric") + .age(20) + .build(); Person jackson = new PersonBuilder() - .age(30) - .nickName("Jackson") - .build(); + .age(30) + .nickName("Jackson") + .build(); personRepository.save(jack); personRepository.save(eric); personRepository.save(jackson); // when Specification specification = Specifications.builder() - .and(OrSpecifications.builder() - .like("name", "%ac%") - .gt("age", 19) - .eq(jack.getCompany() != null, null) - .ne(jack.getNickName() != null, null) - .between(jack.getBirthday() != null, "birthday", new Range(new Date(), new Date())) - .build()) - .build(); + .and(OrSpecifications.builder() + .like("name", "%ac%") + .gt("age", 19) + .eq(jack.getCompany() != null, null) + .ne(jack.getNickName() != null, null) + .between(jack.getBirthday() != null, "birthday", new Range(new Date(), new Date())) + .build()) + .build(); List persons = personRepository.findAll(specification); diff --git a/src/test/java/com/github/wenhao/jpa/integration/SortsTest.java b/src/test/java/com/github/wenhao/jpa/integration/SortsTest.java index d6205eb..7d290c1 100644 --- a/src/test/java/com/github/wenhao/jpa/integration/SortsTest.java +++ b/src/test/java/com/github/wenhao/jpa/integration/SortsTest.java @@ -28,31 +28,31 @@ public class SortsTest { public void should_be_able_to_sort_by_desc() { // given Person jack = new PersonBuilder() - .name("Jack") - .age(18) - .build(); + .name("Jack") + .age(18) + .build(); Person eric = new PersonBuilder() - .name("Eric") - .age(20) - .build(); + .name("Eric") + .age(20) + .build(); Person aaron = new PersonBuilder() - .name("Aaron") - .age(18) - .build(); + .name("Aaron") + .age(18) + .build(); personRepository.save(jack); personRepository.save(eric); personRepository.save(aaron); // when Specification specification = Specifications.builder() - .ne("name", null) - .build(); + .ne("name", null) + .build(); Sort sort = Sorts.builder() - .desc(jack.getAge() != null, "age") - .desc("name") - .desc(jack.getCompany() != null, "company") - .build(); + .desc(jack.getAge() != null, "age") + .desc("name") + .desc(jack.getCompany() != null, "company") + .build(); List persons = personRepository.findAll(specification, sort); @@ -65,31 +65,31 @@ public void should_be_able_to_sort_by_desc() { public void should_be_able_to_sort_by_asc() { // given Person jack = new PersonBuilder() - .name("Jack") - .age(18) - .build(); + .name("Jack") + .age(18) + .build(); Person eric = new PersonBuilder() - .name("Eric") - .age(20) - .build(); + .name("Eric") + .age(20) + .build(); Person aaron = new PersonBuilder() - .name("Aaron") - .age(18) - .build(); + .name("Aaron") + .age(18) + .build(); personRepository.save(jack); personRepository.save(eric); personRepository.save(aaron); // when Specification specification = Specifications.builder() - .ne("name", null) - .build(); + .ne("name", null) + .build(); Sort sort = Sorts.builder() - .asc(jack.getAge() != null, "age") - .asc("name") - .asc(jack.getCompany() != null, "company") - .build(); + .asc(jack.getAge() != null, "age") + .asc("name") + .asc(jack.getCompany() != null, "company") + .build(); List persons = personRepository.findAll(specification, sort);