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.

UUIDTest.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2011 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 static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.UUID;
  22. import org.junit.After;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import com.iciql.Db;
  26. import com.iciql.Iciql.IQColumn;
  27. import com.iciql.Iciql.IQTable;
  28. /**
  29. * Tests of UUID type.
  30. * <p>
  31. * H2 only.
  32. */
  33. public class UUIDTest {
  34. Db db;
  35. @Before
  36. public void setup() {
  37. db = IciqlSuite.openNewDb();
  38. }
  39. @After
  40. public void tearDown() {
  41. db.close();
  42. }
  43. @Test
  44. public void testUUIDs() throws Exception {
  45. if (!IciqlSuite.isH2(db)) {
  46. // do not test non-H2 databases
  47. return;
  48. }
  49. List<UUIDRecord> originals = UUIDRecord.getList();
  50. db.insertAll(originals);
  51. UUIDRecord u = new UUIDRecord();
  52. List<UUIDRecord> retrieved = db.from(u).orderBy(u.id).select();
  53. assertEquals(originals.size(), retrieved.size());
  54. for (int i = 0; i < originals.size(); i++) {
  55. UUIDRecord a = originals.get(i);
  56. UUIDRecord b = retrieved.get(i);
  57. assertTrue(a.equivalentTo(b));
  58. }
  59. UUIDRecord second = db.from(u).where(u.uuid).is(originals.get(1).uuid).selectFirst();
  60. assertTrue(originals.get(1).equivalentTo(second));
  61. }
  62. /**
  63. * A simple class used in this test.
  64. */
  65. @IQTable(name = "UUID_TEST")
  66. public static class UUIDRecord {
  67. @IQColumn(primaryKey = true)
  68. public Integer id;
  69. @IQColumn()
  70. public UUID uuid;
  71. public UUIDRecord() {
  72. // public constructor
  73. }
  74. private UUIDRecord(int id) {
  75. this.id = id;
  76. this.uuid = UUID.randomUUID();
  77. }
  78. public boolean equivalentTo(UUIDRecord b) {
  79. boolean same = true;
  80. same &= id == b.id;
  81. same &= uuid.equals(b.uuid);
  82. return same;
  83. }
  84. public String toString() {
  85. return id + ": " + uuid;
  86. }
  87. public static List<UUIDRecord> getList() {
  88. List<UUIDRecord> list = new ArrayList<UUIDRecord>();
  89. for (int i = 0; i < 10; i++) {
  90. list.add(new UUIDRecord(i + 1));
  91. }
  92. return list;
  93. }
  94. }
  95. }