@Test public void convertsJodaTimeTypesCorrectly() { List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>(); converters.add(new LocalDateToDateConverter()); converters.add(new DateToLocalDateConverter()); List<Class<?>> customSimpleTypes = new ArrayList<Class<?>>(); customSimpleTypes.add(LocalDate.class); mappingContext.setCustomSimpleTypes(customSimpleTypes); converter = new MappingMongoConverter(mappingContext); converter.setConverters(converters); converter.afterPropertiesSet(); Person person = new Person(); person.birthDate = new LocalDate(); DBObject dbObject = new BasicDBObject(); converter.write(person, dbObject); assertTrue(dbObject.get("birthDate") instanceof Date); Person result = converter.read(Person.class, dbObject); assertThat(result.birthDate, is(notNullValue())); }
/** @see DATADOC-128 */ @Test public void ignoresDocumentsStoredTypeIfCompletelyDifferentTypeRequested() { DBObject dbObject = new BasicDBObject(); dbObject.put("birthDate", new LocalDate()); dbObject.put(MappingMongoConverter.CUSTOM_TYPE_KEY, Person.class.getName()); assertThat(converter.read(BirthDateContainer.class, dbObject), is(BirthDateContainer.class)); }
/** @see DATADOC-128 */ @Test public void usesDocumentsStoredTypeIfSubtypeOfRequest() { DBObject dbObject = new BasicDBObject(); dbObject.put("birthDate", new LocalDate()); dbObject.put(MappingMongoConverter.CUSTOM_TYPE_KEY, Person.class.getName()); assertThat(converter.read(Contact.class, dbObject), is(Person.class)); }
/** @see DATADOC-130 */ @Test public void convertsMapTypeCorrectly() { Map<Locale, String> map = Collections.singletonMap(Locale.US, "Foo"); BasicDBObject dbObject = new BasicDBObject(); converter.write(map, dbObject); assertThat(dbObject.get(Locale.US.toString()).toString(), is("Foo")); }
@Test public void writesTypeDiscriminatorIntoRootObject() { Person person = new Person(); person.birthDate = new LocalDate(); DBObject result = new BasicDBObject(); converter.write(person, result); assertThat(result.containsField(MappingMongoConverter.CUSTOM_TYPE_KEY), is(true)); assertThat( result.get(MappingMongoConverter.CUSTOM_TYPE_KEY).toString(), is(Person.class.getName())); }
@Test public void convertsAddressCorrectly() { Address address = new Address(); address.city = "New York"; address.street = "Broadway"; DBObject dbObject = new BasicDBObject(); converter.write(address, dbObject); assertThat(dbObject.get("city").toString(), is("New York")); assertThat(dbObject.get("street").toString(), is("Broadway")); }