|
1 | 1 | /*
|
2 |
| - * Copyright 2011-2014 the original author or authors. |
| 2 | + * Copyright 2011-2016 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.
|
|
19 | 19 | import static org.junit.Assert.*;
|
20 | 20 | import static org.springframework.data.mongodb.core.DBObjectTestUtils.*;
|
21 | 21 |
|
| 22 | +import java.util.Collections; |
| 23 | + |
22 | 24 | import org.bson.types.ObjectId;
|
23 | 25 | import org.hamcrest.Matchers;
|
24 | 26 | import org.junit.Before;
|
25 | 27 | import org.junit.Test;
|
26 | 28 | import org.junit.runner.RunWith;
|
27 | 29 | import org.mockito.Mock;
|
28 | 30 | import org.mockito.runners.MockitoJUnitRunner;
|
| 31 | +import org.springframework.core.convert.converter.Converter; |
| 32 | +import org.springframework.data.convert.WritingConverter; |
| 33 | +import org.springframework.data.mongodb.core.convert.CustomConversions; |
29 | 34 | import org.springframework.data.mongodb.core.convert.DbRefResolver;
|
30 | 35 | import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
31 | 36 | import org.springframework.data.mongodb.core.convert.MongoConverter;
|
32 | 37 | import org.springframework.data.mongodb.core.mapping.Field;
|
33 | 38 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
| 39 | +import org.springframework.data.mongodb.repository.Person.Sex; |
34 | 40 | import org.springframework.data.mongodb.repository.QAddress;
|
35 | 41 | import org.springframework.data.mongodb.repository.QPerson;
|
36 | 42 |
|
@@ -172,10 +178,52 @@ public void shouldConvertCollectionOfObjectIdEvenWhenNestedInOperatorDbObject()
|
172 | 178 | assertThat($in, Matchers.<Object> arrayContaining(firstId, secondId));
|
173 | 179 | }
|
174 | 180 |
|
| 181 | + /** |
| 182 | + * @see DATAMONGO-1485 |
| 183 | + */ |
| 184 | + @Test |
| 185 | + public void takesCustomConversionForEnumsIntoAccount() { |
| 186 | + |
| 187 | + MongoMappingContext context = new MongoMappingContext(); |
| 188 | + |
| 189 | + MappingMongoConverter converter = new MappingMongoConverter(dbFactory, context); |
| 190 | + converter.setCustomConversions(new CustomConversions(Collections.singletonList(new SexTypeWriteConverter()))); |
| 191 | + converter.afterPropertiesSet(); |
| 192 | + |
| 193 | + this.converter = converter; |
| 194 | + this.serializer = new SpringDataMongodbSerializer(this.converter); |
| 195 | + |
| 196 | + Object mappedPredicate = this.serializer.handle(QPerson.person.sex.eq(Sex.FEMALE)); |
| 197 | + |
| 198 | + assertThat(mappedPredicate, is(instanceOf(DBObject.class))); |
| 199 | + assertThat(((DBObject) mappedPredicate).get("sex"), is((Object) "f")); |
| 200 | + } |
| 201 | + |
175 | 202 | class Address {
|
176 | 203 | String id;
|
177 | 204 | String street;
|
178 | 205 | @Field("zip_code") String zipCode;
|
179 | 206 | @Field("bar") String[] foo;
|
180 | 207 | }
|
| 208 | + |
| 209 | + @WritingConverter |
| 210 | + public class SexTypeWriteConverter implements Converter<Sex, String> { |
| 211 | + |
| 212 | + @Override |
| 213 | + public String convert(Sex source) { |
| 214 | + |
| 215 | + if (source == null) { |
| 216 | + return null; |
| 217 | + } |
| 218 | + |
| 219 | + switch (source) { |
| 220 | + case MALE: |
| 221 | + return "m"; |
| 222 | + case FEMALE: |
| 223 | + return "f"; |
| 224 | + default: |
| 225 | + throw new IllegalArgumentException("o_O"); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
181 | 229 | }
|
0 commit comments