mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#858: Add built in methods for java.sql.Time, java.sql.Timestamp and java.sql.Date
This commit is contained in:
parent
5927431791
commit
f88fd6ece9
@ -41,7 +41,10 @@ public class BuiltInMappingMethods {
|
|||||||
new StringToXmlGregorianCalendar( typeFactory ),
|
new StringToXmlGregorianCalendar( typeFactory ),
|
||||||
new XmlGregorianCalendarToString( typeFactory ),
|
new XmlGregorianCalendarToString( typeFactory ),
|
||||||
new CalendarToXmlGregorianCalendar( typeFactory ),
|
new CalendarToXmlGregorianCalendar( typeFactory ),
|
||||||
new XmlGregorianCalendarToCalendar( typeFactory )
|
new XmlGregorianCalendarToCalendar( typeFactory ),
|
||||||
|
new DateToSqlDate( typeFactory ),
|
||||||
|
new DateToSqlTime( typeFactory ),
|
||||||
|
new DateToSqlTimestamp( typeFactory )
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
/**
|
||||||
|
* 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.model.source.builtin;
|
||||||
|
|
||||||
|
import static org.mapstruct.ap.internal.util.Collections.asSet;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||||
|
import org.mapstruct.ap.internal.model.common.Type;
|
||||||
|
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class DateToSqlDate extends BuiltInMethod {
|
||||||
|
|
||||||
|
private final Parameter parameter;
|
||||||
|
private final Type returnType;
|
||||||
|
private final Set<Type> importTypes;
|
||||||
|
|
||||||
|
public DateToSqlDate(TypeFactory typeFactory) {
|
||||||
|
this.parameter = new Parameter( "date", typeFactory.getType( Date.class ) );
|
||||||
|
this.returnType = typeFactory.getType( java.sql.Date.class );
|
||||||
|
|
||||||
|
this.importTypes = asSet(
|
||||||
|
parameter.getType()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doTypeVarsMatch(Type parameter, Type returnType) {
|
||||||
|
//We must make sure that there is only a match when the types are equal. The reason for this is to avoid
|
||||||
|
//ambiguous problems, when the return type is java.util.Date
|
||||||
|
return getReturnType().equals( returnType );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Type> getImportTypes() {
|
||||||
|
return importTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Parameter getParameter() {
|
||||||
|
return parameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getReturnType() {
|
||||||
|
return returnType;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
/**
|
||||||
|
* 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.model.source.builtin;
|
||||||
|
|
||||||
|
import static org.mapstruct.ap.internal.util.Collections.asSet;
|
||||||
|
|
||||||
|
import java.sql.Time;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||||
|
import org.mapstruct.ap.internal.model.common.Type;
|
||||||
|
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class DateToSqlTime extends BuiltInMethod {
|
||||||
|
|
||||||
|
private final Parameter parameter;
|
||||||
|
private final Type returnType;
|
||||||
|
private final Set<Type> importTypes;
|
||||||
|
|
||||||
|
public DateToSqlTime(TypeFactory typeFactory) {
|
||||||
|
this.parameter = new Parameter( "date", typeFactory.getType( Date.class ) );
|
||||||
|
this.returnType = typeFactory.getType( Time.class );
|
||||||
|
|
||||||
|
this.importTypes = asSet(
|
||||||
|
parameter.getType(),
|
||||||
|
returnType
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doTypeVarsMatch(Type parameter, Type returnType) {
|
||||||
|
//We must make sure that there is only a match when the types are equal. The reason for this is to avoid
|
||||||
|
//ambiguous problems, when the return type is java.util.Date
|
||||||
|
return getReturnType().equals( returnType );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Type> getImportTypes() {
|
||||||
|
return importTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Parameter getParameter() {
|
||||||
|
return parameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getReturnType() {
|
||||||
|
return returnType;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
/**
|
||||||
|
* 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.model.source.builtin;
|
||||||
|
|
||||||
|
import static org.mapstruct.ap.internal.util.Collections.asSet;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||||
|
import org.mapstruct.ap.internal.model.common.Type;
|
||||||
|
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class DateToSqlTimestamp extends BuiltInMethod {
|
||||||
|
|
||||||
|
private final Parameter parameter;
|
||||||
|
private final Type returnType;
|
||||||
|
private final Set<Type> importTypes;
|
||||||
|
|
||||||
|
public DateToSqlTimestamp(TypeFactory typeFactory) {
|
||||||
|
this.parameter = new Parameter( "date", typeFactory.getType( Date.class ) );
|
||||||
|
this.returnType = typeFactory.getType( Timestamp.class );
|
||||||
|
|
||||||
|
this.importTypes = asSet(
|
||||||
|
parameter.getType(),
|
||||||
|
returnType
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doTypeVarsMatch(Type parameter, Type returnType) {
|
||||||
|
//We must make sure that there is only a match when the types are equal. The reason for this is to avoid
|
||||||
|
//ambiguous problems, when the return type is java.util.Date
|
||||||
|
return getReturnType().equals( returnType );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Type> getImportTypes() {
|
||||||
|
return importTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Parameter getParameter() {
|
||||||
|
return parameter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getReturnType() {
|
||||||
|
return returnType;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<#--
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
-->
|
||||||
|
private java.sql.Date ${name}( Date date ) {
|
||||||
|
if ( date == null ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new java.sql.Date(date.getTime());
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<#--
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
-->
|
||||||
|
private Time ${name}( Date date ) {
|
||||||
|
if ( date == null ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Time(date.getTime());
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
<#--
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
-->
|
||||||
|
private Timestamp ${name}( Date date ) {
|
||||||
|
if ( date == null ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Timestamp(date.getTime());
|
||||||
|
}
|
@ -20,6 +20,8 @@ package org.mapstruct.ap.test.builtin;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.sql.Time;
|
||||||
|
import java.sql.Timestamp;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
@ -52,6 +54,8 @@ import org.mapstruct.ap.test.builtin.bean.DateProperty;
|
|||||||
import org.mapstruct.ap.test.builtin.bean.JaxbElementListProperty;
|
import org.mapstruct.ap.test.builtin.bean.JaxbElementListProperty;
|
||||||
import org.mapstruct.ap.test.builtin.bean.JaxbElementProperty;
|
import org.mapstruct.ap.test.builtin.bean.JaxbElementProperty;
|
||||||
import org.mapstruct.ap.test.builtin.bean.SqlDateProperty;
|
import org.mapstruct.ap.test.builtin.bean.SqlDateProperty;
|
||||||
|
import org.mapstruct.ap.test.builtin.bean.SqlTimeProperty;
|
||||||
|
import org.mapstruct.ap.test.builtin.bean.SqlTimestampProperty;
|
||||||
import org.mapstruct.ap.test.builtin.bean.StringListProperty;
|
import org.mapstruct.ap.test.builtin.bean.StringListProperty;
|
||||||
import org.mapstruct.ap.test.builtin.bean.StringProperty;
|
import org.mapstruct.ap.test.builtin.bean.StringProperty;
|
||||||
import org.mapstruct.ap.test.builtin.bean.XmlGregorianCalendarProperty;
|
import org.mapstruct.ap.test.builtin.bean.XmlGregorianCalendarProperty;
|
||||||
@ -62,12 +66,14 @@ import org.mapstruct.ap.test.builtin.mapper.CalendarToDateMapper;
|
|||||||
import org.mapstruct.ap.test.builtin.mapper.CalendarToStringMapper;
|
import org.mapstruct.ap.test.builtin.mapper.CalendarToStringMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.CalendarToXmlGregCalMapper;
|
import org.mapstruct.ap.test.builtin.mapper.CalendarToXmlGregCalMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.DateToCalendarMapper;
|
import org.mapstruct.ap.test.builtin.mapper.DateToCalendarMapper;
|
||||||
|
import org.mapstruct.ap.test.builtin.mapper.DateToSqlDateMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.DateToXmlGregCalMapper;
|
import org.mapstruct.ap.test.builtin.mapper.DateToXmlGregCalMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.ErroneousSourceTargetWithSqlDateMapper;
|
|
||||||
import org.mapstruct.ap.test.builtin.mapper.IterableSourceTargetMapper;
|
import org.mapstruct.ap.test.builtin.mapper.IterableSourceTargetMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.JaxbListMapper;
|
import org.mapstruct.ap.test.builtin.mapper.JaxbListMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.JaxbMapper;
|
import org.mapstruct.ap.test.builtin.mapper.JaxbMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.MapSourceTargetMapper;
|
import org.mapstruct.ap.test.builtin.mapper.MapSourceTargetMapper;
|
||||||
|
import org.mapstruct.ap.test.builtin.mapper.SqlTimeMapper;
|
||||||
|
import org.mapstruct.ap.test.builtin.mapper.SqlTimestampMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.StringToCalendarMapper;
|
import org.mapstruct.ap.test.builtin.mapper.StringToCalendarMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.StringToXmlGregCalMapper;
|
import org.mapstruct.ap.test.builtin.mapper.StringToXmlGregCalMapper;
|
||||||
import org.mapstruct.ap.test.builtin.mapper.XmlGregCalToCalendarMapper;
|
import org.mapstruct.ap.test.builtin.mapper.XmlGregCalToCalendarMapper;
|
||||||
@ -77,9 +83,6 @@ import org.mapstruct.ap.test.builtin.source.IterableSource;
|
|||||||
import org.mapstruct.ap.test.builtin.source.MapSource;
|
import org.mapstruct.ap.test.builtin.source.MapSource;
|
||||||
import org.mapstruct.ap.testutil.IssueKey;
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
import org.mapstruct.ap.testutil.WithClasses;
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
|
|
||||||
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
|
|
||||||
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
|
|
||||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -91,6 +94,8 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
|||||||
IterableTarget.class,
|
IterableTarget.class,
|
||||||
MapTarget.class,
|
MapTarget.class,
|
||||||
SqlDateProperty.class,
|
SqlDateProperty.class,
|
||||||
|
SqlTimeProperty.class,
|
||||||
|
SqlTimestampProperty.class,
|
||||||
CalendarProperty.class,
|
CalendarProperty.class,
|
||||||
DateProperty.class,
|
DateProperty.class,
|
||||||
JaxbElementListProperty.class,
|
JaxbElementListProperty.class,
|
||||||
@ -106,6 +111,9 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
|||||||
CalendarToXmlGregCalMapper.class,
|
CalendarToXmlGregCalMapper.class,
|
||||||
DateToCalendarMapper.class,
|
DateToCalendarMapper.class,
|
||||||
DateToXmlGregCalMapper.class,
|
DateToXmlGregCalMapper.class,
|
||||||
|
DateToSqlDateMapper.class,
|
||||||
|
SqlTimeMapper.class,
|
||||||
|
SqlTimestampMapper.class,
|
||||||
IterableSourceTargetMapper.class,
|
IterableSourceTargetMapper.class,
|
||||||
JaxbListMapper.class,
|
JaxbListMapper.class,
|
||||||
JaxbMapper.class,
|
JaxbMapper.class,
|
||||||
@ -262,7 +270,7 @@ public class BuiltInTest {
|
|||||||
|
|
||||||
DateProperty target = CalendarToDateMapper.INSTANCE.map( source );
|
DateProperty target = CalendarToDateMapper.INSTANCE.map( source );
|
||||||
assertThat( target ).isNotNull();
|
assertThat( target ).isNotNull();
|
||||||
assertThat( target.getProp() ).isNotNull();
|
assertThat( target.getProp() ).isNotNull().isInstanceOf( Date.class );
|
||||||
assertThat( target.getProp() ).isEqualTo( createCalendar( "02.03.1999" ).getTime() );
|
assertThat( target.getProp() ).isEqualTo( createCalendar( "02.03.1999" ).getTime() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -354,22 +362,88 @@ public class BuiltInTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@IssueKey( "277" )
|
public void shouldApplyBuiltInOnDateToSqlDate() throws ParseException {
|
||||||
@WithClasses({
|
assertThat( DateToSqlDateMapper.INSTANCE.toTargetWithSqlDate( null ) ).isNull();
|
||||||
ErroneousSourceTargetWithSqlDateMapper.class })
|
DateProperty source = new DateProperty();
|
||||||
@ExpectedCompilationOutcome(
|
source.setProp( createDate( "31-08-1982 10:20:56" ) );
|
||||||
value = CompilationResult.FAILED,
|
|
||||||
diagnostics = {
|
SqlDateProperty target = DateToSqlDateMapper.INSTANCE.toTargetWithSqlDate( source );
|
||||||
@Diagnostic( type = ErroneousSourceTargetWithSqlDateMapper.class,
|
|
||||||
kind = javax.tools.Diagnostic.Kind.ERROR,
|
assertThat( target ).isNotNull();
|
||||||
line = 35,
|
assertThat( target.getProp() ).isNotNull().isInstanceOf( java.sql.Date.class );
|
||||||
messageRegExp = "Can't map property \"java\\.util\\.Date prop\" to "
|
assertThat( target.getProp().toString() ).isEqualTo( "1982-08-31" );
|
||||||
+ "\"java\\.sql\\.Date prop\"" )
|
|
||||||
}
|
|
||||||
)
|
|
||||||
public void shouldNotMapJavaUtilDateToJavaSqlDate() {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldUseSqlDateWhenMappingToDate() throws ParseException {
|
||||||
|
assertThat( DateToSqlDateMapper.INSTANCE.fromSqlDate( null ) ).isNull();
|
||||||
|
|
||||||
|
SqlDateProperty source = new SqlDateProperty();
|
||||||
|
source.setProp( new java.sql.Date( createDate( "31-08-1982 10:20:56" ).getTime() ) );
|
||||||
|
|
||||||
|
DateProperty target = DateToSqlDateMapper.INSTANCE.fromSqlDate( source );
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getProp() ).isNotNull();
|
||||||
|
assertThat( target.getProp().toString() ).isEqualTo( "1982-08-31" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldApplyBuiltInOnDateToSqlTime() throws ParseException {
|
||||||
|
SqlTimeMapper mapper = SqlTimeMapper.INSTANCE;
|
||||||
|
assertThat( mapper.map( null ) ).isNull();
|
||||||
|
DateProperty source = new DateProperty();
|
||||||
|
source.setProp( createDate( "31-08-1982 10:20:56" ) );
|
||||||
|
|
||||||
|
SqlTimeProperty target = mapper.map( source );
|
||||||
|
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getProp() ).isNotNull();
|
||||||
|
assertThat( target.getProp().toString() ).isEqualTo( "10:20:56" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldUseSqlTimeWhenMappingToDate() throws ParseException {
|
||||||
|
SqlTimeMapper mapper = SqlTimeMapper.INSTANCE;
|
||||||
|
assertThat( mapper.fromTime( null ) ).isNull();
|
||||||
|
|
||||||
|
SqlTimeProperty source = new SqlTimeProperty();
|
||||||
|
source.setProp( new Time( createDate( "31-08-1982 10:20:56" ).getTime() ) );
|
||||||
|
|
||||||
|
DateProperty target = mapper.fromTime( source );
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getProp() ).isNotNull();
|
||||||
|
assertThat( target.getProp().toString() ).isEqualTo( "10:20:56" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldApplyBuiltInOnDateToSqlTimestamp() throws ParseException {
|
||||||
|
SqlTimestampMapper mapper = SqlTimestampMapper.INSTANCE;
|
||||||
|
assertThat( mapper.map( null ) ).isNull();
|
||||||
|
DateProperty source = new DateProperty();
|
||||||
|
source.setProp( createDate( "31-08-1982 10:20:56" ) );
|
||||||
|
|
||||||
|
SqlTimestampProperty target = mapper.map( source );
|
||||||
|
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getProp() ).isNotNull();
|
||||||
|
assertThat( target.getProp().toString() ).isEqualTo( "1982-08-31 10:20:56.0" );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldUseSqlTimestampWhenMappingToDate() throws ParseException {
|
||||||
|
SqlTimestampMapper mapper = SqlTimestampMapper.INSTANCE;
|
||||||
|
assertThat( mapper.fromTimestamp( null ) ).isNull();
|
||||||
|
|
||||||
|
SqlTimestampProperty source = new SqlTimestampProperty();
|
||||||
|
source.setProp( new Timestamp( createDate( "31-08-1982 10:20:56" ).getTime() ) );
|
||||||
|
|
||||||
|
DateProperty target = mapper.fromTimestamp( source );
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getProp() ).isNotNull();
|
||||||
|
assertThat( target.getProp().toString() ).isEqualTo( "1982-08-31 10:20:56.0" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private JAXBElement<String> createJaxb(String test) {
|
private JAXBElement<String> createJaxb(String test) {
|
||||||
return new JAXBElement<String>( new QName( "www.mapstruct.org", "test" ), String.class, test );
|
return new JAXBElement<String>( new QName( "www.mapstruct.org", "test" ), String.class, test );
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* 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.test.builtin.bean;
|
||||||
|
|
||||||
|
import java.sql.Time;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class SqlTimeProperty {
|
||||||
|
|
||||||
|
private Time prop;
|
||||||
|
|
||||||
|
public Time getProp() {
|
||||||
|
return prop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProp(Time prop) {
|
||||||
|
this.prop = prop;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* 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.test.builtin.bean;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class SqlTimestampProperty {
|
||||||
|
|
||||||
|
private Timestamp prop;
|
||||||
|
|
||||||
|
public Timestamp getProp() {
|
||||||
|
return prop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProp(Timestamp prop) {
|
||||||
|
this.prop = prop;
|
||||||
|
}
|
||||||
|
}
|
@ -28,9 +28,11 @@ import org.mapstruct.factory.Mappers;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface ErroneousSourceTargetWithSqlDateMapper {
|
public interface DateToSqlDateMapper {
|
||||||
|
|
||||||
ErroneousSourceTargetWithSqlDateMapper INSTANCE = Mappers.getMapper( ErroneousSourceTargetWithSqlDateMapper.class );
|
DateToSqlDateMapper INSTANCE = Mappers.getMapper( DateToSqlDateMapper.class );
|
||||||
|
|
||||||
SqlDateProperty toTargetWithSqlDate(DateProperty source);
|
SqlDateProperty toTargetWithSqlDate(DateProperty source);
|
||||||
|
|
||||||
|
DateProperty fromSqlDate(SqlDateProperty source);
|
||||||
}
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* 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.test.builtin.mapper;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ap.test.builtin.bean.DateProperty;
|
||||||
|
import org.mapstruct.ap.test.builtin.bean.SqlTimeProperty;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Andreas Gudian
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface SqlTimeMapper {
|
||||||
|
|
||||||
|
SqlTimeMapper INSTANCE = Mappers.getMapper( SqlTimeMapper.class );
|
||||||
|
|
||||||
|
SqlTimeProperty map(DateProperty source);
|
||||||
|
|
||||||
|
DateProperty fromTime(SqlTimeProperty source);
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* 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.test.builtin.mapper;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ap.test.builtin.bean.DateProperty;
|
||||||
|
import org.mapstruct.ap.test.builtin.bean.SqlTimestampProperty;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Andreas Gudian
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface SqlTimestampMapper {
|
||||||
|
|
||||||
|
SqlTimestampMapper INSTANCE = Mappers.getMapper( SqlTimestampMapper.class );
|
||||||
|
|
||||||
|
SqlTimestampProperty map(DateProperty source);
|
||||||
|
|
||||||
|
DateProperty fromTimestamp(SqlTimestampProperty source);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user