Repository: cayenne Updated Branches: refs/heads/master 8a444d194 -> 9db82246d
CAY-1626 | Add JodaTime DateTime support * cleanup, proper packages Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/9db82246 Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/9db82246 Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/9db82246 Branch: refs/heads/master Commit: 9db82246d32550fa06929ab97c6bd3b523de0867 Parents: 8a444d1 Author: aadamchik <[email protected]> Authored: Sun May 10 11:01:36 2015 -0400 Committer: aadamchik <[email protected]> Committed: Sun May 10 11:26:03 2015 -0400 ---------------------------------------------------------------------- .../org/apache/cayenne/CayenneJodaModule.java | 48 ------- .../cayenne/access/types/DateTimeType.java | 79 ----------- .../cayenne/access/types/LocalDateTimeType.java | 80 ----------- .../cayenne/access/types/LocalDateType.java | 88 ------------ .../cayenne/access/types/LocalTimeType.java | 92 ------------- .../apache/cayenne/joda/CayenneJodaModule.java | 48 +++++++ .../cayenne/joda/access/types/DateTimeType.java | 75 ++++++++++ .../joda/access/types/LocalDateTimeType.java | 76 ++++++++++ .../joda/access/types/LocalDateType.java | 84 ++++++++++++ .../joda/access/types/LocalTimeType.java | 88 ++++++++++++ .../apache/cayenne/joda/DateTimeTypeTest.java | 62 --------- .../org/apache/cayenne/joda/JodaTestCase.java | 40 ------ .../org/apache/cayenne/joda/JodaTimeIT.java | 137 +++++++++---------- .../cayenne/joda/LocalDateTimeTypeTest.java | 64 --------- .../apache/cayenne/joda/LocalDateTypeTest.java | 76 ---------- .../apache/cayenne/joda/LocalTimeTypeTest.java | 76 ---------- .../joda/access/types/DateTimeTypeTest.java | 63 +++++++++ .../cayenne/joda/access/types/JodaTestCase.java | 40 ++++++ .../access/types/LocalDateTimeTypeTest.java | 65 +++++++++ .../joda/access/types/LocalDateTypeTest.java | 78 +++++++++++ .../joda/access/types/LocalTimeTypeTest.java | 78 +++++++++++ 21 files changed, 761 insertions(+), 776 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/CayenneJodaModule.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/CayenneJodaModule.java b/cayenne-joda/src/main/java/org/apache/cayenne/CayenneJodaModule.java deleted file mode 100644 index a481ea3..0000000 --- a/cayenne-joda/src/main/java/org/apache/cayenne/CayenneJodaModule.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.apache.cayenne; /** - * ************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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. - * ************************************************************** - */ - -import org.apache.cayenne.access.types.DateTimeType; -import org.apache.cayenne.access.types.LocalDateTimeType; -import org.apache.cayenne.access.types.LocalDateType; -import org.apache.cayenne.access.types.LocalTimeType; -import org.apache.cayenne.configuration.Constants; -import org.apache.cayenne.di.Binder; -import org.apache.cayenne.di.Module; - -/** - * Include this module when creating a ServerRuntime in order to add - + support for joda-time ObjAttributes. - */ -public class CayenneJodaModule implements Module { - - public CayenneJodaModule() { - } - - @Override - public void configure(Binder binder) { - binder - .bindList(Constants.SERVER_DEFAULT_TYPES_LIST) - .add(new DateTimeType()) - .add(new LocalDateType()) - .add(new LocalTimeType()) - .add(new LocalDateTimeType()); - } -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/access/types/DateTimeType.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/access/types/DateTimeType.java b/cayenne-joda/src/main/java/org/apache/cayenne/access/types/DateTimeType.java deleted file mode 100644 index 77d5f46..0000000 --- a/cayenne-joda/src/main/java/org/apache/cayenne/access/types/DateTimeType.java +++ /dev/null @@ -1,79 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.cayenne.access.types; - -import org.joda.time.DateTime; - -import java.sql.CallableStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Timestamp; - -/** - * Handles <code>org.joda.time.DateTime</code> type mapping. - */ -public class DateTimeType implements ExtendedType { - - @Override - public String getClassName() { - return DateTime.class.getName(); - } - - @Override - public DateTime materializeObject(ResultSet rs, int index, int type) - throws Exception { - if (rs.getTimestamp(index) != null) { - return new DateTime(rs.getTimestamp(index).getTime()); - } else { - return null; - } - } - - @Override - public DateTime materializeObject(CallableStatement rs, int index, int type) - throws Exception { - if (rs.getTimestamp(index) != null) { - return new DateTime(rs.getTimestamp(index).getTime()); - } else { - return null; - } - } - - @Override - public void setJdbcObject( - PreparedStatement statement, - Object value, - int pos, - int type, - int scale) throws Exception { - - if (value == null) { - statement.setNull(pos, type); - } else { - Timestamp ts = new Timestamp(getMillis(value)); - statement.setTimestamp(pos, ts); - } - } - - protected long getMillis(Object value) { - return ((DateTime) value).getMillis(); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalDateTimeType.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalDateTimeType.java b/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalDateTimeType.java deleted file mode 100644 index 7f6b197..0000000 --- a/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalDateTimeType.java +++ /dev/null @@ -1,80 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.cayenne.access.types; - -import org.joda.time.LocalDateTime; - -import java.sql.CallableStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Timestamp; -import java.sql.Types; - -/** - * Handles <code>org.joda.time.LocalDateTime</code> type mapping. - */ -public class LocalDateTimeType implements ExtendedType { - - @Override - public String getClassName() { - return LocalDateTime.class.getName(); - } - - @Override - public LocalDateTime materializeObject(ResultSet rs, int index, int type) - throws Exception { - if (rs.getTimestamp(index) != null) { - return new LocalDateTime(rs.getTimestamp(index).getTime()); - } else { - return null; - } - } - - @Override - public LocalDateTime materializeObject(CallableStatement rs, int index, int type) - throws Exception { - if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { - return new LocalDateTime(rs.getTimestamp(index).getTime()); - } else { - return null; - } - } - - @Override - public void setJdbcObject( - PreparedStatement statement, - Object value, - int pos, - int type, - int scale) throws Exception { - - if (value == null) { - statement.setNull(pos, type); - } else { - Timestamp ts = new Timestamp(getMillis(value)); - statement.setTimestamp(pos, ts); - } - } - - protected long getMillis(Object value) { - return ((LocalDateTime) value).toDateTime().getMillis(); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalDateType.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalDateType.java b/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalDateType.java deleted file mode 100644 index 3635365..0000000 --- a/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalDateType.java +++ /dev/null @@ -1,88 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.cayenne.access.types; - -import org.joda.time.LocalDate; - -import java.sql.CallableStatement; -import java.sql.Date; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Timestamp; -import java.sql.Types; - -/** - * Handles <code>org.joda.time.LocalDate</code> type mapping. - */ -public class LocalDateType implements ExtendedType { - - @Override - public String getClassName() { - return LocalDate.class.getName(); - } - - @Override - public LocalDate materializeObject(ResultSet rs, int index, int type) - throws Exception { - if (type == Types.DATE && rs.getDate(index) != null) { - return new LocalDate(rs.getDate(index).getTime()); - } else if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { - return new LocalDate(rs.getTimestamp(index).getTime()); - } else { - return null; - } - } - - @Override - public LocalDate materializeObject(CallableStatement rs, int index, int type) - throws Exception { - if (type == Types.DATE && rs.getDate(index) != null) { - return new LocalDate(rs.getDate(index).getTime()); - } else if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { - return new LocalDate(rs.getTimestamp(index).getTime()); - } else { - return null; - } - } - - @Override - public void setJdbcObject( - PreparedStatement statement, - Object value, - int pos, - int type, - int scale) throws Exception { - - if (value == null) { - statement.setNull(pos, type); - } else { - if (type == Types.DATE) { - statement.setDate(pos, new Date(getMillis(value))); - } else { - statement.setTimestamp(pos, new Timestamp(getMillis(value))); - } - } - } - - protected long getMillis(Object value) { - return ((LocalDate) value).toDate().getTime(); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalTimeType.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalTimeType.java b/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalTimeType.java deleted file mode 100644 index 1e152ef..0000000 --- a/cayenne-joda/src/main/java/org/apache/cayenne/access/types/LocalTimeType.java +++ /dev/null @@ -1,92 +0,0 @@ -/***************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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.cayenne.access.types; - -import org.joda.time.DateTimeZone; -import org.joda.time.LocalDate; -import org.joda.time.LocalTime; - -import java.sql.CallableStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Time; -import java.sql.Timestamp; -import java.sql.Types; - -/** - * Handles <code>org.joda.time.LocalTime</code> type mapping. - */ -public class LocalTimeType implements ExtendedType { - - private static final LocalDate EPOCH = new LocalDate(0, DateTimeZone.UTC); - - @Override - public String getClassName() { - return LocalTime.class.getName(); - } - - @Override - public LocalTime materializeObject(ResultSet rs, int index, int type) - throws Exception { - if (type == Types.TIME && rs.getTime(index) != null) { - return new LocalTime(rs.getTime(index).getTime()); - } else if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { - return new LocalTime(rs.getTimestamp(index).getTime()); - } else { - return null; - } - } - - @Override - public LocalTime materializeObject(CallableStatement rs, int index, int type) - throws Exception { - if (type == Types.TIME && rs.getTime(index) != null) { - return new LocalTime(rs.getTime(index).getTime()); - } else if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { - return new LocalTime(rs.getTimestamp(index).getTime()); - } else { - return null; - } - } - - @Override - public void setJdbcObject( - PreparedStatement statement, - Object value, - int pos, - int type, - int scale) throws Exception { - - if (value == null) { - statement.setNull(pos, type); - } else { - if (type == Types.TIME) { - statement.setTime(pos, new Time(getMillis(value))); - } else { - statement.setTimestamp(pos, new Timestamp(getMillis(value))); - } - } - } - - protected long getMillis(Object value) { - return EPOCH.toDateTime((LocalTime) value).getMillis(); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/joda/CayenneJodaModule.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/joda/CayenneJodaModule.java b/cayenne-joda/src/main/java/org/apache/cayenne/joda/CayenneJodaModule.java new file mode 100644 index 0000000..d37e994 --- /dev/null +++ b/cayenne-joda/src/main/java/org/apache/cayenne/joda/CayenneJodaModule.java @@ -0,0 +1,48 @@ +package org.apache.cayenne.joda; + +/** + * ************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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. + * ************************************************************** + */ + +import org.apache.cayenne.configuration.Constants; +import org.apache.cayenne.di.Binder; +import org.apache.cayenne.di.Module; +import org.apache.cayenne.joda.access.types.DateTimeType; +import org.apache.cayenne.joda.access.types.LocalDateTimeType; +import org.apache.cayenne.joda.access.types.LocalDateType; +import org.apache.cayenne.joda.access.types.LocalTimeType; + +/** + * Include this module when creating a ServerRuntime in order to add support for + * joda-time ObjAttributes. + * + * @since 4.0 + */ +public class CayenneJodaModule implements Module { + + public CayenneJodaModule() { + } + + @Override + public void configure(Binder binder) { + binder.bindList(Constants.SERVER_DEFAULT_TYPES_LIST).add(new DateTimeType()).add(new LocalDateType()) + .add(new LocalTimeType()).add(new LocalDateTimeType()); + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/DateTimeType.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/DateTimeType.java b/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/DateTimeType.java new file mode 100644 index 0000000..2af5e91 --- /dev/null +++ b/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/DateTimeType.java @@ -0,0 +1,75 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.cayenne.joda.access.types; + +import org.apache.cayenne.access.types.ExtendedType; +import org.joda.time.DateTime; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Timestamp; + +/** + * Handles <code>org.joda.time.DateTime</code> type mapping. + * + * @since 4.0 + */ +public class DateTimeType implements ExtendedType { + + @Override + public String getClassName() { + return DateTime.class.getName(); + } + + @Override + public DateTime materializeObject(ResultSet rs, int index, int type) throws Exception { + if (rs.getTimestamp(index) != null) { + return new DateTime(rs.getTimestamp(index).getTime()); + } else { + return null; + } + } + + @Override + public DateTime materializeObject(CallableStatement rs, int index, int type) throws Exception { + if (rs.getTimestamp(index) != null) { + return new DateTime(rs.getTimestamp(index).getTime()); + } else { + return null; + } + } + + @Override + public void setJdbcObject(PreparedStatement statement, Object value, int pos, int type, int scale) throws Exception { + + if (value == null) { + statement.setNull(pos, type); + } else { + Timestamp ts = new Timestamp(getMillis(value)); + statement.setTimestamp(pos, ts); + } + } + + protected long getMillis(Object value) { + return ((DateTime) value).getMillis(); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalDateTimeType.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalDateTimeType.java b/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalDateTimeType.java new file mode 100644 index 0000000..ea7fab7 --- /dev/null +++ b/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalDateTimeType.java @@ -0,0 +1,76 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.cayenne.joda.access.types; + +import org.apache.cayenne.access.types.ExtendedType; +import org.joda.time.LocalDateTime; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Timestamp; +import java.sql.Types; + +/** + * Handles <code>org.joda.time.LocalDateTime</code> type mapping. + * + * @since 4.0 + */ +public class LocalDateTimeType implements ExtendedType { + + @Override + public String getClassName() { + return LocalDateTime.class.getName(); + } + + @Override + public LocalDateTime materializeObject(ResultSet rs, int index, int type) throws Exception { + if (rs.getTimestamp(index) != null) { + return new LocalDateTime(rs.getTimestamp(index).getTime()); + } else { + return null; + } + } + + @Override + public LocalDateTime materializeObject(CallableStatement rs, int index, int type) throws Exception { + if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { + return new LocalDateTime(rs.getTimestamp(index).getTime()); + } else { + return null; + } + } + + @Override + public void setJdbcObject(PreparedStatement statement, Object value, int pos, int type, int scale) throws Exception { + + if (value == null) { + statement.setNull(pos, type); + } else { + Timestamp ts = new Timestamp(getMillis(value)); + statement.setTimestamp(pos, ts); + } + } + + protected long getMillis(Object value) { + return ((LocalDateTime) value).toDateTime().getMillis(); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalDateType.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalDateType.java b/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalDateType.java new file mode 100644 index 0000000..7a0a9a0 --- /dev/null +++ b/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalDateType.java @@ -0,0 +1,84 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.cayenne.joda.access.types; + +import java.sql.CallableStatement; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Timestamp; +import java.sql.Types; + +import org.apache.cayenne.access.types.ExtendedType; +import org.joda.time.LocalDate; + +/** + * Handles <code>org.joda.time.LocalDate</code> type mapping. + * + * @since 4.0 + */ +public class LocalDateType implements ExtendedType { + + @Override + public String getClassName() { + return LocalDate.class.getName(); + } + + @Override + public LocalDate materializeObject(ResultSet rs, int index, int type) throws Exception { + if (type == Types.DATE && rs.getDate(index) != null) { + return new LocalDate(rs.getDate(index).getTime()); + } else if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { + return new LocalDate(rs.getTimestamp(index).getTime()); + } else { + return null; + } + } + + @Override + public LocalDate materializeObject(CallableStatement rs, int index, int type) throws Exception { + if (type == Types.DATE && rs.getDate(index) != null) { + return new LocalDate(rs.getDate(index).getTime()); + } else if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { + return new LocalDate(rs.getTimestamp(index).getTime()); + } else { + return null; + } + } + + @Override + public void setJdbcObject(PreparedStatement statement, Object value, int pos, int type, int scale) throws Exception { + + if (value == null) { + statement.setNull(pos, type); + } else { + if (type == Types.DATE) { + statement.setDate(pos, new Date(getMillis(value))); + } else { + statement.setTimestamp(pos, new Timestamp(getMillis(value))); + } + } + } + + protected long getMillis(Object value) { + return ((LocalDate) value).toDate().getTime(); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalTimeType.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalTimeType.java b/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalTimeType.java new file mode 100644 index 0000000..765c4ad --- /dev/null +++ b/cayenne-joda/src/main/java/org/apache/cayenne/joda/access/types/LocalTimeType.java @@ -0,0 +1,88 @@ +/***************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.cayenne.joda.access.types; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Time; +import java.sql.Timestamp; +import java.sql.Types; + +import org.apache.cayenne.access.types.ExtendedType; +import org.joda.time.DateTimeZone; +import org.joda.time.LocalDate; +import org.joda.time.LocalTime; + +/** + * Handles <code>org.joda.time.LocalTime</code> type mapping. + * + * @since 4.0 + */ +public class LocalTimeType implements ExtendedType { + + private static final LocalDate EPOCH = new LocalDate(0, DateTimeZone.UTC); + + @Override + public String getClassName() { + return LocalTime.class.getName(); + } + + @Override + public LocalTime materializeObject(ResultSet rs, int index, int type) throws Exception { + if (type == Types.TIME && rs.getTime(index) != null) { + return new LocalTime(rs.getTime(index).getTime()); + } else if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { + return new LocalTime(rs.getTimestamp(index).getTime()); + } else { + return null; + } + } + + @Override + public LocalTime materializeObject(CallableStatement rs, int index, int type) throws Exception { + if (type == Types.TIME && rs.getTime(index) != null) { + return new LocalTime(rs.getTime(index).getTime()); + } else if (type == Types.TIMESTAMP && rs.getTimestamp(index) != null) { + return new LocalTime(rs.getTimestamp(index).getTime()); + } else { + return null; + } + } + + @Override + public void setJdbcObject(PreparedStatement statement, Object value, int pos, int type, int scale) throws Exception { + + if (value == null) { + statement.setNull(pos, type); + } else { + if (type == Types.TIME) { + statement.setTime(pos, new Time(getMillis(value))); + } else { + statement.setTimestamp(pos, new Timestamp(getMillis(value))); + } + } + } + + protected long getMillis(Object value) { + return EPOCH.toDateTime((LocalTime) value).getMillis(); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/DateTimeTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/DateTimeTypeTest.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/DateTimeTypeTest.java deleted file mode 100644 index 97be842..0000000 --- a/cayenne-joda/src/test/java/org/apache/cayenne/joda/DateTimeTypeTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.apache.cayenne.joda; - -import com.mockrunner.mock.jdbc.MockConnection; -import com.mockrunner.mock.jdbc.MockPreparedStatement; -import org.apache.cayenne.access.types.DateTimeType; -import org.joda.time.DateTime; - -import java.sql.PreparedStatement; -import java.sql.Timestamp; -import java.sql.Types; - -/***************************************************************** -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you 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. -****************************************************************/ - -public class DateTimeTypeTest extends JodaTestCase { - - private DateTimeType type; - - @Override - protected void setUp() throws Exception { - super.setUp(); - type = new DateTimeType(); - } - - public void testMaterializeObjectTimestamp() throws Exception { - Object o = type.materializeObject(resultSet(new Timestamp(0)), 1, Types.TIMESTAMP); - assertEquals(new DateTime(0), o); - } - - public void testSetJdbcObject() throws Exception { - PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?"); - DateTime date = new DateTime(0); - - type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0); - - Object object = ((MockPreparedStatement) statement).getParameter(1); - assertEquals(Timestamp.class, object.getClass()); - assertEquals(date.getMillis(), ((Timestamp) object).getTime()); - - type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0); - - object = ((MockPreparedStatement) statement).getParameter(1); - assertNull(object); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTestCase.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTestCase.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTestCase.java deleted file mode 100644 index 5253aa3..0000000 --- a/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTestCase.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.apache.cayenne.joda; - -import com.mockrunner.mock.jdbc.MockResultSet; -import junit.framework.TestCase; - -import java.sql.ResultSet; -import java.sql.SQLException; - -/** - * ************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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. - * ************************************************************** - */ - -public abstract class JodaTestCase extends TestCase { - - ResultSet resultSet(Object value) throws SQLException { - MockResultSet rs = new MockResultSet("Test"); - rs.addColumn("Col"); - rs.addRow(new Object[]{value}); - rs.next(); - return rs; - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTimeIT.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTimeIT.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTimeIT.java index e360a9a..56040d3 100644 --- a/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTimeIT.java +++ b/cayenne-joda/src/test/java/org/apache/cayenne/joda/JodaTimeIT.java @@ -19,7 +19,11 @@ package org.apache.cayenne.joda; -import org.apache.cayenne.CayenneJodaModule; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.sql.SQLException; + import org.apache.cayenne.ObjectContext; import org.apache.cayenne.configuration.server.ServerRuntime; import org.apache.cayenne.di.Module; @@ -27,7 +31,7 @@ import org.apache.cayenne.joda.db.DateTimeTestEntity; import org.apache.cayenne.joda.db.LocalDateTestEntity; import org.apache.cayenne.joda.db.LocalDateTimeTestEntity; import org.apache.cayenne.joda.db.LocalTimeTestEntity; -import org.apache.cayenne.query.SelectQuery; +import org.apache.cayenne.query.ObjectSelect; import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.LocalDateTime; @@ -35,97 +39,88 @@ import org.joda.time.LocalTime; import org.junit.Before; import org.junit.Test; -import java.sql.SQLException; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - public class JodaTimeIT { - private ServerRuntime runtime; + private ServerRuntime runtime; - @Before - public void setUp() throws Exception { - Module jodaModule = new CayenneJodaModule(); - this.runtime = new ServerRuntime("cayenne-joda.xml", jodaModule); - } + @Before + public void setUp() throws Exception { + Module jodaModule = new CayenneJodaModule(); + this.runtime = new ServerRuntime("cayenne-joda.xml", jodaModule); + } - @Test - public void testJodaDateTime() throws SQLException { - ObjectContext context = runtime.newContext(); + @Test + public void testJodaDateTime() throws SQLException { + ObjectContext context = runtime.newContext(); - DateTimeTestEntity dateTimeTestEntity = context.newObject(DateTimeTestEntity.class); - DateTime dateTime = DateTime.now(); - dateTimeTestEntity.setTimestamp(dateTime); + DateTimeTestEntity dateTimeTestEntity = context.newObject(DateTimeTestEntity.class); + DateTime dateTime = DateTime.now(); + dateTimeTestEntity.setTimestamp(dateTime); - context.commitChanges(); + context.commitChanges(); - SelectQuery q = new SelectQuery(DateTimeTestEntity.class); - DateTimeTestEntity testRead = (DateTimeTestEntity) context.performQuery(q).get(0); + DateTimeTestEntity testRead = ObjectSelect.query(DateTimeTestEntity.class).selectOne(context); - DateTime timestamp = testRead.getTimestamp(); - assertNotNull(timestamp); - assertEquals(DateTime.class, timestamp.getClass()); - assertEquals(dateTime, timestamp); - } + DateTime timestamp = testRead.getTimestamp(); + assertNotNull(timestamp); + assertEquals(DateTime.class, timestamp.getClass()); + assertEquals(dateTime, timestamp); + } - @Test - public void testJodaLocalDate() { - ObjectContext context = runtime.newContext(); + @Test + public void testJodaLocalDate() { + ObjectContext context = runtime.newContext(); - LocalDateTestEntity localDateTestEntity = context.newObject(LocalDateTestEntity.class); - LocalDate localDate = LocalDate.now(); - localDateTestEntity.setDate(localDate); + LocalDateTestEntity localDateTestEntity = context.newObject(LocalDateTestEntity.class); + LocalDate localDate = LocalDate.now(); + localDateTestEntity.setDate(localDate); - context.commitChanges(); + context.commitChanges(); - SelectQuery q = new SelectQuery(LocalDateTestEntity.class); - LocalDateTestEntity testRead = (LocalDateTestEntity) context.performQuery(q).get(0); + LocalDateTestEntity testRead = ObjectSelect.query(LocalDateTestEntity.class).selectOne(context); - LocalDate date = testRead.getDate(); - assertNotNull(date); - assertEquals(LocalDate.class, date.getClass()); - assertEquals(localDate, date); - } + LocalDate date = testRead.getDate(); + assertNotNull(date); + assertEquals(LocalDate.class, date.getClass()); + assertEquals(localDate, date); + } - @Test - public void testJodaLocalTime() { - ObjectContext context = runtime.newContext(); + @Test + public void testJodaLocalTime() { + ObjectContext context = runtime.newContext(); - LocalTimeTestEntity localTimeTestEntity = context.newObject(LocalTimeTestEntity.class); - LocalTime localTime = LocalTime.now(); - localTimeTestEntity.setTime(localTime); + LocalTimeTestEntity localTimeTestEntity = context.newObject(LocalTimeTestEntity.class); + LocalTime localTime = LocalTime.now(); + localTimeTestEntity.setTime(localTime); - context.commitChanges(); + context.commitChanges(); - SelectQuery q = new SelectQuery(LocalTimeTestEntity.class); - LocalTimeTestEntity testRead = (LocalTimeTestEntity) context.performQuery(q).get(0); + LocalTimeTestEntity testRead = ObjectSelect.query(LocalTimeTestEntity.class).selectOne(context); - LocalTime time = testRead.getTime(); - assertNotNull(time); - assertEquals(LocalTime.class, time.getClass()); - assertEquals(localTime.getSecondOfMinute(), time.getSecondOfMinute()); - assertEquals(localTime.getMinuteOfHour(), time.getMinuteOfHour()); - assertEquals(localTime.getHourOfDay(), time.getHourOfDay()); - } + LocalTime time = testRead.getTime(); + assertNotNull(time); + assertEquals(LocalTime.class, time.getClass()); + assertEquals(localTime.getSecondOfMinute(), time.getSecondOfMinute()); + assertEquals(localTime.getMinuteOfHour(), time.getMinuteOfHour()); + assertEquals(localTime.getHourOfDay(), time.getHourOfDay()); + } - @Test - public void testJodaLocalDateTime() { - ObjectContext context = runtime.newContext(); + @Test + public void testJodaLocalDateTime() { + ObjectContext context = runtime.newContext(); - LocalDateTimeTestEntity localDateTimeTestEntity = context.newObject(LocalDateTimeTestEntity.class); - LocalDateTime localDateTime = LocalDateTime.now(); - localDateTimeTestEntity.setTimestamp(localDateTime); + LocalDateTimeTestEntity localDateTimeTestEntity = context.newObject(LocalDateTimeTestEntity.class); + LocalDateTime localDateTime = LocalDateTime.now(); + localDateTimeTestEntity.setTimestamp(localDateTime); - context.commitChanges(); + context.commitChanges(); - SelectQuery q = new SelectQuery(LocalDateTimeTestEntity.class); - LocalDateTimeTestEntity testRead = (LocalDateTimeTestEntity) context.performQuery(q).get(0); + LocalDateTimeTestEntity testRead = ObjectSelect.query(LocalDateTimeTestEntity.class).selectOne(context); - LocalDateTime timestamp = testRead.getTimestamp(); - assertNotNull(timestamp); - assertEquals(LocalDateTime.class, timestamp.getClass()); - assertEquals(localDateTime, timestamp); - } + LocalDateTime timestamp = testRead.getTimestamp(); + assertNotNull(timestamp); + assertEquals(LocalDateTime.class, timestamp.getClass()); + assertEquals(localDateTime, timestamp); + } } http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalDateTimeTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalDateTimeTypeTest.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalDateTimeTypeTest.java deleted file mode 100644 index 12369c8..0000000 --- a/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalDateTimeTypeTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.apache.cayenne.joda; - -import com.mockrunner.mock.jdbc.MockConnection; -import com.mockrunner.mock.jdbc.MockPreparedStatement; -import org.apache.cayenne.access.types.LocalDateTimeType; -import org.joda.time.LocalDateTime; - -import java.sql.PreparedStatement; -import java.sql.Timestamp; -import java.sql.Types; - -/** - * ************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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. - * ************************************************************** - */ - -public class LocalDateTimeTypeTest extends JodaTestCase { - - private LocalDateTimeType type; - - @Override - protected void setUp() throws Exception { - super.setUp(); - type = new LocalDateTimeType(); - } - - public void testMaterializeObjectTimestamp() throws Exception { - Object o = type.materializeObject(resultSet(new Timestamp(0)), 1, Types.TIMESTAMP); - assertEquals(new LocalDateTime(0), o); - } - - public void testSetJdbcObject() throws Exception { - PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?"); - LocalDateTime date = new LocalDateTime(0); - - type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0); - - Object object = ((MockPreparedStatement) statement).getParameter(1); - assertEquals(Timestamp.class, object.getClass()); - assertEquals(date.toDate().getTime(), ((Timestamp) object).getTime()); - - type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0); - - object = ((MockPreparedStatement) statement).getParameter(1); - assertNull(object); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalDateTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalDateTypeTest.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalDateTypeTest.java deleted file mode 100644 index c91ebc3..0000000 --- a/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalDateTypeTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.apache.cayenne.joda; - -import org.apache.cayenne.access.types.LocalDateType; -import com.mockrunner.mock.jdbc.MockConnection; -import com.mockrunner.mock.jdbc.MockPreparedStatement; -import org.joda.time.LocalDate; - -import java.sql.Date; -import java.sql.PreparedStatement; -import java.sql.Timestamp; -import java.sql.Types; - -/** - * ************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - * <p/> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p/> - * 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. - * ************************************************************** - */ - -public class LocalDateTypeTest extends JodaTestCase { - - private LocalDateType type; - - @Override - protected void setUp() throws Exception { - super.setUp(); - type = new LocalDateType(); - } - - public void testMaterializeObjectTimestamp() throws Exception { - Object o = type.materializeObject(resultSet(new Timestamp(0)), 1, Types.TIMESTAMP); - assertEquals(new LocalDate(0), o); - } - - public void testMaterializeObjectDate() throws Exception { - Object o = type.materializeObject(resultSet(new Date(0)), 1, Types.DATE); - assertEquals(new LocalDate(0), o); - } - - public void testSetJdbcObject() throws Exception { - PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?"); - LocalDate savedObject = new LocalDate(0); - - type.setJdbcObject(statement, savedObject, 1, Types.DATE, 0); - - Object object = ((MockPreparedStatement) statement).getParameter(1); - assertEquals(Date.class, object.getClass()); - assertEquals(savedObject.toDate().getTime(), ((Date) object).getTime()); - - type.setJdbcObject(statement, savedObject, 1, Types.TIMESTAMP, 0); - - object = ((MockPreparedStatement) statement).getParameter(1); - assertEquals(Timestamp.class, object.getClass()); - assertEquals(savedObject.toDate().getTime(), ((Timestamp) object).getTime()); - - type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0); - - object = ((MockPreparedStatement) statement).getParameter(1); - assertNull(object); - } - -} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalTimeTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalTimeTypeTest.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalTimeTypeTest.java deleted file mode 100644 index fc7b89f..0000000 --- a/cayenne-joda/src/test/java/org/apache/cayenne/joda/LocalTimeTypeTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.apache.cayenne.joda; - -import org.apache.cayenne.access.types.LocalTimeType; -import com.mockrunner.mock.jdbc.MockConnection; -import com.mockrunner.mock.jdbc.MockPreparedStatement; -import org.joda.time.DateTimeZone; -import org.joda.time.LocalDate; -import org.joda.time.LocalTime; - -import java.sql.PreparedStatement; -import java.sql.Time; -import java.sql.Timestamp; -import java.sql.Types; - -/***************************************************************** -* Licensed to the Apache Software Foundation (ASF) under one -* or more contributor license agreements. See the NOTICE file -* distributed with this work for additional information -* regarding copyright ownership. The ASF licenses this file -* to you 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. -****************************************************************/ - -public class LocalTimeTypeTest extends JodaTestCase { - - private LocalTimeType type; - - @Override - protected void setUp() throws Exception { - super.setUp(); - type = new LocalTimeType(); - } - - public void testMaterializeObjectTimestamp() throws Exception { - Object o = type.materializeObject(resultSet(new Timestamp(0)), 1, Types.TIMESTAMP); - assertEquals(new LocalTime(0), o); - } - - public void testMaterializeObjectTime() throws Exception { - Object o = type.materializeObject(resultSet(new Time(0)), 1, Types.TIME); - assertEquals(new LocalTime(0), o); - } - - public void testSetJdbcObject() throws Exception { - PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?"); - LocalTime date = new LocalTime(0); - - type.setJdbcObject(statement, date, 1, Types.TIME, 0); - - Object object = ((MockPreparedStatement) statement).getParameter(1); - assertEquals(Time.class, object.getClass()); - assertEquals(new LocalDate(0, DateTimeZone.UTC).toDateTime(date).getMillis(), ((Time) object).getTime()); - - type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0); - - object = ((MockPreparedStatement) statement).getParameter(1); - assertEquals(Timestamp.class, object.getClass()); - assertEquals(new LocalDate(0, DateTimeZone.UTC).toDateTime(date).getMillis(), ((Timestamp) object).getTime()); - - type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0); - - object = ((MockPreparedStatement) statement).getParameter(1); - assertNull(object); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/DateTimeTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/DateTimeTypeTest.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/DateTimeTypeTest.java new file mode 100644 index 0000000..748bab3 --- /dev/null +++ b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/DateTimeTypeTest.java @@ -0,0 +1,63 @@ +package org.apache.cayenne.joda.access.types; + +import com.mockrunner.mock.jdbc.MockConnection; +import com.mockrunner.mock.jdbc.MockPreparedStatement; + +import org.apache.cayenne.joda.access.types.DateTimeType; +import org.joda.time.DateTime; + +import java.sql.PreparedStatement; +import java.sql.Timestamp; +import java.sql.Types; + +/***************************************************************** +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you 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. +****************************************************************/ + +public class DateTimeTypeTest extends JodaTestCase { + + private DateTimeType type; + + @Override + protected void setUp() throws Exception { + super.setUp(); + type = new DateTimeType(); + } + + public void testMaterializeObjectTimestamp() throws Exception { + Object o = type.materializeObject(resultSet(new Timestamp(0)), 1, Types.TIMESTAMP); + assertEquals(new DateTime(0), o); + } + + public void testSetJdbcObject() throws Exception { + PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?"); + DateTime date = new DateTime(0); + + type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0); + + Object object = ((MockPreparedStatement) statement).getParameter(1); + assertEquals(Timestamp.class, object.getClass()); + assertEquals(date.getMillis(), ((Timestamp) object).getTime()); + + type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0); + + object = ((MockPreparedStatement) statement).getParameter(1); + assertNull(object); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/JodaTestCase.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/JodaTestCase.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/JodaTestCase.java new file mode 100644 index 0000000..91407eb --- /dev/null +++ b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/JodaTestCase.java @@ -0,0 +1,40 @@ +package org.apache.cayenne.joda.access.types; + +import com.mockrunner.mock.jdbc.MockResultSet; +import junit.framework.TestCase; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * ************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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. + * ************************************************************** + */ + +public abstract class JodaTestCase extends TestCase { + + ResultSet resultSet(Object value) throws SQLException { + MockResultSet rs = new MockResultSet("Test"); + rs.addColumn("Col"); + rs.addRow(new Object[]{value}); + rs.next(); + return rs; + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalDateTimeTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalDateTimeTypeTest.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalDateTimeTypeTest.java new file mode 100644 index 0000000..71038e8 --- /dev/null +++ b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalDateTimeTypeTest.java @@ -0,0 +1,65 @@ +package org.apache.cayenne.joda.access.types; + +import com.mockrunner.mock.jdbc.MockConnection; +import com.mockrunner.mock.jdbc.MockPreparedStatement; + +import org.apache.cayenne.joda.access.types.LocalDateTimeType; +import org.joda.time.LocalDateTime; + +import java.sql.PreparedStatement; +import java.sql.Timestamp; +import java.sql.Types; + +/** + * ************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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. + * ************************************************************** + */ + +public class LocalDateTimeTypeTest extends JodaTestCase { + + private LocalDateTimeType type; + + @Override + protected void setUp() throws Exception { + super.setUp(); + type = new LocalDateTimeType(); + } + + public void testMaterializeObjectTimestamp() throws Exception { + Object o = type.materializeObject(resultSet(new Timestamp(0)), 1, Types.TIMESTAMP); + assertEquals(new LocalDateTime(0), o); + } + + public void testSetJdbcObject() throws Exception { + PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?"); + LocalDateTime date = new LocalDateTime(0); + + type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0); + + Object object = ((MockPreparedStatement) statement).getParameter(1); + assertEquals(Timestamp.class, object.getClass()); + assertEquals(date.toDate().getTime(), ((Timestamp) object).getTime()); + + type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0); + + object = ((MockPreparedStatement) statement).getParameter(1); + assertNull(object); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalDateTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalDateTypeTest.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalDateTypeTest.java new file mode 100644 index 0000000..61f8f95 --- /dev/null +++ b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalDateTypeTest.java @@ -0,0 +1,78 @@ +package org.apache.cayenne.joda.access.types; + +import org.apache.cayenne.joda.access.types.LocalDateType; + +import com.mockrunner.mock.jdbc.MockConnection; +import com.mockrunner.mock.jdbc.MockPreparedStatement; + +import org.joda.time.LocalDate; + +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.Timestamp; +import java.sql.Types; + +/** + * ************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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. + * ************************************************************** + */ + +public class LocalDateTypeTest extends JodaTestCase { + + private LocalDateType type; + + @Override + protected void setUp() throws Exception { + super.setUp(); + type = new LocalDateType(); + } + + public void testMaterializeObjectTimestamp() throws Exception { + Object o = type.materializeObject(resultSet(new Timestamp(0)), 1, Types.TIMESTAMP); + assertEquals(new LocalDate(0), o); + } + + public void testMaterializeObjectDate() throws Exception { + Object o = type.materializeObject(resultSet(new Date(0)), 1, Types.DATE); + assertEquals(new LocalDate(0), o); + } + + public void testSetJdbcObject() throws Exception { + PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?"); + LocalDate savedObject = new LocalDate(0); + + type.setJdbcObject(statement, savedObject, 1, Types.DATE, 0); + + Object object = ((MockPreparedStatement) statement).getParameter(1); + assertEquals(Date.class, object.getClass()); + assertEquals(savedObject.toDate().getTime(), ((Date) object).getTime()); + + type.setJdbcObject(statement, savedObject, 1, Types.TIMESTAMP, 0); + + object = ((MockPreparedStatement) statement).getParameter(1); + assertEquals(Timestamp.class, object.getClass()); + assertEquals(savedObject.toDate().getTime(), ((Timestamp) object).getTime()); + + type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0); + + object = ((MockPreparedStatement) statement).getParameter(1); + assertNull(object); + } + +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/9db82246/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalTimeTypeTest.java ---------------------------------------------------------------------- diff --git a/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalTimeTypeTest.java b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalTimeTypeTest.java new file mode 100644 index 0000000..adfcd4b --- /dev/null +++ b/cayenne-joda/src/test/java/org/apache/cayenne/joda/access/types/LocalTimeTypeTest.java @@ -0,0 +1,78 @@ +package org.apache.cayenne.joda.access.types; + +import org.apache.cayenne.joda.access.types.LocalTimeType; + +import com.mockrunner.mock.jdbc.MockConnection; +import com.mockrunner.mock.jdbc.MockPreparedStatement; + +import org.joda.time.DateTimeZone; +import org.joda.time.LocalDate; +import org.joda.time.LocalTime; + +import java.sql.PreparedStatement; +import java.sql.Time; +import java.sql.Timestamp; +import java.sql.Types; + +/***************************************************************** +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you 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. +****************************************************************/ + +public class LocalTimeTypeTest extends JodaTestCase { + + private LocalTimeType type; + + @Override + protected void setUp() throws Exception { + super.setUp(); + type = new LocalTimeType(); + } + + public void testMaterializeObjectTimestamp() throws Exception { + Object o = type.materializeObject(resultSet(new Timestamp(0)), 1, Types.TIMESTAMP); + assertEquals(new LocalTime(0), o); + } + + public void testMaterializeObjectTime() throws Exception { + Object o = type.materializeObject(resultSet(new Time(0)), 1, Types.TIME); + assertEquals(new LocalTime(0), o); + } + + public void testSetJdbcObject() throws Exception { + PreparedStatement statement = new MockPreparedStatement(new MockConnection(), "update t set c = ?"); + LocalTime date = new LocalTime(0); + + type.setJdbcObject(statement, date, 1, Types.TIME, 0); + + Object object = ((MockPreparedStatement) statement).getParameter(1); + assertEquals(Time.class, object.getClass()); + assertEquals(new LocalDate(0, DateTimeZone.UTC).toDateTime(date).getMillis(), ((Time) object).getTime()); + + type.setJdbcObject(statement, date, 1, Types.TIMESTAMP, 0); + + object = ((MockPreparedStatement) statement).getParameter(1); + assertEquals(Timestamp.class, object.getClass()); + assertEquals(new LocalDate(0, DateTimeZone.UTC).toDateTime(date).getMillis(), ((Timestamp) object).getTime()); + + type.setJdbcObject(statement, null, 1, Types.TIMESTAMP, 0); + + object = ((MockPreparedStatement) statement).getParameter(1); + assertNull(object); + } + +} \ No newline at end of file
