Skip to content

Commit 1d17bc2

Browse files
committed
#153 - Add typed and untyped Examples.
1 parent bda726b commit 1d17bc2

File tree

9 files changed

+397
-79
lines changed

9 files changed

+397
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.springdata.jpa.querybyexample;
18+
19+
import lombok.Data;
20+
import lombok.NoArgsConstructor;
21+
22+
import javax.persistence.Entity;
23+
24+
/**
25+
* Sample class that extends {@link User}.
26+
*
27+
* @author Mark Paluch
28+
*/
29+
@Entity
30+
@Data
31+
@NoArgsConstructor
32+
public class SpecialUser extends User {
33+
34+
public SpecialUser(String firstname, String lastname, Integer age) {
35+
super(firstname, lastname, age);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.springdata.jpa.querybyexample;
18+
19+
import static org.hamcrest.CoreMatchers.*;
20+
import static org.junit.Assert.*;
21+
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
22+
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;
23+
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.SpringApplicationConfiguration;
29+
import org.springframework.data.domain.Example;
30+
import org.springframework.data.domain.ExampleSpec;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
import org.springframework.transaction.annotation.Transactional;
33+
34+
/**
35+
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories.
36+
*
37+
* @author Mark Paluch
38+
*/
39+
@RunWith(SpringJUnit4ClassRunner.class)
40+
@Transactional
41+
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
42+
public class UserRepositoryInheritanceIntegrationTests {
43+
44+
@Autowired UserRepository repository;
45+
46+
User skyler, walter, flynn;
47+
48+
@Before
49+
public void setUp() {
50+
51+
repository.deleteAll();
52+
53+
this.skyler = repository.save(new User("Skyler", "White", 45));
54+
this.walter = repository.save(new SpecialUser("Walter", "White", 50));
55+
this.flynn = repository.save(new SpecialUser("Walter Jr. (Flynn)", "White", 17));
56+
}
57+
58+
/**
59+
* @see DATAJPA-218
60+
*/
61+
@Test
62+
public void countBySimpleExample() {
63+
64+
Example<User> example = Example.of(new SpecialUser(null, "White", null));
65+
66+
assertThat(repository.count(example), is(3L));
67+
}
68+
69+
/**
70+
* @see DATAJPA-218
71+
*/
72+
@Test
73+
public void countUserByTypedExample() {
74+
75+
Example<User> example = Example.of(new SpecialUser(null, "White", null), //
76+
ExampleSpec.typed(User.class));
77+
78+
assertThat(repository.count(example), is(3L));
79+
}
80+
81+
/**
82+
* @see DATAJPA-218
83+
*/
84+
@Test
85+
public void countSpecialUserByTypedExample() {
86+
87+
Example<SpecialUser> example = Example.of(new SpecialUser(null, "White", null), //
88+
ExampleSpec.typed(SpecialUser.class));
89+
90+
assertThat(repository.count(example), is(2L));
91+
}
92+
93+
}

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

+68-67
Original file line numberDiff line numberDiff line change
@@ -40,94 +40,95 @@
4040
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
4141
public class UserRepositoryIntegrationTests {
4242

43-
@Autowired UserRepository repository;
43+
@Autowired
44+
UserRepository repository;
4445

45-
User skyler, walter, flynn, marie, hank;
46+
User skyler, walter, flynn, marie, hank;
4647

47-
@Before
48-
public void setUp() {
48+
@Before
49+
public void setUp() {
4950

50-
repository.deleteAll();
51+
repository.deleteAll();
5152

52-
this.skyler = repository.save(new User("Skyler", "White", 45));
53-
this.walter = repository.save(new User("Walter", "White", 50));
54-
this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17));
55-
this.marie = repository.save(new User("Marie", "Schrader", 38));
56-
this.hank = repository.save(new User("Hank", "Schrader", 43));
57-
}
53+
this.skyler = repository.save(new User("Skyler", "White", 45));
54+
this.walter = repository.save(new User("Walter", "White", 50));
55+
this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17));
56+
this.marie = repository.save(new User("Marie", "Schrader", 38));
57+
this.hank = repository.save(new User("Hank", "Schrader", 43));
58+
}
5859

59-
/**
60-
* @see DATAJPA-218
61-
*/
62-
@Test
63-
public void countBySimpleExample() {
60+
/**
61+
* @see DATAJPA-218
62+
*/
63+
@Test
64+
public void countBySimpleExample() {
6465

65-
Example<User> example = Example.of(new User(null, "White", null));
66+
Example<User> example = Example.of(new User(null, "White", null));
6667

67-
assertThat(repository.count(example), is(3L));
68-
}
68+
assertThat(repository.count(example), is(3L));
69+
}
6970

