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.

ExtendedDateTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (c) 2021 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.time.LocalDate;
  15. import java.time.LocalDateTime;
  16. import java.time.format.DateTimeFormatter;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.Comparator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import com.healthmarketscience.jackcess.Column;
  23. import com.healthmarketscience.jackcess.Cursor;
  24. import com.healthmarketscience.jackcess.DataType;
  25. import com.healthmarketscience.jackcess.Database;
  26. import static com.healthmarketscience.jackcess.DatabaseBuilder.*;
  27. import com.healthmarketscience.jackcess.Index;
  28. import com.healthmarketscience.jackcess.Row;
  29. import com.healthmarketscience.jackcess.Table;
  30. import static com.healthmarketscience.jackcess.TestUtil.*;
  31. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  32. import junit.framework.TestCase;
  33. import org.junit.Assert;
  34. /**
  35. *
  36. * @author James Ahlborn
  37. */
  38. public class ExtendedDateTest extends TestCase
  39. {
  40. public ExtendedDateTest(String name) throws Exception {
  41. super(name);
  42. }
  43. public void testReadExtendedDate() throws Exception {
  44. DateTimeFormatter dtfNoTime = DateTimeFormatter.ofPattern("M/d/yyy");
  45. DateTimeFormatter dtfFull = DateTimeFormatter.ofPattern("M/d/yyy h:mm:ss.SSSSSSS a");
  46. for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.EXT_DATE)) {
  47. Database db = openMem(testDB);
  48. Table t = db.getTable("Table1");
  49. for(Row r : t) {
  50. LocalDateTime ldt = r.getLocalDateTime("DateExt");
  51. String str = r.getString("DateExtStr");
  52. if(ldt != null) {
  53. String str1 = dtfNoTime.format(ldt);
  54. String str2 = dtfFull.format(ldt);
  55. Assert.assertTrue(str1.equals(str) || str2.equals(str));
  56. } else {
  57. Assert.assertNull(str);
  58. }
  59. }
  60. Index idx = t.getIndex("DateExtAsc");
  61. IndexCodesTest.checkIndexEntries(testDB, t, idx);
  62. idx = t.getIndex("DateExtDesc");
  63. IndexCodesTest.checkIndexEntries(testDB, t, idx);
  64. db.close();
  65. }
  66. }
  67. public void testWriteExtendedDate() throws Exception {
  68. for (final Database.FileFormat fileFormat : SUPPORTED_FILEFORMATS) {
  69. JetFormat format = DatabaseImpl.getFileFormatDetails(fileFormat)
  70. .getFormat();
  71. if(!format.isSupportedDataType(DataType.EXT_DATE_TIME)) {
  72. continue;
  73. }
  74. Database db = create(fileFormat);
  75. Table t = newTable("Test")
  76. .addColumn(newColumn("id", DataType.LONG)
  77. .setAutoNumber(true))
  78. .addColumn(newColumn("data1", DataType.TEXT))
  79. .addColumn(newColumn("extDate", DataType.EXT_DATE_TIME))
  80. .addIndex(newIndex("idxAsc").addColumns("extDate"))
  81. .addIndex(newIndex("idxDesc").addColumns(false, "extDate"))
  82. .toTable(db);
  83. Object[] ldts = {
  84. LocalDate.of(2020,6,17),
  85. LocalDate.of(2021,6,14),
  86. LocalDateTime.of(2021,6,14,12,45),
  87. LocalDateTime.of(2021,6,14,1,45),
  88. LocalDateTime.of(2021,6,14,22,45,12,345678900),
  89. LocalDateTime.of(1765,6,14,12,45),
  90. LocalDateTime.of(100,6,14,12,45,00,123456700),
  91. LocalDateTime.of(1265,6,14,12,45)
  92. };
  93. List<Map<String, Object>> expectedTable =
  94. new ArrayList<Map<String, Object>>();
  95. int idx = 1;
  96. for(Object ldt : ldts) {
  97. t.addRow(Column.AUTO_NUMBER, "" + ldt, ldt);
  98. LocalDateTime realLdt = (LocalDateTime)ColumnImpl.toInternalValue(
  99. DataType.EXT_DATE_TIME, ldt, (DatabaseImpl)db);
  100. expectedTable.add(createExpectedRow(
  101. "id", idx++,
  102. "data1", "" + ldt,
  103. "extDate", realLdt));
  104. }
  105. Comparator<Map<String, Object>> comp = (r1, r2) -> {
  106. LocalDateTime l1 = (LocalDateTime)r1.get("extDate");
  107. LocalDateTime l2 = (LocalDateTime)r2.get("extDate");
  108. return l1.compareTo(l2);
  109. };
  110. Collections.sort(expectedTable, comp);
  111. Cursor c = t.newCursor().setIndexByName("idxAsc").toIndexCursor();
  112. assertCursor(expectedTable, c);
  113. Collections.sort(expectedTable, comp.reversed());
  114. c = t.newCursor().setIndexByName("idxDesc").toIndexCursor();
  115. assertCursor(expectedTable, c);
  116. db.close();
  117. }
  118. }
  119. }