diff --git a/pom.xml b/pom.xml index d2dd2080194..8eb34a7b5e4 100644 --- a/pom.xml +++ b/pom.xml @@ -156,12 +156,6 @@ compile true - - org.mybatis - mybatis-typehandlers-jsr310 - 1.0.2 - true - org.slf4j slf4j-api diff --git a/src/main/java/org/apache/ibatis/reflection/Jdk.java b/src/main/java/org/apache/ibatis/reflection/Jdk.java index 0aa2a2f7fe0..f5b809badf6 100644 --- a/src/main/java/org/apache/ibatis/reflection/Jdk.java +++ b/src/main/java/org/apache/ibatis/reflection/Jdk.java @@ -39,6 +39,19 @@ public class Jdk { parameterExists = available; } + public static final boolean dateAndTimeApiExists; + + static { + boolean available = false; + try { + Resources.classForName("java.time.Clock"); + available = true; + } catch (ClassNotFoundException e) { + // ignore + } + dateAndTimeApiExists = available; + } + private Jdk() { super(); } diff --git a/src/main/java/org/apache/ibatis/type/InstantTypeHandler.java b/src/main/java/org/apache/ibatis/type/InstantTypeHandler.java new file mode 100644 index 00000000000..3dee9c35ffa --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/InstantTypeHandler.java @@ -0,0 +1,63 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.time.Instant; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + * @author Tomas Rohovsky + */ +@UsesJava8 +public class InstantTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Instant parameter, JdbcType jdbcType) throws SQLException { + ps.setTimestamp(i, Timestamp.from(parameter)); + } + + @Override + public Instant getNullableResult(ResultSet rs, String columnName) throws SQLException { + Timestamp timestamp = rs.getTimestamp(columnName); + return getInstant(timestamp); + } + + @Override + public Instant getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Timestamp timestamp = rs.getTimestamp(columnIndex); + return getInstant(timestamp); + } + + @Override + public Instant getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Timestamp timestamp = cs.getTimestamp(columnIndex); + return getInstant(timestamp); + } + + private static Instant getInstant(Timestamp timestamp) { + if (timestamp != null) { + return timestamp.toInstant(); + } + return null; + } +} diff --git a/src/main/java/org/apache/ibatis/type/JapaneseDateTypeHandler.java b/src/main/java/org/apache/ibatis/type/JapaneseDateTypeHandler.java new file mode 100644 index 00000000000..729aa9b4cce --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/JapaneseDateTypeHandler.java @@ -0,0 +1,68 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDate; +import java.time.chrono.JapaneseDate; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * Type Handler for {@link JapaneseDate}. + * + * @since 3.4.5 + * @author Kazuki Shimizu + */ +@UsesJava8 +public class JapaneseDateTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, JapaneseDate parameter, JdbcType jdbcType) + throws SQLException { + ps.setDate(i, Date.valueOf(LocalDate.ofEpochDay(parameter.toEpochDay()))); + } + + @Override + public JapaneseDate getNullableResult(ResultSet rs, String columnName) throws SQLException { + Date date = rs.getDate(columnName); + return getJapaneseDate(date); + } + + @Override + public JapaneseDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Date date = rs.getDate(columnIndex); + return getJapaneseDate(date); + } + + @Override + public JapaneseDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Date date = cs.getDate(columnIndex); + return getJapaneseDate(date); + } + + private static JapaneseDate getJapaneseDate(Date date) { + if (date != null) { + return JapaneseDate.from(date.toLocalDate()); + } + return null; + } + +} diff --git a/src/main/java/org/apache/ibatis/type/Java8TypeHandlersRegistrar.java b/src/main/java/org/apache/ibatis/type/Java8TypeHandlersRegistrar.java new file mode 100644 index 00000000000..d37b6cb6b53 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/Java8TypeHandlersRegistrar.java @@ -0,0 +1,53 @@ +/** + * 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.type; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.Year; +import java.time.YearMonth; +import java.time.ZonedDateTime; +import java.time.chrono.JapaneseDate; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + */ +@UsesJava8 +public abstract class Java8TypeHandlersRegistrar { + + public static void registerDateAndTimeHandlers(TypeHandlerRegistry registry) { + registry.register(Instant.class, InstantTypeHandler.class); + registry.register(LocalDateTime.class, LocalDateTimeTypeHandler.class); + registry.register(LocalDate.class, LocalDateTypeHandler.class); + registry.register(LocalTime.class, LocalTimeTypeHandler.class); + registry.register(OffsetDateTime.class, OffsetDateTimeTypeHandler.class); + registry.register(OffsetTime.class, OffsetTimeTypeHandler.class); + registry.register(ZonedDateTime.class, ZonedDateTimeTypeHandler.class); + registry.register(Month.class, MonthTypeHandler.class); + registry.register(Year.class, YearTypeHandler.class); + registry.register(YearMonth.class, YearMonthTypeHandler.class); + registry.register(JapaneseDate.class, JapaneseDateTypeHandler.class); + } + +} diff --git a/src/main/java/org/apache/ibatis/type/LocalDateTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/LocalDateTimeTypeHandler.java new file mode 100644 index 00000000000..a7b377ab3c9 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/LocalDateTimeTypeHandler.java @@ -0,0 +1,64 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.time.LocalDateTime; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + * @author Tomas Rohovsky + */ +@UsesJava8 +public class LocalDateTimeTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) + throws SQLException { + ps.setTimestamp(i, Timestamp.valueOf(parameter)); + } + + @Override + public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException { + Timestamp timestamp = rs.getTimestamp(columnName); + return getLocalDateTime(timestamp); + } + + @Override + public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Timestamp timestamp = rs.getTimestamp(columnIndex); + return getLocalDateTime(timestamp); + } + + @Override + public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Timestamp timestamp = cs.getTimestamp(columnIndex); + return getLocalDateTime(timestamp); + } + + private static LocalDateTime getLocalDateTime(Timestamp timestamp) { + if (timestamp != null) { + return timestamp.toLocalDateTime(); + } + return null; + } +} diff --git a/src/main/java/org/apache/ibatis/type/LocalDateTypeHandler.java b/src/main/java/org/apache/ibatis/type/LocalDateTypeHandler.java new file mode 100644 index 00000000000..896d7657053 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/LocalDateTypeHandler.java @@ -0,0 +1,64 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDate; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + * @author Tomas Rohovsky + */ +@UsesJava8 +public class LocalDateTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType) + throws SQLException { + ps.setDate(i, Date.valueOf(parameter)); + } + + @Override + public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException { + Date date = rs.getDate(columnName); + return getLocalDate(date); + } + + @Override + public LocalDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Date date = rs.getDate(columnIndex); + return getLocalDate(date); + } + + @Override + public LocalDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Date date = cs.getDate(columnIndex); + return getLocalDate(date); + } + + private static LocalDate getLocalDate(Date date) { + if (date != null) { + return date.toLocalDate(); + } + return null; + } +} diff --git a/src/main/java/org/apache/ibatis/type/LocalTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/LocalTimeTypeHandler.java new file mode 100644 index 00000000000..c17a21f5493 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/LocalTimeTypeHandler.java @@ -0,0 +1,64 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Time; +import java.time.LocalTime; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + * @author Tomas Rohovsky + */ +@UsesJava8 +public class LocalTimeTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, LocalTime parameter, JdbcType jdbcType) + throws SQLException { + ps.setTime(i, Time.valueOf(parameter)); + } + + @Override + public LocalTime getNullableResult(ResultSet rs, String columnName) throws SQLException { + Time time = rs.getTime(columnName); + return getLocalTime(time); + } + + @Override + public LocalTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Time time = rs.getTime(columnIndex); + return getLocalTime(time); + } + + @Override + public LocalTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Time time = cs.getTime(columnIndex); + return getLocalTime(time); + } + + private static LocalTime getLocalTime(Time time) { + if (time != null) { + return time.toLocalTime(); + } + return null; + } +} diff --git a/src/main/java/org/apache/ibatis/type/MonthTypeHandler.java b/src/main/java/org/apache/ibatis/type/MonthTypeHandler.java new file mode 100644 index 00000000000..344380be1f9 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/MonthTypeHandler.java @@ -0,0 +1,57 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.Month; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * + * @since 3.4.5 + * @author Björn Raupach + */ +@UsesJava8 +public class MonthTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Month month, JdbcType type) throws SQLException { + ps.setInt(i, month.getValue()); + } + + @Override + public Month getNullableResult(ResultSet rs, String columnName) throws SQLException { + int month = rs.getInt(columnName); + return month == 0 ? null : Month.of(month); + } + + @Override + public Month getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + int month = rs.getInt(columnIndex); + return month == 0 ? null : Month.of(month); + } + + @Override + public Month getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + int month = cs.getInt(columnIndex); + return month == 0 ? null : Month.of(month); + } + +} diff --git a/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java new file mode 100644 index 00000000000..ecee2bd9379 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/OffsetDateTimeTypeHandler.java @@ -0,0 +1,65 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.time.OffsetDateTime; +import java.time.ZoneId; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + * @author Tomas Rohovsky + */ +@UsesJava8 +public class OffsetDateTimeTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, OffsetDateTime parameter, JdbcType jdbcType) + throws SQLException { + ps.setTimestamp(i, Timestamp.from(parameter.toInstant())); + } + + @Override + public OffsetDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException { + Timestamp timestamp = rs.getTimestamp(columnName); + return getOffsetDateTime(timestamp); + } + + @Override + public OffsetDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Timestamp timestamp = rs.getTimestamp(columnIndex); + return getOffsetDateTime(timestamp); + } + + @Override + public OffsetDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Timestamp timestamp = cs.getTimestamp(columnIndex); + return getOffsetDateTime(timestamp); + } + + private static OffsetDateTime getOffsetDateTime(Timestamp timestamp) { + if (timestamp != null) { + return OffsetDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault()); + } + return null; + } +} diff --git a/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java new file mode 100644 index 00000000000..22104eceaf0 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/OffsetTimeTypeHandler.java @@ -0,0 +1,64 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Time; +import java.time.OffsetTime; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + * @author Tomas Rohovsky + */ +@UsesJava8 +public class OffsetTimeTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, OffsetTime parameter, JdbcType jdbcType) + throws SQLException { + ps.setTime(i, Time.valueOf(parameter.toLocalTime())); + } + + @Override + public OffsetTime getNullableResult(ResultSet rs, String columnName) throws SQLException { + Time time = rs.getTime(columnName); + return getOffsetTime(time); + } + + @Override + public OffsetTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Time time = rs.getTime(columnIndex); + return getOffsetTime(time); + } + + @Override + public OffsetTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Time time = cs.getTime(columnIndex); + return getOffsetTime(time); + } + + private static OffsetTime getOffsetTime(Time time) { + if (time != null) { + return time.toLocalTime().atOffset(OffsetTime.now().getOffset()); + } + return null; + } +} diff --git a/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java b/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java index 4123f27a73b..d86183d2659 100644 --- a/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java +++ b/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java @@ -34,6 +34,7 @@ import org.apache.ibatis.io.ResolverUtil; import org.apache.ibatis.io.Resources; +import org.apache.ibatis.reflection.Jdk; /** * @author Clinton Begin @@ -133,24 +134,8 @@ public TypeHandlerRegistry() { register(java.sql.Timestamp.class, new SqlTimestampTypeHandler()); // mybatis-typehandlers-jsr310 - try { - // since 1.0.0 - register("java.time.Instant", "org.apache.ibatis.type.InstantTypeHandler"); - register("java.time.LocalDateTime", "org.apache.ibatis.type.LocalDateTimeTypeHandler"); - register("java.time.LocalDate", "org.apache.ibatis.type.LocalDateTypeHandler"); - register("java.time.LocalTime", "org.apache.ibatis.type.LocalTimeTypeHandler"); - register("java.time.OffsetDateTime", "org.apache.ibatis.type.OffsetDateTimeTypeHandler"); - register("java.time.OffsetTime", "org.apache.ibatis.type.OffsetTimeTypeHandler"); - register("java.time.ZonedDateTime", "org.apache.ibatis.type.ZonedDateTimeTypeHandler"); - // since 1.0.1 - register("java.time.Month", "org.apache.ibatis.type.MonthTypeHandler"); - register("java.time.Year", "org.apache.ibatis.type.YearTypeHandler"); - // since 1.0.2 - register("java.time.YearMonth", "org.apache.ibatis.type.YearMonthTypeHandler"); - register("java.time.chrono.JapaneseDate", "org.apache.ibatis.type.JapaneseDateTypeHandler"); - - } catch (ClassNotFoundException e) { - // no JSR-310 handlers + if (Jdk.dateAndTimeApiExists) { + Java8TypeHandlersRegistrar.registerDateAndTimeHandlers(this); } // issue #273 diff --git a/src/main/java/org/apache/ibatis/type/YearMonthTypeHandler.java b/src/main/java/org/apache/ibatis/type/YearMonthTypeHandler.java new file mode 100644 index 00000000000..31936205706 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/YearMonthTypeHandler.java @@ -0,0 +1,62 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.YearMonth; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * Type Handler for {@link java.time.YearMonth} + * + * YearMonthTypeHandler relies upon + * {@link java.time.YearMonth#parse YearMonth.parse}. Therefore column values + * are expected as strings. The format must be uuuu-MM. Example: "2016-08" + * + * @since 3.4.5 + * @author Björn Raupach + */ +@UsesJava8 +public class YearMonthTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, YearMonth yearMonth, JdbcType jt) throws SQLException { + ps.setString(i, yearMonth.toString()); + } + + @Override + public YearMonth getNullableResult(ResultSet rs, String columnName) throws SQLException { + String value = rs.getString(columnName); + return value == null ? null : YearMonth.parse(value); + } + + @Override + public YearMonth getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + String value = rs.getString(columnIndex); + return value == null ? null : YearMonth.parse(value); + } + + @Override + public YearMonth getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + String value = cs.getString(columnIndex); + return value == null ? null : YearMonth.parse(value); + } + +} diff --git a/src/main/java/org/apache/ibatis/type/YearTypeHandler.java b/src/main/java/org/apache/ibatis/type/YearTypeHandler.java new file mode 100644 index 00000000000..c549db59db4 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/YearTypeHandler.java @@ -0,0 +1,56 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.Year; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + * @author Björn Raupach + */ +@UsesJava8 +public class YearTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, Year year, JdbcType type) throws SQLException { + ps.setInt(i, year.getValue()); + } + + @Override + public Year getNullableResult(ResultSet rs, String columnName) throws SQLException { + int year = rs.getInt(columnName); + return year == 0 ? null : Year.of(year); + } + + @Override + public Year getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + int year = rs.getInt(columnIndex); + return year == 0 ? null : Year.of(year); + } + + @Override + public Year getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + int year = cs.getInt(columnIndex); + return year == 0 ? null : Year.of(year); + } + +} diff --git a/src/main/java/org/apache/ibatis/type/ZonedDateTimeTypeHandler.java b/src/main/java/org/apache/ibatis/type/ZonedDateTimeTypeHandler.java new file mode 100644 index 00000000000..ff4fd7719c5 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/ZonedDateTimeTypeHandler.java @@ -0,0 +1,65 @@ +/** + * 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.type; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import org.apache.ibatis.lang.UsesJava8; + +/** + * @since 3.4.5 + * @author Tomas Rohovsky + */ +@UsesJava8 +public class ZonedDateTimeTypeHandler extends BaseTypeHandler { + + @Override + public void setNonNullParameter(PreparedStatement ps, int i, ZonedDateTime parameter, JdbcType jdbcType) + throws SQLException { + ps.setTimestamp(i, Timestamp.from(parameter.toInstant())); + } + + @Override + public ZonedDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException { + Timestamp timestamp = rs.getTimestamp(columnName); + return getZonedDateTime(timestamp); + } + + @Override + public ZonedDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { + Timestamp timestamp = rs.getTimestamp(columnIndex); + return getZonedDateTime(timestamp); + } + + @Override + public ZonedDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { + Timestamp timestamp = cs.getTimestamp(columnIndex); + return getZonedDateTime(timestamp); + } + + private static ZonedDateTime getZonedDateTime(Timestamp timestamp) { + if (timestamp != null) { + return ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault()); + } + return null; + } +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/InstantTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/InstantTypeHandlerTest.java new file mode 100644 index 00000000000..dfdc8ca2cd4 --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/InstantTypeHandlerTest.java @@ -0,0 +1,86 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.sql.Timestamp; +import java.time.Instant; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.InstantTypeHandler; +import org.apache.ibatis.type.TypeHandler; +import org.junit.Test; + +public class InstantTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new InstantTypeHandler(); + private static final Instant INSTANT = Instant.now(); + private static final Timestamp TIMESTAMP = Timestamp.from(INSTANT); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, INSTANT, null); + verify(ps).setTimestamp(1, TIMESTAMP); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getTimestamp("column")).thenReturn(TIMESTAMP); + assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getTimestamp("column")).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getTimestamp(1)).thenReturn(TIMESTAMP); + assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getTimestamp(1)).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getTimestamp(1)).thenReturn(TIMESTAMP); + assertEquals(INSTANT, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getTimestamp(1)).thenReturn(null); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/JapaneseDateTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/JapaneseDateTypeHandlerTest.java new file mode 100644 index 00000000000..fb099a6e12c --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/JapaneseDateTypeHandlerTest.java @@ -0,0 +1,88 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.sql.Date; +import java.time.LocalDate; +import java.time.chrono.JapaneseDate; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.JapaneseDateTypeHandler; +import org.apache.ibatis.type.TypeHandler; +import org.junit.Test; + +public class JapaneseDateTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new JapaneseDateTypeHandler(); + private static final JapaneseDate JAPANESE_DATE = JapaneseDate.now(); + private static final Date DATE = Date.valueOf(LocalDate.ofEpochDay(JAPANESE_DATE.toEpochDay())); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, JAPANESE_DATE, null); + verify(ps).setDate(1, DATE); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getDate("column")).thenReturn(DATE); + assertEquals(JAPANESE_DATE, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getDate("column")).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getDate(1)).thenReturn(DATE); + assertEquals(JAPANESE_DATE, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getDate(1)).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getDate(1)).thenReturn(DATE); + assertEquals(JAPANESE_DATE, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getDate(1)).thenReturn(null); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } + +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/Jsr310TypeHandlerRegistryTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/Jsr310TypeHandlerRegistryTest.java index 3f72763dbb7..da718e38097 100644 --- a/src/test/java/org/apache/ibatis/type/usesjava8/Jsr310TypeHandlerRegistryTest.java +++ b/src/test/java/org/apache/ibatis/type/usesjava8/Jsr310TypeHandlerRegistryTest.java @@ -15,16 +15,36 @@ */ package org.apache.ibatis.type.usesjava8; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.*; -import org.apache.ibatis.io.Resources; -import org.apache.ibatis.type.*; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.Month; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.Year; +import java.time.YearMonth; +import java.time.ZonedDateTime; +import java.time.chrono.JapaneseDate; + +import org.apache.ibatis.type.InstantTypeHandler; +import org.apache.ibatis.type.JapaneseDateTypeHandler; +import org.apache.ibatis.type.LocalDateTimeTypeHandler; +import org.apache.ibatis.type.LocalDateTypeHandler; +import org.apache.ibatis.type.LocalTimeTypeHandler; +import org.apache.ibatis.type.MonthTypeHandler; +import org.apache.ibatis.type.OffsetDateTimeTypeHandler; +import org.apache.ibatis.type.OffsetTimeTypeHandler; +import org.apache.ibatis.type.TypeHandlerRegistry; +import org.apache.ibatis.type.YearMonthTypeHandler; +import org.apache.ibatis.type.YearTypeHandler; +import org.apache.ibatis.type.ZonedDateTimeTypeHandler; import org.junit.Before; import org.junit.Test; /** - * Tests for auto-detect type handlers of mybatis-typehandlers-jsr310. - * * @author Kazuki Shimizu */ public class Jsr310TypeHandlerRegistryTest { @@ -37,30 +57,28 @@ public void setup() { } @Test - public void testFor_v1_0_0() throws ClassNotFoundException { - assertThat(getTypeHandler("java.time.Instant")).isInstanceOf(InstantTypeHandler.class); - assertThat(getTypeHandler("java.time.LocalDateTime")).isInstanceOf(LocalDateTimeTypeHandler.class); - assertThat(getTypeHandler("java.time.LocalDate")).isInstanceOf(LocalDateTypeHandler.class); - assertThat(getTypeHandler("java.time.LocalTime")).isInstanceOf(LocalTimeTypeHandler.class); - assertThat(getTypeHandler("java.time.OffsetDateTime")).isInstanceOf(OffsetDateTimeTypeHandler.class); - assertThat(getTypeHandler("java.time.OffsetTime")).isInstanceOf(OffsetTimeTypeHandler.class); - assertThat(getTypeHandler("java.time.ZonedDateTime")).isInstanceOf(ZonedDateTimeTypeHandler.class); - } - - @Test - public void testFor_v1_0_1() throws ClassNotFoundException { - assertThat(getTypeHandler("java.time.Month")).isInstanceOf(MonthTypeHandler.class); - assertThat(getTypeHandler("java.time.Year")).isInstanceOf(YearTypeHandler.class); + public void shouldRegisterJsr310TypeHandlers() throws ClassNotFoundException { + assertThat(typeHandlerRegistry.getTypeHandler(Instant.class)) + .isInstanceOf(InstantTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(LocalDateTime.class)) + .isInstanceOf(LocalDateTimeTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(LocalDate.class)) + .isInstanceOf(LocalDateTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(LocalTime.class)) + .isInstanceOf(LocalTimeTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(OffsetDateTime.class)) + .isInstanceOf(OffsetDateTimeTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(OffsetTime.class)) + .isInstanceOf(OffsetTimeTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(ZonedDateTime.class)) + .isInstanceOf(ZonedDateTimeTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(Month.class)) + .isInstanceOf(MonthTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(Year.class)) + .isInstanceOf(YearTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(YearMonth.class)) + .isInstanceOf(YearMonthTypeHandler.class); + assertThat(typeHandlerRegistry.getTypeHandler(JapaneseDate.class)) + .isInstanceOf(JapaneseDateTypeHandler.class); } - - @Test - public void testFor_v1_0_2() throws ClassNotFoundException { - assertThat(getTypeHandler("java.time.YearMonth")).isInstanceOf(YearMonthTypeHandler.class); - assertThat(getTypeHandler("java.time.chrono.JapaneseDate")).isInstanceOf(JapaneseDateTypeHandler.class); - } - - private TypeHandler getTypeHandler(String fqcn) throws ClassNotFoundException { - return typeHandlerRegistry.getTypeHandler(Resources.classForName(fqcn)); - } - } diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/LocalDateTimeTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/LocalDateTimeTypeHandlerTest.java new file mode 100644 index 00000000000..7f54dedc5fb --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/LocalDateTimeTypeHandlerTest.java @@ -0,0 +1,86 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.sql.Timestamp; +import java.time.LocalDateTime; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.LocalDateTimeTypeHandler; +import org.apache.ibatis.type.TypeHandler; +import org.junit.Test; + +public class LocalDateTimeTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new LocalDateTimeTypeHandler(); + private static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.now(); + private static final Timestamp TIMESTAMP = Timestamp.valueOf(LOCAL_DATE_TIME); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, LOCAL_DATE_TIME, null); + verify(ps).setTimestamp(1, TIMESTAMP); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getTimestamp("column")).thenReturn(TIMESTAMP); + assertEquals(LOCAL_DATE_TIME, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getTimestamp("column")).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getTimestamp(1)).thenReturn(TIMESTAMP); + assertEquals(LOCAL_DATE_TIME, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getTimestamp(1)).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getTimestamp(1)).thenReturn(TIMESTAMP); + assertEquals(LOCAL_DATE_TIME, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getTimestamp(1)).thenReturn(null); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/LocalDateTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/LocalDateTypeHandlerTest.java new file mode 100644 index 00000000000..18dca3894a7 --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/LocalDateTypeHandlerTest.java @@ -0,0 +1,86 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.sql.Date; +import java.time.LocalDate; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.LocalDateTypeHandler; +import org.apache.ibatis.type.TypeHandler; +import org.junit.Test; + +public class LocalDateTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new LocalDateTypeHandler(); + private static final LocalDate LOCAL_DATE = LocalDate.now(); + private static final Date DATE = Date.valueOf(LOCAL_DATE); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, LOCAL_DATE, null); + verify(ps).setDate(1, DATE); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getDate("column")).thenReturn(DATE); + assertEquals(LOCAL_DATE, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getDate("column")).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getDate(1)).thenReturn(DATE); + assertEquals(LOCAL_DATE, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getDate(1)).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getDate(1)).thenReturn(DATE); + assertEquals(LOCAL_DATE, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getDate(1)).thenReturn(null); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/LocalTimeTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/LocalTimeTypeHandlerTest.java new file mode 100644 index 00000000000..e5398379b71 --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/LocalTimeTypeHandlerTest.java @@ -0,0 +1,87 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.sql.Time; +import java.time.LocalTime; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.LocalTimeTypeHandler; +import org.apache.ibatis.type.TypeHandler; +import org.junit.Test; + +public class LocalTimeTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new LocalTimeTypeHandler(); + // java.sql.Time doesn't contain millis, so set nano to 0 + private static final LocalTime LOCAL_TIME = LocalTime.now().withNano(0); + private static final Time TIME = Time.valueOf(LOCAL_TIME); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, LOCAL_TIME, null); + verify(ps).setTime(1, TIME); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getTime("column")).thenReturn(TIME); + assertEquals(LOCAL_TIME, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getTime("column")).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getTime(1)).thenReturn(TIME); + assertEquals(LOCAL_TIME, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getTime(1)).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getTime(1)).thenReturn(TIME); + assertEquals(LOCAL_TIME, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getTime(1)).thenReturn(null); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/MonthTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/MonthTypeHandlerTest.java new file mode 100644 index 00000000000..30ca1c3a88d --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/MonthTypeHandlerTest.java @@ -0,0 +1,89 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.time.Month; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.MonthTypeHandler; +import org.apache.ibatis.type.TypeHandler; +import org.junit.Test; + +/** + * + * @author Eduardo Macarron + */ +public class MonthTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new MonthTypeHandler(); + private static final Month INSTANT = Month.JANUARY; + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, INSTANT, null); + verify(ps).setInt(1, INSTANT.getValue()); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getInt("column")).thenReturn(INSTANT.getValue()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getInt("column")).thenReturn(0); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getInt(1)).thenReturn(INSTANT.getValue()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getInt(1)).thenReturn(0); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getInt(1)).thenReturn(INSTANT.getValue()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getInt(1)).thenReturn(0); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } + +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/OffsetDateTimeTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/OffsetDateTimeTypeHandlerTest.java new file mode 100644 index 00000000000..8e72bf85bab --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/OffsetDateTimeTypeHandlerTest.java @@ -0,0 +1,86 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.sql.Timestamp; +import java.time.OffsetDateTime; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.OffsetDateTimeTypeHandler; +import org.apache.ibatis.type.TypeHandler; +import org.junit.Test; + +public class OffsetDateTimeTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler 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); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getTimestamp("column")).thenReturn(TIMESTAMP); + assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getTimestamp("column")).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getTimestamp(1)).thenReturn(TIMESTAMP); + assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getTimestamp(1)).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getTimestamp(1)).thenReturn(TIMESTAMP); + assertEquals(OFFSET_DATE_TIME, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getTimestamp(1)).thenReturn(null); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/OffsetTimeTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/OffsetTimeTypeHandlerTest.java new file mode 100644 index 00000000000..880767be1bf --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/OffsetTimeTypeHandlerTest.java @@ -0,0 +1,87 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.sql.Time; +import java.time.OffsetTime; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.OffsetTimeTypeHandler; +import org.apache.ibatis.type.TypeHandler; +import org.junit.Test; + +public class OffsetTimeTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new OffsetTimeTypeHandler(); + // java.sql.Time doesn't contain millis, so set nano to 0 + private static final OffsetTime OFFSET_TIME = OffsetTime.now().withNano(0); + private static final Time TIME = Time.valueOf(OFFSET_TIME.toLocalTime()); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, OFFSET_TIME, null); + verify(ps).setTime(1, TIME); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getTime("column")).thenReturn(TIME); + assertEquals(OFFSET_TIME, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getTime("column")).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getTime(1)).thenReturn(TIME); + assertEquals(OFFSET_TIME, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getTime(1)).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getTime(1)).thenReturn(TIME); + assertEquals(OFFSET_TIME, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getTime(1)).thenReturn(null); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/YearMonthTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/YearMonthTypeHandlerTest.java new file mode 100644 index 00000000000..89429401822 --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/YearMonthTypeHandlerTest.java @@ -0,0 +1,85 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.time.YearMonth; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.TypeHandler; +import org.apache.ibatis.type.YearMonthTypeHandler; +import org.junit.Test; + +/** + * @author Björn Raupach + */ +public class YearMonthTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new YearMonthTypeHandler(); + private static final YearMonth INSTANT = YearMonth.now(); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, INSTANT, null); + verify(ps).setString(1, INSTANT.toString()); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getString("column")).thenReturn(INSTANT.toString()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getString(1)).thenReturn(INSTANT.toString()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getString(1)).thenReturn(INSTANT.toString()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } + +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/YearTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/YearTypeHandlerTest.java new file mode 100644 index 00000000000..f7b5adbdfb0 --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/YearTypeHandlerTest.java @@ -0,0 +1,89 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.time.Year; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.TypeHandler; +import org.apache.ibatis.type.YearTypeHandler; +import org.junit.Test; + +/** + * + * @author Eduardo Macarron + */ +public class YearTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new YearTypeHandler(); + private static final Year INSTANT = Year.now(); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, INSTANT, null); + verify(ps).setInt(1, INSTANT.getValue()); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getInt("column")).thenReturn(INSTANT.getValue()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getInt("column")).thenReturn(0); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getInt(1)).thenReturn(INSTANT.getValue()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getInt(1)).thenReturn(0); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getInt(1)).thenReturn(INSTANT.getValue()); + assertEquals(INSTANT, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getInt(1)).thenReturn(0); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } + +} diff --git a/src/test/java/org/apache/ibatis/type/usesjava8/ZonedDateTimeTypeHandlerTest.java b/src/test/java/org/apache/ibatis/type/usesjava8/ZonedDateTimeTypeHandlerTest.java new file mode 100644 index 00000000000..b15cf16ae9c --- /dev/null +++ b/src/test/java/org/apache/ibatis/type/usesjava8/ZonedDateTimeTypeHandlerTest.java @@ -0,0 +1,87 @@ +/** + * 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.type.usesjava8; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.sql.Timestamp; +import java.time.ZonedDateTime; + +import org.apache.ibatis.type.BaseTypeHandlerTest; +import org.apache.ibatis.type.TypeHandler; +import org.apache.ibatis.type.ZonedDateTimeTypeHandler; +import org.junit.Test; + +public class ZonedDateTimeTypeHandlerTest extends BaseTypeHandlerTest { + + private static final TypeHandler TYPE_HANDLER = new ZonedDateTimeTypeHandler(); + private static final ZonedDateTime ZONED_DATE_TIME = ZonedDateTime.now(); + private static final Timestamp TIMESTAMP = Timestamp.from(ZONED_DATE_TIME.toInstant()); + + @Override + @Test + public void shouldSetParameter() throws Exception { + TYPE_HANDLER.setParameter(ps, 1, ZONED_DATE_TIME, null); + verify(ps).setTimestamp(1, TIMESTAMP); + } + + @Override + @Test + public void shouldGetResultFromResultSetByName() throws Exception { + when(rs.getTimestamp("column")).thenReturn(TIMESTAMP); + assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByName() throws Exception { + when(rs.getTimestamp("column")).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, "column")); + } + + @Override + @Test + public void shouldGetResultFromResultSetByPosition() throws Exception { + when(rs.getTimestamp(1)).thenReturn(TIMESTAMP); + assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromResultSetByPosition() throws Exception { + when(rs.getTimestamp(1)).thenReturn(null); + when(rs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(rs, 1)); + } + + @Override + @Test + public void shouldGetResultFromCallableStatement() throws Exception { + when(cs.getTimestamp(1)).thenReturn(TIMESTAMP); + assertEquals(ZONED_DATE_TIME, TYPE_HANDLER.getResult(cs, 1)); + } + + @Override + @Test + public void shouldGetResultNullFromCallableStatement() throws Exception { + when(cs.getTimestamp(1)).thenReturn(null); + when(cs.wasNull()).thenReturn(true); + assertNull(TYPE_HANDLER.getResult(cs, 1)); + } + +}