|
33 | 33 | import java.util.HashSet;
|
34 | 34 | import java.util.List;
|
35 | 35 | import java.util.Map;
|
| 36 | +import java.util.UUID; |
36 | 37 |
|
37 | 38 | import org.bson.types.ObjectId;
|
38 | 39 | import org.joda.time.DateTime;
|
|
43 | 44 | import org.junit.rules.ExpectedException;
|
44 | 45 | import org.junit.runner.RunWith;
|
45 | 46 | import org.springframework.beans.factory.annotation.Autowired;
|
| 47 | +import org.springframework.context.ConfigurableApplicationContext; |
46 | 48 | import org.springframework.core.convert.converter.Converter;
|
47 | 49 | import org.springframework.dao.DataAccessException;
|
48 | 50 | import org.springframework.dao.DataIntegrityViolationException;
|
|
70 | 72 | import org.springframework.data.mongodb.core.index.IndexInfo;
|
71 | 73 | import org.springframework.data.mongodb.core.mapping.Field;
|
72 | 74 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
| 75 | +import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener; |
| 76 | +import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent; |
73 | 77 | import org.springframework.data.mongodb.core.query.BasicQuery;
|
74 | 78 | import org.springframework.data.mongodb.core.query.Criteria;
|
75 | 79 | import org.springframework.data.mongodb.core.query.Query;
|
76 | 80 | import org.springframework.data.mongodb.core.query.Update;
|
77 | 81 | import org.springframework.data.util.CloseableIterator;
|
| 82 | +import org.springframework.test.annotation.DirtiesContext; |
78 | 83 | import org.springframework.test.context.ContextConfiguration;
|
79 | 84 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
80 | 85 | import org.springframework.util.ObjectUtils;
|
@@ -114,6 +119,7 @@ public class MongoTemplateTests {
|
114 | 119 |
|
115 | 120 | @Autowired MongoTemplate template;
|
116 | 121 | @Autowired MongoDbFactory factory;
|
| 122 | + @Autowired ConfigurableApplicationContext context; |
117 | 123 |
|
118 | 124 | MongoTemplate mappingTemplate;
|
119 | 125 | org.springframework.data.util.Version mongoVersion;
|
@@ -3164,6 +3170,28 @@ public void updateShouldWorkForTypesContainingGeoJsonTypes() {
|
3164 | 3170 | assertThat(template.findOne(query(where("id").is(wgj.id)), WithGeoJson.class).point, is(equalTo(wgj.point)));
|
3165 | 3171 | }
|
3166 | 3172 |
|
| 3173 | + /** |
| 3174 | + * @see DATAMONGO-1513 |
| 3175 | + */ |
| 3176 | + @Test |
| 3177 | + @DirtiesContext |
| 3178 | + public void populatesIdsAddedByEventListener() { |
| 3179 | + |
| 3180 | + context.addApplicationListener(new AbstractMongoEventListener<Document>() { |
| 3181 | + |
| 3182 | + @Override |
| 3183 | + public void onBeforeSave(BeforeSaveEvent<Document> event) { |
| 3184 | + event.getDBObject().put("_id", UUID.randomUUID().toString()); |
| 3185 | + } |
| 3186 | + }); |
| 3187 | + |
| 3188 | + Document document = new Document(); |
| 3189 | + |
| 3190 | + template.insertAll(Arrays.asList(document)); |
| 3191 | + |
| 3192 | + assertThat(document.id, is(notNullValue())); |
| 3193 | + } |
| 3194 | + |
3167 | 3195 | static class DoucmentWithNamedIdField {
|
3168 | 3196 |
|
3169 | 3197 | @Id String someIdKey;
|
|
0 commit comments