diff options
author | James Moger <james.moger@gitblit.com> | 2014-10-29 17:12:14 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2014-10-29 17:12:14 -0400 |
commit | 8d28bc740c9bcb76186e7572f74a144397e780ce (patch) | |
tree | 1815e1d21df77e352ba2e8106557f71cb5561a8e /src/test | |
parent | bdb2899da4cbb27016d85c5e4fe268ddbccef546 (diff) | |
download | iciql-8d28bc740c9bcb76186e7572f74a144397e780ce.tar.gz iciql-8d28bc740c9bcb76186e7572f74a144397e780ce.zip |
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.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/com/iciql/test/DataTypeAdapterTest.java | 94 | ||||
-rw-r--r-- | src/test/java/com/iciql/test/IciqlSuite.java | 5 | ||||
-rw-r--r-- | src/test/java/com/iciql/test/models/SupportedTypes.java | 5 |
3 files changed, 102 insertions, 2 deletions
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<SupportedTypes> { + + @Override + public Class<SupportedTypes> 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(); |