Skip to content

Commit bda726b

Browse files
committed
#153 - Update Example with changes from reviews.
1 parent b3d9949 commit bda726b

File tree

4 files changed

+57
-70
lines changed

4 files changed

+57
-70
lines changed

jpa/query-by-example/src/main/java/example/springdata/jpa/querybyexample/User.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.persistence.Id;
2121

2222
import lombok.Data;
23+
import lombok.NoArgsConstructor;
2324

2425
/**
2526
* Sample user class.
@@ -28,6 +29,7 @@
2829
*/
2930
@Entity
3031
@Data
32+
@NoArgsConstructor
3133
public class User {
3234

3335
@Id @GeneratedValue //
@@ -36,10 +38,6 @@ public class User {
3638
private String lastname;
3739
private Integer age;
3840

39-
public User() {
40-
super();
41-
}
42-
4341
public User(String firstname, String lastname, Integer age) {
4442

4543
super();

jpa/query-by-example/src/test/java/example/springdata/jpa/querybyexample/UserRepositoryIntegrationTests.java

+16-21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
21+
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;
2122

2223
import org.junit.Before;
2324
import org.junit.Test;
@@ -26,7 +27,6 @@
2627
import org.springframework.boot.test.SpringApplicationConfiguration;
2728
import org.springframework.data.domain.Example;
2829
import org.springframework.data.domain.ExampleSpec;
29-
import org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers;
3030
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3131
import org.springframework.transaction.annotation.Transactional;
3232

@@ -73,11 +73,10 @@ public void countBySimpleExample() {
7373
@Test
7474
public void ignorePropertiesAndMatchByAge() {
7575

76-
Example<User> example = ExampleSpec.of(User.class). //
77-
withIgnorePaths("firstname", "lastname").//
78-
createExample(flynn);
76+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
77+
withIgnorePaths("firstname", "lastname");
7978

80-
assertThat(repository.findOne(example), is(flynn));
79+
assertThat(repository.findOne(Example.of(flynn, exampleSpec)), is(flynn));
8180
}
8281

8382
/**
@@ -86,11 +85,10 @@ public void ignorePropertiesAndMatchByAge() {
8685
@Test
8786
public void substringMatching() {
8887

89-
Example<User> example = ExampleSpec.of(User.class).//
90-
withStringMatcherEnding()//
91-
.createExample(new User("er", null, null));
88+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).//
89+
withStringMatcherEnding();
9290

93-
assertThat(repository.findAll(example), hasItems(skyler, walter));
91+
assertThat(repository.findAll(Example.of(new User("er", null, null), exampleSpec)), hasItems(skyler, walter));
9492
}
9593

9694
/**
@@ -99,13 +97,12 @@ public void substringMatching() {
9997
@Test
10098
public void matchStartingStringsIgnoreCase() {
10199

102-
Example<User> example = ExampleSpec.of(User.class). //
100+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
103101
withIgnorePaths("age").//
104102
withMatcher("firstname", startsWith()).//
105-
withMatcher("lastname", ignoreCase()).//
106-
createExample(new User("Walter", "WHITE", null));
103+
withMatcher("lastname", ignoreCase());
107104

108-
assertThat(repository.findAll(example), hasItems(flynn, walter));
105+
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
109106
}
110107

111108
/**
@@ -114,12 +111,11 @@ public void matchStartingStringsIgnoreCase() {
114111
@Test
115112
public void configuringMatchersUsingLambdas() {
116113

117-
Example<User> example = ExampleSpec.of(User.class).withIgnorePaths("age"). //
114+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).withIgnorePaths("age"). //
118115
withMatcher("firstname", matcher -> matcher.startsWith()). //
119-
withMatcher("lastname", matcher -> matcher.ignoreCase()). //
120-
createExample(new User("Walter", "WHITE", null));
116+
withMatcher("lastname", matcher -> matcher.ignoreCase());
121117

122-
assertThat(repository.findAll(example), hasItems(flynn, walter));
118+
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
123119
}
124120

125121
/**
@@ -128,11 +124,10 @@ public void configuringMatchersUsingLambdas() {
128124
@Test
129125
public void valueTransformer() {
130126

131-
Example<User> example = ExampleSpec.of(User.class). //
132-
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))).//
133-
createExample(new User(null, "White", 99));
127+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
128+
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50)));
134129

135-
assertThat(repository.findAll(example), hasItems(walter));
130+
assertThat(repository.findAll(Example.of(new User(null, "White", 99), exampleSpec)), hasItems(walter));
136131
}
137132

138133
}

mongodb/query-by-example/src/test/java/example/springdata/mongodb/querybyexample/MongoOperationsIntegrationTests.java

+18-20
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,10 @@ public void ignoreNullProperties() {
7878
@Test
7979
public void substringMatching() {
8080

81-
Example<User> example = ExampleSpec.of(User.class).//
82-
withStringMatcherEnding().//
83-
createExample(new User("er", null, null));
81+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).//
82+
withStringMatcherEnding();
8483

85-
assertThat(operations.findByExample(example), hasItems(skyler, walter));
84+
assertThat(operations.findByExample(Example.of(new User("er", null, null), exampleSpec)), hasItems(skyler, walter));
8685
}
8786

8887
/**
@@ -91,11 +90,11 @@ public void substringMatching() {
9190
@Test
9291
public void regexMatching() {
9392

94-
Example<User> example = ExampleSpec.of(User.class).//
95-
withMatcher("firstname", matcher -> matcher.regex()).//
96-
createExample(new User("(Skyl|Walt)er", null, null));
93+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).//
94+
withMatcher("firstname", matcher -> matcher.regex());
9795

98-
assertThat(operations.findByExample(example), hasItems(skyler, walter));
96+
assertThat(operations.findByExample(Example.of(new User("(Skyl|Walt)er", null, null), exampleSpec)),
97+
hasItems(skyler, walter));
9998
}
10099

101100
/**
@@ -104,13 +103,13 @@ public void regexMatching() {
104103
@Test
105104
public void matchStartingStringsIgnoreCase() {
106105

107-
Example<User> example = ExampleSpec.of(User.class). //
106+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
108107
withIgnorePaths("age").//
109108
withMatcher("firstname", startsWith()).//
110-
withMatcher("lastname", ignoreCase()).//
111-
createExample(new User("Walter", "WHITE", null));
109+
withMatcher("lastname", ignoreCase());
112110

113-
assertThat(operations.findByExample(example), hasItems(flynn, walter));
111+
assertThat(operations.findByExample(Example.of(new User("Walter", "WHITE", null), exampleSpec)),
112+
hasItems(flynn, walter));
114113
}
115114

116115
/**
@@ -119,12 +118,12 @@ public void matchStartingStringsIgnoreCase() {
119118
@Test
120119
public void configuringMatchersUsingLambdas() {
121120

122-
Example<User> example = ExampleSpec.of(User.class).withIgnorePaths("age"). //
121+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).withIgnorePaths("age"). //
123122
withMatcher("firstname", matcher -> matcher.startsWith()). //
124-
withMatcher("lastname", matcher -> matcher.ignoreCase()). //
125-
createExample(new User("Walter", "WHITE", null));
123+
withMatcher("lastname", matcher -> matcher.ignoreCase());
126124

127-
assertThat(operations.findByExample(example), hasItems(flynn, walter));
125+
assertThat(operations.findByExample(Example.of(new User("Walter", "WHITE", null), exampleSpec)),
126+
hasItems(flynn, walter));
128127
}
129128

130129
/**
@@ -133,11 +132,10 @@ public void configuringMatchersUsingLambdas() {
133132
@Test
134133
public void valueTransformer() {
135134

136-
Example<User> example = ExampleSpec.of(User.class). //
137-
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))).//
138-
createExample(new User(null, "White", 99));
135+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
136+
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50)));
139137

140-
assertThat(operations.findByExample(example), hasItems(walter));
138+
assertThat(operations.findByExample(Example.of(new User(null, "White", 99), exampleSpec)), hasItems(walter));
141139
}
142140

143141
}

mongodb/query-by-example/src/test/java/example/springdata/mongodb/querybyexample/UserRepositoryIntegrationTests.java

+21-25
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
import static org.hamcrest.CoreMatchers.*;
2020
import static org.junit.Assert.*;
21-
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
21+
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.ignoreCase;
22+
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;
2223

2324
import org.junit.Before;
2425
import org.junit.Test;
@@ -71,11 +72,10 @@ public void countBySimpleExample() {
7172
@Test
7273
public void ignorePropertiesAndMatchByAge() {
7374

74-
Example<User> example = ExampleSpec.of(User.class). //
75-
withIgnorePaths("firstname", "lastname").//
76-
createExample(flynn);
75+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
76+
withIgnorePaths("firstname", "lastname");
7777

78-
assertThat(repository.findOne(example), is(flynn));
78+
assertThat(repository.findOne(Example.of(flynn, exampleSpec)), is(flynn));
7979
}
8080

8181
/**
@@ -84,11 +84,10 @@ public void ignorePropertiesAndMatchByAge() {
8484
@Test
8585
public void substringMatching() {
8686

87-
Example<User> example = ExampleSpec.of(User.class).//
88-
withStringMatcherEnding()//
89-
.createExample(new User("er", null, null));
87+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).//
88+
withStringMatcherEnding();
9089

91-
assertThat(repository.findAll(example), hasItems(skyler, walter));
90+
assertThat(repository.findAll(Example.of(new User("er", null, null), exampleSpec)), hasItems(skyler, walter));
9291
}
9392

9493
/**
@@ -97,11 +96,11 @@ public void substringMatching() {
9796
@Test
9897
public void regexMatching() {
9998

100-
Example<User> example = ExampleSpec.of(User.class).//
101-
withMatcher("firstname", matcher -> matcher.regex()).//
102-
createExample(new User("(Skyl|Walt)er", null, null));
99+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).//
100+
withMatcher("firstname", matcher -> matcher.regex());
103101

104-
assertThat(repository.findAll(example), hasItems(skyler, walter));
102+
assertThat(repository.findAll(Example.of(new User("(Skyl|Walt)er", null, null), exampleSpec)),
103+
hasItems(skyler, walter));
105104
}
106105

107106
/**
@@ -110,13 +109,12 @@ public void regexMatching() {
110109
@Test
111110
public void matchStartingStringsIgnoreCase() {
112111

113-
Example<User> example = ExampleSpec.of(User.class). //
112+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
114113
withIgnorePaths("age").//
115114
withMatcher("firstname", startsWith()).//
116-
withMatcher("lastname", ignoreCase()).//
117-
createExample(new User("Walter", "WHITE", null));
115+
withMatcher("lastname", ignoreCase());
118116

119-
assertThat(repository.findAll(example), hasItems(flynn, walter));
117+
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
120118
}
121119

122120
/**
@@ -125,12 +123,11 @@ public void matchStartingStringsIgnoreCase() {
125123
@Test
126124
public void configuringMatchersUsingLambdas() {
127125

128-
Example<User> example = ExampleSpec.of(User.class).withIgnorePaths("age"). //
126+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).withIgnorePaths("age"). //
129127
withMatcher("firstname", matcher -> matcher.startsWith()). //
130-
withMatcher("lastname", matcher -> matcher.ignoreCase()). //
131-
createExample(new User("Walter", "WHITE", null));
128+
withMatcher("lastname", matcher -> matcher.ignoreCase());
132129

133-
assertThat(repository.findAll(example), hasItems(flynn, walter));
130+
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
134131
}
135132

136133
/**
@@ -139,11 +136,10 @@ public void configuringMatchersUsingLambdas() {
139136
@Test
140137
public void valueTransformer() {
141138

142-
Example<User> example = ExampleSpec.of(User.class). //
143-
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))).//
144-
createExample(new User(null, "White", 99));
139+
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
140+
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50)));
145141

146-
assertThat(repository.findAll(example), hasItems(walter));
142+
assertThat(repository.findAll(Example.of(new User(null, "White", 99), exampleSpec)), hasItems(walter));
147143
}
148144

149145
}

0 commit comments

Comments
 (0)