From 8d28bc740c9bcb76186e7572f74a144397e780ce Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 29 Oct 2014 17:12:14 -0400 Subject: Support data type adapters This allows custom types to be (de)serialized into a standard JDBC type or to support db-specific data types, like the Postgres json and xml types. --- .../java/com/iciql/test/DataTypeAdapterTest.java | 94 ++++++++++++++++++++++ src/test/java/com/iciql/test/IciqlSuite.java | 5 +- .../java/com/iciql/test/models/SupportedTypes.java | 5 +- 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/iciql/test/DataTypeAdapterTest.java (limited to 'src/test') diff --git a/src/test/java/com/iciql/test/DataTypeAdapterTest.java b/src/test/java/com/iciql/test/DataTypeAdapterTest.java new file mode 100644 index 0000000..f10d298 --- /dev/null +++ b/src/test/java/com/iciql/test/DataTypeAdapterTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2014 James Moger. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.iciql.test; + +import java.util.Date; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.iciql.Db; +import com.iciql.Iciql.IQColumn; +import com.iciql.Iciql.IQTable; +import com.iciql.JavaSerializationTypeAdapter; +import com.iciql.test.models.SupportedTypes; + +/** + * Tests insertion and retrieval of a custom data type that is automatically transformed + * by a Java Object Serialization-based type adapter. + */ +public class DataTypeAdapterTest extends Assert { + + private Db db; + + + @Before + public void setUp() { + db = IciqlSuite.openNewDb(); + } + + @After + public void tearDown() { + db.close(); + } + + @Test + public void testSerializedObjectDataType() { + + SerializedObjectTypeAdapterTest row = new SerializedObjectTypeAdapterTest(); + row.received = new Date(); + row.obj = SupportedTypes.createList().get(1); + db.insert(row); + + SerializedObjectTypeAdapterTest table = new SerializedObjectTypeAdapterTest(); + SerializedObjectTypeAdapterTest q1 = db.from(table).selectFirst(); + + assertNotNull(q1); + assertTrue(row.obj.equivalentTo(q1.obj)); + + } + + @IQTable + public static class SerializedObjectTypeAdapterTest { + + @IQColumn(autoIncrement = true, primaryKey = true) + private long id; + + @IQColumn + private java.util.Date received; + + @IQColumn(typeAdapter = SupportedTypesAdapter.class) + private SupportedTypes obj; + + } + + /** + * Maps a SupportedType instance to a BLOB using Java Object serialization. + * + */ + public static class SupportedTypesAdapter extends JavaSerializationTypeAdapter { + + @Override + public Class getJavaType() { + return SupportedTypes.class; + } + + } + +} diff --git a/src/test/java/com/iciql/test/IciqlSuite.java b/src/test/java/com/iciql/test/IciqlSuite.java index c5d7ce8..3829501 100644 --- a/src/test/java/com/iciql/test/IciqlSuite.java +++ b/src/test/java/com/iciql/test/IciqlSuite.java @@ -48,6 +48,7 @@ import com.beust.jcommander.ParameterException; import com.beust.jcommander.Parameters; import com.iciql.Constants; import com.iciql.Db; +import com.iciql.test.DataTypeAdapterTest.SerializedObjectTypeAdapterTest; import com.iciql.test.models.BooleanModel; import com.iciql.test.models.CategoryAnnotationOnly; import com.iciql.test.models.ComplexObject; @@ -93,7 +94,8 @@ import com.iciql.util.Utils; @SuiteClasses({ AliasMapTest.class, AnnotationsTest.class, BooleanModelTest.class, ClobTest.class, ConcurrencyTest.class, EnumsTest.class, ModelsTest.class, PrimitivesTest.class, OneOfTest.class, RuntimeQueryTest.class, SamplesTest.class, UpdateTest.class, UpgradesTest.class, JoinTest.class, - UUIDTest.class, ViewsTest.class, ForeignKeyTest.class, TransactionTest.class, NestedConditionsTest.class }) + UUIDTest.class, ViewsTest.class, ForeignKeyTest.class, TransactionTest.class, NestedConditionsTest.class, + DataTypeAdapterTest.class }) public class IciqlSuite { private static final TestDb[] TEST_DBS = { @@ -191,6 +193,7 @@ public class IciqlSuite { db.dropTable(MultipleBoolsModel.class); db.dropTable(ProductAnnotationOnlyWithForeignKey.class); db.dropTable(CategoryAnnotationOnly.class); + db.dropTable(SerializedObjectTypeAdapterTest.class); return db; } diff --git a/src/test/java/com/iciql/test/models/SupportedTypes.java b/src/test/java/com/iciql/test/models/SupportedTypes.java index 9fa4fbc..489650e 100644 --- a/src/test/java/com/iciql/test/models/SupportedTypes.java +++ b/src/test/java/com/iciql/test/models/SupportedTypes.java @@ -17,6 +17,7 @@ package com.iciql.test.models; +import java.io.Serializable; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; @@ -44,7 +45,9 @@ import com.iciql.util.Utils; @IQTable @IQIndexes({ @IQIndex({ "myLong", "myInteger" }), @IQIndex(type = IndexType.HASH, value = "myString") }) @IQVersion(1) -public class SupportedTypes { +public class SupportedTypes implements Serializable { + + private static final long serialVersionUID = 1L; public static final SupportedTypes SAMPLE = new SupportedTypes(); -- cgit v1.2.3