You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataTypeAdapterTest.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2014 James Moger.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.iciql.test;
  17. import com.iciql.Db;
  18. import com.iciql.Iciql.IQColumn;
  19. import com.iciql.Iciql.IQTable;
  20. import com.iciql.Iciql.TypeAdapter;
  21. import com.iciql.adapter.JavaSerializationTypeAdapter;
  22. import com.iciql.test.models.SupportedTypes;
  23. import org.junit.After;
  24. import org.junit.Assert;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import java.lang.annotation.ElementType;
  28. import java.lang.annotation.Retention;
  29. import java.lang.annotation.RetentionPolicy;
  30. import java.lang.annotation.Target;
  31. import java.util.Date;
  32. /**
  33. * Tests insertion and retrieval of a custom data type that is automatically transformed
  34. * by a Java Object Serialization-based type adapter.
  35. */
  36. public class DataTypeAdapterTest extends Assert {
  37. private Db db;
  38. @Before
  39. public void setUp() {
  40. db = IciqlSuite.openNewDb();
  41. }
  42. @After
  43. public void tearDown() {
  44. db.close();
  45. }
  46. @Test
  47. public void testSerializedObjectDataType() {
  48. SerializedObjectTypeAdapterTest row = new SerializedObjectTypeAdapterTest();
  49. row.received = new Date();
  50. row.obj = SupportedTypes.createList().get(1);
  51. db.insert(row);
  52. SerializedObjectTypeAdapterTest table = new SerializedObjectTypeAdapterTest();
  53. SerializedObjectTypeAdapterTest q1 = db.from(table).selectFirst();
  54. assertNotNull(q1);
  55. assertTrue(row.obj.equivalentTo(q1.obj));
  56. }
  57. @IQTable(name = "dataTypeAdapters")
  58. public static class SerializedObjectTypeAdapterTest {
  59. @IQColumn(autoIncrement = true, primaryKey = true)
  60. public long id;
  61. @IQColumn
  62. public java.util.Date received;
  63. @IQColumn
  64. @SupportedTypesAdapter
  65. public SupportedTypes obj;
  66. }
  67. /**
  68. * Maps a SupportedType instance to a BLOB using Java Object serialization.
  69. */
  70. public static class SupportedTypesAdapterImpl extends JavaSerializationTypeAdapter<SupportedTypes> {
  71. @Override
  72. public Class<SupportedTypes> getJavaType() {
  73. return SupportedTypes.class;
  74. }
  75. }
  76. @Retention(RetentionPolicy.RUNTIME)
  77. @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
  78. @TypeAdapter(SupportedTypesAdapterImpl.class)
  79. public @interface SupportedTypesAdapter {
  80. }
  81. }