-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,6 +20,7 @@ | |||||
import java.sql.ResultSet; | ||||||
import java.sql.SQLException; | ||||||
import java.sql.Timestamp; | ||||||
import java.sql.Types; | ||||||
import java.time.OffsetDateTime; | ||||||
import java.time.ZoneId; | ||||||
|
||||||
|
@@ -32,31 +33,23 @@ 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 (OffsetDateTime) rs.getObject(columnName); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specify the second argument of
Suggested change
|
||||||
} | ||||||
|
||||||
@Override | ||||||
public OffsetDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { | ||||||
Timestamp timestamp = rs.getTimestamp(columnIndex); | ||||||
return getOffsetDateTime(timestamp); | ||||||
return (OffsetDateTime) rs.getObject(columnIndex); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specify the second argument of
Suggested change
|
||||||
} | ||||||
|
||||||
@Override | ||||||
public OffsetDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { | ||||||
Timestamp timestamp = cs.getTimestamp(columnIndex); | ||||||
return getOffsetDateTime(timestamp); | ||||||
return (OffsetDateTime) cs.getObject(columnIndex); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specify the second argument of
Suggested change
|
||||||
} | ||||||
|
||||||
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 ( | ||
datetime timestamp with time zone | ||
); | ||
|
||
insert into records values ('2017-08-20 20:00:00+02:00'); | ||
insert into records values ('2017-08-20 20:00:00-04:00'); | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||
/** | ||||
* 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.usesjava8.timestamp_with_timezone; | ||||
|
||||
import org.apache.ibatis.submitted.usesjava8.use_actual_param_name.*; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused import.
Suggested change
|
||||
import java.util.List; | ||||
|
||||
import org.apache.ibatis.annotations.Select; | ||||
|
||||
public interface Mapper { | ||||
|
||||
@Select("select * from records") | ||||
List<Record> selectAll(); | ||||
|
||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?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 mapper | ||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
|
||
<mapper namespace="org.apache.ibatis.submitted.usesjava8.timestamp_with_timezone.Mapper"> | ||
|
||
</mapper> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty file. Please delete it. |
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.usesjava8.timestamp_with_timezone; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move the files to We no longer use |
||||
|
||||
|
||||
import java.io.Reader; | ||||
import java.sql.Connection; | ||||
import java.util.List; | ||||
|
||||
import org.apache.ibatis.io.Resources; | ||||
import org.apache.ibatis.jdbc.ScriptRunner; | ||||
import org.apache.ibatis.session.SqlSession; | ||||
import org.apache.ibatis.session.SqlSessionFactory; | ||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||||
import org.apache.ibatis.submitted.usesjava8.use_actual_param_name.User; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused import.
Suggested change
|
||||
import org.junit.Assert; | ||||
import static org.junit.Assert.assertNotNull; | ||||
import org.junit.BeforeClass; | ||||
import org.junit.Test; | ||||
|
||||
public class OffsetDateTimeTypeHandlerTest { | ||||
|
||||
private static SqlSessionFactory sqlSessionFactory; | ||||
|
||||
@BeforeClass | ||||
public static void setUp() throws Exception { | ||||
// create an SqlSessionFactory | ||||
Reader reader = Resources.getResourceAsReader( | ||||
"org/apache/ibatis/submitted/usesjava8/timestamp_with_timezone/mybatis-config.xml"); | ||||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); | ||||
reader.close(); | ||||
|
||||
// populate in-memory database | ||||
SqlSession session = sqlSessionFactory.openSession(); | ||||
Connection conn = session.getConnection(); | ||||
reader = Resources.getResourceAsReader( | ||||
"org/apache/ibatis/submitted/usesjava8/timestamp_with_timezone/CreateDB.sql"); | ||||
ScriptRunner runner = new ScriptRunner(conn); | ||||
runner.setLogWriter(null); | ||||
runner.runScript(reader); | ||||
conn.close(); | ||||
reader.close(); | ||||
session.close(); | ||||
} | ||||
|
||||
@Test | ||||
public void shouldPreserveDateTimeWithOffset() { | ||||
SqlSession sqlSession = sqlSessionFactory.openSession(); | ||||
try { | ||||
Mapper mapper = sqlSession.getMapper(Mapper.class); | ||||
List<Record> records = mapper.selectAll(); | ||||
|
||||
for (Record record : records) { | ||||
System.out.println(record.getDatetime()); | ||||
} | ||||
|
||||
} finally { | ||||
sqlSession.close(); | ||||
} | ||||
} | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be two tests with assertions at least (SELECT and INSERT). |
||||
|
||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* 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.usesjava8.timestamp_with_timezone; | ||
|
||
import java.time.OffsetDateTime; | ||
|
||
public class Record { | ||
|
||
private OffsetDateTime datetime; | ||
|
||
public OffsetDateTime getDatetime() { | ||
return datetime; | ||
} | ||
|
||
public void setDatetime(OffsetDateTime datetime) { | ||
this.datetime = datetime; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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.usesjava8.timestamp_with_timezone.Mapper" /> | ||
</mappers> | ||
|
||
</configuration> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some imports are not used.