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.

CursorBuilderTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  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;
  14. import junit.framework.TestCase;
  15. import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
  16. import com.healthmarketscience.jackcess.impl.IndexImpl;
  17. /**
  18. * @author James Ahlborn
  19. */
  20. public class CursorBuilderTest extends TestCase {
  21. public CursorBuilderTest(String name) throws Exception {
  22. super(name);
  23. }
  24. private static void assertCursor(
  25. Cursor expected, Cursor found)
  26. {
  27. assertSame(expected.getTable(), found.getTable());
  28. if(expected instanceof IndexCursor) {
  29. assertSame(((IndexCursor)expected).getIndex(),
  30. ((IndexCursor)found).getIndex());
  31. }
  32. assertEquals(expected.getSavepoint().getCurrentPosition(),
  33. found.getSavepoint().getCurrentPosition());
  34. }
  35. public void test() throws Exception
  36. {
  37. for (final TestDB indexCursorDB : CursorTest.INDEX_CURSOR_DBS) {
  38. Database db = CursorTest.createTestIndexTable(indexCursorDB);
  39. Table table = db.getTable("test");
  40. IndexImpl idx = (IndexImpl)table.getIndexes().get(0);
  41. Cursor expected = CursorBuilder.createCursor(table);
  42. Cursor found = table.newCursor().toCursor();
  43. assertCursor(expected, found);
  44. expected = CursorBuilder.createCursor(idx);
  45. found = idx.newCursor().toCursor();
  46. assertCursor(expected, found);
  47. expected = CursorBuilder.createCursor(idx);
  48. found = table.newCursor()
  49. .setIndexByName("id")
  50. .toCursor();
  51. assertCursor(expected, found);
  52. try {
  53. table.newCursor()
  54. .setIndexByName("foo");
  55. fail("IllegalArgumentException should have been thrown");
  56. } catch(IllegalArgumentException ignored) {
  57. // success
  58. }
  59. expected = CursorBuilder.createCursor(idx);
  60. found = table.newCursor()
  61. .setIndexByColumns(table.getColumn("id"))
  62. .toCursor();
  63. assertCursor(expected, found);
  64. try {
  65. table.newCursor()
  66. .setIndexByColumns(table.getColumn("value"));
  67. fail("IllegalArgumentException should have been thrown");
  68. } catch(IllegalArgumentException ignored) {
  69. // success
  70. }
  71. try {
  72. table.newCursor()
  73. .setIndexByColumns(table.getColumn("id"), table.getColumn("value"));
  74. fail("IllegalArgumentException should have been thrown");
  75. } catch(IllegalArgumentException ignored) {
  76. // success
  77. }
  78. expected = CursorBuilder.createCursor(table);
  79. expected.beforeFirst();
  80. found = table.newCursor()
  81. .beforeFirst()
  82. .toCursor();
  83. assertCursor(expected, found);
  84. expected = CursorBuilder.createCursor(table);
  85. expected.afterLast();
  86. found = table.newCursor()
  87. .afterLast()
  88. .toCursor();
  89. assertCursor(expected, found);
  90. expected = CursorBuilder.createCursor(table);
  91. expected.moveNextRows(2);
  92. Cursor.Savepoint sp = expected.getSavepoint();
  93. found = table.newCursor()
  94. .afterLast()
  95. .restoreSavepoint(sp)
  96. .toCursor();
  97. assertCursor(expected, found);
  98. expected = CursorBuilder.createCursor(idx);
  99. expected.moveNextRows(2);
  100. sp = expected.getSavepoint();
  101. found = idx.newCursor()
  102. .beforeFirst()
  103. .restoreSavepoint(sp)
  104. .toCursor();
  105. assertCursor(expected, found);
  106. expected = CursorBuilder.createCursor(idx,
  107. idx.constructIndexRowFromEntry(3),
  108. null);
  109. found = idx.newCursor()
  110. .setStartEntry(3)
  111. .toCursor();
  112. assertCursor(expected, found);
  113. expected = CursorBuilder.createCursor(idx,
  114. idx.constructIndexRowFromEntry(3),
  115. false,
  116. idx.constructIndexRowFromEntry(7),
  117. false);
  118. found = idx.newCursor()
  119. .setStartEntry(3)
  120. .setStartRowInclusive(false)
  121. .setEndEntry(7)
  122. .setEndRowInclusive(false)
  123. .toCursor();
  124. assertCursor(expected, found);
  125. db.close();
  126. }
  127. }
  128. }