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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright (c) 2007 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import junit.framework.TestCase;
  24. import static com.healthmarketscience.jackcess.JetFormatTest.*;
  25. /**
  26. * @author James Ahlborn
  27. */
  28. public class CursorBuilderTest extends TestCase {
  29. public CursorBuilderTest(String name) throws Exception {
  30. super(name);
  31. }
  32. private static void assertCursor(
  33. Cursor expected, Cursor found)
  34. {
  35. assertSame(expected.getTable(), found.getTable());
  36. assertSame(expected.getIndex(), found.getIndex());
  37. assertEquals(expected.getSavepoint().getCurrentPosition(),
  38. found.getSavepoint().getCurrentPosition());
  39. }
  40. public void test() throws Exception
  41. {
  42. for (final TestDB indexCursorDB : CursorTest.INDEX_CURSOR_DBS) {
  43. Database db = CursorTest.createTestIndexTable(indexCursorDB);
  44. Table table = db.getTable("test");
  45. Index idx = table.getIndexes().get(0);
  46. Cursor expected = Cursor.createCursor(table);
  47. Cursor found = new CursorBuilder(table).toCursor();
  48. assertCursor(expected, found);
  49. expected = Cursor.createIndexCursor(table, idx);
  50. found = new CursorBuilder(table)
  51. .setIndex(idx)
  52. .toCursor();
  53. assertCursor(expected, found);
  54. expected = Cursor.createIndexCursor(table, idx);
  55. found = new CursorBuilder(table)
  56. .setIndexByName("id")
  57. .toCursor();
  58. assertCursor(expected, found);
  59. try {
  60. new CursorBuilder(table)
  61. .setIndexByName("foo");
  62. fail("IllegalArgumentException should have been thrown");
  63. } catch(IllegalArgumentException ignored) {
  64. // success
  65. }
  66. expected = Cursor.createIndexCursor(table, idx);
  67. found = new CursorBuilder(table)
  68. .setIndexByColumns(table.getColumn("id"))
  69. .toCursor();
  70. assertCursor(expected, found);
  71. try {
  72. new CursorBuilder(table)
  73. .setIndexByColumns(table.getColumn("value"));
  74. fail("IllegalArgumentException should have been thrown");
  75. } catch(IllegalArgumentException ignored) {
  76. // success
  77. }
  78. try {
  79. new CursorBuilder(table)
  80. .setIndexByColumns(table.getColumn("id"), table.getColumn("value"));
  81. fail("IllegalArgumentException should have been thrown");
  82. } catch(IllegalArgumentException ignored) {
  83. // success
  84. }
  85. expected = Cursor.createCursor(table);
  86. expected.beforeFirst();
  87. found = new CursorBuilder(table)
  88. .beforeFirst()
  89. .toCursor();
  90. assertCursor(expected, found);
  91. expected = Cursor.createCursor(table);
  92. expected.afterLast();
  93. found = new CursorBuilder(table)
  94. .afterLast()
  95. .toCursor();
  96. assertCursor(expected, found);
  97. expected = Cursor.createCursor(table);
  98. expected.moveNextRows(2);
  99. Cursor.Savepoint sp = expected.getSavepoint();
  100. found = new CursorBuilder(table)
  101. .afterLast()
  102. .restoreSavepoint(sp)
  103. .toCursor();
  104. assertCursor(expected, found);
  105. expected = Cursor.createIndexCursor(table, idx);
  106. expected.moveNextRows(2);
  107. sp = expected.getSavepoint();
  108. found = new CursorBuilder(table)
  109. .setIndex(idx)
  110. .beforeFirst()
  111. .restoreSavepoint(sp)
  112. .toCursor();
  113. assertCursor(expected, found);
  114. expected = Cursor.createIndexCursor(table, idx,
  115. idx.constructIndexRowFromEntry(3),
  116. null);
  117. found = new CursorBuilder(table)
  118. .setIndex(idx)
  119. .setStartEntry(3)
  120. .toCursor();
  121. assertCursor(expected, found);
  122. expected = Cursor.createIndexCursor(table, idx,
  123. idx.constructIndexRowFromEntry(3),
  124. false,
  125. idx.constructIndexRowFromEntry(7),
  126. false);
  127. found = new CursorBuilder(table)
  128. .setIndex(idx)
  129. .setStartEntry(3)
  130. .setStartRowInclusive(false)
  131. .setEndEntry(7)
  132. .setEndRowInclusive(false)
  133. .toCursor();
  134. assertCursor(expected, found);
  135. db.close();
  136. }
  137. }
  138. }