Skip to content

Commit b91ec53

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATAMONGO-1146 - Added QueryDslMongoRepository.exists(…) which accepts a Querydsl predicate.
Added explicit test case for QueryDslMongoRepository. Related tickets: DATACMNS-636. Original pull request: spring-projects#270.
1 parent ce0624b commit b91ec53

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepository.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -161,6 +161,14 @@ public long count(Predicate predicate) {
161161
return createQueryFor(predicate).count();
162162
}
163163

164+
/*
165+
* (non-Javadoc)
166+
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#exists(com.mysema.query.types.Predicate)
167+
*/
168+
public boolean exists(Predicate predicate) {
169+
return createQueryFor(predicate).exists();
170+
}
171+
164172
/**
165173
* Creates a {@link MongodbQuery} for the given {@link Predicate}.
166174
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2015 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+
package org.springframework.data.mongodb.repository.support;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import java.util.Arrays;
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.data.mongodb.core.MongoOperations;
28+
import org.springframework.data.mongodb.repository.Person;
29+
import org.springframework.data.mongodb.repository.QPerson;
30+
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
31+
import org.springframework.test.context.ContextConfiguration;
32+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33+
34+
import com.mysema.query.types.Predicate;
35+
36+
/**
37+
* Integration test for {@link QueryDslMongoRepository}.
38+
*
39+
* @author Thomas Darimont
40+
*/
41+
@ContextConfiguration(
42+
locations = "/org/springframework/data/mongodb/repository/PersonRepositoryIntegrationTests-context.xml")
43+
@RunWith(SpringJUnit4ClassRunner.class)
44+
public class QueryDslMongoRepositoryIntegrationTests {
45+
46+
@Autowired MongoOperations operations;
47+
QueryDslMongoRepository<Person, String> repository;
48+
49+
Person dave, oliver, carter;
50+
QPerson person;
51+
52+
@Before
53+
public void setup() {
54+
55+
MongoRepositoryFactory factory = new MongoRepositoryFactory(operations);
56+
MongoEntityInformation<Person, String> entityInformation = factory.getEntityInformation(Person.class);
57+
repository = new QueryDslMongoRepository<Person, String>(entityInformation, operations);
58+
59+
operations.dropCollection(Person.class);
60+
61+
dave = new Person("Dave", "Matthews", 42);
62+
oliver = new Person("Oliver August", "Matthews", 4);
63+
carter = new Person("Carter", "Beauford", 49);
64+
65+
person = new QPerson("person");
66+
67+
repository.save(Arrays.asList(oliver, dave, carter));
68+
}
69+
70+
/**
71+
* @see DATAMONGO-1146
72+
*/
73+
@Test
74+
public void shouldSupportExistsWithPredicate() throws Exception {
75+
76+
assertThat(repository.exists(person.firstname.eq("Dave")), is(true));
77+
assertThat(repository.exists(person.firstname.eq("Unknown")), is(false));
78+
assertThat(repository.exists((Predicate) null), is(true));
79+
}
80+
}

0 commit comments

Comments
 (0)