Skip to content

Commit 3d2c5a9

Browse files
committed
DATAMONGO-1753 - IndexEnsuringQueryCreationListener now skips query methods without a predicate.
1 parent 489f3bb commit 3d2c5a9

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import org.slf4j.Logger;
2323
import org.slf4j.LoggerFactory;
2424
import org.springframework.data.domain.Sort;
25-
import org.springframework.data.domain.Sort.Order;
2625
import org.springframework.data.domain.Sort.Direction;
27-
import org.springframework.data.mongodb.core.index.IndexOperationsProvider;
26+
import org.springframework.data.domain.Sort.Order;
2827
import org.springframework.data.mongodb.core.MongoOperations;
2928
import org.springframework.data.mongodb.core.index.Index;
29+
import org.springframework.data.mongodb.core.index.IndexOperationsProvider;
3030
import org.springframework.data.mongodb.repository.query.MongoEntityMetadata;
3131
import org.springframework.data.mongodb.repository.query.PartTreeMongoQuery;
3232
import org.springframework.data.repository.core.support.QueryCreationListener;
@@ -68,6 +68,11 @@ public IndexEnsuringQueryCreationListener(IndexOperationsProvider indexOperation
6868
public void onCreation(PartTreeMongoQuery query) {
6969

7070
PartTree tree = query.getTree();
71+
72+
if (!tree.hasPredicate()) {
73+
return;
74+
}
75+
7176
Index index = new Index();
7277
index.named(query.getQueryMethod().getName());
7378
Sort sort = tree.getSort();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2017 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.mockito.ArgumentMatchers.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.Answers;
25+
import org.mockito.Mock;
26+
import org.mockito.junit.MockitoJUnitRunner;
27+
import org.springframework.data.mongodb.core.index.IndexOperationsProvider;
28+
import org.springframework.data.mongodb.repository.query.PartTreeMongoQuery;
29+
import org.springframework.data.repository.query.parser.PartTree;
30+
31+
/**
32+
* Unit tests for {@link IndexEnsuringQueryCreationListener}.
33+
*
34+
* @author Oliver Gierke
35+
*/
36+
@RunWith(MockitoJUnitRunner.class)
37+
public class IndexEnsuringQueryCreationListenerUnitTests {
38+
39+
IndexEnsuringQueryCreationListener listener;
40+
41+
@Mock IndexOperationsProvider provider;
42+
43+
@Before
44+
public void setUp() {
45+
this.listener = new IndexEnsuringQueryCreationListener(provider);
46+
}
47+
48+
@Test // DATAMONGO-1753
49+
public void skipsQueryCreationForMethodWithoutPredicate() {
50+
51+
PartTree tree = mock(PartTree.class);
52+
when(tree.hasPredicate()).thenReturn(false);
53+
54+
PartTreeMongoQuery query = mock(PartTreeMongoQuery.class, Answers.RETURNS_MOCKS);
55+
when(query.getTree()).thenReturn(tree);
56+
57+
listener.onCreation(query);
58+
59+
verify(provider, times(0)).indexOps(any());
60+
}
61+
62+
interface SampleRepository {
63+
64+
Object findAllBy();
65+
}
66+
}

0 commit comments

Comments
 (0)