Repository: ignite Updated Branches: refs/heads/master 7414c9956 -> 418778076
IGNITE-10594 Remove specific JSR310 handling, add java.time test to regular suite. - Fixes #5736. Fixes #5605. Signed-off-by: Ilya Kasnacheev <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/41877807 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/41877807 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/41877807 Branch: refs/heads/master Commit: 4187780760bd67723db72c70e69205fe933d37e6 Parents: 7414c99 Author: Ilya Kasnacheev <[email protected]> Authored: Mon Dec 24 15:00:45 2018 +0300 Committer: Ilya Kasnacheev <[email protected]> Committed: Mon Dec 24 15:00:45 2018 +0300 ---------------------------------------------------------------------- .../internal/processors/query/QueryUtils.java | 9 +- .../util/Jsr310Java8DateTimeApiUtils.java | 80 ---- .../processors/query/h2/H2DatabaseType.java | 14 +- .../processors/query/h2/dml/DmlUtils.java | 43 +- ...cheQueryEntityWithDateTimeApiFieldsTest.java | 405 +++++++++++++++++++ .../IgniteBinaryCacheQueryTestSuite2.java | 3 + ...ityWithJsr310Java8DateTimeApiFieldsTest.java | 363 ----------------- ...QueryJsr310Java8DateTimeApiAbstractTest.java | 72 ---- ...yJsr310Java8DateTimeApiSupportTestSuite.java | 42 -- 9 files changed, 441 insertions(+), 590 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java index e251bd6..c90c45c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/QueryUtils.java @@ -21,6 +21,9 @@ import java.lang.reflect.Method; import java.math.BigDecimal; import java.sql.Time; import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -58,7 +61,6 @@ import org.apache.ignite.internal.processors.query.property.QueryMethodsAccessor import org.apache.ignite.internal.processors.query.property.QueryPropertyAccessor; import org.apache.ignite.internal.processors.query.property.QueryReadOnlyMethodsAccessor; import org.apache.ignite.internal.processors.query.schema.SchemaOperationException; -import org.apache.ignite.internal.util.Jsr310Java8DateTimeApiUtils; import org.apache.ignite.internal.util.typedef.F; import org.apache.ignite.internal.util.typedef.internal.A; import org.apache.ignite.internal.util.typedef.internal.U; @@ -126,13 +128,14 @@ public class QueryUtils { Timestamp.class, Date.class, java.sql.Date.class, + LocalTime.class, + LocalDate.class, + LocalDateTime.class, String.class, UUID.class, byte[].class )); - sqlClasses.addAll(Jsr310Java8DateTimeApiUtils.jsr310ApiClasses()); - return sqlClasses; } http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/core/src/main/java/org/apache/ignite/internal/util/Jsr310Java8DateTimeApiUtils.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/Jsr310Java8DateTimeApiUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/util/Jsr310Java8DateTimeApiUtils.java deleted file mode 100644 index 9febf19..0000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/Jsr310Java8DateTimeApiUtils.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.ignite.internal.util; - -import org.apache.ignite.internal.util.typedef.internal.U; -import org.jetbrains.annotations.NotNull; - -import java.util.ArrayList; -import java.util.Collection; - -/** - * Provides utility functions for JSR-310 Java 8 Date and Time API types - * based on reflection. - */ -public final class Jsr310Java8DateTimeApiUtils { - /** Class<java.time.LocalTime>. */ - private static final Class<?> LOCAL_TIME_CLASS = U.classForName("java.time.LocalTime", null); - - /** Class<java.time.LocalDate>. */ - private static final Class<?> LOCAL_DATE_CLASS = U.classForName("java.time.LocalDate", null); - - /** Class<java.time.LocalDateTime>. */ - private static final Class<?> LOCAL_DATE_TIME_CLASS = U.classForName("java.time.LocalDateTime", null); - - /** JSR-310 API classes. */ - private static final Collection<Class<?>> JSR_310_API_CLASSES = createJsr310ApiClassesCollection(); - - /** - * Creates a collection of the available JSR-310 classes. - * - * @return Collection of the available JSR-310 classes. - */ - @NotNull private static Collection<Class<?>> createJsr310ApiClassesCollection() { - Collection<Class<?>> res = new ArrayList<>(3); - - if (LOCAL_DATE_CLASS != null) - res.add(LOCAL_DATE_CLASS); - - if (LOCAL_TIME_CLASS != null) - res.add(LOCAL_TIME_CLASS); - - if (LOCAL_DATE_TIME_CLASS != null) - res.add(LOCAL_DATE_TIME_CLASS); - - return res; - } - - /** - * Default private constructor. - * - * <p>Prevents creation of instances of this class.</p> - */ - private Jsr310Java8DateTimeApiUtils() { - // No-op - } - - /** - * Returns the available JSR-310 classes. - * - * @return Available JSR-310 classes. - */ - @NotNull public static Collection<Class<?>> jsr310ApiClasses() { - return JSR_310_API_CLASSES; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2DatabaseType.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2DatabaseType.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2DatabaseType.java index 3d7faca..ea7449a 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2DatabaseType.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2DatabaseType.java @@ -143,14 +143,12 @@ public enum H2DatabaseType { if (DataType.isGeometryClass(cls)) return GEOMETRY; - if (LocalDateTimeUtils.isJava8DateApiPresent()) { - if (LocalDateTimeUtils.LOCAL_DATE == cls) - return DATE; - else if (LocalDateTimeUtils.LOCAL_TIME == cls) - return TIME; - else if (LocalDateTimeUtils.LOCAL_DATE_TIME == cls) - return TIMESTAMP; - } + if (LocalDateTimeUtils.LOCAL_DATE == cls) + return DATE; + else if (LocalDateTimeUtils.LOCAL_TIME == cls) + return TIME; + else if (LocalDateTimeUtils.LOCAL_DATE_TIME == cls) + return TIMESTAMP; return cls.isArray() && !cls.getComponentType().isPrimitive() ? ARRAY : OTHER; } http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/dml/DmlUtils.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/dml/DmlUtils.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/dml/DmlUtils.java index 36355fb..7be823d 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/dml/DmlUtils.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/dml/DmlUtils.java @@ -59,41 +59,41 @@ public class DmlUtils { Class<?> currCls = val.getClass(); try { - if (val instanceof Date && currCls != Date.class && expCls == Date.class) { - // H2 thinks that java.util.Date is always a Timestamp, while binary marshaller expects - // precise Date instance. Let's satisfy it. + // H2 thinks that java.util.Date is always a Timestamp, while binary marshaller expects + // precise Date instance. Let's satisfy it. + if (val instanceof Date && currCls != Date.class && expCls == Date.class) return new Date(((Date) val).getTime()); - } // User-given UUID is always serialized by H2 to byte array, so we have to deserialize manually - if (type == Value.UUID && currCls == byte[].class) - return U.unmarshal(desc.context().marshaller(), (byte[]) val, + if (type == Value.UUID && currCls == byte[].class) { + return U.unmarshal(desc.context().marshaller(), (byte[])val, U.resolveClassLoader(desc.context().gridConfig())); + } - if (LocalDateTimeUtils.isJava8DateApiPresent()) { - if (val instanceof Timestamp && LocalDateTimeUtils.LOCAL_DATE_TIME ==expCls) - return LocalDateTimeUtils.valueToLocalDateTime(ValueTimestamp.get((Timestamp) val)); - - if (val instanceof Date && LocalDateTimeUtils.LOCAL_DATE == expCls) - return LocalDateTimeUtils.valueToLocalDate(ValueDate.fromDateValue( - DateTimeUtils.dateValueFromDate(((Date) val).getTime()))); + if (val instanceof Timestamp && LocalDateTimeUtils.LOCAL_DATE_TIME == expCls) + return LocalDateTimeUtils.valueToLocalDateTime(ValueTimestamp.get((Timestamp)val)); - if (val instanceof Time && LocalDateTimeUtils.LOCAL_TIME == expCls) - return LocalDateTimeUtils.valueToLocalTime(ValueTime.get((Time) val)); + if (val instanceof Date && LocalDateTimeUtils.LOCAL_DATE == expCls) { + return LocalDateTimeUtils.valueToLocalDate(ValueDate.fromDateValue( + DateTimeUtils.dateValueFromDate(((Date)val).getTime()))); } + if (val instanceof Time && LocalDateTimeUtils.LOCAL_TIME == expCls) + return LocalDateTimeUtils.valueToLocalTime(ValueTime.get((Time)val)); + // We have to convert arrays of reference types manually - // see https://issues.apache.org/jira/browse/IGNITE-4327 // Still, we only can convert from Object[] to something more precise. if (type == Value.ARRAY && currCls != expCls) { - if (currCls != Object[].class) + if (currCls != Object[].class) { throw new IgniteCheckedException("Unexpected array type - only conversion from Object[] " + "is assumed"); + } // Why would otherwise type be Value.ARRAY? assert expCls.isArray(); - Object[] curr = (Object[]) val; + Object[] curr = (Object[])val; Object newArr = Array.newInstance(expCls.getComponentType(), curr.length); @@ -104,11 +104,10 @@ public class DmlUtils { Object res = H2Utils.convert(val, desc.indexing(), type); - if (res instanceof Date && res.getClass() != Date.class && expCls == Date.class) { - // We can get a Timestamp instead of Date when converting a String to Date - // without query - let's handle this - return new Date(((Date) res).getTime()); - } + // We can get a Timestamp instead of Date when converting a String to Date + // without query - let's handle this + if (res instanceof Date && res.getClass() != Date.class && expCls == Date.class) + return new Date(((Date)res).getTime()); return res; } http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/CacheQueryEntityWithDateTimeApiFieldsTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/CacheQueryEntityWithDateTimeApiFieldsTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/CacheQueryEntityWithDateTimeApiFieldsTest.java new file mode 100644 index 0000000..d340b02 --- /dev/null +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/CacheQueryEntityWithDateTimeApiFieldsTest.java @@ -0,0 +1,405 @@ +/* + * 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.ignite.internal.processors.query.h2; + +import java.io.Serializable; +import java.sql.Date; +import java.sql.Time; +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.List; +import java.util.Objects; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.CacheWriteSynchronizationMode; +import org.apache.ignite.cache.query.SqlFieldsQuery; +import org.apache.ignite.cache.query.annotations.QuerySqlField; +import org.apache.ignite.configuration.CacheConfiguration; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +/** + * Tests queries against entities with Java 8 Date and Time API fields. + */ +@RunWith(JUnit4.class) +public class CacheQueryEntityWithDateTimeApiFieldsTest extends GridCommonAbstractTest { + /** + * The number of days subtracted from the current time when constructing + * {@link LocalDate} and {@link LocalDateTime} + * instances. + */ + private static final long DAYS_BEFORE_NOW = 10; + + /** {@link LocalTime} instance. */ + private static final LocalTime SAMPLE_TIME = LocalTime.now().minusHours(10); + + /** {@link LocalDate} instance. */ + private static final LocalDate SAMPLE_DATE = LocalDate.now().minusDays(DAYS_BEFORE_NOW); + + /** {@link LocalDateTime} instance. */ + private static final LocalDateTime SAMPLE_DATE_TIME = LocalDateTime.of(SAMPLE_DATE, LocalTime.MIDNIGHT); + + /** Cache. */ + private IgniteCache<Long, EntityWithDateTimeFields> cache; + + /** Entity with Date and Time fields instance. */ + private final EntityWithDateTimeFields entity = + new EntityWithDateTimeFields(1L, SAMPLE_TIME, SAMPLE_DATE, SAMPLE_DATE_TIME); + + /** + * Creates a cache configuration with the specified cache name + * and indexed type key/value pairs. + * + * @param cacheName Cache name + * @param indexedTypes key/value pairs according to {@link CacheConfiguration#setIndexedTypes(Class[])}. + * @param <K> Key type. + * @param <V> Value type. + * @return Cache configuration. + */ + protected static <K, V> CacheConfiguration<K, V> createCacheConfig(String cacheName, Class<?>... indexedTypes) { + return new CacheConfiguration<K, V>(cacheName) + .setCacheMode(CacheMode.REPLICATED) + .setAtomicityMode(CacheAtomicityMode.ATOMIC) + .setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC) + .setIndexedTypes(indexedTypes); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + } + + /** + * Creates a cache configuration. + * + * @return Cache configuration. + */ + private static CacheConfiguration<Long, EntityWithDateTimeFields> createCacheConfig() { + return createCacheConfig( + "entityWithJava8DataTimeFields", Long.class, EntityWithDateTimeFields.class + ); + } + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + Ignite ignite = startGridsMultiThreaded(1, true); + cache = ignite.getOrCreateCache(createCacheConfig()); + + cache.put(entity.getId(), entity); + } + + /** + * Tests insertion of an entity. + * + * @throws Exception If failed. + */ + @Test + public void testInsertEntityFields() throws Exception { + cache.remove(entity.getId()); + + assertEquals(0, cache.size()); + + SqlFieldsQuery qry = new SqlFieldsQuery( + "insert into EntityWithDateTimeFields(_key, id, locTime, locDate, locDateTime) values(?, ?, ?, ?, ?)" + ).setArgs( + entity.getId(), entity.getId(), entity.getLocalTime(), entity.getLocalDate(), entity.getLocalDateTime() + ); + + List<List<?>> qryResults = cache.query(qry).getAll(); + + assertEquals(1, qryResults.size()); + assertEquals(1L, qryResults.get(0).get(0)); + assertEquals(1, cache.size()); + assertEquals(entity, cache.get(entity.getId())); + } + + /** + * Tests that DATEDIFF SQL function works for {@link LocalDateTime} + * fields with the time part set to midnight. + * + * @throws Exception If failed. + */ + @Test + public void testDateDiffForLocalDateTimeFieldAtMidnight() throws Exception { + SqlFieldsQuery qry = + new SqlFieldsQuery("select DATEDIFF('DAY', locDateTime, CURRENT_DATE ()) from EntityWithDateTimeFields"); + + List<List<?>> qryResults = cache.query(qry).getAll(); + + assertEquals(1, qryResults.size()); + assertTrue((Long)qryResults.get(0).get(0) >= DAYS_BEFORE_NOW); + } + + /** + * Tests that selection for a {@link LocalTime} field returns {@link Time}. + * + * @throws Exception If failed. + */ + @Test + public void testSelectLocalTimeFieldReturnsTime() throws Exception { + SqlFieldsQuery qry = new SqlFieldsQuery("select locTime from EntityWithDateTimeFields"); + + List<List<?>> qryResults = cache.query(qry).getAll(); + + assertEquals(1, qryResults.size()); + assertEquals(Time.class, qryResults.get(0).get(0).getClass()); + } + + /** + * Tests that selection for a {@link LocalDate} field returns {@link Date}. + * + * @throws Exception If failed. + */ + @Test + public void testSelectLocalDateFieldReturnsDate() throws Exception { + SqlFieldsQuery qry = new SqlFieldsQuery("select locDate from EntityWithDateTimeFields"); + + List<List<?>> qryResults = cache.query(qry).getAll(); + + assertEquals(1, qryResults.size()); + assertEquals(Date.class, qryResults.get(0).get(0).getClass()); + } + + /** + * Tests that selection for a {@link LocalDateTime} field returns {@link Timestamp}. + * + * @throws Exception If failed. + */ + @Test + public void testSelectLocalDateTimeFieldReturnsTimestamp() throws Exception { + SqlFieldsQuery qry = new SqlFieldsQuery("select locDateTime from EntityWithDateTimeFields"); + + List<List<?>> qryResults = cache.query(qry).getAll(); + + assertEquals(1, qryResults.size()); + assertEquals(Timestamp.class, qryResults.get(0).get(0).getClass()); + } + + /** + * Tests selection of an entity by a {@link LocalTime} field. + */ + @Test + public void testSelectByAllFields() { + SqlFieldsQuery qry = new SqlFieldsQuery( + "select locDate from EntityWithDateTimeFields where locTime = ? and locDate = ? and locDateTime = ?" + ).setArgs(entity.getLocalTime(), entity.getLocalDate(), entity.getLocalDateTime()); + + List<List<?>> qryResults = cache.query(qry).getAll(); + + assertEquals(1, qryResults.size()); + assertEquals(Date.valueOf(entity.getLocalDate()), qryResults.get(0).get(0)); + } + + /** + * Tests updating of all Date and Time fields. + */ + @Test + public void testUpdateAllFields() { + EntityWithDateTimeFields expEntity = new EntityWithDateTimeFields(entity); + + expEntity.setLocalTime(expEntity.getLocalTime().plusHours(1)); + expEntity.setLocalDate(expEntity.getLocalDate().plusDays(1)); + expEntity.setLocalDateTime(LocalDateTime.of(expEntity.getLocalDate(), expEntity.getLocalTime())); + + SqlFieldsQuery qry = new SqlFieldsQuery( + "update EntityWithDateTimeFields set locTime = ?, locDate = ?, locDateTime = ? where id = ?" + ).setArgs(expEntity.getLocalTime(), expEntity.getLocalDate(), expEntity.getLocalDateTime(), entity.getId()); + + List<List<?>> qryResults = cache.query(qry).getAll(); + + assertEquals(1, qryResults.size()); + assertEquals(1L, qryResults.get(0).get(0)); + assertEquals(expEntity, cache.get(expEntity.getId())); + } + + /** + * Tests deleting by all Date and Time fields. + */ + @Test + public void testDeleteByAllFields() { + SqlFieldsQuery qry = new SqlFieldsQuery( + "delete from EntityWithDateTimeFields where locTime = ? and locDate = ? and locDateTime = ?" + ).setArgs(entity.getLocalTime(), entity.getLocalDate(), entity.getLocalDateTime()); + + List<List<?>> qryResults = cache.query(qry).getAll(); + + assertEquals(1, qryResults.size()); + assertEquals(1L, qryResults.get(0).get(0)); + assertEquals(0, cache.size()); + } + + /** + * Entity containing Java 8 Date and Time fields. + */ + private static class EntityWithDateTimeFields implements Serializable { + /** Serial version UID. */ + private static final long serialVersionUID = 1L; + + /** ID. */ + @QuerySqlField(index = true) + private Long id; + + /** {@link LocalTime} field. */ + @QuerySqlField(index = true) + private LocalTime locTime; + + /** {@link LocalDate} field. */ + @QuerySqlField(index = true) + private LocalDate locDate; + + /** {@link LocalDateTime} field. */ + @QuerySqlField(index = true) + private LocalDateTime locDateTime; + + /** + * Default constructor. + */ + EntityWithDateTimeFields() { + } + + /** + * Copy constructor. + * + * @param entity Entity to copy from. + */ + EntityWithDateTimeFields(EntityWithDateTimeFields entity) { + id = entity.id; + locTime = LocalTime.from(entity.locTime); + locDate = LocalDate.from(entity.locDate); + locDateTime = LocalDateTime.from(entity.locDateTime); + } + + /** + * Constructor. + * + * @param id ID. + * @param locTime {@link LocalTime} value. + * @param locDate {@link LocalDate} value. + * @param locDateTime {@link LocalDateTime} value. + */ + EntityWithDateTimeFields(Long id, LocalTime locTime, LocalDate locDate, LocalDateTime locDateTime) { + this.id = id; + this.locTime = locTime; + this.locDate = locDate; + this.locDateTime = locDateTime; + } + + /** + * Returns the ID. + * + * @return ID. + */ + public Long getId() { + return id; + } + + /** + * Sets the ID. + * + * @param id ID. + */ + public void setId(Long id) { + this.id = id; + } + + /** + * Returns the {@link LocalDateTime} field value + * + * @return {@link LocalDateTime} field value; + */ + public LocalDateTime getLocalDateTime() { + return locDateTime; + } + + /** + * Returns the {@link LocalDateTime} field value. + * + * @param locDateTime {@link LocalDateTime} value. + */ + public void setLocalDateTime(LocalDateTime locDateTime) { + this.locDateTime = locDateTime; + } + + /** + * Returns the {@link LocalDate} field value. + * + * @return {@link LocalDate} field value. + */ + public LocalDate getLocalDate() { + return locDate; + } + + /** + * Sets the {@link LocalDate} field value. + * + * @param locDate {@link LocalDate} value. + */ + public void setLocalDate(LocalDate locDate) { + this.locDate = locDate; + } + + /** + * Returns the {@link LocalTime} field value. + * + * @return {@link LocalTime} field value. + */ + public LocalTime getLocalTime() { + return locTime; + } + + /** + * Sets the {@link LocalTime} field value. + * + * @param locTime {@link LocalTime} value. + */ + public void setLocalTime(LocalTime locTime) { + this.locTime = locTime; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (o == null || getClass() != o.getClass()) + return false; + + EntityWithDateTimeFields fields = (EntityWithDateTimeFields)o; + + return Objects.equals(id, fields.id) && Objects.equals(locDateTime, fields.locDateTime) && + Objects.equals(locDate, fields.locDate) && Objects.equals(locTime, fields.locTime); + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + return Objects.hash(id, locDateTime, locDate, locTime); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "EntityWithDateTimeFields{" + "id=" + id + ", locDateTime=" + locDateTime + ", locDate=" + locDate + + ", locTime=" + locTime + '}'; + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite2.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite2.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite2.java index cd58c1a..e4c918e 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite2.java +++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteBinaryCacheQueryTestSuite2.java @@ -52,6 +52,7 @@ import org.apache.ignite.internal.processors.query.IgniteCacheGroupsCompareQuery import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlDistributedJoinSelfTest; import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlSegmentedIndexMultiNodeSelfTest; import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlSegmentedIndexSelfTest; +import org.apache.ignite.internal.processors.query.h2.CacheQueryEntityWithDateTimeApiFieldsTest; import org.apache.ignite.internal.processors.query.h2.twostep.CacheQueryMemoryLeakTest; import org.apache.ignite.internal.processors.query.h2.twostep.CreateTableWithDateKeySelfTest; import org.apache.ignite.internal.processors.query.h2.twostep.DisappearedCacheCauseRetryMessageSelfTest; @@ -122,6 +123,8 @@ public class IgniteBinaryCacheQueryTestSuite2 { suite.addTest(new JUnit4TestAdapter(CreateTableWithDateKeySelfTest.class)); + suite.addTest(new JUnit4TestAdapter(CacheQueryEntityWithDateTimeApiFieldsTest.class)); + suite.addTest(new JUnit4TestAdapter(NonCollocatedRetryMessageSelfTest.class)); suite.addTest(new JUnit4TestAdapter(RetryCauseMessageSelfTest.class)); suite.addTest(new JUnit4TestAdapter(DisappearedCacheCauseRetryMessageSelfTest.class)); http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/indexing/src/test/java8/org/apache/ignite/internal/processors/query/h2/CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java8/org/apache/ignite/internal/processors/query/h2/CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest.java b/modules/indexing/src/test/java8/org/apache/ignite/internal/processors/query/h2/CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest.java deleted file mode 100644 index 6a144fc..0000000 --- a/modules/indexing/src/test/java8/org/apache/ignite/internal/processors/query/h2/CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest.java +++ /dev/null @@ -1,363 +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.ignite.internal.processors.query.h2; - -import java.io.Serializable; -import java.sql.Date; -import java.sql.Time; -import java.sql.Timestamp; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.util.List; -import java.util.Objects; -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.cache.query.SqlFieldsQuery; -import org.apache.ignite.cache.query.annotations.QuerySqlField; -import org.apache.ignite.configuration.CacheConfiguration; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Tests queries against entities with JSR-310 Java 8 Date and Time API fields. - */ -@RunWith(JUnit4.class) -public class CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest extends CacheQueryJsr310Java8DateTimeApiAbstractTest { - /** - * Entity containing JSR-310 fields. - */ - private static class EntityWithJsr310Fields implements Serializable { - - /** Serial version UID. */ - private static final long serialVersionUID = 1L; - - /** ID. */ - @QuerySqlField(index = true) - private Long id; - - /** {@link LocalTime} field. */ - @QuerySqlField(index = true) - private LocalTime locTime; - - /** {@link LocalDate} field. */ - @QuerySqlField(index = true) - private LocalDate locDate; - - /** {@link LocalDateTime} field. */ - @QuerySqlField(index = true) - private LocalDateTime locDateTime; - - /** - * Default constructor. - */ - EntityWithJsr310Fields() { - } - - /** - * Copy constructor. - * - * @param entity Entity to copy from. - */ - EntityWithJsr310Fields(EntityWithJsr310Fields entity) { - id = entity.id; - locTime = LocalTime.from(entity.locTime); - locDate = LocalDate.from(entity.locDate); - locDateTime = LocalDateTime.from(entity.locDateTime); - } - - /** - * Constructor. - * - * @param id ID. - * @param locTime {@link LocalTime} value. - * @param locDate {@link LocalDate} value. - * @param locDateTime {@link LocalDateTime} value. - */ - EntityWithJsr310Fields(Long id, LocalTime locTime, LocalDate locDate, LocalDateTime locDateTime) { - this.id = id; - this.locTime = locTime; - this.locDate = locDate; - this.locDateTime = locDateTime; - } - - /** - * Returns the ID. - * - * @return ID. - */ - public Long getId() { - return id; - } - - /** - * Sets the ID. - * - * @param id ID. - */ - public void setId(Long id) { - this.id = id; - } - - /** - * Returns the {@link LocalDateTime} field value - * - * @return {@link LocalDateTime} field value; - */ - public LocalDateTime getLocalDateTime() { - return locDateTime; - } - - /** - * Returns the {@link LocalDateTime} field value. - * - * @param locDateTime {@link LocalDateTime} value. - */ - public void setLocalDateTime(LocalDateTime locDateTime) { - this.locDateTime = locDateTime; - } - - /** - * Returns the {@link LocalDate} field value. - * - * @return {@link LocalDate} field value. - */ - public LocalDate getLocalDate() { - return locDate; - } - - /** - * Sets the {@link LocalDate} field value. - * - * @param locDate {@link LocalDate} value. - */ - public void setLocalDate(LocalDate locDate) { - this.locDate = locDate; - } - - /** - * Returns the {@link LocalTime} field value. - * - * @return {@link LocalTime} field value. - */ - public LocalTime getLocalTime() { - return locTime; - } - - /** - * Sets the {@link LocalTime} field value. - * - * @param locTime {@link LocalTime} value. - */ - public void setLocalTime(LocalTime locTime) { - this.locTime = locTime; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - EntityWithJsr310Fields fields = (EntityWithJsr310Fields)o; - - return Objects.equals(id, fields.id) && Objects.equals(locDateTime, fields.locDateTime) && - Objects.equals(locDate, fields.locDate) && Objects.equals(locTime, fields.locTime); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - return Objects.hash(id, locDateTime, locDate, locTime); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "EntityWithJsr310Fields{" + "id=" + id + ", locDateTime=" + locDateTime + ", locDate=" + locDate + - ", locTime=" + locTime + '}'; - } - } - - /** Cache. */ - private IgniteCache<Long, EntityWithJsr310Fields> cache; - - /** Entity with JSR-310 fields instance. */ - private final EntityWithJsr310Fields entity = - new EntityWithJsr310Fields(1L, LOCAL_TIME, LOCAL_DATE, LOCAL_DATE_TIME); - - /** - * Creates a cache configuration. - * - * @return Cache configuration. - */ - private static CacheConfiguration<Long, EntityWithJsr310Fields> createCacheConfig() { - return createCacheConfig( - "entityWithJava8DataTimeFields", Long.class, EntityWithJsr310Fields.class - ); - } - - /** {@inheritDoc} */ - @Override protected void beforeTest() throws Exception { - Ignite ignite = startGridsMultiThreaded(1, true); - cache = ignite.getOrCreateCache(createCacheConfig()); - - cache.put(entity.getId(), entity); - } - - /** - * Tests insertion of an entity. - * - * @throws Exception If failed. - */ - @Test - public void testInsertEntityFields() throws Exception { - cache.remove(entity.getId()); - - assertEquals(0, cache.size()); - - SqlFieldsQuery qry = new SqlFieldsQuery( - "insert into EntityWithJsr310Fields(_key, id, locTime, locDate, locDateTime) values(?, ?, ?, ?, ?)" - ).setArgs( - entity.getId(), entity.getId(), entity.getLocalTime(), entity.getLocalDate(), entity.getLocalDateTime() - ); - - List<List<?>> qryResults = cache.query(qry).getAll(); - - assertEquals(1, qryResults.size()); - assertEquals(1L, qryResults.get(0).get(0)); - assertEquals(1, cache.size()); - assertEquals(entity, cache.get(entity.getId())); - } - - /** - * Tests that DATEDIFF SQL function works for {@link LocalDateTime} - * fields with the time part set to midnight. - * - * @throws Exception If failed. - */ - @Test - public void testDateDiffForLocalDateTimeFieldAtMidnight() throws Exception { - SqlFieldsQuery qry = - new SqlFieldsQuery("select DATEDIFF('DAY', locDateTime, CURRENT_DATE ()) from EntityWithJsr310Fields"); - - List<List<?>> qryResults = cache.query(qry).getAll(); - - assertEquals(1, qryResults.size()); - assertTrue((Long)qryResults.get(0).get(0) >= DAYS_BEFORE_NOW); - } - - /** - * Tests that selection for a {@link LocalTime} field returns {@link Time}. - * - * @throws Exception If failed. - */ - @Test - public void testSelectLocalTimeFieldReturnsTime() throws Exception { - SqlFieldsQuery qry = new SqlFieldsQuery("select locTime from EntityWithJsr310Fields"); - - List<List<?>> qryResults = cache.query(qry).getAll(); - - assertEquals(1, qryResults.size()); - assertEquals(Time.class, qryResults.get(0).get(0).getClass()); - } - - /** - * Tests that selection for a {@link LocalDate} field returns {@link Date}. - * - * @throws Exception If failed. - */ - @Test - public void testSelectLocalDateFieldReturnsDate() throws Exception { - SqlFieldsQuery qry = new SqlFieldsQuery("select locDate from EntityWithJsr310Fields"); - - List<List<?>> qryResults = cache.query(qry).getAll(); - - assertEquals(1, qryResults.size()); - assertEquals(Date.class, qryResults.get(0).get(0).getClass()); - } - - /** - * Tests that selection for a {@link LocalDateTime} field returns {@link Timestamp}. - * - * @throws Exception If failed. - */ - @Test - public void testSelectLocalDateTimeFieldReturnsTimestamp() throws Exception { - SqlFieldsQuery qry = new SqlFieldsQuery("select locDateTime from EntityWithJsr310Fields"); - - List<List<?>> qryResults = cache.query(qry).getAll(); - - assertEquals(1, qryResults.size()); - assertEquals(Timestamp.class, qryResults.get(0).get(0).getClass()); - } - - /** - * Tests selection of an entity by a {@link LocalTime} field. - */ - @Test - public void testSelectByAllJsr310Fields() { - SqlFieldsQuery qry = new SqlFieldsQuery( - "select locDate from EntityWithJsr310Fields where locTime = ? and locDate = ? and locDateTime = ?" - ).setArgs(entity.getLocalTime(), entity.getLocalDate(), entity.getLocalDateTime()); - - List<List<?>> qryResults = cache.query(qry).getAll(); - - assertEquals(1, qryResults.size()); - assertEquals(Date.valueOf(entity.getLocalDate()), qryResults.get(0).get(0)); - } - - /** - * Tests updating of all JSR-310 fields. - */ - @Test - public void testUpdateAllJsr310Fields() { - EntityWithJsr310Fields expEntity = new EntityWithJsr310Fields(entity); - - expEntity.setLocalTime(expEntity.getLocalTime().plusHours(1)); - expEntity.setLocalDate(expEntity.getLocalDate().plusDays(1)); - expEntity.setLocalDateTime(LocalDateTime.of(expEntity.getLocalDate(), expEntity.getLocalTime())); - - SqlFieldsQuery qry = new SqlFieldsQuery( - "update EntityWithJsr310Fields set locTime = ?, locDate = ?, locDateTime = ? where id = ?" - ).setArgs(expEntity.getLocalTime(), expEntity.getLocalDate(), expEntity.getLocalDateTime(), entity.getId()); - - List<List<?>> qryResults = cache.query(qry).getAll(); - - assertEquals(1, qryResults.size()); - assertEquals(1L, qryResults.get(0).get(0)); - assertEquals(expEntity, cache.get(expEntity.getId())); - } - - /** - * Tests deleting by all JSR-310 fields. - */ - @Test - public void testDeleteByAllJsr310Fields() { - SqlFieldsQuery qry = new SqlFieldsQuery( - "delete from EntityWithJsr310Fields where locTime = ? and locDate = ? and locDateTime = ?" - ).setArgs(entity.getLocalTime(), entity.getLocalDate(), entity.getLocalDateTime()); - - List<List<?>> qryResults = cache.query(qry).getAll(); - - assertEquals(1, qryResults.size()); - assertEquals(1L, qryResults.get(0).get(0)); - assertEquals(0, cache.size()); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/indexing/src/test/java8/org/apache/ignite/internal/processors/query/h2/CacheQueryJsr310Java8DateTimeApiAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java8/org/apache/ignite/internal/processors/query/h2/CacheQueryJsr310Java8DateTimeApiAbstractTest.java b/modules/indexing/src/test/java8/org/apache/ignite/internal/processors/query/h2/CacheQueryJsr310Java8DateTimeApiAbstractTest.java deleted file mode 100644 index 61c5d1d..0000000 --- a/modules/indexing/src/test/java8/org/apache/ignite/internal/processors/query/h2/CacheQueryJsr310Java8DateTimeApiAbstractTest.java +++ /dev/null @@ -1,72 +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.ignite.internal.processors.query.h2; - -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import org.apache.ignite.cache.CacheAtomicityMode; -import org.apache.ignite.cache.CacheMode; -import org.apache.ignite.cache.CacheWriteSynchronizationMode; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.configuration.IgniteConfiguration; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; - -/** - * Base class for JSR-310 Java 8 Date and Time API queries tests. - */ -public abstract class CacheQueryJsr310Java8DateTimeApiAbstractTest extends GridCommonAbstractTest { - /** {@link LocalTime} instance. */ - protected static final LocalTime LOCAL_TIME = LocalTime.now().minusHours(10); - - /** - * The number of days subtracted from the current time when constructing - * {@link LocalDate} and {@link LocalDateTime} - * instances. - */ - protected static final long DAYS_BEFORE_NOW = 10; - - /** {@link LocalDate} instance. */ - protected static final LocalDate LOCAL_DATE = LocalDate.now().minusDays(DAYS_BEFORE_NOW); - - /** {@link LocalDateTime} instance. */ - protected static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(LOCAL_DATE, LocalTime.MIDNIGHT); - - /** - * Creates a cache configuration with the specified cache name - * and indexed type key/value pairs. - * - * @param cacheName Cache name - * @param indexedTypes key/value pairs according to {@link CacheConfiguration#setIndexedTypes(Class[])}. - * @param <K> Key type. - * @param <V> Value type. - * @return Cache configuration. - */ - protected static <K, V> CacheConfiguration<K, V> createCacheConfig(String cacheName, Class<?>... indexedTypes) { - return new CacheConfiguration<K, V>(cacheName) - .setCacheMode(CacheMode.REPLICATED) - .setAtomicityMode(CacheAtomicityMode.ATOMIC) - .setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC) - .setIndexedTypes(indexedTypes); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - stopAllGrids(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/41877807/modules/indexing/src/test/java8/org/apache/ignite/testsuites/CacheQueryJsr310Java8DateTimeApiSupportTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java8/org/apache/ignite/testsuites/CacheQueryJsr310Java8DateTimeApiSupportTestSuite.java b/modules/indexing/src/test/java8/org/apache/ignite/testsuites/CacheQueryJsr310Java8DateTimeApiSupportTestSuite.java deleted file mode 100644 index 7999218..0000000 --- a/modules/indexing/src/test/java8/org/apache/ignite/testsuites/CacheQueryJsr310Java8DateTimeApiSupportTestSuite.java +++ /dev/null @@ -1,42 +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.ignite.testsuites; - -import junit.framework.JUnit4TestAdapter; -import junit.framework.TestSuite; -import org.apache.ignite.internal.processors.query.h2.CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest; -import org.junit.runner.RunWith; -import org.junit.runners.AllTests; - -/** - * Test suite for JSR-310 Java 8 Date and Time API queries. - */ -@RunWith(AllTests.class) -public class CacheQueryJsr310Java8DateTimeApiSupportTestSuite { - /** - * @return Test suite. - * @throws Exception If failed. - */ - public static TestSuite suite() throws Exception { - TestSuite suite = new TestSuite("JSR-310 Java 8 Date and Time API Cache Queries Test Suite"); - - suite.addTest(new JUnit4TestAdapter(CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest.class)); - - return suite; - } -}
