Attempting to get a JodaTime to Date Converter working

889 views
Skip to first unread message

Carl

unread,
Nov 11, 2011, 12:01:05 PM11/11/11
to modelmapper
I'm trying to get a JodaTime to java.util.Date Converter to work with
no success.

The following unit test shows what I'm trying to do:

import static org.fest.assertions.Assertions.*;

import java.util.Date;

import org.joda.time.DateTime;
import org.junit.Test;
import org.modelmapper.AbstractConverter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;

public class ModelMapperTest {

static class Source {
private DateTime dateProperty;

public DateTime getDateProperty() {
return this.dateProperty;
}

public void setDateProperty(DateTime dateProperty) {
this.dateProperty = dateProperty;
}
}

static class Destination {
private Date dateProperty;

public Date getDateProperty() {
return this.dateProperty;
}

public void setDateProperty(Date dateProperty) {
this.dateProperty = dateProperty;
}
}

@Test
public void testJodaTimeConversion() {
Source source = new Source();
source.setDateProperty(new DateTime());

ModelMapper modelMapper = new ModelMapper();
final AbstractConverter<DateTime, Date> jodaTimeConverter =
new AbstractConverter<DateTime, Date>() {
@Override
protected Date convert(DateTime source) {
return source == null ? null : source.toDate();
}
};
modelMapper.addConverter(jodaTimeConverter);

Destination destination = modelMapper.map(source,
Destination.class);

assertThat(destination.getDateProperty()).isEqualTo(source.getDateProperty().toDate());
}
}

but I get the following error:

ModelMapper configuration errors:

1) The destination property java.util.Date.setMonth() matches multiple
source property hierarchies:

ModelMapperTest$Source.getDateProperty()/
org.joda.time.base.AbstractDateTime.getDayOfMonth()
com.netapp.dfm.webui.server.mapping.ModelMapperTest
$Source.getDateProperty()/
org.joda.time.base.AbstractDateTime.getMonthOfYear()

1 error
org.modelmapper.ConfigurationException: ModelMapper configuration
errors:

1) The destination property java.util.Date.setMonth() matches multiple
source property hierarchies:

ModelMapperTest$Source.getDateProperty()/
org.joda.time.base.AbstractDateTime.getDayOfMonth()
ModelMapperTest$Source.getDateProperty()/
org.joda.time.base.AbstractDateTime.getMonthOfYear()

1 error
at
org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:
229)
at
org.modelmapper.internal.PropertyMappingBuilder.matchDestination(PropertyMappingBuilder.java:
122)
at
org.modelmapper.internal.PropertyMappingBuilder.matchDestination(PropertyMappingBuilder.java:
112)
at
org.modelmapper.internal.PropertyMappingBuilder.matchDestination(PropertyMappingBuilder.java:
112)
at
org.modelmapper.internal.PropertyMappingBuilder.build(PropertyMappingBuilder.java:
74)
at
org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:
95)
at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:
93)
at ModelMapperTest.testJodaTimeConversion(ModelMapperTest.java:
59)



It would appear my converter is not getting invoked.

I've tried adding
modelMapper.addMappings(new PropertyMap<Source, Destination>()
{
@Override
protected void configure() {

using(jodaTimeConverter).map(source).setDateProperty(null);
}
});

but with the same result.

Any ideas?

Thanks in advance.

- Carl

Krzysztof Konwisarz

unread,
Apr 10, 2013, 8:19:13 AM4/10/13
to model...@googlegroups.com, carl....@gmail.com
Hey,

upping this one. Having exaclty the same issues with 0.5.6. Is the approach not right? It seems that modelmapper is looking for a method to get Date from DateTime (and it finds two, hence confusion) without actually using the converter.

@Carl - have you maybe got it working nicely?

Jonathan Halterman

unread,
Apr 10, 2013, 12:33:20 PM4/10/13
to model...@googlegroups.com, carl....@gmail.com
Krzysztof,

Carl's test passes using 0.5.6. Are you seeing different results with a similar use case?

- jonathan

Krzysztof Konwisarz

unread,
Apr 11, 2013, 3:24:39 AM4/11/13
to model...@googlegroups.com
Hi,

using Carl's test I've found the difference. My code was configured through calling using(converter) in a PropertyMap. This worked for me for Date->DateTime conversion, but not the other way round. Seems this way should be working too, right? Anyway, configuring mapper with converter directly will work and look better for me, so I'm good :)

Here is the test for it:

import org.joda.time.DateTime;
import org.junit.Test;
import org.modelmapper.AbstractConverter;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;

import java.util.Date;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class ModelMapperTest {

    static class Source {
        private DateTime dateProperty;

        public DateTime getDateProperty() {
            return this.dateProperty;
        }

        public void setDateProperty(DateTime dateProperty) {
            this.dateProperty = dateProperty;
        }
    }

    static class Destination {
        private Date dateProperty;

        public Date getDateProperty() {
            return this.dateProperty;
        }

        public void setDateProperty(Date dateProperty) {
            this.dateProperty = dateProperty;
        }
    }

    @Test
    public void testJodaTimeConversion() {
        Source source = new Source();
        source.setDateProperty(new DateTime());

        ModelMapper modelMapper = new ModelMapper();
        final AbstractConverter<DateTime, Date> jodaTimeConverter =
                new AbstractConverter<DateTime, Date>() {
                    @Override
                    protected Date convert(DateTime source) {
                        return source == null ? null : source.toDate();
                    }
                };

        PropertyMap propertyMap = new  PropertyMap<Source, Destination>() {
            @Override
            protected void configure() {
                using(jodaTimeConverter).map(source.getDateProperty()).setDateProperty(null);
            }
        };

        Destination destination = modelMapper.map(source, Destination.class);

        assertThat(destination.getDateProperty(), equalTo(source.getDateProperty().toDate()));
    }
}


--
You received this message because you are subscribed to a topic in the Google Groups "modelmapper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/modelmapper/8uOCNR54O7U/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to modelmapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages