|
37 | 37 | import java.util.List;
|
38 | 38 | import java.util.Locale;
|
39 | 39 | import java.util.Map;
|
| 40 | +import java.util.UUID; |
40 | 41 |
|
41 | 42 | import org.bson.types.ObjectId;
|
42 | 43 | import org.joda.time.DateTime;
|
|
47 | 48 | import org.junit.rules.ExpectedException;
|
48 | 49 | import org.junit.runner.RunWith;
|
49 | 50 | import org.springframework.beans.factory.annotation.Autowired;
|
| 51 | +import org.springframework.context.ConfigurableApplicationContext; |
50 | 52 | import org.springframework.core.convert.converter.Converter;
|
51 | 53 | import org.springframework.dao.DataAccessException;
|
52 | 54 | import org.springframework.dao.DataIntegrityViolationException;
|
|
74 | 76 | import org.springframework.data.mongodb.core.index.IndexInfo;
|
75 | 77 | import org.springframework.data.mongodb.core.mapping.Field;
|
76 | 78 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
| 79 | +import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; |
| 80 | +import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent; |
77 | 81 | import org.springframework.data.mongodb.core.query.BasicQuery;
|
78 | 82 | import org.springframework.data.mongodb.core.query.Criteria;
|
79 | 83 | import org.springframework.data.mongodb.core.query.Query;
|
80 | 84 | import org.springframework.data.mongodb.core.query.Update;
|
81 | 85 | import org.springframework.data.util.CloseableIterator;
|
| 86 | +import org.springframework.test.annotation.DirtiesContext; |
82 | 87 | import org.springframework.test.context.ContextConfiguration;
|
83 | 88 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
84 | 89 | import org.springframework.util.ObjectUtils;
|
@@ -123,6 +128,7 @@ public class MongoTemplateTests {
|
123 | 128 |
|
124 | 129 | @Autowired MongoTemplate template;
|
125 | 130 | @Autowired MongoDbFactory factory;
|
| 131 | + @Autowired ConfigurableApplicationContext context; |
126 | 132 |
|
127 | 133 | MongoTemplate mappingTemplate;
|
128 | 134 | org.springframework.data.util.Version mongoVersion;
|
@@ -3442,6 +3448,28 @@ public void shouldFetchMapOfLazyReferencesCorrectly() {
|
3442 | 3448 | assertThat(target.lazyDbRefAnnotatedMap.values(), contains(two, one));
|
3443 | 3449 | }
|
3444 | 3450 |
|
| 3451 | + /** |
| 3452 | + * @see DATAMONGO-1513 |
| 3453 | + */ |
| 3454 | + @Test |
| 3455 | + @DirtiesContext |
| 3456 | + public void populatesIdsAddedByEventListener() { |
| 3457 | + |
| 3458 | + context.addApplicationListener(new AbstractMongoEventListener<Document>() { |
| 3459 | + |
| 3460 | + @Override |
| 3461 | + public void onBeforeSave(BeforeSaveEvent<Document> event) { |
| 3462 | + event.getDBObject().put("_id", UUID.randomUUID().toString()); |
| 3463 | + } |
| 3464 | + }); |
| 3465 | + |
| 3466 | + Document document = new Document(); |
| 3467 | + |
| 3468 | + template.insertAll(Arrays.asList(document)); |
| 3469 | + |
| 3470 | + assertThat(document.id, is(notNullValue())); |
| 3471 | + } |
| 3472 | + |
3445 | 3473 | static class TypeWithNumbers {
|
3446 | 3474 |
|
3447 | 3475 | @Id String id;
|
|
0 commit comments