| 30 | |
| 31 | |
| 32 | public class FindWithModifierTest extends JongoTestBase { |
| 33 | |
| 34 | private MongoCollection collection; |
| 35 | |
| 36 | @Before |
| 37 | public void setUp() throws Exception { |
| 38 | collection = createEmptyCollection("friends"); |
| 39 | } |
| 40 | |
| 41 | @After |
| 42 | public void tearDown() throws Exception { |
| 43 | dropCollection("friends"); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void canFindWithHint() throws Exception { |
| 48 | /* given */ |
| 49 | Friend noName = new Friend(new ObjectId(), null); |
| 50 | collection.save(noName); |
| 51 | |
| 52 | collection.ensureIndex("{name: 1}", "{sparse: true}"); |
| 53 | |
| 54 | /* when */ |
| 55 | // force to use _id index instead of name index which is sparsed |
| 56 | Iterator<Friend> friends = collection.find().hint("{$natural: 1}").sort("{name: 1}").as(Friend.class); |
| 57 | |
| 58 | /* then */ |
| 59 | assertThat(friends.hasNext()).isTrue(); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void canUseQueryModifier() throws Exception { |
| 64 | /* given */ |
| 65 | collection.save(new Friend(new ObjectId(), "John")); |
| 66 | collection.save(new Friend(new ObjectId(), "Robert")); |
| 67 | |
| 68 | /* when */ |
| 69 | Iterator<Friend> friends = collection.find() |
| 70 | .with(new QueryModifier() { |
| 71 | public void modify(DBCursor cursor) { |
| 72 | cursor.limit(1); |
| 73 | } |
| 74 | }) |
| 75 | .as(Friend.class); |
| 76 | |
| 77 | /* then */ |
| 78 | assertThat(friends.hasNext()).isTrue(); |
| 79 | friends.next(); |
| 80 | assertThat(friends.hasNext()).isFalse(); |
| 81 | } |
| 82 | |
| 83 | } |
nothing calls this directly
no outgoing calls
no test coverage detected