|
1 | 1 | /*
|
2 |
| - * Copyright 2010-2014 the original author or authors. |
| 2 | + * Copyright 2010-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
52 | 52 | import org.springframework.data.mongodb.core.convert.QueryMapper;
|
53 | 53 | import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator;
|
54 | 54 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
| 55 | +import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions; |
55 | 56 | import org.springframework.data.mongodb.core.query.BasicQuery;
|
56 | 57 | import org.springframework.data.mongodb.core.query.Criteria;
|
57 | 58 | import org.springframework.data.mongodb.core.query.NearQuery;
|
|
66 | 67 | import com.mongodb.DBCollection;
|
67 | 68 | import com.mongodb.DBCursor;
|
68 | 69 | import com.mongodb.DBObject;
|
| 70 | +import com.mongodb.MapReduceCommand; |
| 71 | +import com.mongodb.MapReduceOutput; |
69 | 72 | import com.mongodb.Mongo;
|
70 | 73 | import com.mongodb.MongoException;
|
71 | 74 | import com.mongodb.ReadPreference;
|
@@ -422,6 +425,112 @@ public void geoNearShouldIgnoreReadPreferenceWhenNotSet() {
|
422 | 425 | verify(this.db, times(1)).command(Mockito.any(DBObject.class));
|
423 | 426 | }
|
424 | 427 |
|
| 428 | + /** |
| 429 | + * @see DATAMONGO-1334 |
| 430 | + */ |
| 431 | + @Test |
| 432 | + public void mapReduceShouldUseZeroAsDefaultLimit() { |
| 433 | + |
| 434 | + ArgumentCaptor<MapReduceCommand> captor = ArgumentCaptor.forClass(MapReduceCommand.class); |
| 435 | + |
| 436 | + MapReduceOutput output = mock(MapReduceOutput.class); |
| 437 | + when(output.results()).thenReturn(Collections.<DBObject> emptySet()); |
| 438 | + when(collection.mapReduce(Mockito.any(MapReduceCommand.class))).thenReturn(output); |
| 439 | + |
| 440 | + Query query = new BasicQuery("{'foo':'bar'}"); |
| 441 | + |
| 442 | + template.mapReduce(query, "collection", "function(){}", "function(key,values){}", Wrapper.class); |
| 443 | + |
| 444 | + verify(collection).mapReduce(captor.capture()); |
| 445 | + |
| 446 | + assertThat(captor.getValue().getLimit(), is(0)); |
| 447 | + } |
| 448 | + |
| 449 | + /** |
| 450 | + * @see DATAMONGO-1334 |
| 451 | + */ |
| 452 | + @Test |
| 453 | + public void mapReduceShouldPickUpLimitFromQuery() { |
| 454 | + |
| 455 | + ArgumentCaptor<MapReduceCommand> captor = ArgumentCaptor.forClass(MapReduceCommand.class); |
| 456 | + |
| 457 | + MapReduceOutput output = mock(MapReduceOutput.class); |
| 458 | + when(output.results()).thenReturn(Collections.<DBObject> emptySet()); |
| 459 | + when(collection.mapReduce(Mockito.any(MapReduceCommand.class))).thenReturn(output); |
| 460 | + |
| 461 | + Query query = new BasicQuery("{'foo':'bar'}"); |
| 462 | + query.limit(100); |
| 463 | + |
| 464 | + template.mapReduce(query, "collection", "function(){}", "function(key,values){}", Wrapper.class); |
| 465 | + |
| 466 | + verify(collection).mapReduce(captor.capture()); |
| 467 | + |
| 468 | + assertThat(captor.getValue().getLimit(), is(100)); |
| 469 | + } |
| 470 | + |
| 471 | + /** |
| 472 | + * @see DATAMONGO-1334 |
| 473 | + */ |
| 474 | + @Test |
| 475 | + public void mapReduceShouldPickUpLimitFromOptions() { |
| 476 | + |
| 477 | + ArgumentCaptor<MapReduceCommand> captor = ArgumentCaptor.forClass(MapReduceCommand.class); |
| 478 | + |
| 479 | + MapReduceOutput output = mock(MapReduceOutput.class); |
| 480 | + when(output.results()).thenReturn(Collections.<DBObject> emptySet()); |
| 481 | + when(collection.mapReduce(Mockito.any(MapReduceCommand.class))).thenReturn(output); |
| 482 | + |
| 483 | + Query query = new BasicQuery("{'foo':'bar'}"); |
| 484 | + |
| 485 | + template.mapReduce(query, "collection", "function(){}", "function(key,values){}", |
| 486 | + new MapReduceOptions().limit(1000), Wrapper.class); |
| 487 | + |
| 488 | + verify(collection).mapReduce(captor.capture()); |
| 489 | + assertThat(captor.getValue().getLimit(), is(1000)); |
| 490 | + } |
| 491 | + |
| 492 | + /** |
| 493 | + * @see DATAMONGO-1334 |
| 494 | + */ |
| 495 | + @Test |
| 496 | + public void mapReduceShouldPickUpLimitFromOptionsWhenQueryIsNotPresent() { |
| 497 | + |
| 498 | + ArgumentCaptor<MapReduceCommand> captor = ArgumentCaptor.forClass(MapReduceCommand.class); |
| 499 | + |
| 500 | + MapReduceOutput output = mock(MapReduceOutput.class); |
| 501 | + when(output.results()).thenReturn(Collections.<DBObject> emptySet()); |
| 502 | + when(collection.mapReduce(Mockito.any(MapReduceCommand.class))).thenReturn(output); |
| 503 | + |
| 504 | + template.mapReduce("collection", "function(){}", "function(key,values){}", new MapReduceOptions().limit(1000), |
| 505 | + Wrapper.class); |
| 506 | + |
| 507 | + verify(collection).mapReduce(captor.capture()); |
| 508 | + assertThat(captor.getValue().getLimit(), is(1000)); |
| 509 | + } |
| 510 | + |
| 511 | + /** |
| 512 | + * @see DATAMONGO-1334 |
| 513 | + */ |
| 514 | + @Test |
| 515 | + public void mapReduceShouldPickUpLimitFromOptionsEvenWhenQueryDefinesItDifferently() { |
| 516 | + |
| 517 | + ArgumentCaptor<MapReduceCommand> captor = ArgumentCaptor.forClass(MapReduceCommand.class); |
| 518 | + |
| 519 | + MapReduceOutput output = mock(MapReduceOutput.class); |
| 520 | + when(output.results()).thenReturn(Collections.<DBObject> emptySet()); |
| 521 | + when(collection.mapReduce(Mockito.any(MapReduceCommand.class))).thenReturn(output); |
| 522 | + |
| 523 | + Query query = new BasicQuery("{'foo':'bar'}"); |
| 524 | + query.limit(100); |
| 525 | + |
| 526 | + template.mapReduce(query, "collection", "function(){}", "function(key,values){}", |
| 527 | + new MapReduceOptions().limit(1000), Wrapper.class); |
| 528 | + |
| 529 | + verify(collection).mapReduce(captor.capture()); |
| 530 | + |
| 531 | + assertThat(captor.getValue().getLimit(), is(1000)); |
| 532 | + } |
| 533 | + |
425 | 534 | class AutogenerateableId {
|
426 | 535 |
|
427 | 536 | @Id BigInteger id;
|
|
0 commit comments