70-
/**
71-
* @see DATAJPA-218
72-
*/
73-
@Test
74-
public void ignorePropertiesAndMatchByAge() {
71+
/**
72+
* @see DATAJPA-218
73+
*/
74+
@Test
75+
public void ignorePropertiesAndMatchByAge() {
7576

76-
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
77-
withIgnorePaths("firstname", "lastname");
77+
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
78+
withIgnorePaths("firstname", "lastname");
7879

79-
assertThat(repository.findOne(Example.of(flynn, exampleSpec)), is(flynn));
80-
}
80+
assertThat(repository.findOne(Example.of(flynn, exampleSpec)), is(flynn));
81+
}
8182

82-
/**
83-
* @see DATAJPA-218
84-
*/
85-
@Test
86-
public void substringMatching() {
83+
/**
84+
* @see DATAJPA-218
85+
*/
86+
@Test
87+
public void substringMatching() {
8788

88-
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).//
89-
withStringMatcherEnding();
89+
ExampleSpec exampleSpec = ExampleSpec.untyped().//
90+
withStringMatcherEnding();
9091

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

94-
/**
95-
* @see DATAJPA-218
96-
*/
97-
@Test
98-
public void matchStartingStringsIgnoreCase() {
95+
/**
96+
* @see DATAJPA-218
97+
*/
98+
@Test
99+
public void matchStartingStringsIgnoreCase() {
99100

100-
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
101-
withIgnorePaths("age").//
102-
withMatcher("firstname", startsWith()).//
103-
withMatcher("lastname", ignoreCase());
101+
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
102+
withIgnorePaths("age").//
103+
withMatcher("firstname", startsWith()).//
104+
withMatcher("lastname", ignoreCase());
104105

105-
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
106-
}
106+
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
107+
}
107108

108-
/**
109-
* @see DATAJPA-218
110-
*/
111-
@Test
112-
public void configuringMatchersUsingLambdas() {
109+
/**
110+
* @see DATAJPA-218
111+
*/
112+
@Test
113+
public void configuringMatchersUsingLambdas() {
113114

114-
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class).withIgnorePaths("age"). //
115-
withMatcher("firstname", matcher -> matcher.startsWith()). //
116-
withMatcher("lastname", matcher -> matcher.ignoreCase());
115+
ExampleSpec exampleSpec = ExampleSpec.untyped().withIgnorePaths("age"). //
116+
withMatcher("firstname", matcher -> matcher.startsWith()). //
117+
withMatcher("lastname", matcher -> matcher.ignoreCase());
117118

118-
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
119-
}
119+
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
120+
}
120121

121-
/**
122-
* @see DATAJPA-218
123-
*/
124-
@Test
125-
public void valueTransformer() {
122+
/**
123+
* @see DATAJPA-218
124+
*/
125+
@Test
126+
public void valueTransformer() {
126127

127-
ExampleSpec<User> exampleSpec = ExampleSpec.of(User.class). //
128-
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50)));
128+
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
129+
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50)));
129130

130-
assertThat(repository.findAll(Example.of(new User(null, "White", 99), exampleSpec)), hasItems(walter));
131-
}
131+
assertThat(repository.findAll(Example.of(new User(null, "White", 99), exampleSpec)), hasItems(walter));
132+
}
132133

133134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.springdata.mongodb.querybyexample;
18+
19+
import lombok.Data;
20+
import lombok.RequiredArgsConstructor;
21+
22+
import org.bson.types.ObjectId;
23+
import org.springframework.data.annotation.Id;
24+
import org.springframework.data.mongodb.core.mapping.Document;
25+
26+
/**
27+
* Sample contact class.
28+
*
29+
* @author Mark Paluch
30+
*/
31+
@Data
32+
@RequiredArgsConstructor
33+
@Document(collection = "collectionStoringTwoTypes")
34+
public class Contact {
35+
36+
@Id //
37+
private ObjectId id;
38+
private final String firstname;
39+
private final String lastname;
40+
private final Integer age;
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.springdata.mongodb.querybyexample;
18+
19+
import org.springframework.data.repository.CrudRepository;
20+
import org.springframework.data.repository.query.QueryByExampleExecutor;
21+
22+
/**
23+
* Simple repository interface for {@link Contact} instances. The interface implements {@link QueryByExampleExecutor} and
24+
* allows execution of methods accepting {@link org.springframework.data.domain.Example}.
25+
*
26+
* @author Mark Paluch
27+
*/
28+
public interface ContactRepository extends CrudRepository<Contact, Long>, QueryByExampleExecutor<Contact> {
29+
30+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
@Data
3333
@RequiredArgsConstructor
34-
@Document
34+
@Document(collection = "collectionStoringTwoTypes")
3535
public class User {
3636

3737
@Id //

0 commit comments

Comments
 (0)