Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing OffsetDateTimeHandler which retains Offset from UTC #1368

Merged
merged 4 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/org/apache/ibatis/type/JdbcType.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public enum JdbcType {
ROWID(Types.ROWID), // JDK6
LONGNVARCHAR(Types.LONGNVARCHAR), // JDK6
SQLXML(Types.SQLXML), // JDK6
DATETIMEOFFSET(-155); // SQL Server 2008
DATETIMEOFFSET(-155), // SQL Server 2008
TIME_WITH_TIMEZONE(Types.TIME_WITH_TIMEZONE), // JDBC 4.2 JDK8
TIMESTAMP_WITH_TIMEZONE(Types.TIMESTAMP_WITH_TIMEZONE); // JDBC 4.2 JDK8

public final int TYPE_CODE;
private static Map<Integer,JdbcType> codeLookup = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.ZoneId;

/**
* @since 3.4.5
Expand All @@ -32,31 +30,22 @@ public class OffsetDateTimeTypeHandler extends BaseTypeHandler<OffsetDateTime> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, OffsetDateTime parameter, JdbcType jdbcType)
throws SQLException {
ps.setTimestamp(i, Timestamp.from(parameter.toInstant()));
ps.setObject(i, parameter);
}

@Override
public OffsetDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName);
return getOffsetDateTime(timestamp);
return rs.getObject(columnName, OffsetDateTime.class);
}

@Override
public OffsetDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex);
return getOffsetDateTime(timestamp);
return rs.getObject(columnIndex, OffsetDateTime.class);
}

@Override
public OffsetDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp timestamp = cs.getTimestamp(columnIndex);
return getOffsetDateTime(timestamp);
return cs.getObject(columnIndex, OffsetDateTime.class);
}

private static OffsetDateTime getOffsetDateTime(Timestamp timestamp) {
if (timestamp != null) {
return OffsetDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--
-- Copyright 2009-2017 the original author or authors.
--
-- 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.
--

drop table records if exists;

create table records (
id int,
odt timestamp with time zone
);

insert into records (id, odt) values
(1, '2018-01-02 11:22:33.123456000+01:23');
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2009-2017 the original author or authors.
*
* 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.apache.ibatis.submitted.timestamp_with_timezone;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

public interface Mapper {

@Select("select id, odt from records where id = #{id}")
Record selectById(Integer id);

@Insert("insert into records (id, odt) values (#{id}, #{odt})")
int insertOffsetDateTime(Record record);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2009-2017 the original author or authors.
*
* 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.apache.ibatis.submitted.timestamp_with_timezone;

import java.time.OffsetDateTime;

public class Record {

private Integer id;

private OffsetDateTime odt;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public OffsetDateTime getOdt() {
return odt;
}

public void setOdt(OffsetDateTime odt) {
this.odt = odt;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2009-2017 the original author or authors.
*
* 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.apache.ibatis.submitted.timestamp_with_timezone;

import static org.junit.jupiter.api.Assertions.*;

import java.io.Reader;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class TimestampWithTimezoneTypeHandlerTest {

private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
public static void setUp() throws Exception {
try (Reader reader = Resources
.getResourceAsReader("org/apache/ibatis/submitted/timestamp_with_timezone/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/timestamp_with_timezone/CreateDB.sql");
}

@Test
public void shouldSelectOffsetDateTime() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
Record record = mapper.selectById(1);
assertEquals(OffsetDateTime.of(2018, 1, 2, 11, 22, 33, 123456000, ZoneOffset.ofHoursMinutes(1, 23)),
record.getOdt());
}
}

@Test
public void shouldInsertOffsetDateTime() {
OffsetDateTime odt = OffsetDateTime.of(2018, 1, 2, 11, 22, 33, 123456000, ZoneOffset.ofHoursMinutes(1, 23));
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
Record record = new Record();
record.setId(2);
record.setOdt(odt);
int result = mapper.insertOffsetDateTime(record);
assertEquals(1, result);
sqlSession.commit();
}
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
Record record = mapper.selectById(2);
assertEquals(odt, record.getOdt());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2017 the original author or authors.
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.
-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver" />
<property name="url"
value="jdbc:hsqldb:mem:timestampwithtimzeone" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper
class="org.apache.ibatis.submitted.timestamp_with_timezone.Mapper" />
</mappers>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.sql.Timestamp;
import java.time.OffsetDateTime;

import org.junit.jupiter.api.Test;
Expand All @@ -27,60 +26,60 @@ public class OffsetDateTimeTypeHandlerTest extends BaseTypeHandlerTest {

private static final TypeHandler<OffsetDateTime> TYPE_HANDLER = new OffsetDateTimeTypeHandler();
private static final OffsetDateTime OFFSET_DATE_TIME = OffsetDateTime.now();
private static final Timestamp TIMESTAMP = Timestamp.from(OFFSET_DATE_TIME.toInstant());

@Override
@Test
public void shouldSetParameter() throws Exception {
TYPE_HANDLER.setParameter(ps, 1, OFFSET_DATE_TIME, null);
verify(ps).setTimestamp(1, TIMESTAMP);
verify(ps).setObject(1, OFFSET_DATE_TIME);
}

@Override
@Test
public void shouldGetResultFromResultSetByName() throws Exception {
when(rs.getTimestamp("column")).thenReturn(TIMESTAMP);
when(rs.getObject("column", OffsetDateTime.class)).thenReturn(OFFSET_DATE_TIME);
assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(rs, "column"));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromResultSetByName() throws Exception {
when(rs.getTimestamp("column")).thenReturn(null);
when(rs.getObject("column", OffsetDateTime.class)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(rs, "column"));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultFromResultSetByPosition() throws Exception {
when(rs.getTimestamp(1)).thenReturn(TIMESTAMP);
when(rs.getObject(1, OffsetDateTime.class)).thenReturn(OFFSET_DATE_TIME);
assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(rs, 1));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromResultSetByPosition() throws Exception {
when(rs.getTimestamp(1)).thenReturn(null);
when(rs.getObject(1, OffsetDateTime.class)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(rs, 1));
verify(rs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultFromCallableStatement() throws Exception {
when(cs.getTimestamp(1)).thenReturn(TIMESTAMP);
when(cs.getObject(1, OffsetDateTime.class)).thenReturn(OFFSET_DATE_TIME);
assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(cs, 1));
verify(cs, never()).wasNull();
}

@Override
@Test
public void shouldGetResultNullFromCallableStatement() throws Exception {
when(cs.getTimestamp(1)).thenReturn(null);
when(cs.getObject(1, OffsetDateTime.class)).thenReturn(null);
assertNull(TYPE_HANDLER.getResult(cs, 1));
verify(cs, never()).wasNull();
}

}