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.

IndexTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 java.io.File;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.SortedSet;
  31. import java.util.TreeSet;
  32. import junit.framework.TestCase;
  33. import static com.healthmarketscience.jackcess.DatabaseTest.*;
  34. /**
  35. * @author James Ahlborn
  36. */
  37. public class IndexTest extends TestCase {
  38. public IndexTest(String name) {
  39. super(name);
  40. }
  41. public void testByteOrder() throws Exception {
  42. byte b1 = (byte)0x00;
  43. byte b2 = (byte)0x01;
  44. byte b3 = (byte)0x7F;
  45. byte b4 = (byte)0x80;
  46. byte b5 = (byte)0xFF;
  47. assertTrue(ByteUtil.asUnsignedByte(b1) < ByteUtil.asUnsignedByte(b2));
  48. assertTrue(ByteUtil.asUnsignedByte(b2) < ByteUtil.asUnsignedByte(b3));
  49. assertTrue(ByteUtil.asUnsignedByte(b3) < ByteUtil.asUnsignedByte(b4));
  50. assertTrue(ByteUtil.asUnsignedByte(b4) < ByteUtil.asUnsignedByte(b5));
  51. }
  52. public void testByteCodeComparator() {
  53. byte[] b0 = null;
  54. byte[] b1 = new byte[]{(byte)0x00};
  55. byte[] b2 = new byte[]{(byte)0x00, (byte)0x00};
  56. byte[] b3 = new byte[]{(byte)0x00, (byte)0x01};
  57. byte[] b4 = new byte[]{(byte)0x01};
  58. byte[] b5 = new byte[]{(byte)0x80};
  59. byte[] b6 = new byte[]{(byte)0xFF};
  60. byte[] b7 = new byte[]{(byte)0xFF, (byte)0x00};
  61. byte[] b8 = new byte[]{(byte)0xFF, (byte)0x01};
  62. List<byte[]> expectedList = Arrays.<byte[]>asList(b0, b1, b2, b3, b4,
  63. b5, b6, b7, b8);
  64. SortedSet<byte[]> sortedSet = new TreeSet<byte[]>(
  65. Index.BYTE_CODE_COMPARATOR);
  66. sortedSet.addAll(expectedList);
  67. assertEquals(expectedList, new ArrayList<byte[]>(sortedSet));
  68. }
  69. public void testPrimaryKey() throws Exception {
  70. Table table = open().getTable("Table1");
  71. Map<String, Boolean> foundPKs = new HashMap<String, Boolean>();
  72. for(Index index : table.getIndexes()) {
  73. foundPKs.put(index.getColumns().iterator().next().getName(),
  74. index.isPrimaryKey());
  75. }
  76. Map<String, Boolean> expectedPKs = new HashMap<String, Boolean>();
  77. expectedPKs.put("A", Boolean.TRUE);
  78. expectedPKs.put("B", Boolean.FALSE);
  79. assertEquals(expectedPKs, foundPKs);
  80. }
  81. public void testIndexSlots() throws Exception
  82. {
  83. Database mdb = open(new File("test/data/indexTest.mdb"));
  84. Table table = mdb.getTable("Table1");
  85. for(Index idx : table.getIndexes()) {
  86. idx.initialize();
  87. }
  88. assertEquals(4, table.getIndexes().size());
  89. assertEquals(4, table.getIndexSlotCount());
  90. checkIndexColumns(table,
  91. "id", "id",
  92. "PrimaryKey", "id",
  93. "Table2Table1", "otherfk1",
  94. "Table3Table1", "otherfk2");
  95. table = mdb.getTable("Table2");
  96. for(Index idx : table.getIndexes()) {
  97. idx.initialize();
  98. }
  99. assertEquals(2, table.getIndexes().size());
  100. assertEquals(3, table.getIndexSlotCount());
  101. checkIndexColumns(table,
  102. "id", "id",
  103. "PrimaryKey", "id");
  104. table = mdb.getTable("Table3");
  105. for(Index idx : table.getIndexes()) {
  106. idx.initialize();
  107. }
  108. assertEquals(2, table.getIndexes().size());
  109. assertEquals(3, table.getIndexSlotCount());
  110. checkIndexColumns(table,
  111. "id", "id",
  112. "PrimaryKey", "id");
  113. }
  114. public void testComplexIndex() throws Exception
  115. {
  116. // this file has an index with "compressed" entries and node pages
  117. File origFile = new File("test/data/compIndexTest.mdb");
  118. Database db = open(origFile);
  119. Table t = db.getTable("Table1");
  120. Index index = t.getIndexes().get(0);
  121. assertFalse(index.isInitialized());
  122. assertEquals(512, countRows(t));
  123. assertEquals(512, index.getEntryCount());
  124. db.close();
  125. // copy to temp file and attempt to edit
  126. db = openCopy(origFile);
  127. t = db.getTable("Table1");
  128. index = t.getIndexes().get(0);
  129. System.out.println("IndexTest: Index type: " + index.getClass());
  130. try {
  131. t.addRow(99, "abc", "def");
  132. if(index instanceof SimpleIndex) {
  133. // SimpleIndex doesn't support writing these indexes
  134. fail("Should have thrown UnsupportedOperationException");
  135. }
  136. } catch(UnsupportedOperationException e) {
  137. // success
  138. if(index instanceof BigIndex) {
  139. throw e;
  140. }
  141. }
  142. }
  143. public void testEntryDeletion() throws Exception {
  144. Table table = openCopy(new File("test/data/test.mdb")).getTable("Table1");
  145. for(int i = 0; i < 10; ++i) {
  146. table.addRow("foo" + i, "bar" + i, (byte)42 + i, (short)53 + i, 13 * i,
  147. (6.7d / i), null, null, true);
  148. }
  149. table.reset();
  150. assertRowCount(12, table);
  151. for(Index index : table.getIndexes()) {
  152. assertEquals(12, index.getEntryCount());
  153. }
  154. table.reset();
  155. table.getNextRow();
  156. table.getNextRow();
  157. table.deleteCurrentRow();
  158. table.getNextRow();
  159. table.deleteCurrentRow();
  160. table.getNextRow();
  161. table.getNextRow();
  162. table.deleteCurrentRow();
  163. table.getNextRow();
  164. table.getNextRow();
  165. table.getNextRow();
  166. table.deleteCurrentRow();
  167. table.reset();
  168. assertRowCount(8, table);
  169. for(Index index : table.getIndexes()) {
  170. assertEquals(8, index.getEntryCount());
  171. }
  172. }
  173. public void testIgnoreNulls() throws Exception
  174. {
  175. Database db = openCopy(new File("test/data/testIndexProperties.mdb"));
  176. doTestIgnoreNulls(db, "TableIgnoreNulls1");
  177. doTestIgnoreNulls(db, "TableIgnoreNulls2");
  178. db.close();
  179. }
  180. private void doTestIgnoreNulls(Database db, String tableName)
  181. throws Exception
  182. {
  183. Table orig = db.getTable(tableName);
  184. Index origI = orig.getIndex("DataIndex");
  185. Table temp = db.getTable(tableName + "_temp");
  186. Index tempI = temp.getIndex("DataIndex");
  187. // copy from orig table to temp table
  188. for(Map<String,Object> row : orig) {
  189. temp.addRow(orig.asRow(row));
  190. }
  191. assertEquals(origI.getEntryCount(), tempI.getEntryCount());
  192. Cursor origC = Cursor.createIndexCursor(orig, origI);
  193. Cursor tempC = Cursor.createIndexCursor(temp, tempI);
  194. while(true) {
  195. boolean origHasNext = origC.moveToNextRow();
  196. boolean tempHasNext = tempC.moveToNextRow();
  197. assertTrue(origHasNext == tempHasNext);
  198. if(!origHasNext) {
  199. break;
  200. }
  201. Map<String,Object> origRow = origC.getCurrentRow();
  202. Cursor.Position origCurPos = origC.getSavepoint().getCurrentPosition();
  203. Map<String,Object> tempRow = tempC.getCurrentRow();
  204. Cursor.Position tempCurPos = tempC.getSavepoint().getCurrentPosition();
  205. assertEquals(origRow, tempRow);
  206. assertEquals(IndexCodesTest.entryToString(origCurPos),
  207. IndexCodesTest.entryToString(tempCurPos));
  208. }
  209. }
  210. public void testUnique() throws Exception
  211. {
  212. Database db = openCopy(new File("test/data/testIndexProperties.mdb"));
  213. Table t = db.getTable("TableUnique1_temp");
  214. Index index = t.getIndex("DataIndex");
  215. doTestUnique(t, index, 1,
  216. null, true,
  217. "unique data", true,
  218. null, true,
  219. "more", false,
  220. "stuff", false,
  221. "unique data", false);
  222. t = db.getTable("TableUnique2_temp");
  223. index = t.getIndex("DataIndex");
  224. doTestUnique(t, index, 2,
  225. null, null, true,
  226. "unique data", 42, true,
  227. "unique data", null, true,
  228. null, null, true,
  229. "some", 42, true,
  230. "more unique data", 13, true,
  231. null, -4242, true,
  232. "another row", -3462, false,
  233. null, 49, false,
  234. "more", null, false,
  235. "unique data", 42, false,
  236. "unique data", null, false,
  237. null, -4242, false);
  238. db.close();
  239. }
  240. private void doTestUnique(Table t, Index index, int numValues,
  241. Object... testData)
  242. throws Exception
  243. {
  244. for(int i = 0; i < testData.length; i += (numValues + 1)) {
  245. Object[] row = new Object[numValues + 1];
  246. row[0] = "testRow" + i;
  247. for(int j = 1; j < (numValues + 1); ++j) {
  248. row[j] = testData[i + j - 1];
  249. }
  250. boolean expectedSuccess = (Boolean)testData[i + numValues];
  251. IOException failure = null;
  252. try {
  253. index.addRow(row, new RowId(400 + i, 0));
  254. } catch(IOException e) {
  255. failure = e;
  256. }
  257. if(expectedSuccess) {
  258. assertNull(failure);
  259. } else {
  260. assertTrue(failure != null);
  261. assertTrue(failure.getMessage().contains("uniqueness"));
  262. }
  263. }
  264. }
  265. public void testUniqueEntryCount() throws Exception {
  266. Database db = openCopy(new File("test/data/test.mdb"));
  267. Table table = db.getTable("Table1");
  268. Index indA = table.getIndex("PrimaryKey");
  269. Index indB = table.getIndex("B");
  270. assertEquals(2, indA.getUniqueEntryCount());
  271. assertEquals(2, indB.getUniqueEntryCount());
  272. List<String> bElems = Arrays.asList("bar", null, "baz", "argle", null,
  273. "bazzle", "37", "bar", "bar", "BAZ");
  274. for(int i = 0; i < 10; ++i) {
  275. table.addRow("foo" + i, bElems.get(i), (byte)42 + i, (short)53 + i,
  276. 13 * i, (6.7d / i), null, null, true);
  277. }
  278. assertEquals(12, indA.getEntryCount());
  279. assertEquals(12, indB.getEntryCount());
  280. assertEquals(12, indA.getUniqueEntryCount());
  281. assertEquals(8, indB.getUniqueEntryCount());
  282. table = null;
  283. indA = null;
  284. indB = null;
  285. table = db.getTable("Table1");
  286. indA = table.getIndex("PrimaryKey");
  287. indB = table.getIndex("B");
  288. assertEquals(12, indA.getEntryCount());
  289. assertEquals(12, indB.getEntryCount());
  290. assertEquals(12, indA.getUniqueEntryCount());
  291. assertEquals(8, indB.getUniqueEntryCount());
  292. Cursor c = Cursor.createCursor(table);
  293. assertTrue(c.moveToNextRow());
  294. Map<String,Object> row = c.getCurrentRow();
  295. assertEquals("abcdefg", row.get("A"));
  296. assertEquals("hijklmnop", row.get("B"));
  297. c.deleteCurrentRow();
  298. assertEquals(11, indA.getEntryCount());
  299. assertEquals(11, indB.getEntryCount());
  300. assertEquals(12, indA.getUniqueEntryCount());
  301. assertEquals(8, indB.getUniqueEntryCount());
  302. db.close();
  303. }
  304. private void checkIndexColumns(Table table, String... idxInfo)
  305. throws Exception
  306. {
  307. Map<String, String> expectedIndexes = new HashMap<String, String>();
  308. for(int i = 0; i < idxInfo.length; i+=2) {
  309. expectedIndexes.put(idxInfo[i], idxInfo[i+1]);
  310. }
  311. for(Index idx : table.getIndexes()) {
  312. String colName = expectedIndexes.get(idx.getName());
  313. assertEquals(1, idx.getColumns().size());
  314. assertEquals(colName, idx.getColumns().get(0).getName());
  315. if("PrimaryKey".equals(idx.getName())) {
  316. assertTrue(idx.isPrimaryKey());
  317. } else {
  318. assertFalse(idx.isPrimaryKey());
  319. }
  320. }
  321. }
  322. }