use SimpleConversion for java.sql mappings

This commit is contained in:
Filip Hrisafov 2016-08-24 22:15:34 +02:00 committed by Gunnar Morling
parent f88fd6ece9
commit c5dc2ccb92
8 changed files with 228 additions and 5 deletions

View File

@ -22,6 +22,8 @@ import static org.mapstruct.ap.internal.conversion.ReverseConversion.reverse;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
@ -189,6 +191,9 @@ public class Conversions {
register( Enum.class, String.class, new EnumStringConversion() );
register( Date.class, String.class, new DateToStringConversion() );
register( BigDecimal.class, BigInteger.class, new BigDecimalToBigIntegerConversion() );
register( Date.class, Time.class, new DateToSqlTimeConversion() );
register( Date.class, java.sql.Date.class, new DateToSqlDateConversion() );
register( Date.class, Timestamp.class, new DateToSqlTimestampConversion() );
}
private void registerJodaConversions() {

View File

@ -0,0 +1,40 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.internal.conversion;
import org.mapstruct.ap.internal.model.common.ConversionContext;
/**
* Conversion between {@link java.util.Date} and {@link java.sql.Date}.
*
* @author Filip Hrisafov
*/
public class DateToSqlDateConversion extends SimpleConversion {
@Override
protected String getToExpression(ConversionContext conversionContext) {
return "new java.sql.Date( <SOURCE>.getTime() )";
}
@Override
protected String getFromExpression(ConversionContext conversionContext) {
return "<SOURCE>";
}
}

View File

@ -0,0 +1,39 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.internal.conversion;
import org.mapstruct.ap.internal.model.common.ConversionContext;
/**
* Conversion between {@link java.util.Date} and {@link java.sql.Time}.
*
* @author Filip Hrisafov
*/
public class DateToSqlTimeConversion extends SimpleConversion {
@Override
protected String getToExpression(ConversionContext conversionContext) {
return "new java.sql.Time( <SOURCE>.getTime() )";
}
@Override
protected String getFromExpression(ConversionContext conversionContext) {
return "<SOURCE>";
}
}

View File

@ -0,0 +1,39 @@
/**
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.internal.conversion;
import org.mapstruct.ap.internal.model.common.ConversionContext;
/**
* Conversion between {@link java.util.Date} and {@link java.sql.Timestamp}.
*
* @author Filip Hrisafov
*/
public class DateToSqlTimestampConversion extends SimpleConversion {
@Override
protected String getToExpression(ConversionContext conversionContext) {
return "new java.sql.Timestamp( <SOURCE>.getTime() )";
}
@Override
protected String getFromExpression(ConversionContext conversionContext) {
return "<SOURCE>";
}
}

View File

@ -41,11 +41,7 @@ public class BuiltInMappingMethods {
new StringToXmlGregorianCalendar( typeFactory ),
new XmlGregorianCalendarToString( typeFactory ),
new CalendarToXmlGregorianCalendar( typeFactory ),
new XmlGregorianCalendarToCalendar( typeFactory ),
new DateToSqlDate( typeFactory ),
new DateToSqlTime( typeFactory ),
new DateToSqlTimestamp( typeFactory )
new XmlGregorianCalendarToCalendar( typeFactory )
);
if ( isJaxbAvailable( typeFactory ) ) {

View File

@ -20,7 +20,10 @@ package org.mapstruct.ap.test.conversion.date;
import static org.assertj.core.api.Assertions.assertThat;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
@ -161,4 +164,46 @@ public class DateConversionTest {
new GregorianCalendar( 2013, 3, 11 ).getTime()
} );
}
@IssueKey("858")
@Test
public void shouldApplyDateToSqlConversion() throws Exception {
GregorianCalendar time = new GregorianCalendar( 2016, Calendar.AUGUST, 24, 20, 30, 30 );
GregorianCalendar sqlDate = new GregorianCalendar( 2016, Calendar.AUGUST, 23, 21, 35, 35 );
GregorianCalendar timestamp = new GregorianCalendar( 2016, Calendar.AUGUST, 22, 21, 35, 35 );
Source source = new Source();
source.setTime( time.getTime() );
source.setSqlDate( sqlDate.getTime() );
source.setTimestamp( timestamp.getTime() );
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
Time expectedTime = new Time( time.getTime().getTime() );
java.sql.Date expectedSqlDate = new java.sql.Date( sqlDate.getTime().getTime() );
Timestamp expectedTimestamp = new Timestamp( timestamp.getTime().getTime() );
assertThat( target.getTime() ).isEqualTo( expectedTime );
assertThat( target.getSqlDate() ).isEqualTo( expectedSqlDate );
assertThat( target.getTimestamp() ).isEqualTo( expectedTimestamp );
}
@IssueKey("858")
@Test
public void shouldApplySqlToDateConversion() throws Exception {
Target target = new Target();
GregorianCalendar time = new GregorianCalendar( 2016, Calendar.AUGUST, 24, 20, 30, 30 );
GregorianCalendar sqlDate = new GregorianCalendar( 2016, Calendar.AUGUST, 23, 21, 35, 35 );
GregorianCalendar timestamp = new GregorianCalendar( 2016, Calendar.AUGUST, 22, 21, 35, 35 );
target.setTime( new Time( time.getTime().getTime() ) );
target.setSqlDate( new java.sql.Date( sqlDate.getTime().getTime() ) );
target.setTimestamp( new Timestamp( timestamp.getTime().getTime() ) );
Source source = SourceTargetMapper.INSTANCE.targetToSource( target );
assertThat( source ).isNotNull();
assertThat( source.getTime() ).isEqualTo( target.getTime() );
assertThat( source.getSqlDate() ).isEqualTo( target.getSqlDate() );
assertThat( source.getTimestamp() ).isEqualTo( target.getTimestamp() );
}
}

View File

@ -24,6 +24,9 @@ public class Source {
private Date date;
private Date anotherDate;
private Date time;
private Date sqlDate;
private Date timestamp;
public Date getDate() {
return date;
@ -40,4 +43,29 @@ public class Source {
public void setAnotherDate(Date anotherDate) {
this.anotherDate = anotherDate;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public Date getSqlDate() {
return sqlDate;
}
public void setSqlDate(Date sqlDate) {
this.sqlDate = sqlDate;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}

View File

@ -18,10 +18,17 @@
*/
package org.mapstruct.ap.test.conversion.date;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
public class Target {
private String date;
private String anotherDate;
private Time time;
private Date sqlDate;
private Timestamp timestamp;
public String getDate() {
return date;
@ -38,4 +45,28 @@ public class Target {
public void setAnotherDate(String anotherDate) {
this.anotherDate = anotherDate;
}
public Time getTime() {
return time;
}
public void setTime(Time time) {
this.time = time;
}
public Date getSqlDate() {
return sqlDate;
}
public void setSqlDate(Date sqlDate) {
this.sqlDate = sqlDate;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
}