#153 - Polishing.

Simplified domain model using Lombok. Moved configuration classes into test source folder.

Original pull request: #154.
This commit is contained in:
Oliver Gierke
2016-03-17 18:59:38 +01:00
parent beabdf8a79
commit 1f94bb12f1
21 changed files with 311 additions and 338 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jpa.querybyexample;
import lombok.Data;
@@ -25,6 +24,7 @@ import javax.persistence.Entity;
* Sample class that extends {@link User}.
*
* @author Mark Paluch
* @author Oliver Gierke
*/
@Entity
@Data

View File

@@ -15,36 +15,27 @@
*/
package example.springdata.jpa.querybyexample;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Sample user class.
*
* @author Mark Paluch
* @author Oliver Gierke
*/
@Entity
@Data
@NoArgsConstructor
@NoArgsConstructor(force = true)
@RequiredArgsConstructor
public class User {
@Id @GeneratedValue //
private Long id;
private String firstname;
private String lastname;
private Integer age;
public User(String firstname, String lastname, Integer age) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}
private @Id @GeneratedValue Long id;
private final String firstname, lastname;
private final Integer age;
}

View File

@@ -24,6 +24,4 @@ import org.springframework.data.repository.query.QueryByExampleExecutor;
*
* @author Mark Paluch
*/
public interface UserRepository extends CrudRepository<User, Long>, QueryByExampleExecutor<User> {
}
public interface UserRepository extends CrudRepository<User, Long>, QueryByExampleExecutor<User> {}

View File

@@ -1 +0,0 @@
spring.datasource.separator=/;

View File

@@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="error" />
<root level="error">
<appender-ref ref="console" />
</root>
</configuration>

View File

@@ -15,18 +15,11 @@
*/
package example.springdata.jpa.querybyexample;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author Mark Paluch
* @author Oliver Gierke
*/
@Configuration
@EnableAutoConfiguration
@EntityScan(basePackageClasses = { ApplicationConfiguration.class })
@EnableJpaAuditing
public class ApplicationConfiguration {
}
@SpringBootApplication
public class ApplicationConfiguration {}

View File

@@ -13,13 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jpa.querybyexample;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;
import org.junit.Before;
import org.junit.Test;
@@ -27,14 +24,15 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleSpec;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories.
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories and entities
* using inheritance.
*
* @author Mark Paluch
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@@ -56,38 +54,18 @@ public class UserRepositoryInheritanceIntegrationTests {
}
/**
* @see DATAJPA-218
* @see #153
*/
@Test
public void countBySimpleExample() {
Example<User> example = Example.of(new SpecialUser(null, "White", null));
assertThat(repository.count(example), is(3L));
public void countByExample() {
assertThat(repository.count(Example.of(new User(null, "White", null))), is(3L));
}
/**
* @see DATAJPA-218
* @see #153
*/
@Test
public void countUserByTypedExample() {
Example<User> example = Example.of(new SpecialUser(null, "White", null), //
ExampleSpec.typed(User.class));
assertThat(repository.count(example), is(3L));
public void countSubtypesByExample() {
assertThat(repository.count(Example.of(new SpecialUser(null, "White", null))), is(2L));
}
/**
* @see DATAJPA-218
*/
@Test
public void countSpecialUserByTypedExample() {
Example<SpecialUser> example = Example.of(new SpecialUser(null, "White", null), //
ExampleSpec.typed(SpecialUser.class));
assertThat(repository.count(example), is(2L));
}
}

View File

@@ -17,8 +17,9 @@ package example.springdata.jpa.querybyexample;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;
import static org.springframework.data.domain.ExampleMatcher.*;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.*;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
import org.junit.Before;
import org.junit.Test;
@@ -26,7 +27,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleSpec;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@@ -34,101 +35,104 @@ import org.springframework.transaction.annotation.Transactional;
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories.
*
* @author Mark Paluch
* @author Oliver Gierke
*/
@SuppressWarnings("unused")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
public class UserRepositoryIntegrationTests {
@Autowired
UserRepository repository;
@Autowired UserRepository repository;
User skyler, walter, flynn, marie, hank;
User skyler, walter, flynn, marie, hank;
@Before
public void setUp() {
@Before
public void setUp() {
repository.deleteAll();
repository.deleteAll();
this.skyler = repository.save(new User("Skyler", "White", 45));
this.walter = repository.save(new User("Walter", "White", 50));
this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17));
this.marie = repository.save(new User("Marie", "Schrader", 38));
this.hank = repository.save(new User("Hank", "Schrader", 43));
}
this.skyler = repository.save(new User("Skyler", "White", 45));
this.walter = repository.save(new User("Walter", "White", 50));
this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17));
this.marie = repository.save(new User("Marie", "Schrader", 38));
this.hank = repository.save(new User("Hank", "Schrader", 43));
}
/**
* @see DATAJPA-218
*/
@Test
public void countBySimpleExample() {
/**
* @see #153
*/
@Test
public void countBySimpleExample() {
Example<User> example = Example.of(new User(null, "White", null));
Example<User> example = Example.of(new User(null, "White", null));
assertThat(repository.count(example), is(3L));
}
assertThat(repository.count(example), is(3L));
}
/**
* @see DATAJPA-218
*/
@Test
public void ignorePropertiesAndMatchByAge() {
/**
* @see #153
*/
@Test
public void ignorePropertiesAndMatchByAge() {
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
withIgnorePaths("firstname", "lastname");
Example<User> example = Example.of(flynn, matching().//
withIgnorePaths("firstname", "lastname"));
assertThat(repository.findOne(Example.of(flynn, exampleSpec)), is(flynn));
}
assertThat(repository.findOne(example), is(flynn));
}
/**
* @see DATAJPA-218
*/
@Test
public void substringMatching() {
/**
* @see #153
*/
@Test
public void substringMatching() {
ExampleSpec exampleSpec = ExampleSpec.untyped().//
withStringMatcherEnding();
Example<User> example = Example.of(new User("er", null, null), matching().//
withStringMatcher(StringMatcher.ENDING));
assertThat(repository.findAll(Example.of(new User("er", null, null), exampleSpec)), hasItems(skyler, walter));
}
assertThat(repository.findAll(example), hasItems(skyler, walter));
}
/**
* @see DATAJPA-218
*/
@Test
public void matchStartingStringsIgnoreCase() {
/**
* @see #153
*/
@Test
public void matchStartingStringsIgnoreCase() {
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
withIgnorePaths("age").//
withMatcher("firstname", startsWith()).//
withMatcher("lastname", ignoreCase());
Example<User> example = Example.of(new User("Walter", "WHITE", null),
matching().//
withIgnorePaths("age").//
withMatcher("firstname", startsWith()).//
withMatcher("lastname", ignoreCase()));
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
}
assertThat(repository.findAll(example), hasItems(flynn, walter));
}
/**
* @see DATAJPA-218
*/
@Test
public void configuringMatchersUsingLambdas() {
/**
* @see #153
*/
@Test
public void configuringMatchersUsingLambdas() {
ExampleSpec exampleSpec = ExampleSpec.untyped().withIgnorePaths("age"). //
withMatcher("firstname", matcher -> matcher.startsWith()). //
withMatcher("lastname", matcher -> matcher.ignoreCase());
Example<User> example = Example.of(new User("Walter", "WHITE", null),
matching().//
withIgnorePaths("age").//
withMatcher("firstname", matcher -> matcher.startsWith()).//
withMatcher("lastname", matcher -> matcher.ignoreCase()));
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
}
assertThat(repository.findAll(example), hasItems(flynn, walter));
}
/**
* @see DATAJPA-218
*/
@Test
public void valueTransformer() {
/**
* @see #153
*/
@Test
public void valueTransformer() {
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50)));
assertThat(repository.findAll(Example.of(new User(null, "White", 99), exampleSpec)), hasItems(walter));
}
Example<User> example = Example.of(new User(null, "White", 99), matching(). //
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))));
assertThat(repository.findAll(example), hasItems(walter));
}
}